How to draw hatches
Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2124
Printed Date: 25 Jan 26 at 12:50PM Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com
Topic: How to draw hatches
Posted By: kaiken1987
Subject: How to draw hatches
Date Posted: 31 Jan 12 at 2:56PM
|
I'm trying to find a way to draw filled polygons with holes in them, something like the widows GDI polypolygon function. The only way I've been able to figure out is to call GetCanvasDC, RenderToDC, use the polypolygon function and then call LoadFromCanvasDC. That seems terribly slow and inefficient and I assume the must be a better way then that.
|
Replies:
Posted By: AndrewC
Date Posted: 01 Feb 12 at 8:25AM
|
Here is some code to draw a hollow star..
// Draw a hollow star
QP.SetFillColor(1, 0, 0); QP.SetLineColor(0, 1, 0);
QP.StartPath(100, 60); QP.AddLineToPath(306, 600); QP.AddLineToPath(512, 60); QP.AddLineToPath(20, 400); QP.AddLineToPath(592, 400); QP.ClosePath();
QP.DrawPathEvenOdd(2);
// Draw a hollow donut
QP.NewPage();
QP.SetFillColor(1, 0, 0); QP.SetLineColor(0, 1, 0);
QP.StartPath(100, 200); QP.AddArcToPath(200, 200, 360); QP.MovePath(150, 200); QP.AddArcToPath(200, 200, 360); QP.ClosePath(); QP.DrawPathEvenOdd(2);
|
Posted By: kaiken1987
Date Posted: 02 Feb 12 at 1:58PM
|
Thank you that worked for me very well.
|
Posted By: edvoigt
Date Posted: 03 Feb 12 at 8:18PM
Hi,
there is something more to say. Because hatch or not hatch is a question of orientation.
 My first example shows a normal drawn box and for clipping a path consisting of a circle and a hatch-box in it. Here the source: QP.SetOrigin(0); // Bottomleft QP.SetMeasurementUnits(1); // Millimeter QP.SetPageDimensions(210, 297);
QP.SetLineWidth(0.1); QP.SetLineColor(1, 0, 0); QP.SetFillColor(1, 1, 0); x0 := 25; x1 := 175; y0 := 170; y1 := 270; r := 70; // 1. draw without clipping QP.DrawBox(x0, y1, x1-x0, y1-y0, 1); QP.DrawCircle((x0+x1)/2, y1-r, r, 0);
QP.SaveState; // for later use without clipping // 2. define clipping path QP.StartPath((x0+x1)/2, y1); QP.AddArcToPath((x0+x1)/2, y1-r, 360); // the "normal" path for clipping // 3. hatch-box-building // hatch or not hatch is a question of orientation QP.MovePath(x0+20, y0+75); // upper left corner QP.AddLineToPath(x0+20, y0+50); // lower left QP.AddLineToPath(x0+50, y0+50); // lower right QP.AddLineToPath(x0+50, y0+75); // upper right QP.AddLineToPath(x0+20, y0+75); // close hatch, important! QP.SetClippingPath; // 4. draw clipped things // it will be drawn only inside the first circle and outside our hatchbox QP.DrawBox(x0, y1, x1-x0, y1-y0, 0); for i:=0 to 52 do QP.DrawLine(x1-i*5, y0, x1, y0+i*5); QP.SetFillColor(0, 1, 0); QP.DrawCircle(x0+50, y0+75, 10, 2);
QP.LoadState; // stop clipping from here
QP.SetLineColor(0, 0, 0); QP.DrawLine(x0, y0, (x0+x1)/2, y1);
The second example is more interessting. Using four lines two triangles are builded, but only one of them becomes to be hatch.
That's so because of orientation during AddLineToPath-sequence.
 Wandering from P1 over P2 to P3 upper triangle is ever on your right side. But the other Triangle is on the way from P3 over P4 to P1 on left side. Areas on right side are are parts of the path, but areas on left side dont't belong to path, they are hatches.
Source for this: x0 := 100; y1 := y0-50; y0 := 20; r := 30; QP.SaveState; // for later use without clipping QP.StartPath(x0-r-5, y1+5); //first a box as clipping area QP.AddLineToPath(x0+2*r, y1); QP.AddLineToPath(x0+2*r, y0-1); QP.AddLineToPath(x0-r, y0-1); QP.AddLineToPath(x0-r, y1); // and now a special kind of half hatch QP.MovePath(x0, y1); QP.AddLineToPath(x0+r, y1); QP.AddLineToPath(x0-r, y0); QP.AddLineToPath(x0+r, y0); QP.AddLineToPath(x0, y1); QP.SetClippingPath; QP.DrawBox(x0-r, y1, 2*r, y1-y0, 1); // show hatch construction QP.SetLineColor(0, 0, 0); QP.SetLineWidth(1.0); QP.DrawLine(x0, y1, x0+r, y1); // show the contour QP.DrawLine(x0+r, y1, x0-r, y0); QP.DrawLine(x0-r, y0, x0+r, y0); QP.DrawLine(x0+r, y0, x0, y1);
QP.LoadState; // stop clipping now again QP.DrawText(x0, y1+2, 'P1'); QP.DrawText(x0+r, y1+2, 'P2'); QP.DrawText(x0-r, y0-5, 'P3'); QP.DrawText(x0+r, y0-5, 'P4');
Cheers, Werner
|
|