Reply-To: "Paul Nicholls" From: "Paul Nicholls" Newsgroups: borland.public.delphi.graphics References: <3ba7dbaa_1@dnews> <3ba7e4b4_2@dnews> Subject: Re: Checking of PointInRegion C code to Delphi translation... Date: Wed, 19 Sep 2001 12:48:17 +1000 Lines: 152 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 165.228.6.5 Message-ID: <3ba806d5_2@dnews> X-Trace: dnews 1000867541 165.228.6.5 (18 Sep 2001 19:45:41 -0700) Path: dnews Xref: dnews borland.public.delphi.graphics:41971 Thanks people, but after some recoding I have gotten the functions to work just fine... Thanks and here is the final code I have (working) {************************************************************************* * FUNCTION: CCW (CounterClockWise) * * PURPOSE * Determines, given three points, if when travelling from the first to * the second to the third, we travel in a counterclockwise direction. * * RETURN VALUE * (int) 1 if the movement is in a counterclockwise direction, -1 if * not. *************************************************************************} function CCW(p0,p1,p2:TPoint): Integer; var dx1, dx2: LongInt; dy1, dy2: LongInt; begin dx1 := p1.x - p0.x ; dx2 := p2.x - p0.x ; dy1 := p1.y - p0.y ; dy2 := p2.y - p0.y ; {* This is basically a slope comparison: we don't do divisions because * of divide by zero possibilities with pure horizontal and pure * vertical lines. *} if((dx1 * dy2) > (dy1 * dx2))then Result := 1 else Result := -1; end; {************************************************************************* * FUNCTION: Intersect * * PURPOSE * Given two line segments, determine if they intersect. * * RETURN VALUE * TRUE if they intersect, FALSE if not. *************************************************************************} function Intersect(p1,p2,p3,p4: TPoint): Boolean; begin Result := (((CCW(p1, p2, p3) * CCW(p1, p2, p4)) <= 0)and (( CCW(p3, p4, p1) * CCW(p3, p4, p2) <= 0))) ; end; {************************************************************************* * FUNCTION: G_PtInPolyRect * * PURPOSE * This routine determines if a point is within the smallest rectangle * that encloses a polygon. * * RETURN VALUE * (BOOL) TRUE or FALSE depending on whether the point is in the rect or * not. *************************************************************************} function G_PtInPolyRect(PolyPoints: array of TPoint; ptTest: TPoint; var prbound: TRect): Boolean; var xmin, xmax, ymin, ymax: Integer; pt: TPoint; i: Word; begin xmin := MaxInt; ymin := MaxInt; xmax := -MaxInt; ymax := -MaxInt; for i := 0 to High(PolyPoints) do begin pt := PolyPoints[i]; if (pt.x < xmin)then xmin := pt.x; if (pt.x > xmax)then xmax := pt.x; if (pt.y < ymin)then ymin := pt.y; if (pt.y > ymax)then ymax := pt.y; end; prbound := Rect(xmin, ymin, xmax, ymax); Result := PtInRect(prbound,ptTest); end; {************************************************************************* * FUNCTION: G_PtInPolygon * * PURPOSE * This routine determines if the point passed is in the polygon. It uses * the classical polygon hit-testing algorithm: a horizontal ray starting * at the point is extended infinitely rightwards and the number of * polygon edges that intersect the ray are counted. If the number is odd, * the point is inside the polygon. * * RETURN VALUE * (BOOL) TRUE if the point is inside the polygon, FALSE if not. *************************************************************************} function G_PtInPolygon(PolyPoints: array of TPoint; ptTest: TPoint): Boolean; var i: Integer; pt1, pt2: TPoint; wnumintsct: Word; prbound: TRect; begin wnumintsct := 0; Result := False; if(not G_PtInPolyRect(PolyPoints,ptTest,prbound))then Exit; pt1 := ptTest; pt2 := ptTest; pt2.x := prbound.Right+50; // Now go through each of the lines in the polygon and see if it // intersects for i := 0 to High(PolyPoints)-1 do if(Intersect(ptTest,pt2,PolyPoints[i],PolyPoints[i+1]))then Inc(wnumintsct); // And the last line if(Intersect(ptTest, pt2, PolyPoints[High(PolyPoints)], PolyPoints[0]))then Inc(wnumintsct); // If wnumintsct is odd then the point is inside the polygon Result := Odd(wnumintsct); end; Thanks again, and sorry for the trouble, Paul Nicholls (Delphi 5) Live long and optimise! "Life is a beach - every so often you are swept out to sea by a rip and get into trouble!" - Paul Nicholls Home Page: www.southcom.com.au/~phantom < IF YOU WANT TO EARN MONEY WHILE YOU SURF ON THE NET GO HERE: > < http://www.alladvantage.com/go.asp?refid=BEM-274 >