POLYGON CLIPPING???

POLYGON CLIPPING???

Post by Mohican of Mohaw » Fri, 19 Feb 1999 04:00:00



Hi all,

Hmm, i got this problem with filled polygons
and someone redirected me to this newsgroup
and said that a lot of gdi gurus where over here
so let me just state my problem:)

whenever i pass an array of tpoint to a canvas.polygon
or a canvas.polyline or whatever function, and the array
contains points that are OUTSIDE the clipping region of
canvas.cliprect, no clipping gets done. so gdi just keeps
drawing those polygons outside the cliprect (in unallocated memory)
which causes it to crash sometime.

i am using delphi4 proffesional and inspected the source
of graphics.pas but the polygon function is acutally in a
gdi dll so i can't see the code, but i saw somehting disturbing
in the comments in the implenation of graphics.pas somehting
about a left out list, which contained clipping regions..hell.

so my question is, why doesn't vcl/gdi do my polygon clipping
for me, it should do it for me. anyone?.. please help

Mohican
IRIS International
The Netherlands

ps- please reply to group and by email:)

 
 
 

POLYGON CLIPPING???

Post by Chris Hi » Fri, 19 Feb 1999 04:00:00


GDI doesn't clip the vertices of the polygon (and then reconstruct the
"remaining" polygon).  It clips the output pixels (on a raster
device).  I think you may have a different problem causing your
program to crash.  Windows will not write to pixels outside the canvas
bounds.  Perhaps you should give an example of what you think should
happen when a vertex is outside the clipping region.

Since Canvas.ClipRect is read-only, it can't really be used to clip
your output.  I think the documentation for TCanvas.ClipRect (as of
Delphi 3) is very confusing and just plain wrong.

If you have a specific area that you want to clip, use then Windows
API (this is what the comment in graphics.pas was referring to, the
VCL doesn't support setting a clip region).  First create a clip
region using a member of the Create*Rgn family.  For example, to
create a rectangle clip region use CreateRectRgn.  Use SelectClipRgn
to use the region as a clipping region for a particular canvas.
DeleteObject the Region handle.  Draw your polygon.  When you are done
clipping, you can reset the clip region using SelectClipRgn.

On Thu, 18 Feb 1999 13:27:21 +0100, "Mohican of Mohawk"


>Hi all,

>Hmm, i got this problem with filled polygons
>and someone redirected me to this newsgroup
>and said that a lot of gdi gurus where over here
>so let me just state my problem:)

>whenever i pass an array of tpoint to a canvas.polygon
>or a canvas.polyline or whatever function, and the array
>contains points that are OUTSIDE the clipping region of
>canvas.cliprect, no clipping gets done. so gdi just keeps
>drawing those polygons outside the cliprect (in unallocated memory)
>which causes it to crash sometime.

>i am using delphi4 proffesional and inspected the source
>of graphics.pas but the polygon function is acutally in a
>gdi dll so i can't see the code, but i saw somehting disturbing
>in the comments in the implenation of graphics.pas somehting
>about a left out list, which contained clipping regions..hell.

>so my question is, why doesn't vcl/gdi do my polygon clipping
>for me, it should do it for me. anyone?.. please help

>Mohican
>IRIS International
>The Netherlands

>ps- please reply to group and by email:)

Chris Hill


 
 
 

POLYGON CLIPPING???

Post by Mohican of Mohaw » Fri, 19 Feb 1999 04:00:00


First of all, thanx for the respone, made a lot of sense to me
but i still have some questions,
i am quite sure the crashes of my program are created by
the drawing of the polygons.

Per example if i have one quite large (big area) polygon
which i draw wich canvas.polygon, and i zoom in to the
polygon (the area of the polygon that is outside the clipping
region gets bigger) it crashes, this only happens with big
polygons and a reasonalbe zoomfactor.
When i remove the canvas.polygon function and use
lineto/moveto and a line clipping alogrithm everything
goes well and no crashes at all, but that not what i want.

I see that you mention that windows will not output
pixels outside of the canvas on a raster device,
i am not sure of the meaning of raster device,
but i am quite sure that windows is letting pixels
being drawn outside of my canvas, could it be that
a bitmap is not a raster device, i draw the polygons
on a TBitmap's canvas, and then copyrect that bitmap
to a paintbox..

tottally lost,

Mohican.
IRIS International

>GDI doesn't clip the vertices of the polygon (and then reconstruct the
>"remaining" polygon).  It clips the output pixels (on a raster
>device).  I think you may have a different problem causing your
>program to crash.  Windows will not write to pixels outside the canvas
>bounds.  Perhaps you should give an example of what you think should
>happen when a vertex is outside the clipping region.

