GR32_Geometry.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. unit GR32_Geometry;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Additional Math Routines for Graphics32
  23. *
  24. * The Initial Developers of the Original Code are
  25. * Mattias Andersson <[email protected]>
  26. * Michael Hansen <[email protected]>
  27. *
  28. * Portions created by the Initial Developers are Copyright (C) 2005-2012
  29. * the Initial Developers. All Rights Reserved.
  30. *
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. uses
  36. Math, Types, GR32;
  37. type
  38. TLinePos = (lpStart, lpEnd, lpBoth, lpNeither);
  39. // TFloat Overloads
  40. function Average(const V1, V2: TFloatPoint): TFloatPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  41. function CrossProduct(const V1, V2: TFloatPoint): TFloat; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  42. function Dot(const V1, V2: TFloatPoint): TFloat; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  43. function Distance(const V1, V2: TFloatPoint): TFloat; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  44. function SqrDistance(const V1, V2: TFloatPoint): TFloat; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  45. function GetPointAtAngleFromPoint(const Pt: TFloatPoint; const Dist, Radians: Single): TFloatPoint; overload;
  46. function GetAngleOfPt2FromPt1(const Pt1, Pt2: TFloatPoint): Single; overload;
  47. function GetUnitNormal(const Pt1, Pt2: TFloatPoint): TFloatPoint; overload;
  48. procedure GetUnitNormal(const Pt1, Pt2: TFloatPoint; out Result: TFloatPoint); overload;
  49. function GetUnitVector(const Pt1, Pt2: TFloatPoint): TFloatPoint; overload;
  50. function OffsetPoint(const Pt: TFloatPoint; DeltaX, DeltaY: TFloat): TFloatPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  51. function OffsetPoint(const Pt, Delta: TFloatPoint): TFloatPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  52. function OffsetRect(const Rct: TFloatRect; const DeltaX, DeltaY: TFloat): TFloatRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  53. function OffsetRect(const Rct: TFloatRect; const Delta: TFloatPoint): TFloatRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  54. function Shorten(const Pts: TArrayOfFloatPoint; Delta: TFloat; LinePos: TLinePos): TArrayOfFloatPoint; overload;
  55. function PointInPolygon(const Pt: TFloatPoint; const Pts: TArrayOfFloatPoint): Boolean; overload;
  56. function SegmentIntersect(const P1, P2, P3, P4: TFloatPoint; out IntersectPoint: TFloatPoint): Boolean; overload;
  57. function PerpendicularDistance(const P, P1, P2: TFloatPoint): TFloat; overload;
  58. function SamePoint(const A, B: TFloatPoint; SqrDist: Double): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  59. // TFixed Overloads
  60. function Average(const V1, V2: TFixedPoint): TFixedPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  61. function CrossProduct(const V1, V2: TFixedPoint): TFixed; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  62. function Dot(const V1, V2: TFixedPoint): TFixed; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  63. function Distance(const V1, V2: TFixedPoint): TFixed; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  64. function SqrDistance(const V1, V2: TFixedPoint): TFixed; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  65. function GetPointAtAngleFromPoint(const Pt: TFixedPoint; const Dist, Radians: Single): TFixedPoint; overload;
  66. function GetAngleOfPt2FromPt1(Pt1, Pt2: TFixedPoint): Single; overload;
  67. function GetUnitVector(const Pt1, Pt2: TFixedPoint): TFloatPoint; overload;
  68. function GetUnitNormal(const Pt1, Pt2: TFixedPoint): TFloatPoint; overload;
  69. function OffsetPoint(const Pt: TFixedPoint; DeltaX, DeltaY: TFixed): TFixedPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  70. function OffsetPoint(const Pt: TFixedPoint; DeltaX, DeltaY: TFloat): TFixedPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  71. function OffsetPoint(const Pt: TFixedPoint; const Delta: TFixedPoint): TFixedPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  72. function OffsetPoint(const Pt: TFixedPoint; const Delta: TFloatPoint): TFixedPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  73. function OffsetRect(const Rct: TFixedRect; const DeltaX, DeltaY: TFixed): TFixedRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  74. function OffsetRect(const Rct: TFixedRect; const DeltaX, DeltaY: TFloat): TFixedRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  75. function OffsetRect(const Rct: TFixedRect; const Delta: TFixedPoint): TFixedRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  76. function OffsetRect(const Rct: TFixedRect; const Delta: TFloatPoint): TFixedRect; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  77. function Shorten(const Pts: TArrayOfFixedPoint; Delta: TFloat; LinePos: TLinePos): TArrayOfFixedPoint; overload;
  78. function PointInPolygon(const Pt: TFixedPoint; const Pts: array of TFixedPoint): Boolean; overload;
  79. function SegmentIntersect(const P1, P2, P3, P4: TFixedPoint; out IntersectPoint: TFixedPoint): Boolean; overload;
  80. function PerpendicularDistance(const P, P1, P2: TFixedPoint): TFixed; overload;
  81. function SamePoint(const A, B: TFixedPoint; SqrDist: TFixed): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  82. // Integer Overloads
  83. function Average(const V1, V2: TPoint): TPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  84. function CrossProduct(const V1, V2: TPoint): Integer; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  85. function Dot(const V1, V2: TPoint): Integer; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  86. function Distance(const V1, V2: TPoint): TFloat; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  87. function SqrDistance(const V1, V2: TPoint): Integer; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  88. function OffsetPoint(const Pt: TPoint; DeltaX, DeltaY: Integer): TPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  89. function OffsetPoint(const Pt, Delta: TPoint): TPoint; overload;{$IFDEF USEINLINING} inline; {$ENDIF}
  90. function PerpendicularDistance(const P, P1, P2: TPoint): TFloat; overload;
  91. function SamePoint(const A, B: TPoint; SqrDist: integer): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  92. const
  93. CRad01 = Pi / 180;
  94. CRad30 = Pi / 6;
  95. CRad45 = Pi / 4;
  96. CRad60 = Pi / 3;
  97. CRad90 = Pi / 2;
  98. CRad180 = Pi;
  99. CRad270 = CRad90 * 3;
  100. CRad360 = CRad180 * 2;
  101. CDegToRad = Pi / 180;
  102. CRadToDeg = 180 / Pi;
  103. implementation
  104. uses
  105. GR32_Math;
  106. function Average(const V1, V2: TFloatPoint): TFloatPoint;
  107. begin
  108. Result.X := (V1.X + V2.X) * 0.5;
  109. Result.Y := (V1.Y + V2.Y) * 0.5;
  110. end;
  111. function CrossProduct(const V1, V2: TFloatPoint): TFloat;
  112. begin
  113. Result := V1.X * V2.Y - V1.Y * V2.X;
  114. end;
  115. function Dot(const V1, V2: TFloatPoint): TFloat;
  116. begin
  117. Result := V1.X * V2.X + V1.Y * V2.Y;
  118. end;
  119. function Distance(const V1, V2: TFloatPoint): TFloat;
  120. begin
  121. Result := GR32_Math.Hypot(V2.X - V1.X, V2.Y - V1.Y);
  122. end;
  123. function SqrDistance(const V1, V2: TFloatPoint): TFloat;
  124. begin
  125. Result := Sqr(V2.X - V1.X) + Sqr(V2.Y - V1.Y);
  126. end;
  127. function GetPointAtAngleFromPoint(const Pt: TFloatPoint;
  128. const Dist, Radians: TFloat): TFloatPoint; overload;
  129. var
  130. SinAng, CosAng: TFloat;
  131. begin
  132. GR32_Math.SinCos(Radians, SinAng, CosAng);
  133. Result.X := Dist * CosAng + Pt.X;
  134. Result.Y := -Dist * SinAng + Pt.Y; // Y axis is positive down
  135. end;
  136. function GetAngleOfPt2FromPt1(const Pt1, Pt2: TFloatPoint): Single;
  137. var
  138. X, Y: TFloat;
  139. begin
  140. X := Pt2.X - Pt1.X;
  141. Y := Pt2.Y - Pt1.Y;
  142. if X = 0 then
  143. begin
  144. if Y > 0 then Result := CRad270 else Result := CRad90;
  145. end else
  146. begin
  147. Result := ArcTan2(-Y, X);
  148. if Result < 0 then Result := Result + CRad360;
  149. end;
  150. end;
  151. function GetUnitVector(const Pt1, Pt2: TFloatPoint): TFloatPoint;
  152. var
  153. Delta: TFloatPoint;
  154. Temp: TFloat;
  155. begin
  156. Delta.X := (Pt2.X - Pt1.X);
  157. Delta.Y := (Pt2.Y - Pt1.Y);
  158. if (Delta.X = 0) and (Delta.Y = 0) then
  159. Result := FloatPoint(0, 0)
  160. else
  161. begin
  162. Temp := 1 / GR32_Math.Hypot(Delta.X, Delta.Y);
  163. Result.X := Delta.X * Temp;
  164. Result.Y := Delta.Y * Temp;
  165. end;
  166. end;
  167. function GetUnitNormal(const Pt1, Pt2: TFloatPoint): TFloatPoint;
  168. begin
  169. GetUnitNormal(Pt1, Pt2, Result);
  170. end;
  171. procedure GetUnitNormal(const Pt1, Pt2: TFloatPoint; out Result: TFloatPoint);
  172. var
  173. Delta: TFloatPoint;
  174. Temp: TFloat;
  175. begin
  176. Delta.X := (Pt2.X - Pt1.X);
  177. Delta.Y := (Pt2.Y - Pt1.Y);
  178. if (Delta.X = 0) and (Delta.Y = 0) then
  179. Result := FloatPoint(0, 0)
  180. else
  181. begin
  182. Temp := 1 / GR32_Math.Hypot(Delta.X, Delta.Y);
  183. Delta.X := Delta.X * Temp;
  184. Delta.Y := Delta.Y * Temp;
  185. end;
  186. Result.X := Delta.Y; // ie perpendicular to
  187. Result.Y := -Delta.X; // the unit vector
  188. end;
  189. function OffsetPoint(const Pt: TFloatPoint; DeltaX, DeltaY: TFloat): TFloatPoint;
  190. begin
  191. Result.X := Pt.X + DeltaX;
  192. Result.Y := Pt.Y + DeltaY;
  193. end;
  194. function OffsetPoint(const Pt, Delta: TFloatPoint): TFloatPoint;
  195. begin
  196. Result.X := Pt.X + Delta.X;
  197. Result.Y := Pt.Y + Delta.Y;
  198. end;
  199. function OffsetRect(const Rct: TFloatRect; const DeltaX, DeltaY: TFloat): TFloatRect;
  200. begin
  201. Result.TopLeft := OffsetPoint(Rct.TopLeft, DeltaX, DeltaY);
  202. Result.BottomRight := OffsetPoint(Rct.BottomRight, DeltaX, DeltaY);
  203. end;
  204. function OffsetRect(const Rct: TFloatRect; const Delta: TFloatPoint): TFloatRect;
  205. begin
  206. Result.TopLeft := OffsetPoint(Rct.TopLeft, Delta);
  207. Result.BottomRight := OffsetPoint(Rct.BottomRight, Delta);
  208. end;
  209. function Shorten(const Pts: TArrayOfFloatPoint;
  210. Delta: TFloat; LinePos: TLinePos): TArrayOfFloatPoint;
  211. var
  212. Index, HighI: integer;
  213. Dist, DeltaSqr: TFloat;
  214. UnitVec: TFloatPoint;
  215. procedure FixStart;
  216. begin
  217. Index := 1;
  218. while (Index < HighI) and (SqrDistance(Pts[Index], Pts[0]) < DeltaSqr) do
  219. Inc(Index);
  220. UnitVec := GetUnitVector(Pts[Index], Pts[0]);
  221. Dist := Distance(Pts[Index], Pts[0]) - Delta;
  222. if Index > 1 then
  223. begin
  224. HighI := HighI - Index + 1;
  225. Move(Result[Index], Result[1], SizeOf(TFloatPoint) * HighI);
  226. SetLength(Result, HighI + 1);
  227. end;
  228. Result[0] := OffsetPoint(Result[1], UnitVec.X * Dist, UnitVec.Y * Dist);
  229. end;
  230. procedure FixEnd;
  231. begin
  232. Index := HighI - 1;
  233. while (Index > 0) and (SqrDistance(Pts[Index],Pts[HighI]) < DeltaSqr) do
  234. Dec(Index);
  235. UnitVec := GetUnitVector(Pts[Index],Pts[HighI]);
  236. Dist := Distance(Pts[Index], Pts[HighI]) - Delta;
  237. if Index + 1 < HighI then SetLength(Result, Index + 2);
  238. Result[Index + 1] := OffsetPoint(Result[Index], UnitVec.X * Dist, UnitVec.Y * Dist);
  239. end;
  240. begin
  241. Result := Pts;
  242. HighI := High(Pts);
  243. DeltaSqr := Delta * Delta;
  244. if HighI < 1 then Exit;
  245. case LinePos of
  246. lpStart: FixStart;
  247. lpEnd : FixEnd;
  248. lpBoth : begin FixStart; FixEnd; end;
  249. end;
  250. end;
  251. function PointInPolygon(const Pt: TFloatPoint; const Pts: TArrayOfFloatPoint): Boolean;
  252. var
  253. Index: Integer;
  254. iPt, jPt: PFloatPoint;
  255. begin
  256. Result := False;
  257. iPt := @Pts[0];
  258. jPt := @Pts[High(Pts)];
  259. for Index := 0 to High(Pts) do
  260. begin
  261. Result := Result xor (((Pt.Y >= iPt.Y) xor (Pt.Y >= jPt.Y)) and
  262. ((Pt.X - iPt.X) < ((jPt.X - iPt.X) * (Pt.Y -iPt.Y) / (jPt.Y - iPt.Y))));
  263. jPt := iPt;
  264. Inc(iPt);
  265. end;
  266. end;
  267. function SegmentIntersect(const P1, P2, P3, P4: TFloatPoint;
  268. out IntersectPoint: TFloatPoint): Boolean;
  269. var
  270. m1, b1, m2, b2: TFloat;
  271. begin
  272. // see http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
  273. Result := False;
  274. if (P2.X = P1.X) then
  275. begin
  276. if (P4.X = P3.X) then Exit; // parallel lines
  277. m2 := (P4.Y - P3.Y) / (P4.X - P3.X);
  278. b2 := P3.Y - m2 * P3.X;
  279. IntersectPoint.X := P1.X;
  280. IntersectPoint.Y := m2 * P1.X + b2;
  281. Result := (((IntersectPoint.Y < P2.Y) = (IntersectPoint.Y > P1.Y)) or
  282. (IntersectPoint.Y = P2.Y) or (IntersectPoint.Y = P1.Y)) and
  283. (((IntersectPoint.Y < P3.Y) = (IntersectPoint.Y > P4.Y)) or
  284. (IntersectPoint.Y = P3.Y) or (IntersectPoint.Y = P4.Y));
  285. end
  286. else if (P4.X = P3.X) then
  287. begin
  288. m1 := (P2.Y - P1.Y) / (P2.X - P1.X);
  289. b1 := P1.Y - m1 * P1.X;
  290. IntersectPoint.X := P3.X;
  291. IntersectPoint.Y := m1 * P3.X + b1;
  292. Result := (((IntersectPoint.Y < P2.Y) = (IntersectPoint.Y > P1.Y)) or
  293. (IntersectPoint.Y = P2.Y) or (IntersectPoint.Y = P1.Y)) and
  294. (((IntersectPoint.Y < P3.Y) = (IntersectPoint.Y > P4.Y)) or
  295. (IntersectPoint.Y = P3.Y) or (IntersectPoint.Y = P4.Y));
  296. end else
  297. begin
  298. m1 := (P2.Y - P1.Y) / (P2.X - P1.X);
  299. b1 := P1.Y - m1 * P1.X;
  300. m2 := (P4.Y - P3.Y) / (P4.X - P3.X);
  301. b2 := P3.Y - m2 * P3.X;
  302. if m1 = m2 then Exit; // parallel lines
  303. IntersectPoint.X := (b2 - b1) / (m1 - m2);
  304. IntersectPoint.Y := m1 * IntersectPoint.X + b1;
  305. Result := (((IntersectPoint.X < P2.X) = (IntersectPoint.X > P1.X)) or
  306. (IntersectPoint.X = P2.X) or (IntersectPoint.X = P1.X)) and
  307. (((IntersectPoint.X < P3.X) = (IntersectPoint.X > P4.X)) or
  308. (IntersectPoint.X = P3.X) or (IntersectPoint.X = P4.X));
  309. end;
  310. end;
  311. function PerpendicularDistance(const P, P1, P2: TFloatPoint): TFloat;
  312. begin
  313. Result := Abs((P.x - P2.x) * (P1.y - P2.y) - (P.y - P2.y) * (P1.x - P2.x)) /
  314. GR32_Math.Hypot(P1.x - P2.x, P1.y - P2.y);
  315. end;
  316. function SamePoint(const A, B: TFloatPoint; SqrDist: Double): Boolean;
  317. begin
  318. Result := SqrDistance(A, B) < SqrDist;
  319. end;
  320. // Fixed overloads
  321. function Average(const V1, V2: TFixedPoint): TFixedPoint;
  322. begin
  323. Result.X := (V1.X + V2.X) div 2;
  324. Result.Y := (V1.Y + V2.Y) div 2;
  325. end;
  326. function CrossProduct(const V1, V2: TFixedPoint): TFixed;
  327. begin
  328. Result := FixedMul(V1.X, V2.Y) - FixedMul(V1.Y, V2.X);
  329. end;
  330. function Dot(const V1, V2: TFixedPoint): TFixed;
  331. begin
  332. Result := FixedMul(V1.X, V2.X) + FixedMul(V1.Y, V2.Y);
  333. end;
  334. function Distance(const V1, V2: TFixedPoint): TFixed;
  335. begin
  336. Result :=
  337. Fixed(Hypot((V2.X - V1.X) * FixedToFloat, (V2.Y - V1.Y) * FixedToFloat));
  338. end;
  339. function SqrDistance(const V1, V2: TFixedPoint): TFixed;
  340. begin
  341. Result := FixedSqr(V2.X - V1.X) + FixedSqr(V2.Y - V1.Y);
  342. end;
  343. function GetPointAtAngleFromPoint(const Pt: TFixedPoint;
  344. const Dist, Radians: TFloat): TFixedPoint;
  345. var
  346. SinAng, CosAng: TFloat;
  347. begin
  348. GR32_Math.SinCos(Radians, SinAng, CosAng);
  349. Result.X := Round(Dist * CosAng * FixedOne) + Pt.X;
  350. Result.Y := -Round(Dist * SinAng * FixedOne) + Pt.Y; // Y axis is positive down
  351. end;
  352. function GetAngleOfPt2FromPt1(Pt1, Pt2: TFixedPoint): Single;
  353. begin
  354. with Pt2 do
  355. begin
  356. X := X - Pt1.X;
  357. Y := Y - Pt1.Y;
  358. if X = 0 then
  359. begin
  360. if Y > 0 then Result := CRad270 else Result := CRad90;
  361. end else
  362. begin
  363. Result := ArcTan2(-Y,X);
  364. if Result < 0 then Result := Result + CRad360;
  365. end;
  366. end;
  367. end;
  368. function GetUnitVector(const Pt1, Pt2: TFixedPoint): TFloatPoint;
  369. var
  370. Delta: TFloatPoint;
  371. Temp: Single;
  372. begin
  373. Delta.X := (Pt2.X - Pt1.X) * FixedToFloat;
  374. Delta.Y := (Pt2.Y - Pt1.Y) * FixedToFloat;
  375. if (Delta.X = 0) and (Delta.Y = 0) then
  376. begin
  377. Result := FloatPoint(0,0);
  378. end else
  379. begin
  380. Temp := 1 / GR32_Math.Hypot(Delta.X, Delta.Y);
  381. Result.X := Delta.X * Temp;
  382. Result.Y := Delta.Y * Temp;
  383. end;
  384. end;
  385. function GetUnitNormal(const Pt1, Pt2: TFixedPoint): TFloatPoint;
  386. var
  387. Delta: TFloatPoint;
  388. Temp: Single;
  389. begin
  390. Delta.X := (Pt2.X - Pt1.X) * FixedToFloat;
  391. Delta.Y := (Pt2.Y - Pt1.Y) * FixedToFloat;
  392. if (Delta.X = 0) and (Delta.Y = 0) then
  393. begin
  394. Result := FloatPoint(0,0);
  395. end else
  396. begin
  397. Temp := 1 / GR32_Math.Hypot(Delta.X, Delta.Y);
  398. Delta.X := Delta.X * Temp;
  399. Delta.Y := Delta.Y * Temp;
  400. end;
  401. Result.X := Delta.Y; // ie perpendicular to
  402. Result.Y := -Delta.X; // the unit vector
  403. end;
  404. function OffsetPoint(const Pt: TFixedPoint; DeltaX, DeltaY: TFixed): TFixedPoint;
  405. begin
  406. Result.X := Pt.X + DeltaX;
  407. Result.Y := Pt.Y + DeltaY;
  408. end;
  409. function OffsetPoint(const Pt: TFixedPoint; DeltaX, DeltaY: TFloat): TFixedPoint;
  410. begin
  411. Result.X := Pt.X + Fixed(DeltaX);
  412. Result.Y := Pt.Y + Fixed(DeltaY);
  413. end;
  414. function OffsetPoint(const Pt: TFixedPoint; const Delta: TFixedPoint): TFixedPoint;
  415. begin
  416. Result.X := Pt.X + Delta.X;
  417. Result.Y := Pt.Y + Delta.Y;
  418. end;
  419. function OffsetPoint(const Pt: TFixedPoint; const Delta: TFloatPoint): TFixedPoint;
  420. begin
  421. Result.X := Pt.X + Fixed(Delta.X);
  422. Result.Y := Pt.Y + Fixed(Delta.Y);
  423. end;
  424. function OffsetRect(const Rct: TFixedRect; const DeltaX, DeltaY: TFixed): TFixedRect;
  425. begin
  426. Result.TopLeft := OffsetPoint(Rct.TopLeft, DeltaX, DeltaY);
  427. Result.BottomRight := OffsetPoint(Rct.BottomRight, DeltaX, DeltaY);
  428. end;
  429. function OffsetRect(const Rct: TFixedRect; const Delta: TFixedPoint): TFixedRect;
  430. begin
  431. Result.TopLeft := OffsetPoint(Rct.TopLeft, Delta);
  432. Result.BottomRight := OffsetPoint(Rct.BottomRight, Delta);
  433. end;
  434. function OffsetRect(const Rct: TFixedRect; const DeltaX, DeltaY: TFloat): TFixedRect;
  435. var
  436. DX, DY: TFixed;
  437. begin
  438. DX := Fixed(DeltaX);
  439. DY := Fixed(DeltaY);
  440. Result.TopLeft := OffsetPoint(Rct.TopLeft, DX, DY);
  441. Result.BottomRight := OffsetPoint(Rct.BottomRight, DX, DY);
  442. end;
  443. function OffsetRect(const Rct: TFixedRect; const Delta: TFloatPoint): TFixedRect;
  444. var
  445. DX, DY: TFixed;
  446. begin
  447. DX := Fixed(Delta.X);
  448. DY := Fixed(Delta.Y);
  449. Result.TopLeft := OffsetPoint(Rct.TopLeft, DX, DY);
  450. Result.BottomRight := OffsetPoint(Rct.BottomRight, DX, DY);
  451. end;
  452. function Shorten(const Pts: TArrayOfFixedPoint;
  453. Delta: TFloat; LinePos: TLinePos): TArrayOfFixedPoint;
  454. var
  455. Index, HighI: integer;
  456. Dist, DeltaSqr: TFloat;
  457. UnitVec: TFloatPoint;
  458. procedure FixStart;
  459. begin
  460. Index := 1;
  461. while (Index < HighI) and (SqrDistance(Pts[Index],Pts[0]) < DeltaSqr) do Inc(Index);
  462. UnitVec := GetUnitVector(Pts[Index], Pts[0]);
  463. Dist := Distance(Pts[Index],Pts[0]) - Delta;
  464. if Index > 1 then
  465. begin
  466. Move(Result[Index], Result[1], SizeOf(TFloatPoint) * (HighI - Index + 1));
  467. SetLength(Result, HighI - Index + 2);
  468. HighI := HighI - Index + 1;
  469. end;
  470. Result[0] := OffsetPoint(Result[1], UnitVec.X * Dist, UnitVec.Y * Dist);
  471. end;
  472. procedure FixEnd;
  473. begin
  474. Index := HighI -1;
  475. while (Index > 0) and (SqrDistance(Pts[Index],Pts[HighI]) < DeltaSqr) do Dec(Index);
  476. UnitVec := GetUnitVector(Pts[Index],Pts[HighI]);
  477. Dist := Distance(Pts[Index],Pts[HighI]) - Delta;
  478. if Index + 1 < HighI then SetLength(Result, Index + 2);
  479. Result[Index + 1] := OffsetPoint(Result[Index], UnitVec.X * Dist, UnitVec.Y * Dist);
  480. end;
  481. begin
  482. Result := Pts;
  483. HighI := High(Pts);
  484. DeltaSqr := Delta * Delta;
  485. if HighI < 1 then Exit;
  486. case LinePos of
  487. lpStart: FixStart;
  488. lpEnd : FixEnd;
  489. lpBoth : begin FixStart; FixEnd; end;
  490. end;
  491. end;
  492. function PointInPolygon(const Pt: TFixedPoint; const Pts: array of TFixedPoint): Boolean;
  493. var
  494. I: Integer;
  495. iPt, jPt: PFixedPoint;
  496. begin
  497. Result := False;
  498. iPt := @Pts[0];
  499. jPt := @Pts[High(Pts)];
  500. for I := 0 to High(Pts) do
  501. begin
  502. Result := Result xor (((Pt.Y >= iPt.Y) xor (Pt.Y >= jPt.Y)) and
  503. (Pt.X - iPt.X < MulDiv(jPt.X - iPt.X, Pt.Y - iPt.Y, jPt.Y - iPt.Y)));
  504. jPt := iPt;
  505. Inc(iPt);
  506. end;
  507. end;
  508. function SegmentIntersect(const P1, P2, P3, P4: TFixedPoint;
  509. out IntersectPoint: TFixedPoint): Boolean;
  510. var
  511. m1,b1,m2,b2: TFloat;
  512. begin
  513. Result := False;
  514. if (P2.X = P1.X) then
  515. begin
  516. if (P4.X = P3.X) then Exit; // parallel lines
  517. m2 := (P4.Y - P3.Y) / (P4.X - P3.X);
  518. b2 := P3.Y - m2 * P3.X;
  519. IntersectPoint.X := P1.X;
  520. IntersectPoint.Y := Round(m2 * P1.X + b2);
  521. Result := (IntersectPoint.Y < P2.Y) = (IntersectPoint.Y > P1.Y);
  522. end
  523. else if (P4.X = P3.X) then
  524. begin
  525. m1 := (P2.Y - P1.Y) / (P2.X - P1.X);
  526. b1 := P1.Y - m1 * P1.X;
  527. IntersectPoint.X := P3.X;
  528. IntersectPoint.Y := Round(m1 * P3.X + b1);
  529. Result := (IntersectPoint.Y < P3.Y) = (IntersectPoint.Y > P4.Y);
  530. end else
  531. begin
  532. m1 := (P2.Y - P1.Y) / (P2.X - P1.X);
  533. b1 := P1.Y - m1 * P1.X;
  534. m2 := (P4.Y - P3.Y) / (P4.X - P3.X);
  535. b2 := P3.Y - m2 * P3.X;
  536. if m1 = m2 then Exit; // parallel lines
  537. IntersectPoint.X := Round((b2 - b1) / (m1 - m2));
  538. IntersectPoint.Y := Round(m1 * IntersectPoint.X + b1);
  539. Result := ((IntersectPoint.X < P2.X) = (IntersectPoint.X > P1.X));
  540. end;
  541. end;
  542. function PerpendicularDistance(const P, P1, P2: TFixedPoint): TFixed;
  543. begin
  544. Result := Fixed(Abs((P.x - P2.x) * (P1.y - P2.y) - (P.y - P2.y) *
  545. (P1.x - P2.x)) * FixedToFloat / Hypot((P1.x - P2.x) * FixedToFloat,
  546. (P1.y - P2.y) * FixedToFloat));
  547. end;
  548. function SamePoint(const A, B: TFixedPoint; SqrDist: TFixed): Boolean; overload;
  549. begin
  550. Result := SqrDistance(A, B) < SqrDist;
  551. end;
  552. // Integer overloads
  553. function Average(const V1, V2: TPoint): TPoint;
  554. begin
  555. Result.X := (V1.X + V2.X) div 2;
  556. Result.Y := (V1.Y + V2.Y) div 2;
  557. end;
  558. function CrossProduct(const V1, V2: TPoint): Integer;
  559. begin
  560. Result := V1.X * V2.Y - V1.Y * V2.X;
  561. end;
  562. function Dot(const V1, V2: TPoint): Integer;
  563. begin
  564. Result := V1.X * V2.X + V1.Y * V2.Y;
  565. end;
  566. function Distance(const V1, V2: TPoint): TFloat;
  567. begin
  568. Result := Hypot(Integer(V2.X - V1.X), Integer(V2.Y - V1.Y));
  569. end;
  570. function SqrDistance(const V1, V2: TPoint): Integer;
  571. begin
  572. Result := Sqr(V2.X - V1.X) + Sqr(V2.Y - V1.Y);
  573. end;
  574. function OffsetPoint(const Pt: TPoint; DeltaX, DeltaY: Integer): TPoint;
  575. begin
  576. Result.X := Pt.X + DeltaX;
  577. Result.Y := Pt.Y + DeltaY;
  578. end;
  579. function OffsetPoint(const Pt, Delta: TPoint): TPoint;
  580. begin
  581. Result.X := Pt.X + Delta.X;
  582. Result.Y := Pt.Y + Delta.Y;
  583. end;
  584. function PerpendicularDistance(const P, P1, P2: TPoint): TFloat;
  585. begin
  586. Result := Abs((P.x - P2.x) * (P1.y - P2.y) - (P.y - P2.y) * (P1.x - P2.x)) /
  587. Math.Hypot(P1.x - P2.x, P1.y - P2.y);
  588. end;
  589. function SamePoint(const A, B: TPoint; SqrDist: integer): Boolean; overload;
  590. begin
  591. Result := SqrDistance(A, B) < SqrDist;
  592. end;
  593. end.