Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2003-12-09

In delphi, we can draw a polygon by using array of TPoint :

Canvas.Polygon(Points: Array of TPoint);

How can we know that a TPoint is "inside" that polygon ? I meant like "hit test" function.

For rectangle (x1,y1,x2,y2), I can use this "If" clause :

// Testing P.X and P.Y

if (P.X >= x1) And (P.Y >= y1) And (P.X <= x2) And (P.Y <= y2) then HIT := TRUE;

Anyone know the algorithm ?

Canvas.Polygon(Points: Array of TPoint);

How can we know that a TPoint is "inside" that polygon ? I meant like "hit test" function.

For rectangle (x1,y1,x2,y2), I can use this "If" clause :

// Testing P.X and P.Y

if (P.X >= x1) And (P.Y >= y1) And (P.X <= x2) And (P.Y <= y2) then HIT := TRUE;

Anyone know the algorithm ?

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

3 Comments

You could create a region, and use the PtInRegion to determine if the point falls inside the polygon.

eg:

function PtInPolygon(Point: TPoint; Polygon: Array of TPoint): Boolean;

var hrgnPoly: HRGN;

begin

// Create polygon region

hrgnPoly:=CreatePolygonRgn

// Make sure the region was created

if (hrgnPoly = 0) then

// Failure

result:=False

else

begin

// Check to see if point lies inside of the region

result:=PtInRegion(hrgnPol

// Delete the region

DeleteObject(hrgnPoly);

end;

end;

Example usage (draws a triangle on the form and checks to see if mouse down event is in the triangle):

procedure TForm1.FormMouseDown(Sende

begin

if PtInPolygon(Point(X, Y), [Point(100, 100), Point(140, 1), Point(180, 100)]) then

ShowMessage('Point in polygon');

end;

procedure TForm1.FormPaint(Sender: TObject);

begin

// Draw the polygon so you have something visual to go by

Canvas.Polygon([Point(100,

end;

Also, for the rectangle, there is a PtInRect function that you could use...

Hope this helps,

Russell

function PointInPoly(APoint : TPoint ; APoly : array of TPoint) : boolean;

Var

i, j : integer;

npol : integer;

begin

Result := false;

npol := length(APoly);

for i := 0 to npol - 1 do begin

j := (i + 1) mod npol;

if ((((APoly[i].Y <= APoint.Y) and (APoint.Y < APoly[j].Y)) or

((APoly[j].Y <= APoint.Y) and (APoint.Y < APoly[i].Y))) and

(APoint.X < (APoly[j].X - APoly[i].X) * (APoint.Y - APoly[i].Y) /

(APoly[j].Y - APoly[i].Y) + APoly[i].X)) then

Result := not Result;

end;

end;

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Course of the Month11 days, 11 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.