>Since Canvas.ClipRect is read-only, it can't really be used to clip
>your output.  I think the documentation for TCanvas.ClipRect (as of
>Delphi 3) is very confusing and just plain wrong.

>If you have a specific area that you want to clip, use then Windows
>API (this is what the comment in graphics.pas was referring to, the
>VCL doesn't support setting a clip region).  First create a clip
>region using a member of the Create*Rgn family.  For example, to
>create a rectangle clip region use CreateRectRgn.  Use SelectClipRgn
>to use the region as a clipping region for a particular canvas.
>DeleteObject the Region handle.  Draw your polygon.  When you are done
>clipping, you can reset the clip region using SelectClipRgn.

>On Thu, 18 Feb 1999 13:27:21 +0100, "Mohican of Mohawk"

>>Hi all,

>>Hmm, i got this problem with filled polygons
>>and someone redirected me to this newsgroup
>>and said that a lot of gdi gurus where over here
>>so let me just state my problem:)

>>whenever i pass an array of tpoint to a canvas.polygon
>>or a canvas.polyline or whatever function, and the array
>>contains points that are OUTSIDE the clipping region of
>>canvas.cliprect, no clipping gets done. so gdi just keeps
>>drawing those polygons outside the cliprect (in unallocated memory)
>>which causes it to crash sometime.

>>i am using delphi4 proffesional and inspected the source
>>of graphics.pas but the polygon function is acutally in a
>>gdi dll so i can't see the code, but i saw somehting disturbing
>>in the comments in the implenation of graphics.pas somehting
>>about a left out list, which contained clipping regions..hell.

>>so my question is, why doesn't vcl/gdi do my polygon clipping
>>for me, it should do it for me. anyone?.. please help

>>Mohican
>>IRIS International
>>The Netherlands

>>ps- please reply to group and by email:)

>Chris Hill


 
 
 

POLYGON CLIPPING???

Post by Chris Hi » Fri, 19 Feb 1999 04:00:00


If you using Win9x, how large are the coordinates that are outside the
bitmap?  If they are greater than 2^15-1 or less than -2^15, you might
be running into problems with the coordinate limitations of Win9x
(this problem shouldn't be a present on NT which supports much larger
coordinates, ~2^28).  Even if your coordinates are just very large
(+-3000), there may be problems with the Polygon function or the
driver.  It might not be a problem with the magnitude of individual
coordinates, but the overall range of the points (which defines the
size of the Polygon) Perhaps another reader might know about a
specific problem with large coordinates in your scenario (Joe? <g>).

If you are using a TBitmap.Canvas, Windows should not draw outside of
the bitmap unless there is a Windows or video driver bug.

On Thu, 18 Feb 1999 19:52:59 +0100, "Mohican of Mohawk"


>First of all, thanx for the respone, made a lot of sense to me
>but i still have some questions,
>i am quite sure the crashes of my program are created by
>the drawing of the polygons.

>Per example if i have one quite large (big area) polygon
>which i draw wich canvas.polygon, and i zoom in to the
>polygon (the area of the polygon that is outside the clipping
>region gets bigger) it crashes, this only happens with big
>polygons and a reasonalbe zoomfactor.
>When i remove the canvas.polygon function and use
>lineto/moveto and a line clipping alogrithm everything
>goes well and no crashes at all, but that not what i want.

>I see that you mention that windows will not output
>pixels outside of the canvas on a raster device,
>i am not sure of the meaning of raster device,
>but i am quite sure that windows is letting pixels
>being drawn outside of my canvas, could it be that
>a bitmap is not a raster device, i draw the polygons
>on a TBitmap's canvas, and then copyrect that bitmap
>to a paintbox..

>tottally lost,

Chris Hill

 
 
 

POLYGON CLIPPING???

Post by Mohican of Mohaw » Fri, 19 Feb 1999 04:00:00


indeed, the coordinates could become large fast when
zooming in to big polyons (by big i mean, big area)
The polygon definition is in worldcoordinates, those
are floats and i convert them to screen coordinates.
hmmm, yaiks this makes the problem even bigger
for me, looks like i am really have to do some polygon
clipping then, boy i hate that, i could a half working
algorithm, with the only thing going wrong is the
clipping at the edges of the clipping rectangle,
(eg:leftTop,rightBottom).. hmmm. maybe there is
a way of rejecting certain world2screen conversions
if they lay outside of the cliprect and then decreasing
them several orders of mangitude, so polygon won't
flip over the size. anyway thanx alot for the help.

