1. Home
  2. Computing & Technology
  3. Delphi Programming

Is Point in Polygon?

By Zarko Gajic, About.com

This solution checks how many times you cross a line to pass from point X,Y to the edge of the polygon...

~~~~~~~~~~~~~~~~~~~~~~~~~
function PtInPoly
   (const Points: Array of TPoint; X,Y: Integer):
   Boolean;
var Count, K, J : Integer;
begin
  Result := False;
  Count := Length(Points) ;
  J := Count-1;
  for K := 0 to Count-1 do begin
   if ((Points[K].Y <=Y) and (Y < Points[J].Y)) or
      ((Points[J].Y <=Y) and (Y < Points[K].Y)) then
   begin
    if (x < (Points[j].X - Points[K].X) *
       (y - Points[K].Y) /
       (Points[j].Y - Points[K].Y) + Points[K].X) then
        Result := not Result;
    end;
    J := K;
  end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» TButton with multiline Caption
« Create an Auto-Run CD

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2001 Delphi Tips
  7. Is Point in Polygon?

©2009 About.com, a part of The New York Times Company.

All rights reserved.