Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiDemoFiremonkeyIOSOSXWindows

Polygon Morphing And Line Intersection With Delphi XE6 Firemonkey On Windows And OSX

Delphi XE6 Firemonkey Morph A Polygon And Intersect A LineDeveloper zedalaye has a project up on Github which demonstrates how to intersect a polygon that is morphing in real time with a line. The original project was for Delphi XE2 but I have updated the project for Delphi XE6 Firemonkey. I was able to get it working on Windows in XE6 but under Android there is a painting issue. Basically it has a polygon shape in the center of a TPaintBox. There is a line that rotates from the center to the edges of the box and wherever the line intersects with the polygon a point is drawn. At the same time the polygon is morphing it’s shape so the point where the line is intersecting the polygon is constantly changing. There is some interesting code in here including two custom classes that derive from the TAnimation component for handling the animation of the point and the polygon. Take a look at the two objects here:
TPointAnimation = class(TAnimation)
TBorder = (bTop, bRight, bBottom, bLeft);
private var
FBorder: TBorder;
FControl: TControl;
FEndSegment: TPointF;
FFirstPass: Boolean;
procedure ProcessAnimation; override;
constructor Create(AOwner: TComponent); override;
property Control: TControl read FControl write FControl;
property Point: TPointF read FEndSegment;

TPolygonAnimation = class(TAnimation)
FControl: TControl;
FStock: array of TPolygon;
FBase, FTarget: Integer;
FShape: TPolygon;
procedure SetControl(const Value: TControl);
procedure ResamplePolygon(Index: Integer; FinalPoints: Integer);
procedure ProcessAnimation; override;
procedure Start; override;
constructor Create(AOwner: TComponent); override;
property Control: TControl read FControl write SetControl;
property Shape: TPolygon read FShape;

And here is the intersect function which will give you the TPointF where two lines intersect.
function Intersect(const A1, A2, B1, B2: TPointF; out P: TPointF): Boolean;

{ Returns the determinant }
function Det(P1, P2: TPointF): Single; inline;
Result := P1.X * P2.Y - P1.Y * P2.X;

{ Returns True if Min(X1, X2) <= X < Max(X1, X2) }
function InSignedRange(const X, X1, X2: Single): Boolean; inline;
Result := (X < X1) xor (X < X2);

A, B, AB: TPointF;
dAB, dBAB, dAAB: Single;
Result := False;

A := A2 - A1;
B := B2 - B1;

dAB := Det(A, B);

if dAB = 0 then
Exit; { vectors A and B hold by (A1,A2) and (B1,B2) are colinear }

AB := A1 - B1;

dAAB := Det(A, AB);
dBAB := Det(B, AB);

if InSignedRange(dAAB, 0, dAB) and InSignedRange(dBAB, 0, dAB) then
Result := True;
dBAB := dBAB / dAB;
P.X := A1.X + dBAB * A.X;
P.Y := A1.Y + dBAB * A.Y;

Head over and check out the source code for the polygon morphing and line intersection project on Github.

Or download the updated polygon morphing and line intersection project for Delphi XE6 Firemonkey.


Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes


Ryzen 9 5950x: One Billion Lines Of Delphi Code Compiled In ~5 Minutes On 16 Cores


DelphiCon 2020 FireMonkey Sessions Available Starting November 19


Powerful Commercial Gorilla3D Framework Upgraded For Delphi FireMonkey On Windows And Android

Sign up for our Newsletter and
stay informed

Leave a Reply