Mohican
IRIS International


>If you using Win9x, how large are the coordinates that are outside the
>bitmap?  If they are greater than 2^15-1 or less than -2^15, you might
>be running into problems with the coordinate limitations of Win9x
>(this problem shouldn't be a present on NT which supports much larger
>coordinates, ~2^28).  Even if your coordinates are just very large
>(+-3000), there may be problems with the Polygon function or the
>driver.  It might not be a problem with the magnitude of individual
>coordinates, but the overall range of the points (which defines the
>size of the Polygon) Perhaps another reader might know about a
>specific problem with large coordinates in your scenario (Joe? <g>).

>If you are using a TBitmap.Canvas, Windows should not draw outside of
>the bitmap unless there is a Windows or video driver bug.

>Chris Hill


 
 
 

POLYGON CLIPPING???

Post by Joe C. Hech » Sat, 20 Feb 1999 04:00:00



> If you using Win9x, how large are the coordinates that are outside the
> bitmap?  If they are greater than 2^15-1 or less than -2^15, you might
> be running into problems with the coordinate limitations of Win9x
> (this problem shouldn't be a present on NT which supports much larger
> coordinates, ~2^28).  Even if your coordinates are just very large
> (+-3000), there may be problems with the Polygon function or the

....

I was just getting ready to chime in with this one.

Non NT versions of Windows require the coordinate
spread be within the range of +-32000. Note: Its not
too difficult to clip a set of polypoints to a
rectangle.

Joe
--
Joe C. Hecht
http://home1.gte.net/joehecht/index.htm

 
 
 

POLYGON CLIPPING???

Post by Ross Garne » Tue, 23 Feb 1999 04:00:00


. Note: Its not

Quote:> too difficult to clip a set of polypoints to a
> rectangle.

have you some code for doing this please

ross

 
 
 

POLYGON CLIPPING???

Post by Joe C. Hech » Tue, 23 Feb 1999 04:00:00




> . Note: Its not
> > too difficult to clip a set of polypoints to a
> > rectangle.

> have you some code for doing this please

Yes, but my Polygon clipping code is very propritory,
as I sell it to make my living.

But here is some psudo code to get you going in the right
direction:

Run through each line (vertices) of the polygon:

1) If the line completely inside the rectangle then
do nothing.

2) If the line is totally outside the rectangle, then
remove it.

3) if the line crosses an edge of the rectangle, then
find the point of intersection, and reset the line
accordinaly.

Joe
--
Joe C. Hecht
http://home1.gte.net/joehecht/index.htm

 
 
 

POLYGON CLIPPING???

Post by Earl F. Glyn » Tue, 23 Feb 1999 04:00:00





>> . Note: Its not
>> > too difficult to clip a set of polypoints to a
>> > rectangle.

>> have you some code for doing this please

>Yes, but my Polygon clipping code is very propritory,
>as I sell it to make my living.

You can find the Sutherland-Hodgman Polygon Clipping Algorithm in
"Computer Graphics -- Principles and Practice" by Foley et al,
Section 3.14.1, pp. 124-127.

You might try the link in Section B, Algorithms, at
www.efg2.com/lab/library/graphics.htm

efg
_________________________________
efg's Computer Lab:       www.efg2.com/lab
Delphi Books:  www.efg2.com/lab/TechBooks/Delphi.htm


Overland Park, KS  USA

 
 
 

POLYGON CLIPPING???

Post by Mohican of Mohaw » Wed, 24 Feb 1999 04:00:00


Hmm, i got a polygon clipping routine, that almost works
completly, with regards to almost, it seems to go wrong
when a polygon intersects with an egde of the clipping
rectangle, it looks like a vertice should be added to the
polygon at the edge of clipping rectangle, weird..

btw ,if i get this working, i will be more then happy
to share the source ofcourse.

Mohican





>>> . Note: Its not
>>> > too difficult to clip a set of polypoints to a
>>> > rectangle.

>>> have you some code for doing this please

>>Yes, but my Polygon clipping code is very propritory,
>>as I sell it to make my living.

>You can find the Sutherland-Hodgman Polygon Clipping Algorithm in
>"Computer Graphics -- Principles and Practice" by Foley et al,
>Section 3.14.1, pp. 124-127.

>You might try the link in Section B, Algorithms, at
>www.efg2.com/lab/library/graphics.htm

>efg
>_________________________________
>efg's Computer Lab:       www.efg2.com/lab
>Delphi Books:  www.efg2.com/lab/TechBooks/Delphi.htm


>Overland Park, KS  USA