2 comments. Current rating: (2 votes). Leave comments and/ or rate it.
Question:
It is of interest to determin if a point is within a given polygon. My polygon is represented as an array of TPoints.
Answer:
A quick way to do this is to use the Windows API function PtInRegion. If not already avalaible, call CreatePolygonRgn() to create a region and pass the region's handle to PtInRegion().
Don't forget to free Windows' region handle afterwards.
A neat trick is to use the Length() function to obtain the number of points in the polygon.
// PointInPolygon() function by Andreas Filsinger
function PointInPolygon (const x,y : integer; aPolygon: arrayof TPoint): boolean;
var
PolyHandle: hRgn;
begin
PolyHandle := CreatePolygonRgn(aPolygon[0], Length(aPolygon), Winding);
result := PtInRegion(PolyHandle,X,Y);
DeleteObject(PolyHandle);
end;
You don't like the formatting? Check out SourceCoder then!
Comments:
2006-01-27, 05:54:52
anonymous from United Kingdom
2007-01-06, 13:40:55
[hidden] from Israel
that's really great
Delphi is just my hobby, and i'm not a programmer.
however, i want to make something that uses polygons that are made randomly.
can you teach us how to make random shaped polygons?