Re: Point/Polygon Routine [message #5596 is a reply to message #5401] |
Wed, 03 January 1996 00:00  |
jlaw
Messages: 11 Registered: November 1995
|
Junior Member |
|
|
In article 760@ncar.ucar.edu, cavanaug@uars1.acd.ucar.edu (Charles Cavanaugh) writes:
>
> Does anyone have a routine that determines if a given 2-D point is inside a
> given 2-D polygon?
I did have, but I left it behind when I changed jobs.
Are you any good at finite elements??( This is where I looked this up)
Consider a triangle of points I,J,K with coordinates
( Xi,Yi) , (Xj,Yj ) , (Xk,Yk).
the Area of the triangle is then
( 1 Xi Yi )
0.5 * det ( 1 Xj Yj )
( 1 Xk Yk )
Now take the point P at ( Xp, Yp ).
( Strictly, if P is inside the triangle) we can define "Area coordinates"
as follows:
Li = area(pjk) / area(ijk)
Lj = area(pki) / area(ijk)
Lk = area(pij) / area(ijk)
If P is indeed inside the triangle, all the area coordinates are positive.
But if P is outside the triangle, one or more is negative.
For a polygon, split it into a set of triangles. If the point is inside one
of the triangles, it must be inside the polygon.
I think this all depends on the points being placed in the correct sense
around the triangle ( ie clockwise or anticlockwise.)You had better experiment
a little, but that is the basic principle.
It is not actually many lines of code.
all the best
f.
|
|
|