Here are three functions that come in handy when working with geometrical figures.
All three use a TLine type defined as a record containing two endpoints P1 and P2.
IntersectIntersect takes two line definitions as input and returns true if the two line intersect. An additional parameter, PointOnBorder, is set to true if the lines touch but do not cross, . This is a faster, but harder-to-understand version of the IntersectingLines routine presented earlier.
PointPerpendicularLineGiven a line and a point not on the line, construct a line through the point and perpendicular to the line. The trick here is to determine the slope of the given line, m, and take advantage of the fact that the slope of a perpendicular line is -1/m. Given the equations of the two lines we can solve them as decribed in Intersecting Lines to determine the point of intersection.
AngledLineFromLineGiven a line, a point on (or near) the line, an angle, and a length, construct a line through the point with the specified length and at the specified angle from the given line. The simplest approach to this problem is to draw a line segment of a given length, L, at a given angle, A, through a given point, P. The new point is defined by P2.X=P1.X+Lcos(A) and P2.Y=P1.Y+Lsin(A). The only problem remaining is to determine angle A. The required angle equals the angle of the given line from the horizon plus the given angle, and the angle of the given line from the horizon is given by the inverse tangent of its slope. Here's the bit of Delphi code that does that for line L1 with endpoints p1 and p2:
With L1 do
begin
if p1.x<>p2.x then {slope is not infinite}
theta:=arctan2((p1.y-p2.y),(p2.x-p1.x))
else {vertical line} if p2.y>p1.y then theta:=pi/2 else
theta:=-pi/2;
end;
Addendum June 11, 2004:
Point In Polygon
Given an arbitrary polygon and a point, determine if the point is inside or outside of the polygon. (The red point in the image at left is outside of the polygon.)
The algorithm extends a line from the point to "infinity: and counts how many times it intersects the polygon. If odd, the point is internal; if even, the point is external to the polygon. There are a few messy details here in detecting cases where the point is on an edge or vertex of the polygon or when the extension line passes through a vertex.
Addendum October 27,2005: I finally came across an application that required several of the subroutines described above. As a result, the routines have been moved to a separate unit in version 4 of our common library file (DFFLibV04).
I also discovered that the Intersect function described above, returns incorrect results under some conditions. I've replaced it with another version (LinesIntersect), which seems to be more bulletproof but does not have the PointOnBorder feature. Intersect will return when I find that elusive bug.
![]() |
|
Polygon inflated by 12 pixels |
Addendum January 14, 2007: Three new routines were added to the UGeometry unit today and Version 3 of the Geometry test program illustrates their use. The routines, PolygonArea, InflatePolygon, and PolygonClockwise help to "inflate" (or deflate) a given polygon by given amount. The value is the distance which the edges are to be moved while retaining their slope. In order to decide which direction to move each edge, we need to know whether the polygon was built in a clockwise or counterclockwise direction. A common algorithm for calculating the area of a general polygon happens to produce a value which is negative if the direction were clockwise and positive otherwise. So PolygonArea is called mainly to determine which direction to shift the edges, and the area is provided as a "freebie" side effect.
Two bugs were corrected in UGeometry also. The Intersect bug described previously has been corrected (intersection points which fell exactly on the other line were not identified as such when the 2nd line ended on the 1st line). And in the AngledLineFromLine procedure, right and left side directions were reversed when the line was vertical.
The updated UGeometry is located in DFFLibV09 (or later) versions of our library file. For this release only, in order to avoid downloading the entire library for this one changed unit, UGeometry is also included in the source code download for the Geometry test program.
Click here to download source code (requires a one time download of DFFFLIBV09 or later)
Click here to download current Library version Library
| Created: October 24, 2002 |
Modified: November 07, 2008 |