# Determine if point lies on selected polygon

## Recommended Posts

Howdy gents,

is there a way how to determine if point (X,Y given) lies on pre-selected polygon (polyline, line) or even on multiple of those? Or lies in certain distance (tolerance offset) from those pre-selected linear objects?

FUNCTION PtInPoly(pX,pY: REAL; h: HANDLE): BOOLEAN;

Miguel,

Doesn't this just tell you if the point is inside the area of the polygon? Do you know a trick to make it return only objects that are actually on the line?

Yes, I know a way but involves a bit more work.

You can use:

PROCEDURE LineLineIntersection( l1start :POINT;

l1end :POINT;

l2start :POINT;

l2end :POINT;

VAR parallel :BOOLEAN;

VAR intOnLines:BOOLEAN;

VAR sectpt :POINT);

line1 is the segment between two vertices of the polygon.

line2 is a line perpendicular to the segment passing through the point in question.

intOnlines will return true if the intersecting point is on the polygon line segment.

Or you can test for distance with the resulting sectpt if within a tolerance factor.

Before this procedure was added, I calculated the distance from the intersecting point to each end of the line segment. If the sum of the 2 distances equaled the length of the line segment then the point was on the line.

By the way, according to the definition of PtInPoly, all points within or on the polygon will return true.

My understanding is that "on the polygon" means on a line segment of the polygon.

There is also this function:

PtOnLine(Pt, BegPt, EndPt: VECTOR; Tolerance: REAL) : BOOLEAN;

Use it in a loop for each side of your Poly.

Raymond

There is also this function:

PtOnLine(Pt, BegPt, EndPt: VECTOR; Tolerance: REAL) : BOOLEAN;

Thanks Raymond, sounds promising!

rrrrr...... this function isn't documented in VW function reference

This one's an easy one to guess. By trial usage I determined the following:

PtOnLine returns TRUE if point Pt is on the line segment defined between points BegPt and EndPt, inclusive; or within a distance, defined by Tolerance, of that line segment. Otherwise it returns FALSE.

Pt = point to be tested, stored as a vector (Pt.x, Pt.y, Pt.z)

BegPt = first point of line segment, stored as a vector (BegPt.x, BegPt.y, BegPt.z)

EndPt = second point of line segment, stored as a vector (EndPt.x, EndPt.y, EndPt.z)

Notes:

1) For 2D lines and points, the z component of the three vectors is 0.

2) Because of the nature of real numbers stored in computers, Tolerance should not be set to 0. Make it a suitably small number for your use. 1e-6 or 1e-9 should work for most architectural needs. Anything greater than 1e-323 will work for the number system employed by VW (at least as I've tested it on a Mac).

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×