Debian Codegraph @

components/Image32/source/Img32.Vector.pas

raw
1 unit Img32.Vector;
2
3 (*******************************************************************************
4 * Author : Angus Johnson *
5 * Version : 4.7 *
6 * Date : 6 January 2025 *
7 * Website : http://www.angusj.com *
8 * Copyright : Angus Johnson 2019-2025 *
9 * *
10 * Purpose : Vector drawing for TImage32 *
11 * *
12 * License : Use, modification & distribution is subject to *
13 * Boost Software License Ver 1 *
14 * http://www.boost.org/LICENSE_1_0.txt *
15 *******************************************************************************)
16
17 interface
18
19 {$I Img32.inc}
20
21 uses
22 SysUtils, Classes, Math, Types, Img32;
23
24 type
25 TArrowStyle = (asNone, asSimple, asFancy, asDiamond, asCircle, asTail);
26 // TJoinStyle:
27 // jsSquare - Convex joins will be truncated using a 'squaring' edge.
28 // The mid-points of these squaring edges will also be exactly the offset
29 // (ie delta) distance away from their origins (ie the starting vertices).
30 // jsButt - joins are similar to 'squared' joins except that squaring
31 // won't occur at a fixed distance. While bevelled joins may not be as
32 // pretty as squared joins, bevelling will be much faster than squaring.
33 // And perhaps this is why bevelling (rather than squaring) is preferred
34 // in numerous graphics display formats (including SVG & PDF documents).
35 TJoinStyle = (jsAuto, jsSquare, jsButt, jsMiter, jsRound);
36 TEndStyle = (esPolygon = 0, esClosed = 0, esButt, esSquare, esRound);
37 TPathEnd = (peStart, peEnd, peBothEnds);
38 TSplineType = (stQuadratic, stCubic);
39 TFillRule = (frEvenOdd, frNonZero, frPositive, frNegative);
40 TImg32FillRule = TFillRule; //useful whenever there's ambiguity with Clipper
41
42 TSizeD = {$IFDEF RECORD_METHODS} record {$ELSE} object {$ENDIF}
43 cx : double;
44 cy : double;
45 function average: double;
46 property Width: Double read cx write cx;
47 property Height: Double read cy write cy;
48 end;
49
50 TRectWH = {$IFDEF RECORD_METHODS} record {$ELSE} object {$ENDIF}
51 public
52 Left, Top, Width, Height: double;
53 function IsEmpty: Boolean;
54 function IsValid: Boolean;
55 function Right: double;
56 function Bottom: double;
57 function Contains(const Pt: TPoint): Boolean; overload;
58 function Contains(const Pt: TPointD): Boolean; overload;
59 function MidPoint: TPointD;
60 function RectD: TRectD;
61 function Rect: TRect;
62 end;
63
64 function RectWH(left, top, width, height: integer): TRectWH; overload;
65 function RectWH(left, top, width, height: double ): TRectWH; overload;
66 function RectWH(const rec: TRectD): TRectWH; overload;
67
68 //InflateRect: missing in Delphi 7
69 procedure InflateRect(var rec: TRect; dx, dy: integer); overload;
70 procedure InflateRect(var rec: TRectD; dx, dy: double); overload;
71
72 function NormalizeRect(var rect: TRect): Boolean;
73
74 function PrePendPoint(const pt: TPointD; const p: TPathD): TPathD; overload;
75 procedure PrePendPoint(const pt: TPointD; const p: TPathD; var Result: TPathD); overload;
76 function PrePendPoints(const pt1, pt2: TPointD; const p: TPathD): TPathD;
77
78 function Rectangle(const rec: TRect): TPathD; overload;
79 function Rectangle(const rec: TRectD): TPathD; overload;
80 function Rectangle(l, t, r, b: double): TPathD; overload;
81
82 function RoundRect(const rec: TRect; radius: integer): TPathD; overload;
83 function RoundRect(const rec: TRectD; radius: double): TPathD; overload;
84 function RoundRect(const rec: TRect; radius: TPoint): TPathD; overload;
85 function RoundRect(const rec: TRectD; radius: TPointD): TPathD; overload;
86
87 function Ellipse(const rec: TRect; steps: integer = 0): TPathD; overload;
88 function Ellipse(const rec: TRectD; steps: integer = 0): TPathD; overload;
89 function Ellipse(const rec: TRectD; pendingScale: double): TPathD; overload;
90
91 function RotatedEllipse(const rec: TRectD; angle: double; steps: integer = 0): TPathD; overload;
92 function RotatedEllipse(const rec: TRectD; angle: double; pendingScale: double): TPathD; overload;
93
94 function AngleToEllipticalAngle(const ellRec: TRectD; angle: double): double;
95
96 function EllipticalAngleToAngle(const ellRec: TRectD; angle: double): double;
97
98 function Circle(const pt: TPoint; radius: double): TPathD; overload;
99 function Circle(const pt: TPointD; radius: double): TPathD; overload;
100 function Circle(const pt: TPointD; radius: double; pendingScale: double): TPathD; overload;
101
102 function CalcCircleFrom3Points(const p1,p2,p3: TPointD;
103 out centre: TPointD; out radius: double): Boolean;
104
105 function Star(const rec: TRectD; points: integer; indentFrac: double = 0.4): TPathD; overload;
106 function Star(const focalPt: TPointD;
107 innerRadius, outerRadius: double; points: integer): TPathD; overload;
108
109 function Arc(const rec: TRectD;
110 startAngle, endAngle: double; scale: double = 0): TPathD;
111
112 function Pie(const rec: TRectD;
113 StartAngle, EndAngle: double; scale: double = 0): TPathD;
114
115 function FlattenQBezier(const pt1, pt2, pt3: TPointD;
116 tolerance: double = 0.0): TPathD; overload;
117 function FlattenQBezier(const pts: TPathD;
118 tolerance: double = 0.0): TPathD; overload;
119 function FlattenQBezier(const firstPt: TPointD; const pts: TPathD;
120 tolerance: double = 0.0): TPathD; overload;
121
122 function GetPointInQuadBezier(const a,b,c: TPointD; t: double): TPointD;
123
124 function FlattenCBezier(const pt1, pt2, pt3, pt4: TPointD;
125 tolerance: double = 0.0): TPathD; overload;
126 function FlattenCBezier(const path: TPathD;
127 tolerance: double = 0.0): TPathD; overload;
128 function FlattenCBezier(const paths: TPathsD;
129 tolerance: double = 0.0): TPathsD; overload;
130 function FlattenCBezier(const firstPt: TPointD; const pts: TPathD;
131 tolerance: double = 0.0): TPathD; overload;
132
133 function GetPointInCubicBezier(const a,b,c,d: TPointD; t: double): TPointD;
134
135 //FlattenCSpline: Approximates the 'S' command inside the 'd' property of an
136 //SVG path. (See https://www.w3.org/TR/SVG/paths.html#DProperty)
137 function FlattenCSpline(const pts: TPathD;
138 tolerance: double = 0.0): TPathD; overload;
139 function FlattenCSpline(const priorCtrlPt, startPt: TPointD;
140 const pts: TPathD; tolerance: double = 0.0): TPathD; overload;
141
142 //FlattenQSpline: Approximates the 'T' command inside the 'd' property of an
143 //SVG path. (See https://www.w3.org/TR/SVG/paths.html#DProperty)
144 function FlattenQSpline(const pts: TPathD;
145 tolerance: double = 0.0): TPathD; overload;
146 function FlattenQSpline(const priorCtrlPt, startPt: TPointD;
147 const pts: TPathD; tolerance: double = 0.0): TPathD; overload;
148
149 //ArrowHead: The ctrlPt's only function is to control the angle of the arrow.
150 function ArrowHead(const arrowTip, ctrlPt: TPointD; size: double;
151 arrowStyle: TArrowStyle): TPathD;
152
153 function GetDefaultArrowHeadSize(lineWidth: double): double;
154
155 procedure AdjustPoint(var pt: TPointD;
156 const referencePt: TPointD; delta: double);
157
158 function ShortenPath(const path: TPathD;
159 pathEnd: TPathEnd; amount: double): TPathD;
160
161 //GetDashPath: Returns a polyline (not polygons)
162 function GetDashedPath(const path: TPathD;
163 closed: Boolean; const pattern: TArrayOfDouble;
164 patternOffset: PDouble): TPathsD;
165
166 function GetDashedOutLine(const path: TPathD;
167 closed: Boolean; const pattern: TArrayOfDouble;
168 patternOffset: PDouble; lineWidth: double;
169 joinStyle: TJoinStyle; endStyle: TEndStyle): TPathsD;
170
171 function TranslatePoint(const pt: TPoint; dx, dy: integer): TPoint; overload;
172 function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD; overload;
173
174 function TranslatePath(const path: TPathD;
175 dx, dy: double): TPathD; overload;
176 function TranslatePath(const paths: TPathsD;
177 dx, dy: double): TPathsD; overload;
178 function TranslatePath(const ppp: TArrayOfPathsD;
179 dx, dy: double): TArrayOfPathsD; overload;
180
181 function Paths(const path: TPathD): TPathsD;
182 {$IFDEF INLINING} inline; {$ENDIF}
183
184 //CopyPath: note that only dynamic string arrays are copy-on-write
185 function CopyPath(const path: TPathD): TPathD;
186 {$IFDEF INLINING} inline; {$ENDIF}
187 function CopyPaths(const paths: TPathsD): TPathsD;
188
189 function ScalePoint(const pt: TPointD; scale: double): TPointD; overload;
190 {$IFDEF INLINING} inline; {$ENDIF}
191 function ScalePoint(const pt: TPointD; sx, sy: double): TPointD; overload;
192 {$IFDEF INLINING} inline; {$ENDIF}
193 function ScalePath(const path: TPathD;
194 sx, sy: double): TPathD; overload;
195
196 function ScalePath(const path: TPathD;
197 scale: double): TPathD; overload;
198 function ScalePath(const paths: TPathsD;
199 sx, sy: double): TPathsD; overload;
200 function ScalePath(const paths: TPathsD;
201 scale: double): TPathsD; overload;
202
203 function ScaleRect(const rec: TRect; scale: double): TRect; overload;
204 function ScaleRect(const rec: TRectD; scale: double): TRectD; overload;
205 function ScaleRect(const rec: TRect; sx, sy: double): TRect; overload;
206 function ScaleRect(const rec: TRectD; sx, sy: double): TRectD; overload;
207
208 function ScalePathToFit(const path: TPathD; const rec: TRect): TPathD;
209 function ScalePathsToFit(const paths: TPathsD; const rec: TRect): TPathsD;
210
211 function ReversePath(const path: TPathD): TPathD; overload;
212 function ReversePath(const paths: TPathsD): TPathsD; overload;
213
214 function OpenPathToFlatPolygon(const path: TPathD): TPathD;
215
216 procedure AppendPoint(var path: TPathD; const extra: TPointD);
217
218 // AppendPath - adds TPathD & TPathsD objects to the end of
219 // TPathsD (or TArrayOfPathsD) objects
220 procedure AppendPath(var paths: TPathsD; const extra: TPathD); overload;
221 procedure AppendPath(var paths: TPathsD; const extra: TPathsD); overload;
222 procedure AppendPath(var ppp: TArrayOfPathsD; const extra: TPathsD); overload;
223
224 // ConcatPaths - concats multiple paths into a single path.
225 // It also avoids point duplicates where path joins
226 procedure ConcatPaths(var dstPath: TPathD; const path: TPathD); overload;
227 procedure ConcatPaths(var dstPath: TPathD; const paths: TPathsD); overload;
228
229 function GetAngle(const origin, pt: TPoint): double; overload;
230 function GetAngle(const origin, pt: TPointD): double; overload;
231 function GetAngle(const a, b, c: TPoint): double; overload;
232 function GetAngle(const a, b, c: TPointD): double; overload;
233
234 procedure GetSinCos(angle: double; out sinA, cosA: double);
235
236 function GetPointAtAngleAndDist(const origin: TPointD;
237 angle, distance: double): TPointD;
238
239 function IntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD; overload;
240 function IntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPointD; out ip: TPointD): Boolean; overload;
241
242 function SegmentIntersectPt(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD;
243 function SegmentsIntersect(const ln1a, ln1b, ln2a, ln2b: TPointD;
244 out ip: TPointD): Boolean;
245
246 procedure RotatePoint(var pt: TPointD;
247 const focalPoint: TPointD; sinA, cosA: double); overload;
248 procedure RotatePoint(var pt: TPointD;
249 const focalPoint: TPointD; angleRad: double); overload;
250
251 function RotatePath(const path: TPathD;
252 const focalPoint: TPointD; angleRads: double): TPathD; overload;
253 function RotatePath(const paths: TPathsD;
254 const focalPoint: TPointD; angleRads: double): TPathsD; overload;
255
256 //function MakePath(const pts: array of integer): TPathD; overload;
257 function MakePath(const pts: array of double): TPathD; overload;
258 function MakePath(const pt: TPointD): TPathD; overload;
259
260 function GetBounds(const path: TPathD): TRect; overload;
261 function GetBounds(const paths: TPathsD): TRect; overload;
262
263 function GetBoundsD(const path: TPathD): TRectD; overload;
264 function GetBoundsD(const paths: TPathsD): TRectD; overload;
265 function GetBoundsD(const paths: TArrayOfPathsD): TRectD; overload;
266
267 function GetRotatedRectBounds(const rec: TRect; angle: double): TRect; overload;
268 function GetRotatedRectBounds(const rec: TRectD; angle: double): TRectD; overload;
269
270 function Rect(const recD: TRectD): TRect; overload;
271 function Rect(const left,top,right,bottom: integer): TRect; overload;
272
273 function PtInRect(const rec: TRectD; const pt: TPointD): Boolean; overload;
274
275 function Size(cx, cy: integer): TSize;
276 function SizeD(cx, cy: double): TSizeD;
277
278 function IsClockwise(const path: TPathD): Boolean;
279
280 // IsSimpleRectanglePath returns true if the specified path has only one polygon
281 // with 4 points that describe a rectangle.
282 function IsSimpleRectanglePath(const paths: TPathsD; var R: TRect): Boolean; overload;
283 function IsSimpleRectanglePath(const path: TPathD; var R: TRect): Boolean; overload;
284
285 function Area(const path: TPathD): Double; overload;
286
287 function RectsEqual(const rec1, rec2: TRect): Boolean;
288
289 procedure TranslateRect(var rec: TRect; dx, dy: integer); overload;
290 procedure TranslateRect(var rec: TRectD; dx, dy: double); overload;
291
292 function MakeSquare(rec: TRect): TRect;
293
294 function IsValid(value: integer): Boolean; overload;
295 function IsValid(value: double): Boolean; overload;
296 function IsValid(const pt: TPoint): Boolean; overload;
297 function IsValid(const pt: TPointD): Boolean; overload;
298 function IsValid(const rec: TRect): Boolean; overload;
299
300 function Point(X,Y: Integer): TPoint; overload;
301 function Point(const pt: TPointD): TPoint; overload;
302
303 function PointsEqual(const pt1, pt2: TPointD): Boolean; overload;
304 {$IFDEF INLINING} inline; {$ENDIF}
305
306 function PointsNearEqual(const pt1, pt2: TPoint;
307 dist: integer): Boolean; overload;
308 function PointsNearEqual(const pt1, pt2: TPointD;
309 distSqrd: double): Boolean; overload;
310 {$IFDEF INLINING} inline; {$ENDIF}
311
312 function StripNearDuplicates(const path: TPathD;
313 minDist: double; isClosedPath: Boolean): TPathD; overload;
314 function StripNearDuplicates(const paths: TPathsD;
315 minLength: double; isClosedPaths: Boolean): TPathsD; overload;
316
317 function MidPoint(const rec: TRect): TPoint; overload;
318 function MidPoint(const rec: TRectD): TPointD; overload;
319 function MidPoint(const pt1, pt2: TPoint): TPoint; overload;
320 function MidPoint(const pt1, pt2: TPointD): TPointD; overload;
321
322 function Average(val1, val2: integer): integer; overload;
323 function Average(val1, val2: double): double; overload;
324
325 function ReflectPoint(const pt, pivot: TPointD): TPointD;
326 {$IFDEF INLINING} inline; {$ENDIF}
327
328 function RectsOverlap(const rec1, rec2: TRect): Boolean;
329
330 function IsSameRect(const rec1, rec2: TRect): Boolean;
331
332 function RectsIntersect(const rec1, rec2: TRect): Boolean; overload;
333 function RectsIntersect(const rec1, rec2: TRectD): Boolean; overload;
334 function IntersectRect(const rec1, rec2: TRectD): TRectD; overload;
335
336 // UnionRect: this behaves differently to types.UnionRect
337 // in that if either parameter is empty the other parameter is returned
338 function UnionRect(const rec1, rec2: TRect): TRect; overload;
339 function UnionRect(const rec1, rec2: TRectD): TRectD; overload;
340
341 //these 2 functions are only needed to support older versions of Delphi
342 function MakeArrayOfInteger(const ints: array of integer): TArrayOfInteger;
343 function MakeArrayOfDouble(const doubles: array of double): TArrayOfDouble;
344
345 function CrossProduct(const vector1, vector2: TPointD): double; overload;
346 {$IFDEF INLINING} inline; {$ENDIF}
347 function CrossProduct(const pt1, pt2, pt3: TPointD): double; overload;
348 {$IFDEF INLINING} inline; {$ENDIF}
349 function CrossProduct(const pt1, pt2, pt3, pt4: TPointD): double; overload;
350 {$IFDEF INLINING} inline; {$ENDIF}
351
352 function DotProduct(const vector1, vector2: TPointD): double; overload;
353 {$IFDEF INLINING} inline; {$ENDIF}
354 function DotProduct(const pt1, pt2, pt3: TPointD): double; overload;
355 {$IFDEF INLINING} inline; {$ENDIF}
356
357 function TurnsLeft(const pt1, pt2, pt3: TPointD): boolean;
358 {$IFDEF INLINING} inline; {$ENDIF}
359 function TurnsRight(const pt1, pt2, pt3: TPointD): boolean;
360 {$IFDEF INLINING} inline; {$ENDIF}
361
362 function IsPathConvex(const path: TPathD): Boolean;
363
364 function NormalizeVector(const vec: TPointD): TPointD;
365 {$IFDEF INLINING} inline; {$ENDIF}
366
367 //GetUnitVector: Used internally
368 function GetUnitVector(const pt1, pt2: TPointD): TPointD;
369
370 //GetUnitNormal: Used internally
371 function GetUnitNormal(const pt1, pt2: TPointD): TPointD; overload;
372 {$IFDEF INLINING} inline; {$ENDIF}
373 function GetUnitNormal(const pt1, pt2: TPointD; out norm: TPointD): Boolean; overload;
374 {$IFDEF INLINING} inline; {$ENDIF}
375
376 function GetAvgUnitVector(const vec1, vec2: TPointD): TPointD;
377 {$IFDEF INLINING} inline; {$ENDIF}
378
379 //GetVectors: Used internally
380 function GetVectors(const path: TPathD): TPathD;
381 //GetNormals: Used internally
382
383 function GetNormals(const path: TPathD): TPathD;
384
385 //DistanceSqrd: Used internally
386 function DistanceSqrd(const pt1, pt2: TPoint): double; overload;
387 {$IFDEF INLINE} inline; {$ENDIF}
388 //DistanceSqrd: Used internally
389 function DistanceSqrd(const pt1, pt2: TPointD): double; overload;
390 {$IFDEF INLINE} inline; {$ENDIF}
391
392 function Distance(const pt1, pt2: TPoint): double; overload;
393 {$IFDEF INLINE} inline; {$ENDIF}
394 function Distance(const pt1, pt2: TPointD): double; overload;
395 {$IFDEF INLINE} inline; {$ENDIF}
396 function Distance(const path: TPathD; stopAt: integer = 0): double; overload;
397
398 function GetDistances(const path: TPathD): TArrayOfDouble;
399
400 function GetCumulativeDistances(const path: TPathD): TArrayOfDouble;
401
402 function PerpendicularDistSqrd(const pt, line1, line2: TPointD): double;
403
404 function PointInPolygon(const pt: TPointD;
405 const polygon: TPathD; fillRule: TFillRule): Boolean;
406
407 function PointInPolygons(const pt: TPointD;
408 const polygons: TPathsD; fillRule: TFillRule): Boolean;
409
410 function PerpendicularDist(const pt, line1, line2: TPointD): double;
411
412 function ClosestPointOnLine(const pt, linePt1, linePt2: TPointD): TPointD;
413
414 function ClosestPointOnSegment(const pt, segPt1, segPt2: TPointD): TPointD;
415
416 function IsPointInEllipse(const ellipseRec: TRect; const pt: TPoint): Boolean;
417
418 //GetLineEllipseIntersects: Gets the intersection of a line and
419 //an ellipse. The function succeeds when the line either touches
420 //tangentially or passes through the ellipse. If the line touches
421 //tangentially, the coordintates returned in pt1 and pt2 will match.
422 function GetLineEllipseIntersects(const ellipseRec: TRect;
423 var linePt1, linePt2: TPointD): Boolean;
424
425 function GetPtOnEllipseFromAngle(const ellipseRect: TRectD; angle: double): TPointD;
426
427 function GetPtOnRotatedEllipseFromAngle(const ellipseRect: TRectD;
428 ellipseRotAngle, angle: double): TPointD;
429
430 function GetEllipticalAngleFromPoint(const ellipseRect: TRectD;
431 const pt: TPointD): double;
432
433 function GetRotatedEllipticalAngleFromPoint(const ellipseRect: TRectD;
434 ellipseRotAngle: double; pt: TPointD): double;
435
436 function GetClosestPtOnRotatedEllipse(const ellipseRect: TRectD;
437 ellipseRotation: double; const pt: TPointD): TPointD;
438
439 // RoughOutline: outlines are **rough** because they will contain numerous
440 // self-intersections and negative area regions. (This untidiness will be
441 // hidden as long as the NonZero fill rule is applied when rendering, and
442 // this function will be **much** faster than Img32.Clipper.InflatePaths.)
443 // The 'scale' parameter doesn't actually scale the returned outline, it's
444 // only a warning of future scaling and used to guide the returned precision.
445 // RoughOutline is intended mostly for internal use.
446 function RoughOutline(const line: TPathD; lineWidth: double;
447 joinStyle: TJoinStyle; endStyle: TEndStyle;
448 miterLim: double = 0; scale: double = 1.0): TPathsD; overload;
449 function RoughOutline(const lines: TPathsD; lineWidth: double;
450 joinStyle: TJoinStyle; endStyle: TEndStyle;
451 miterLim: double = 0; scale: double = 1.0): TPathsD; overload;
452
453 // Grow: For the same reasons stated in RoughOutline's comments above,
454 // this function is also intended mostly for internal use
455 function Grow(const path, normals: TPathD; delta: double;
456 joinStyle: TJoinStyle; miterLim: double = 0; scale: double = 1.0; isOpen: Boolean = false): TPathD;
457
458 function ValueAlmostZero(val: double; epsilon: double = 0.001): Boolean;
459 function ValueAlmostOne(val: double; epsilon: double = 0.001): Boolean;
460 const
461 Invalid = -MaxInt;
462 InvalidD = -Infinity;
463 NullPoint : TPoint = (X: 0; Y: 0);
464 NullPointD : TPointD = (X: 0; Y: 0);
465 InvalidPoint : TPoint = (X: -MaxInt; Y: -MaxInt);
466 InvalidPointD : TPointD = (X: -Infinity; Y: -Infinity);
467 NullRect : TRect = (left: 0; top: 0; right: 0; Bottom: 0);
468 NullRectD : TRectD = (left: 0; top: 0; right: 0; Bottom: 0);
469 InvalidRect : TRect = (left: MaxInt; top: MaxInt; right: 0; Bottom: 0);
470 BezierTolerance: double = 0.25;
471 DoubleTolerance: double = 1.0e-12;
472 var
473 //AutoWidthThreshold: When JoinStyle = jsAuto, this is the threshold at
474 //which line joins will be rounded instead of squared. With wider strokes,
475 //rounded joins generally look better, but as rounding is more complex it
476 //also requries more processing and hence is slower to execute.
477 AutoWidthThreshold: double = 5.0;
478 //When lines are too narrow, they become too faint to sensibly draw
479 MinStrokeWidth: double = 0.5;
480 //Miter limit avoids excessive spikes when line offsetting
481 DefaultMiterLimit: double = 4.0;
482
483 resourcestring
484 rsInvalidMatrix = 'Invalid matrix.'; //nb: always start with IdentityMatrix
485
486 implementation
487
488 uses
489 Img32.Transform;
490
491 resourcestring
492 rsInvalidQBezier = 'Invalid number of control points for a QBezier';
493 rsInvalidCBezier = 'Invalid number of control points for a CBezier';
494
495 const
496 BuffSize = 64;
497
498 {$IFDEF CPUX86}
499 // Use faster Trunc for x86 code in this unit.
500 Trunc: function(Value: Double): Integer = __Trunc;
501 {$ENDIF CPUX86}
502
503 //------------------------------------------------------------------------------
504 // TSizeD
505 //------------------------------------------------------------------------------
506
507 function TSizeD.average: double;
508 begin
509 Result := (cx + cy) * 0.5;
510 end;
511
512 //------------------------------------------------------------------------------
513 // TRectWH record/object.
514 //------------------------------------------------------------------------------
515
516 function TRectWH.IsEmpty: Boolean;
517 begin
518 Result := (Width <= 0) or (Height <= 0);
519 end;
520 //------------------------------------------------------------------------------
521
522 function TRectWH.IsValid: Boolean;
523 begin
524 Result := (Left <> InvalidD) and (Top <> InvalidD)
525 and (Width >= 0) and (Height >= 0);
526 end;
527 //------------------------------------------------------------------------------
528
529 function TRectWH.Right: double;
530 begin
531 Result := Left + Width;
532 end;
533 //------------------------------------------------------------------------------
534
535 function TRectWH.Bottom: double;
536 begin
537 Result := Top + Height;
538 end;
539 //------------------------------------------------------------------------------
540
541 function TRectWH.Contains(const Pt: TPoint): Boolean;
542 begin
543 Result := (pt.X >= Left) and (pt.X <= Left + Width) and
544 (pt.Y >= Top) and (pt.Y <= Top + Height)
545 end;
546 //------------------------------------------------------------------------------
547
548 function TRectWH.Contains(const Pt: TPointD): Boolean;
549 begin
550 Result := (pt.X >= Left) and (pt.X <= Left + Width) and
551 (pt.Y >= Top) and (pt.Y <= Top + Height)
552 end;
553 //------------------------------------------------------------------------------
554
555 function TRectWH.MidPoint: TPointD;
556 begin
557 Result := PointD(left + Width * 0.5, top + Height * 0.5);
558 end;
559 //------------------------------------------------------------------------------
560
561 function TRectWH.RectD: TRectD;
562 begin
563 Result := Img32.RectD(left, top, left + Width, top + Height);
564 end;
565 //------------------------------------------------------------------------------
566
567 function TRectWH.Rect: TRect;
568 begin
569 Result := Img32.Vector.Rect(RectD);
570 end;
571 //------------------------------------------------------------------------------
572
573 function RectWH(left, top, width, height: integer): TRectWH;
574 begin
575 Result.Left := left;
576 Result.Top := top;
577 Result.Width := width;
578 Result.Height := height;
579 end;
580 //------------------------------------------------------------------------------
581
582 function RectWH(left, top, width, height: double): TRectWH;
583 begin
584 Result.Left := left;
585 Result.Top := top;
586 Result.Width := width;
587 Result.Height := height;
588 end;
589 //------------------------------------------------------------------------------
590
591 function RectWH(const rec: TRectD): TRectWH;
592 begin
593 Result.Left := rec.left;
594 Result.Top := rec.top;
595 Result.Width := rec.width;
596 Result.Height := rec.height;
597 end;
598 //------------------------------------------------------------------------------
599
600 function RectsEqual(const rec1, rec2: TRect): Boolean;
601 begin
602 result := (rec1.Left = rec2.Left) and (rec1.Top = rec2.Top) and
603 (rec1.Right = rec2.Right) and (rec1.Bottom = rec2.Bottom);
604 end;
605 //------------------------------------------------------------------------------
606
607 function Rect(const left, top, right, bottom: integer): TRect;
608 begin
609 Result.Left := left;
610 Result.Top := top;
611 Result.Right := right;
612 Result.Bottom := bottom;
613 end;
614 //------------------------------------------------------------------------------
615
616 function IsValid(value: integer): Boolean;
617 begin
618 Result := value <> -MaxInt;
619 end;
620 //------------------------------------------------------------------------------
621
622 function IsValid(value: double): Boolean;
623 begin
624 Result := value <> InvalidD;
625 end;
626 //------------------------------------------------------------------------------
627
628 function IsValid(const pt: TPoint): Boolean;
629 begin
630 result := (pt.X <> Invalid) and (pt.Y <> Invalid);
631 end;
632 //------------------------------------------------------------------------------
633
634 function IsValid(const pt: TPointD): Boolean;
635 begin
636 result := (pt.X <> -Infinity) and (pt.Y <> -Infinity);
637 end;
638 //------------------------------------------------------------------------------
639
640 function IsValid(const rec: TRect): Boolean;
641 begin
642 result := (rec.Left <> MaxInt) and (rec.Top <> MaxInt);
643 end;
644 //------------------------------------------------------------------------------
645
646 function Point(X,Y: Integer): TPoint;
647 begin
648 result.X := X;
649 result.Y := Y;
650 end;
651 //------------------------------------------------------------------------------
652
653 function Point(const pt: TPointD): TPoint;
654 begin
655 result.X := Round(pt.x);
656 result.Y := Round(pt.y);
657 end;
658 //------------------------------------------------------------------------------
659
660 function PointsEqual(const pt1, pt2: TPointD): Boolean;
661 begin
662 result := (pt1.X = pt2.X) and (pt1.Y = pt2.Y);
663 end;
664 //------------------------------------------------------------------------------
665
666 function PointsNearEqual(const pt1, pt2: TPoint; dist: integer): Boolean;
667 begin
668 Result := (Abs(pt1.X - pt2.X) <= dist) and (Abs(pt1.Y - pt2.Y) < dist);
669 end;
670 //------------------------------------------------------------------------------
671
672 function PointsNearEqual(const pt1, pt2: TPointD; distSqrd: double): Boolean;
673 begin
674 Result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y) < distSqrd;
675 end;
676 //------------------------------------------------------------------------------
677
678 function StripNearDuplicates(const path: TPathD;
679 minDist: double; isClosedPath: Boolean): TPathD;
680 var
681 i,j, len: integer;
682 begin
683 len := length(path);
684 NewPointDArray(Result, len, True);
685 if len = 0 then Exit;
686 Result[0] := path[0];
687 j := 0;
688 minDist := minDist * minDist;
689 for i := 1 to len -1 do
690 if not PointsNearEqual(Result[j], path[i], minDist) then
691 begin
692 inc(j);
693 Result[j] := path[i];
694 end;
695 if isClosedPath and
696 PointsNearEqual(Result[j], Result[0], minDist) then dec(j);
697 SetLength(Result, j +1);
698 end;
699 //------------------------------------------------------------------------------
700
701 function StripNearDuplicates(const paths: TPathsD;
702 minLength: double; isClosedPaths: Boolean): TPathsD;
703 var
704 i, len: integer;
705 begin
706 len := Length(paths);
707 SetLength(Result, len);
708 for i := 0 to len -1 do
709 Result[i] := StripNearDuplicates(paths[i], minLength, isClosedPaths);
710 end;
711 //------------------------------------------------------------------------------
712
713 function ValueAlmostZero(val: double; epsilon: double = 0.001): Boolean;
714 {$IFDEF INLINE} inline; {$ENDIF}
715 begin
716 Result := Abs(val) < epsilon;
717 end;
718 //------------------------------------------------------------------------------
719
720 function ValueAlmostOne(val: double; epsilon: double = 0.001): Boolean;
721 {$IFDEF INLINE} inline; {$ENDIF}
722 begin
723 Result := Abs(val-1) < epsilon;
724 end;
725 //------------------------------------------------------------------------------
726
727 procedure GetSinCos(angle: double; out sinA, cosA: double);
728 {$IFDEF INLINE} inline; {$ENDIF}
729 {$IFNDEF FPC}
730 var s, c: extended;
731 {$ENDIF}
732 begin
733 {$IFDEF FPC}
734 Math.SinCos(angle, sinA, cosA);
735 {$ELSE}
736 Math.SinCos(angle, s, c);
737 sinA := s; cosA := c;
738 {$ENDIF}
739 end;
740 //------------------------------------------------------------------------------
741
742 function GetRotatedRectBounds(const rec: TRect; angle: double): TRect;
743 var
744 p: TPathD;
745 mp: TPointD;
746 begin
747 p := Rectangle(rec);
748 mp := PointD((rec.Left + rec.Right)/2, (rec.Top + rec.Bottom)/2);
749 if angle <> 0 then
750 p := RotatePath(p, mp, angle);
751 Result := GetBounds(p);
752 end;
753 //------------------------------------------------------------------------------
754
755 function GetRotatedRectBounds(const rec: TRectD; angle: double): TRectD;
756 var
757 p: TPathD;
758 mp: TPointD;
759 begin
760 p := Rectangle(rec);
761 mp := PointD((rec.Left + rec.Right)/2, (rec.Top + rec.Bottom)/2);
762 if angle <> 0 then
763 p := RotatePath(p, mp, angle);
764 Result := GetBoundsD(p);
765 end;
766 //------------------------------------------------------------------------------
767
768 function Rect(const recD: TRectD): TRect;
769 begin
770 // see https://github.com/AngusJohnson/Image32/issues/15
771 Result.Left := Floor(recD.Left + DoubleTolerance);
772 Result.Top := Floor(recD.Top + DoubleTolerance);
773 Result.Right := Ceil(recD.Right - DoubleTolerance);
774 Result.Bottom := Ceil(recD.Bottom - DoubleTolerance);
775 end;
776 //------------------------------------------------------------------------------
777
778 function PtInRect(const rec: TRectD; const pt: TPointD): Boolean;
779 begin
780 Result := (pt.X >= rec.Left) and (pt.X < rec.Right) and
781 (pt.Y >= rec.Top) and (pt.Y < rec.Bottom);
782 end;
783 //------------------------------------------------------------------------------
784
785 function Size(cx, cy: integer): TSize;
786 begin
787 Result.cx := cx;
788 Result.cy := cy;
789 end;
790 //------------------------------------------------------------------------------
791
792 function SizeD(cx, cy: double): TSizeD;
793 begin
794 Result.cx := cx;
795 Result.cy := cy;
796 end;
797 //------------------------------------------------------------------------------
798
799 function IsClockwise(const path: TPathD): Boolean;
800 begin
801 Result := Area(path) > 0;
802 end;
803 //------------------------------------------------------------------------------
804
805 function IsSimpleRectanglePath(const path: TPathD; var R: TRect): Boolean;
806 type
807 TLastMatch = (lmX, lmY);
808 var
809 i: Integer;
810 lastMatch: TLastMatch;
811 begin
812 Result := False;
813 // If we have a single path with 4 points, it could be a rectangle
814 if Length(path) = 4 then
815 begin
816 // For a rectangle the X and Y coordinates of the points alternate
817 // in being equal
818 if path[0].X = path[3].X then
819 lastMatch := lmX
820 else if path[0].Y = path[3].Y then
821 lastMatch := lmY
822 else
823 Exit;
824
825 R.Left := Trunc(path[0].X);
826 R.Top := Trunc(path[0].Y);
827 R.Right := Ceil(path[0].X);
828 R.Bottom := Ceil(path[0].Y);
829 for i := 1 to 3 do
830 begin
831 case lastMatch of
832 lmY: // now the X-coordinates must be equal
833 begin
834 if path[i].X <> path[i - 1].X then Exit;
835 lastMatch := lmX;
836 R.Top := Min(R.Top, Trunc(path[i].Y));
837 R.Bottom := Max(R.Bottom, Ceil(path[i].Y));
838 end;
839 lmX: // now the Y-coordinates must be equal
840 begin
841 if path[i].Y <> path[i - 1].Y then Exit;
842 lastMatch := lmY;
843 R.Left := Min(R.Left, Trunc(path[i].X));
844 R.Right := Max(R.Right, Ceil(path[i].X));
845 end;
846 end;
847 end;
848 Result := True;
849 end;
850 end;
851
852 //------------------------------------------------------------------------------
853 function IsSimpleRectanglePath(const paths: TPathsD; var R: TRect): Boolean;
854 begin
855 if (Length(paths) = 1) and (Length(paths[0]) = 4) then
856 Result := IsSimpleRectanglePath(paths[0], r)
857 else
858 Result := False;
859 end;
860 //------------------------------------------------------------------------------
861
862 function Area(const path: TPathD): Double;
863 var
864 i, j, highI: Integer;
865 d: Double;
866 begin
867 Result := 0.0;
868 highI := High(path);
869 if (highI < 2) then Exit;
870 j := highI;
871 for i := 0 to highI do
872 begin
873 d := (path[j].X + path[i].X);
874 Result := Result + d * (path[j].Y - path[i].Y);
875 j := i;
876 end;
877 Result := -Result * 0.5;
878 end;
879 //------------------------------------------------------------------------------
880
881 procedure TranslateRect(var rec: TRect; dx, dy: integer);
882 begin
883 rec.Left := rec.Left + dx;
884 rec.Top := rec.Top + dy;
885 rec.Right := rec.Right + dx;
886 rec.Bottom := rec.Bottom + dy;
887 end;
888 //------------------------------------------------------------------------------
889
890 procedure TranslateRect(var rec: TRectD; dx, dy: double);
891 begin
892 rec.Left := rec.Left + dx;
893 rec.Top := rec.Top + dy;
894 rec.Right := rec.Right + dx;
895 rec.Bottom := rec.Bottom + dy;
896 end;
897 //------------------------------------------------------------------------------
898
899 function MakeSquare(rec: TRect): TRect;
900 var
901 i: integer;
902 begin
903 Result := rec;
904 i := ((rec.Right - rec.Left) + (rec.Bottom - rec.Top)) div 2;
905 Result.Right := Result.Left + i;
906 Result.Bottom := Result.Top + i;
907 end;
908 //------------------------------------------------------------------------------
909
910 function MidPoint(const rec: TRect): TPoint;
911 begin
912 Result.X := (rec.Left + rec.Right) div 2;
913 Result.Y := (rec.Top + rec.Bottom) div 2;
914 end;
915 //------------------------------------------------------------------------------
916
917 function MidPoint(const rec: TRectD): TPointD;
918 begin
919 Result.X := (rec.Left + rec.Right) * 0.5;
920 Result.Y := (rec.Top + rec.Bottom) * 0.5;
921 end;
922 //------------------------------------------------------------------------------
923
924 function MidPoint(const pt1, pt2: TPoint): TPoint;
925 begin
926 Result.X := (pt1.X + pt2.X) div 2;
927 Result.Y := (pt1.Y + pt2.Y) div 2;
928 end;
929 //------------------------------------------------------------------------------
930
931 function MidPoint(const pt1, pt2: TPointD): TPointD;
932 begin
933 Result.X := (pt1.X + pt2.X) * 0.5;
934 Result.Y := (pt1.Y + pt2.Y) * 0.5;
935 end;
936 //------------------------------------------------------------------------------
937
938 function Average(val1, val2: integer): integer;
939 begin
940 Result := (val1 + val2) div 2;
941 end;
942 //------------------------------------------------------------------------------
943
944 function Average(val1, val2: double): double;
945 begin
946 Result := (val1 + val2) * 0.5;
947 end;
948 //------------------------------------------------------------------------------
949
950 function RectsOverlap(const rec1, rec2: TRect): Boolean;
951 begin
952 Result := (rec1.Left < rec2.Right) and (rec1.Right > rec2.Left) and
953 (rec1.Top < rec2.Bottom) and (rec1.Bottom > rec2.Top);
954 end;
955 //------------------------------------------------------------------------------
956
957 function IsSameRect(const rec1, rec2: TRect): Boolean;
958 begin
959 Result := (rec1.Left = rec2.Left) and (rec1.Top = rec2.Top) and
960 (rec1.Right = rec2.Right) and (rec1.Bottom = rec2.Bottom);
961 end;
962 //------------------------------------------------------------------------------
963
964 function RectsIntersect(const rec1, rec2: TRect): Boolean;
965 var
966 dummy: TRect;
967 begin
968 Result := Types.IntersectRect(dummy, rec1, rec2);
969 end;
970 //------------------------------------------------------------------------------
971
972 function RectsIntersect(const rec1, rec2: TRectD): Boolean;
973 begin
974 Result := not IntersectRect(rec1, rec2).IsEmpty;
975 end;
976 //------------------------------------------------------------------------------
977
978 function IntersectRect(const rec1, rec2: TRectD): TRectD;
979 begin
980 result.Left := Max(rec1.Left, rec2.Left);
981 result.Top := Max(rec1.Top, rec2.Top);
982 result.Right := Min(rec1.Right, rec2.Right);
983 result.Bottom := Min(rec1.Bottom, rec2.Bottom);
984 end;
985 //------------------------------------------------------------------------------
986
987 function UnionRect(const rec1, rec2: TRect): TRect;
988 begin
989 if IsEmptyRect(rec1) then
990 Result := rec2
991 else if IsEmptyRect(rec2) then
992 Result := rec1
993 else
994 begin
995 result.Left := Min(rec1.Left, rec2.Left);
996 result.Top := Min(rec1.Top, rec2.Top);
997 result.Right := Max(rec1.Right, rec2.Right);
998 result.Bottom := Max(rec1.Bottom, rec2.Bottom);
999 end;
1000 end;
1001 //------------------------------------------------------------------------------
1002
1003 function UnionRect(const rec1, rec2: TRectD): TRectD;
1004 begin
1005 if IsEmptyRect(rec1) then
1006 Result := rec2
1007 else if IsEmptyRect(rec2) then
1008 Result := rec1
1009 else
1010 begin
1011 result.Left := Min(rec1.Left, rec2.Left);
1012 result.Top := Min(rec1.Top, rec2.Top);
1013 result.Right := Max(rec1.Right, rec2.Right);
1014 result.Bottom := Max(rec1.Bottom, rec2.Bottom);
1015 end;
1016 end;
1017 //------------------------------------------------------------------------------
1018
1019 function MakeArrayOfInteger(const ints: array of integer): TArrayOfInteger;
1020 var
1021 i, len: integer;
1022 begin
1023 len := Length(ints);
1024 NewIntegerArray(Result, len, True);
1025 for i := 0 to len -1 do Result[i] := ints[i];
1026 end;
1027 //------------------------------------------------------------------------------
1028
1029 function MakeArrayOfDouble(const doubles: array of double): TArrayOfDouble;
1030 var
1031 i, len: integer;
1032 begin
1033 len := Length(doubles);
1034 SetLength(Result, len);
1035 for i := 0 to len -1 do Result[i] := doubles[i];
1036 end;
1037 //------------------------------------------------------------------------------
1038
1039 function CrossProduct(const vector1, vector2: TPointD): double;
1040 begin
1041 result := vector1.X * vector2.Y - vector2.X * vector1.Y;
1042 end;
1043 //------------------------------------------------------------------------------
1044
1045 function CrossProduct(const pt1, pt2, pt3: TPointD): double;
1046 var
1047 x1,x2,y1,y2: double;
1048 begin
1049 x1 := pt2.X - pt1.X;
1050 y1 := pt2.Y - pt1.Y;
1051 x2 := pt3.X - pt2.X;
1052 y2 := pt3.Y - pt2.Y;
1053 result := (x1 * y2 - y1 * x2);
1054 end;
1055 //---------------------------------------------------------------------------
1056
1057 function CrossProduct(const pt1, pt2, pt3, pt4: TPointD): double;
1058 var
1059 x1,x2,y1,y2: double;
1060 begin
1061 x1 := pt2.X - pt1.X;
1062 y1 := pt2.Y - pt1.Y;
1063 x2 := pt4.X - pt3.X;
1064 y2 := pt4.Y - pt3.Y;
1065 result := (x1 * y2 - y1 * x2);
1066 end;
1067 //---------------------------------------------------------------------------
1068
1069 function DotProduct(const vector1, vector2: TPointD): double;
1070 begin
1071 result := vector1.X * vector2.X + vector1.Y * vector2.Y;
1072 end;
1073 //------------------------------------------------------------------------------
1074
1075 function DotProduct(const pt1, pt2, pt3: TPointD): double;
1076 var
1077 x1,x2,y1,y2: double;
1078 begin
1079 x1 := pt2.X - pt1.X;
1080 y1 := pt2.Y - pt1.Y;
1081 x2 := pt2.X - pt3.X;
1082 y2 := pt2.Y - pt3.Y;
1083 result := (x1 * x2 + y1 * y2);
1084 end;
1085 //------------------------------------------------------------------------------
1086
1087 function TurnsLeft(const pt1, pt2, pt3: TPointD): boolean;
1088 begin
1089 result := CrossProduct(pt1, pt2, pt3) < 0;
1090 end;
1091 //------------------------------------------------------------------------------
1092
1093 function TurnsRight(const pt1, pt2, pt3: TPointD): boolean;
1094 begin
1095 result := CrossProduct(pt1, pt2, pt3) > 0;
1096 end;
1097 //------------------------------------------------------------------------------
1098
1099 function IsPathConvex(const path: TPathD): Boolean;
1100 var
1101 i, pathLen: integer;
1102 dir: boolean;
1103 begin
1104 result := false;
1105 pathLen := length(path);
1106 if pathLen < 3 then Exit;
1107 //get the winding direction of the first angle
1108 dir := TurnsRight(path[0], path[1], path[2]);
1109 //check that each other angle has the same winding direction
1110 for i := 1 to pathLen -1 do
1111 if TurnsRight(path[i], path[(i+1) mod pathLen],
1112 path[(i+2) mod pathLen]) <> dir then Exit;
1113 result := true;
1114 end;
1115 //------------------------------------------------------------------------------
1116
1117 function GetUnitVector(const pt1, pt2: TPointD): TPointD;
1118 var
1119 dx, dy, inverseHypot: Double;
1120 begin
1121 if (pt1.x = pt2.x) and (pt1.y = pt2.y) then
1122 begin
1123 Result.X := 0;
1124 Result.Y := 0;
1125 Exit;
1126 end;
1127 dx := (pt2.X - pt1.X);
1128 dy := (pt2.Y - pt1.Y);
1129 inverseHypot := 1 / Hypot(dx, dy);
1130 dx := dx * inverseHypot;
1131 dy := dy * inverseHypot;
1132 Result.X := dx;
1133 Result.Y := dy;
1134 end;
1135 //------------------------------------------------------------------------------
1136
1137 function GetUnitNormal(const pt1, pt2: TPointD): TPointD;
1138 begin
1139 if not GetUnitNormal(pt1, pt2, Result) then
1140 Result := NullPointD;
1141 end;
1142 //------------------------------------------------------------------------------
1143
1144 function GetUnitNormal(const pt1, pt2: TPointD; out norm: TPointD): Boolean;
1145 var
1146 dx, dy, inverseHypot: Double;
1147 begin
1148 result := not PointsNearEqual(pt1, pt2, 0.001);
1149 if not result then Exit;
1150 dx := (pt2.X - pt1.X);
1151 dy := (pt2.Y - pt1.Y);
1152 inverseHypot := 1 / Hypot(dx, dy);
1153 dx := dx * inverseHypot;
1154 dy := dy * inverseHypot;
1155 norm.X := dy;
1156 norm.Y := -dx
1157 end;
1158 //------------------------------------------------------------------------------
1159
1160 function NormalizeVector(const vec: TPointD): TPointD;
1161 var
1162 h, inverseHypot: Double;
1163 begin
1164 h := Hypot(vec.X, vec.Y);
1165 if ValueAlmostZero(h, 0.001) then
1166 begin
1167 Result := NullPointD;
1168 Exit;
1169 end;
1170 inverseHypot := 1 / h;
1171 Result.X := vec.X * inverseHypot;
1172 Result.Y := vec.Y * inverseHypot;
1173 end;
1174 //------------------------------------------------------------------------------
1175
1176 function GetAvgUnitVector(const vec1, vec2: TPointD): TPointD;
1177 begin
1178 Result := NormalizeVector(PointD(vec1.X + vec2.X, vec1.Y + vec2.Y));
1179 end;
1180 //------------------------------------------------------------------------------
1181
1182 function Paths(const path: TPathD): TPathsD;
1183 begin
1184 SetLength(Result, 1);
1185 result[0] := Copy(path, 0, length(path));
1186 end;
1187 //------------------------------------------------------------------------------
1188
1189 function CopyPath(const path: TPathD): TPathD;
1190 begin
1191 Result := Copy(path, 0, Length(path));
1192 end;
1193 //------------------------------------------------------------------------------
1194
1195 function CopyPaths(const paths: TPathsD): TPathsD;
1196 var
1197 i, len1: integer;
1198 begin
1199 len1 := length(paths);
1200 setLength(result, len1);
1201 for i := 0 to len1 -1 do
1202 result[i] := Copy(paths[i], 0, length(paths[i]));
1203 end;
1204 //------------------------------------------------------------------------------
1205
1206 function TranslatePoint(const pt: TPoint; dx, dy: integer): TPoint;
1207 begin
1208 result.x := pt.x + dx;
1209 result.y := pt.y + dy;
1210 end;
1211 //------------------------------------------------------------------------------
1212
1213 function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD;
1214 begin
1215 result.x := pt.x + dx;
1216 result.y := pt.y + dy;
1217 end;
1218 //------------------------------------------------------------------------------
1219
1220 function TranslatePath(const path: TPathD; dx, dy: double): TPathD;
1221 var
1222 i, len: integer;
1223 begin
1224 len := length(path);
1225 NewPointDArray(result, len, True);
1226 for i := 0 to len -1 do
1227 begin
1228 result[i].x := path[i].x + dx;
1229 result[i].y := path[i].y + dy;
1230 end;
1231 end;
1232 //------------------------------------------------------------------------------
1233
1234 function TranslatePath(const paths: TPathsD;
1235 dx, dy: double): TPathsD;
1236 var
1237 i,len: integer;
1238 begin
1239 len := length(paths);
1240 setLength(result, len);
1241 for i := 0 to len -1 do
1242 result[i] := TranslatePath(paths[i], dx, dy);
1243 end;
1244 //------------------------------------------------------------------------------
1245
1246 function TranslatePath(const ppp: TArrayOfPathsD; dx, dy: double): TArrayOfPathsD;
1247 var
1248 i,len: integer;
1249 begin
1250 len := length(ppp);
1251 setLength(result, len);
1252 for i := 0 to len -1 do
1253 result[i] := TranslatePath(ppp[i], dx, dy);
1254 end;
1255 //------------------------------------------------------------------------------
1256
1257 function ScalePoint(const pt: TPointD; scale: double): TPointD;
1258 begin
1259 Result.X := pt.X * scale;
1260 Result.Y := pt.Y * scale;
1261 end;
1262 //------------------------------------------------------------------------------
1263
1264 function ScalePoint(const pt: TPointD; sx, sy: double): TPointD;
1265 begin
1266 Result.X := pt.X * sx;
1267 Result.Y := pt.Y * sy;
1268 end;
1269 //------------------------------------------------------------------------------
1270
1271 function ScalePath(const path: TPathD; sx, sy: double): TPathD;
1272 var
1273 i, len: integer;
1274 begin
1275 if (sx = 0) or (sy = 0) then
1276 Result := nil
1277 else if ((sx = 1) and (sy = 1)) then
1278 begin
1279 Result := Copy(path, 0, Length(path));
1280 end else
1281 begin
1282 len := length(path);
1283 NewPointDArray(result, len, True);
1284 for i := 0 to len -1 do
1285 begin
1286 result[i].x := path[i].x * sx;
1287 result[i].y := path[i].y * sy;
1288 end;
1289 end;
1290 end;
1291 //------------------------------------------------------------------------------
1292
1293 function ScalePath(const path: TPathD;
1294 scale: double): TPathD;
1295 begin
1296 result := ScalePath(path, scale, scale);
1297 end;
1298 //------------------------------------------------------------------------------
1299
1300 function ScalePath(const paths: TPathsD;
1301 sx, sy: double): TPathsD;
1302 var
1303 i,len: integer;
1304 begin
1305 len := length(paths);
1306 setLength(result, len);
1307 for i := 0 to len -1 do
1308 result[i] := ScalePath(paths[i], sx, sy);
1309 end;
1310 //------------------------------------------------------------------------------
1311
1312 function ScalePath(const paths: TPathsD;
1313 scale: double): TPathsD;
1314 begin
1315 result := ScalePath(paths, scale, scale);
1316 end;
1317 //------------------------------------------------------------------------------
1318
1319 function ScaleRect(const rec: TRect; scale: double): TRect;
1320 begin
1321 result := rec;
1322 Result.Left := Round(Result.Left * scale);
1323 Result.Top := Round(Result.Top * scale);
1324 Result.Right := Round(Result.Right * scale);
1325 Result.Bottom := Round(Result.Bottom * scale);
1326 end;
1327 //------------------------------------------------------------------------------
1328
1329 function ScaleRect(const rec: TRect; sx, sy: double): TRect;
1330 begin
1331 result := rec;
1332 Result.Left := Round(Result.Left * sx);
1333 Result.Top := Round(Result.Top * sy);
1334 Result.Right := Round(Result.Right * sx);
1335 Result.Bottom := Round(Result.Bottom * sy);
1336 end;
1337 //------------------------------------------------------------------------------
1338
1339 function ScaleRect(const rec: TRectD; scale: double): TRectD;
1340 begin
1341 result := rec;
1342 Result.Left := Result.Left * scale;
1343 Result.Top := Result.Top * scale;
1344 Result.Right := Result.Right * scale;
1345 Result.Bottom := Result.Bottom * scale;
1346 end;
1347 //------------------------------------------------------------------------------
1348
1349 function ScaleRect(const rec: TRectD; sx, sy: double): TRectD;
1350 begin
1351 result := rec;
1352 Result.Left := Result.Left * sx;
1353 Result.Top := Result.Top * sy;
1354 Result.Right := Result.Right * sx;
1355 Result.Bottom := Result.Bottom * sy;
1356 end;
1357 //------------------------------------------------------------------------------
1358
1359 function ScalePathToFit(const path: TPathD; const rec: TRect): TPathD;
1360 var
1361 pathWidth, pathHeight, outHeight, outWidth: integer;
1362 pathBounds: TRect;
1363 scale: double;
1364 begin
1365 pathBounds := GetBounds(path);
1366 RectWidthHeight(pathBounds, pathWidth, pathHeight);
1367 RectWidthHeight(rec, outWidth, outHeight);
1368 Result := TranslatePath(path,
1369 rec.Left - pathBounds.Left, rec.Top - pathBounds.Top);
1370 if outWidth / pathWidth < outHeight / pathHeight then
1371 scale := outWidth / pathWidth else
1372 scale := outHeight / pathHeight;
1373 Result := ScalePath(Result, scale, scale);
1374 end;
1375 //------------------------------------------------------------------------------
1376
1377 function ScalePathsToFit(const paths: TPathsD; const rec: TRect): TPathsD;
1378 var
1379 pathWidth, pathHeight, outHeight, outWidth: integer;
1380 pathBounds: TRect;
1381 scale: double;
1382 begin
1383 pathBounds := GetBounds(paths);
1384 RectWidthHeight(pathBounds, pathWidth, pathHeight);
1385 RectWidthHeight(rec, outWidth, outHeight);
1386 Result := TranslatePath(paths,
1387 rec.Left - pathBounds.Left, rec.Top - pathBounds.Top);
1388 if outWidth / pathWidth < outHeight / pathHeight then
1389 scale := outWidth / pathWidth else
1390 scale := outHeight / pathHeight;
1391 Result := ScalePath(Result, scale, scale);
1392 end;
1393 //------------------------------------------------------------------------------
1394
1395 function ReversePath(const path: TPathD): TPathD;
1396 var
1397 i, highI: integer;
1398 begin
1399 highI := High(path);
1400 NewPointDArray(result, highI +1, True);
1401 for i := 0 to highI do
1402 result[i] := path[highI -i];
1403 end;
1404 //------------------------------------------------------------------------------
1405
1406 function ReversePath(const paths: TPathsD): TPathsD;
1407 var
1408 i, len: integer;
1409 begin
1410 len := Length(paths);
1411 SetLength(result, len);
1412 for i := 0 to len -1 do
1413 result[i] := ReversePath(paths[i]);
1414 end;
1415 //------------------------------------------------------------------------------
1416
1417 function OpenPathToFlatPolygon(const path: TPathD): TPathD;
1418 var
1419 i, len, len2: integer;
1420 begin
1421 len := Length(path);
1422 len2 := Max(0, len - 2);
1423 NewPointDArray(Result, len + len2, True);
1424 if len = 0 then Exit;
1425 Move(path[0], Result[0], len * SizeOf(TPointD));
1426 if len2 = 0 then Exit;
1427 for i := 0 to len - 3 do
1428 result[len + i] := path[len - 2 -i];
1429 end;
1430 //------------------------------------------------------------------------------
1431
1432 function GetVectors(const path: TPathD): TPathD;
1433 var
1434 i,j, len: cardinal;
1435 pt: TPointD;
1436 begin
1437 len := length(path);
1438 NewPointDArray(result, len, True);
1439 if len = 0 then Exit;
1440 pt := path[0];
1441 //skip duplicates
1442 i := len -1;
1443 while (i > 0) and
1444 (path[i].X = pt.X) and (path[i].Y = pt.Y) do dec(i);
1445 if (i = 0) then
1446 begin
1447 //all points are equal!
1448 for i := 0 to len -1 do result[i] := PointD(0,0);
1449 Exit;
1450 end;
1451 result[i] := GetUnitVector(path[i], pt);
1452 //fix up any duplicates at the end of the path
1453 for j := i +1 to len -1 do
1454 result[j] := result[j-1];
1455 //with at least one valid vector, we can now
1456 //safely get the remaining vectors
1457 pt := path[i];
1458 for i := i -1 downto 0 do
1459 begin
1460 if (path[i].X <> pt.X) or (path[i].Y <> pt.Y) then
1461 begin
1462 result[i] := GetUnitVector(path[i], pt);
1463 pt := path[i];
1464 end else
1465 result[i] := result[i+1]
1466 end;
1467 end;
1468 //------------------------------------------------------------------------------
1469
1470 function GetNormals(const path: TPathD): TPathD;
1471 var
1472 i, highI: integer;
1473 last: TPointD;
1474 begin
1475 highI := High(path);
1476 NewPointDArray(result, highI+1, True);
1477 if highI < 0 then Exit;
1478
1479 last := NullPointD;
1480 for i := 0 to highI -1 do
1481 begin
1482 if GetUnitNormal(path[i], path[i+1], result[i]) then
1483 last := result[i] else
1484 result[i] := last;
1485 end;
1486 if GetUnitNormal(path[highI], path[0], result[highI]) then
1487 last := result[highI];
1488
1489 for i := 0 to highI do
1490 begin
1491 if (result[i].X <> 0) or (result[i].Y <> 0) then Break;
1492 result[i] := last;
1493 end;
1494
1495 end;
1496 //------------------------------------------------------------------------------
1497
1498 function DistanceSqrd(const pt1, pt2: TPoint): double;
1499 begin
1500 result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y);
1501 end;
1502 //------------------------------------------------------------------------------
1503
1504 function DistanceSqrd(const pt1, pt2: TPointD): double;
1505 begin
1506 result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y);
1507 end;
1508 //------------------------------------------------------------------------------
1509
1510 function Distance(const pt1, pt2: TPoint): double;
1511 begin
1512 Result := Sqrt(DistanceSqrd(pt1, pt2));
1513 end;
1514 //------------------------------------------------------------------------------
1515
1516 function Distance(const pt1, pt2: TPointD): double;
1517 begin
1518 Result := Sqrt(DistanceSqrd(pt1, pt2));
1519 end;
1520 //------------------------------------------------------------------------------
1521
1522 function Distance(const path: TPathD; stopAt: integer): double;
1523 var
1524 i, highI: integer;
1525 begin
1526 Result := 0;
1527 highI := High(path);
1528 if (stopAt > 0) and (stopAt < HighI) then highI := stopAt;
1529 for i := 1 to highI do
1530 Result := Result + Distance(path[i-1],path[i]);
1531 end;
1532 //------------------------------------------------------------------------------
1533
1534 function GetDistances(const path: TPathD): TArrayOfDouble;
1535 var
1536 i, len: integer;
1537 begin
1538 len := Length(path);
1539 SetLength(Result, len);
1540 if len = 0 then Exit;
1541 Result[0] := 0;
1542 for i := 1 to len -1 do
1543 Result[i] := Distance(path[i-1], path[i]);
1544 end;
1545 //------------------------------------------------------------------------------
1546
1547 function GetCumulativeDistances(const path: TPathD): TArrayOfDouble;
1548 var
1549 i, len: integer;
1550 begin
1551 len := Length(path);
1552 SetLength(Result, len);
1553 if len = 0 then Exit;
1554 Result[0] := 0;
1555 for i := 1 to len -1 do
1556 Result[i] := Result[i-1] + Distance(path[i-1], path[i]);
1557 end;
1558 //------------------------------------------------------------------------------
1559
1560 function PerpendicularDistSqrd(const pt, line1, line2: TPointD): double;
1561 var
1562 a,b,c,d: double;
1563 begin
1564 if PointsEqual(line1, line2) then
1565 begin
1566 Result := DistanceSqrd(pt, line1);
1567 end else
1568 begin
1569 a := pt.X - line1.X;
1570 b := pt.Y - line1.Y;
1571 c := line2.X - line1.X;
1572 d := line2.Y - line1.Y;
1573 if (c = 0) and (d = 0) then
1574 result := 0 else
1575 result := Sqr(a * d - c * b) / (c * c + d * d);
1576 end;
1577 end;
1578 //------------------------------------------------------------------------------
1579
1580 function PointInPolyWindingCount(const pt: TPointD;
1581 const path: TPathD; out PointOnEdgeDir: integer): integer;
1582 var
1583 i, len: integer;
1584 prevPt: TPointD;
1585 isAbove: Boolean;
1586 crossProd: double;
1587 begin
1588 //nb: PointOnEdgeDir == 0 unless 'pt' is on 'path'
1589 Result := 0;
1590 PointOnEdgeDir := 0;
1591 i := 0;
1592 len := Length(path);
1593 if len = 0 then Exit;
1594 prevPt := path[len-1];
1595 while (i < len) and (path[i].Y = prevPt.Y) do inc(i);
1596 if i = len then Exit;
1597 isAbove := (prevPt.Y < pt.Y);
1598 while (i < len) do
1599 begin
1600 if isAbove then
1601 begin
1602 while (i < len) and (path[i].Y < pt.Y) do inc(i);
1603 if i = len then break
1604 else if i > 0 then prevPt := path[i -1];
1605 crossProd := CrossProduct(prevPt, path[i], pt);
1606 if crossProd = 0 then
1607 begin
1608 PointOnEdgeDir := -1;
1609 //nb: could safely exit here with frNonZero or frEvenOdd fill rules
1610 end
1611 else if crossProd < 0 then dec(Result);
1612 end else
1613 begin
1614 while (i < len) and (path[i].Y > pt.Y) do inc(i);
1615 if i = len then break
1616 else if i > 0 then prevPt := path[i -1];
1617 crossProd := CrossProduct(prevPt, path[i], pt);
1618 if crossProd = 0 then
1619 begin
1620 PointOnEdgeDir := 1;
1621 //nb: could safely exit here with frNonZero or frEvenOdd fill rules
1622 end
1623 else if crossProd > 0 then inc(Result);
1624 end;
1625 inc(i);
1626 isAbove := not isAbove;
1627 end;
1628 end;
1629 //------------------------------------------------------------------------------
1630
1631 function PointInPolygon(const pt: TPointD;
1632 const polygon: TPathD; fillRule: TFillRule): Boolean;
1633 var
1634 wc: integer;
1635 PointOnEdgeDir: integer;
1636 begin
1637 wc := PointInPolyWindingCount(pt, polygon, PointOnEdgeDir);
1638 case fillRule of
1639 frEvenOdd: result := (PointOnEdgeDir <> 0) or Odd(wc);
1640 frNonZero: result := (PointOnEdgeDir <> 0) or (wc <> 0);
1641 frPositive: result := (PointOnEdgeDir + wc > 0);
1642 else {frNegative} result := (PointOnEdgeDir + wc < 0);
1643 end;
1644 end;
1645 //------------------------------------------------------------------------------
1646
1647 function PointInPolysWindingCount(const pt: TPointD;
1648 const paths: TPathsD; out PointOnEdgeDir: integer): integer;
1649 var
1650 i,j, len: integer;
1651 p: TPathD;
1652 prevPt: TPointD;
1653 isAbove: Boolean;
1654 crossProd: double;
1655 begin
1656 //nb: PointOnEdgeDir == 0 unless 'pt' is on 'path'
1657 Result := 0;
1658 PointOnEdgeDir := 0;
1659 for i := 0 to High(paths) do
1660 begin
1661 j := 0;
1662 p := paths[i];
1663 len := Length(p);
1664 if len < 3 then Continue;
1665 prevPt := p[len-1];
1666 while (j < len) and (p[j].Y = prevPt.Y) do inc(j);
1667 if j = len then continue;
1668 isAbove := (prevPt.Y < pt.Y);
1669 while (j < len) do
1670 begin
1671 if isAbove then
1672 begin
1673 while (j < len) and (p[j].Y < pt.Y) do inc(j);
1674 if j = len then break
1675 else if j > 0 then prevPt := p[j -1];
1676 crossProd := CrossProduct(prevPt, p[j], pt);
1677 if crossProd = 0 then PointOnEdgeDir := -1
1678 else if crossProd < 0 then dec(Result);
1679 end else
1680 begin
1681 while (j < len) and (p[j].Y > pt.Y) do inc(j);
1682 if j = len then break
1683 else if j > 0 then prevPt := p[j -1];
1684 crossProd := CrossProduct(prevPt, p[j], pt);
1685 if crossProd = 0 then PointOnEdgeDir := 1
1686 else if crossProd > 0 then inc(Result);
1687 end;
1688 inc(j);
1689 isAbove := not isAbove;
1690 end;
1691 end;
1692 end;
1693 //------------------------------------------------------------------------------
1694
1695 function PointInPolygons(const pt: TPointD;
1696 const polygons: TPathsD; fillRule: TFillRule): Boolean;
1697 var
1698 wc: integer;
1699 PointOnEdgeDir: integer;
1700 begin
1701 wc := PointInPolysWindingCount(pt, polygons, PointOnEdgeDir);
1702 case fillRule of
1703 frEvenOdd: result := (PointOnEdgeDir <> 0) or Odd(wc);
1704 frNonZero: result := (PointOnEdgeDir <> 0) or (wc <> 0);
1705 frPositive: result := (PointOnEdgeDir + wc > 0);
1706 else {frNegative} result := (PointOnEdgeDir + wc < 0);
1707 end;
1708 end;
1709 //------------------------------------------------------------------------------
1710
1711 function PerpendicularDist(const pt, line1, line2: TPointD): double;
1712 var
1713 a,b,c,d: double;
1714 begin
1715 //given: cross product of 2 vectors = area of parallelogram
1716 //and given: area of parallelogram = length base * height
1717 //height (ie perpendic. dist.) = cross product of 2 vectors / length base
1718 a := pt.X - line1.X;
1719 b := pt.Y - line1.Y;
1720 c := line2.X - line1.X;
1721 d := line2.Y - line1.Y;
1722 result := abs(a * d - c * b) / Sqrt(c * c + d * d);
1723 end;
1724 //------------------------------------------------------------------------------
1725
1726 function ClosestPoint(const pt, linePt1, linePt2: TPointD;
1727 constrainToSegment: Boolean): TPointD;
1728 var
1729 q: double;
1730 begin
1731 if (linePt1.X = linePt2.X) and (linePt1.Y = linePt2.Y) then
1732 begin
1733 Result := linePt1;
1734 end else
1735 begin
1736 q := ((pt.X-linePt1.X)*(linePt2.X-linePt1.X) +
1737 (pt.Y-linePt1.Y)*(linePt2.Y-linePt1.Y)) /
1738 (sqr(linePt2.X-linePt1.X) + sqr(linePt2.Y-linePt1.Y));
1739 if constrainToSegment then
1740 begin
1741 if q < 0 then q := 0 else if q > 1 then q := 1;
1742 end;
1743 Result.X := (1-q)*linePt1.X + q*linePt2.X;
1744 Result.Y := (1-q)*linePt1.Y + q*linePt2.Y;
1745 end;
1746 end;
1747 //------------------------------------------------------------------------------
1748
1749 function ClosestPointOnLine(const pt, linePt1, linePt2: TPointD): TPointD;
1750 begin
1751 result := ClosestPoint(pt, linePt1, linePt2, false);
1752 end;
1753 //------------------------------------------------------------------------------
1754
1755 function ClosestPointOnSegment(const pt, segPt1, segPt2: TPointD): TPointD;
1756 begin
1757 result := ClosestPoint(pt, segPt1, segPt2, true);
1758 end;
1759 //------------------------------------------------------------------------------
1760
1761 function GetPtOnEllipseFromAngle(const ellipseRect: TRectD;
1762 angle: double): TPointD;
1763 var
1764 sn, co: double;
1765 begin
1766 NormalizeAngle(angle);
1767 GetSinCos(angle, sn, co);
1768 Result.X := ellipseRect.MidPoint.X + ellipseRect.Width/2 * co;
1769 Result.Y := ellipseRect.MidPoint.Y + ellipseRect.Height/2 * sn;
1770 end;
1771 //------------------------------------------------------------------------------
1772
1773 function GetEllipticalAngleFromPoint(const ellipseRect: TRectD;
1774 const pt: TPointD): double;
1775 begin
1776 with ellipseRect do
1777 Result := ArcTan2(Width/Height * (pt.Y - MidPoint.Y), (pt.X - MidPoint.X));
1778 end;
1779 //------------------------------------------------------------------------------
1780
1781 function GetRotatedEllipticalAngleFromPoint(const ellipseRect: TRectD;
1782 ellipseRotAngle: double; pt: TPointD): double;
1783 begin
1784 Result := 0;
1785 if ellipseRect.IsEmpty then Exit;
1786 RotatePoint(pt, ellipseRect.MidPoint, -ellipseRotAngle);
1787 Result := GetEllipticalAngleFromPoint(ellipseRect, pt);
1788 end;
1789 //------------------------------------------------------------------------------
1790
1791 function GetPtOnRotatedEllipseFromAngle(const ellipseRect: TRectD;
1792 ellipseRotAngle, angle: double): TPointD;
1793 begin
1794 Result := GetPtOnEllipseFromAngle(ellipseRect, angle);
1795 if ellipseRotAngle <> 0 then
1796 img32.Vector.RotatePoint(Result, ellipseRect.MidPoint, ellipseRotAngle);
1797 end;
1798 //------------------------------------------------------------------------------
1799
1800 function GetClosestPtOnRotatedEllipse(const ellipseRect: TRectD;
1801 ellipseRotation: double; const pt: TPointD): TPointD;
1802 var
1803 pt2: TPointD;
1804 angle: double;
1805 begin
1806 pt2 := pt;
1807 Img32.Vector.RotatePoint(pt2, ellipseRect.MidPoint, -ellipseRotation);
1808 angle := GetEllipticalAngleFromPoint(ellipseRect, pt2);
1809 Result := GetPtOnEllipseFromAngle(ellipseRect, angle);
1810 Img32.Vector.RotatePoint(Result, ellipseRect.MidPoint, ellipseRotation);
1811 end;
1812 //------------------------------------------------------------------------------
1813
1814 function IsPointInEllipse(const ellipseRec: TRect; const pt: TPoint): Boolean;
1815 var
1816 rec: TRectD;
1817 w,h: integer;
1818 x,y, y2, a,b, dx,dy: double;
1819 begin
1820 RectWidthHeight(ellipseRec, w, h);
1821 a := w * 0.5;
1822 b := h * 0.5;
1823 dx := ellipseRec.Left + a;
1824 dy := ellipseRec.Top + b;
1825 rec := RectD(ellipseRec);
1826 TranslateRect(rec, -dx, -dy);
1827 x := pt.X -dx; y := pt.Y -dy;
1828 //first make sure pt is inside rect
1829 Result := (abs(x) <= a) and (abs(y) <= b);
1830 if not result then Exit;
1831 //given (x*x)/(a*a) + (y*y)/(b*b) = 1
1832 //then y*y = b*b(1 - (x*x)/(a*a))
1833 //nb: contents of Sqrt below will always be positive
1834 //since the substituted x must be within ellipseRec bounds
1835 y2 := Sqrt((b*b*(1 - (x*x)/(a*a))));
1836 Result := (y >= -y2) and (y <= y2);
1837 end;
1838 //------------------------------------------------------------------------------
1839
1840 function GetLineEllipseIntersects(const ellipseRec: TRect;
1841 var linePt1, linePt2: TPointD): Boolean;
1842 var
1843 dx, dy, m,a,b,c,q: double;
1844 qa,qb,qc,qs: double;
1845 rec: TRectD;
1846 pt1, pt2: TPointD;
1847 begin
1848 rec := RectD(ellipseRec);
1849 a := rec.Width *0.5;
1850 b := rec.Height *0.5;
1851 //offset ellipseRect so it's centered over the coordinate origin
1852 dx := ellipseRec.Left + a; dy := ellipseRec.Top + b;
1853 TranslateRect(rec, -dx, -dy);
1854 pt1 := TranslatePoint(linePt1, -dx, -dy);
1855 pt2 := TranslatePoint(linePt2, -dx, -dy);
1856 //equation of ellipse = (x*x)/(a*a) + (y*y)/(b*b) = 1
1857 //equation of line = y = mx + c;
1858 if (pt1.X = pt2.X) then //vertical line (ie infinite slope)
1859 begin
1860 //given x = K, then y*y = b*b(1 - (x*x)/(a*a))
1861 q := (b*b)*(1 - Sqr(pt1.X)/(a*a));
1862 result := q >= 0;
1863 if not result then Exit;
1864 q := Sqrt(q);
1865 pt1.Y := q;
1866 pt2.Y := -q;
1867 end else
1868 begin
1869 //using simultaneous equations and substitution
1870 //given y = mx + c
1871 m := (pt1.Y - pt2.Y)/(pt1.X - pt2.X);
1872 c := pt1.Y - m * pt1.X;
1873 //given (x*x)/(a*a) + (y*y)/(b*b) = 1
1874 //(x*x)/(a*a)*(b*b) + (y*y) = (b*b)
1875 //(b*b)/(a*a) *(x*x) + Sqr(m*x +c) = (b*b)
1876 //(b*b)/(a*a) *(x*x) + (m*m)*(x*x) + 2*m*x*c +c*c = b*b
1877 //((b*b)/(a*a) +(m*m)) *(x*x) + 2*m*c*(x) + (c*c) - (b*b) = 0
1878 //solving quadratic equation
1879 qa := ((b*b)/(a*a) +(m*m));
1880 qb := 2*m*c;
1881 qc := (c*c) - (b*b);
1882 qs := (qb*qb) - 4*qa*qc;
1883 Result := qs >= 0;
1884 if not result then Exit;
1885 qs := Sqrt(qs);
1886 pt1.X := (-qb +qs)/(2 * qa);
1887 pt1.Y := m * pt1.X + c;
1888 pt2.X := (-qb -qs)/(2 * qa);
1889 pt2.Y := m * pt2.X + c;
1890 end;
1891 //finally reverse initial offset
1892 linePt1 := TranslatePoint(pt1, dx, dy);
1893 linePt2 := TranslatePoint(pt2, dx, dy);
1894 end;
1895 //------------------------------------------------------------------------------
1896
1897 function Sign(const value: Double): integer; {$IFDEF INLINE} inline; {$ENDIF}
1898 begin
1899 if value < 0 then Result := -1
1900 else if value > 0 then Result := 1
1901 else Result := 0;
1902 end;
1903 //------------------------------------------------------------------------------
1904
1905 function ApplyNormal(const pt, norm: TPointD; delta: double): TPointD;
1906 {$IFDEF INLINE} inline; {$ENDIF}
1907 begin
1908 result := PointD(pt.X + norm.X * delta, pt.Y + norm.Y * delta);
1909 end;
1910 //------------------------------------------------------------------------------
1911
1912 procedure AppendPoint(var path: TPathD; const extra: TPointD);
1913 var
1914 len: integer;
1915 begin
1916 len := length(path);
1917 SetLengthUninit(path, len +1);
1918 path[len] := extra;
1919 end;
1920 //------------------------------------------------------------------------------
1921
1922 procedure AppendPath(var paths: TPathsD; const extra: TPathD);
1923 var
1924 len1, len2: integer;
1925 begin
1926 len2 := length(extra);
1927 if len2 = 0 then Exit;
1928 len1 := length(paths);
1929 setLength(paths, len1 + 1);
1930 paths[len1] := Copy(extra, 0, len2);
1931 end;
1932 //------------------------------------------------------------------------------
1933
1934 procedure AppendPath(var paths: TPathsD; const extra: TPathsD);
1935 var
1936 i, len1, len2: integer;
1937 begin
1938 len2 := length(extra);
1939 if len2 = 0 then Exit;
1940 len1 := length(paths);
1941 setLength(paths, len1 + len2);
1942 for i := 0 to len2 -1 do
1943 paths[len1+i] := Copy(extra[i], 0, length(extra[i]));
1944 end;
1945 //------------------------------------------------------------------------------
1946
1947 procedure AppendPath(var ppp: TArrayOfPathsD; const extra: TPathsD);
1948 var
1949 len: integer;
1950 begin
1951 len := length(ppp);
1952 setLength(ppp, len + 1);
1953 if Assigned(extra) then
1954 AppendPath(ppp[len], extra) else
1955 ppp[len] := nil;
1956 end;
1957 //------------------------------------------------------------------------------
1958
1959 procedure ConcatPaths(var dstPath: TPathD; const path: TPathD); overload;
1960 var
1961 len, pathLen: integer;
1962 begin
1963 // calculate the length of the final array
1964 len := Length(dstPath);
1965 pathLen := Length(path);
1966 if pathLen = 0 then Exit;
1967 // Avoid point duplicates where paths join
1968 if (len > 0) and PointsEqual(dstPath[len -1], path[0]) then dec(len);
1969 // fill the array
1970 SetLengthUninit(dstPath, len + pathLen);
1971 Move(path[0], dstPath[len], pathLen * SizeOf(TPointD));
1972 end;
1973 //------------------------------------------------------------------------------
1974
1975 procedure ConcatPaths(var dstPath: TPathD; const paths: TPathsD);
1976 var
1977 i, len, pathLen, offset: integer;
1978 begin
1979 // calculate the length of the final array
1980 len := 0;
1981 for i := 0 to high(paths) do
1982 begin
1983 pathLen := Length(paths[i]);
1984 if pathLen > 0 then
1985 begin
1986 // Skip the start-point if it matches the previous path's end-point
1987 if (i > 0) and PointsEqual(paths[i][0], paths[i -1][high(paths[i -1])]) then
1988 dec(pathLen);
1989 inc(len, pathLen);
1990 end;
1991 end;
1992 SetLengthUninit(dstPath, len);
1993
1994 // fill the array
1995 len := 0;
1996 for i := 0 to high(paths) do
1997 begin
1998 pathLen := Length(paths[i]);
1999 if pathLen > 0 then
2000 begin
2001 offset := 0;
2002 // Skip the start-point if it matches the previous path's end-point
2003 if (i > 0) and PointsEqual(paths[i][0], paths[i -1][high(paths[i -1])]) then
2004 begin
2005 dec(pathLen);
2006 offset := 1;
2007 end;
2008 // Skip if we have a path with only one point and that point also matches
2009 // the previous path's end-point.
2010 if pathLen > 0 then
2011 begin
2012 Move(paths[i][offset], dstPath[len], pathLen * SizeOf(TPointD));
2013 inc(len, pathLen);
2014 end;
2015 end;
2016 end;
2017 end;
2018 //------------------------------------------------------------------------------
2019
2020 procedure RotatePoint(var pt: TPointD;
2021 const focalPoint: TPointD; sinA, cosA: double);
2022 var
2023 tmpX, tmpY: double;
2024 begin
2025 tmpX := pt.X-focalPoint.X;
2026 tmpY := pt.Y-focalPoint.Y;
2027 pt.X := tmpX * cosA - tmpY * sinA + focalPoint.X;
2028 pt.Y := tmpX * sinA + tmpY * cosA + focalPoint.Y;
2029 end;
2030 //------------------------------------------------------------------------------
2031
2032 procedure RotatePoint(var pt: TPointD;
2033 const focalPoint: TPointD; angleRad: double);
2034 var
2035 sinA, cosA: double;
2036 begin
2037 if angleRad = 0 then Exit;
2038 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2039 angleRad := -angleRad;
2040 {$ENDIF}
2041 GetSinCos(angleRad, sinA, cosA);
2042 RotatePoint(pt, focalPoint, sinA, cosA);
2043 end;
2044 //------------------------------------------------------------------------------
2045
2046 function RotatePathInternal(const path: TPathD;
2047 const focalPoint: TPointD; sinA, cosA: double): TPathD;
2048 var
2049 i: integer;
2050 x,y: double;
2051 begin
2052 NewPointDArray(Result, length(path), True);
2053 for i := 0 to high(path) do
2054 begin
2055 x := path[i].X - focalPoint.X;
2056 y := path[i].Y - focalPoint.Y;
2057 Result[i].X := x * cosA - y * sinA + focalPoint.X;
2058 Result[i].Y := x * sinA + y * cosA + focalPoint.Y;
2059 end;
2060 end;
2061 //------------------------------------------------------------------------------
2062
2063 function RotatePath(const path: TPathD;
2064 const focalPoint: TPointD; angleRads: double): TPathD;
2065 var
2066 sinA, cosA: double;
2067 begin
2068 if angleRads = 0 then
2069 begin
2070 Result := path;
2071 Exit;
2072 end;
2073 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2074 angleRads := -angleRads;
2075 {$ENDIF}
2076 GetSinCos(angleRads, sinA, cosA);
2077 Result := RotatePathInternal(path, focalPoint, sinA, cosA);
2078 end;
2079 //------------------------------------------------------------------------------
2080
2081 function RotatePath(const paths: TPathsD;
2082 const focalPoint: TPointD; angleRads: double): TPathsD;
2083 var
2084 i: integer;
2085 sinA, cosA: double;
2086 fp: TPointD;
2087 begin
2088 Result := paths;
2089 if not IsValid(angleRads) then Exit;
2090 NormalizeAngle(angleRads);
2091 if angleRads = 0 then Exit;
2092 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2093 angleRads := -angleRads;
2094 {$ENDIF}
2095 GetSinCos(angleRads, sinA, cosA);
2096 SetLength(Result, length(paths));
2097 if IsValid(focalPoint) then
2098 fp := focalPoint else
2099 fp := GetBoundsD(paths).MidPoint;
2100 for i := 0 to high(paths) do
2101 Result[i] := RotatePathInternal(paths[i], fp, sinA, cosA);
2102 end;
2103 //------------------------------------------------------------------------------
2104
2105 function GetAngle(const origin, pt: TPoint): double;
2106 var
2107 x,y: double;
2108 begin
2109 x := pt.X - origin.X;
2110 y := pt.Y - origin.Y;
2111 if x = 0 then
2112 begin
2113 if y > 0 then result := angle90
2114 else result := -angle90;
2115 end
2116 else if y = 0 then
2117 begin
2118 if x > 0 then result := 0
2119 else result := angle180;
2120 end else
2121 result := arctan2(y, x); //range between -Pi and Pi
2122 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2123 Result := -Result;
2124 {$ENDIF}
2125 end;
2126 //------------------------------------------------------------------------------
2127
2128 function GetAngle(const origin, pt: TPointD): double;
2129 var
2130 x,y: double;
2131 begin
2132 x := pt.X - origin.X;
2133 y := pt.Y - origin.Y;
2134 if x = 0 then
2135 begin
2136 if y > 0 then result := angle90
2137 else result := -angle90;
2138 end
2139 else if y = 0 then
2140 begin
2141 if x > 0 then result := 0
2142 else result := angle180;
2143 end else
2144 result := arctan2(y, x); //range between -Pi and Pi
2145 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2146 Result := -Result;
2147 {$ENDIF}
2148 end;
2149 //------------------------------------------------------------------------------
2150
2151 function GetAngle(const a, b, c: TPoint): double;
2152 var
2153 ab, bc: TPointD;
2154 dp, cp: double;
2155 begin
2156 //https://stackoverflow.com/a/3487062/359538
2157 ab := PointD(b.x - a.x, b.y - a.y);
2158 bc := PointD(b.x - c.x, b.y - c.y);
2159 dp := (ab.x * bc.x + ab.y * bc.y);
2160 cp := (ab.x * bc.y - ab.y * bc.x);
2161 Result := arctan2(cp, dp); //range between -Pi and Pi
2162 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2163 Result := -Result;
2164 {$ENDIF}
2165 end;
2166 //------------------------------------------------------------------------------
2167
2168 function GetAngle(const a, b, c: TPointD): double;
2169 var
2170 ab, bc: TPointD;
2171 dp, cp: double;
2172 begin
2173 //https://stackoverflow.com/a/3487062/359538
2174 ab := PointD(b.x - a.x, b.y - a.y);
2175 bc := PointD(b.x - c.x, b.y - c.y);
2176 dp := (ab.x * bc.x + ab.y * bc.y);
2177 cp := (ab.x * bc.y - ab.y * bc.x);
2178 Result := arctan2(cp, dp); //range between -Pi and Pi
2179 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
2180 Result := -Result;
2181 {$ENDIF}
2182 end;
2183 //------------------------------------------------------------------------------
2184
2185 function GetPointAtAngleAndDist(const origin: TPointD;
2186 angle, distance: double): TPointD;
2187 begin
2188 Result := origin;
2189 Result.X := Result.X + distance;
2190 RotatePoint(Result, origin, angle);
2191 end;
2192 //------------------------------------------------------------------------------
2193
2194 function IntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD;
2195 var
2196 m1,b1,m2,b2: double;
2197 begin
2198 result := InvalidPointD;
2199 //see http://paulbourke.net/geometry/pointlineplane/
2200 if (ln1B.X = ln1A.X) then
2201 begin
2202 if (ln2B.X = ln2A.X) then exit; //parallel lines
2203 m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
2204 b2 := ln2A.Y - m2 * ln2A.X;
2205 Result.X := ln1A.X;
2206 Result.Y := m2*ln1A.X + b2;
2207 end
2208 else if (ln2B.X = ln2A.X) then
2209 begin
2210 m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
2211 b1 := ln1A.Y - m1 * ln1A.X;
2212 Result.X := ln2A.X;
2213 Result.Y := m1*ln2A.X + b1;
2214 end else
2215 begin
2216 m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
2217 b1 := ln1A.Y - m1 * ln1A.X;
2218 m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
2219 b2 := ln2A.Y - m2 * ln2A.X;
2220 if m1 = m2 then exit; //parallel lines
2221 Result.X := (b2 - b1)/(m1 - m2);
2222 Result.Y := m1 * Result.X + b1;
2223 end;
2224 end;
2225 //------------------------------------------------------------------------------
2226
2227 function IntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPointD;
2228 out ip: TPointD): Boolean;
2229 begin
2230 ip := IntersectPoint(ln1a, ln1b, ln2a, ln2b);
2231 Result := IsValid(ip);
2232 end;
2233 //------------------------------------------------------------------------------
2234
2235 function SegmentIntersectPt(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD;
2236 var
2237 pqd,r,s : TPointD; //scalar vectors;
2238 rs, t : double;
2239 begin
2240 //https://stackoverflow.com/a/565282/359538
2241 Result := InvalidPointD;
2242 r := PointD(ln1b.X - ln1a.X, ln1b.Y - ln1a.Y);
2243 s := PointD(ln2b.X - ln2a.X, ln2b.Y - ln2a.Y);
2244 rs := CrossProduct(r,s);
2245 if Abs(rs) < 1 then Exit;
2246 pqd.X := ln2a.X - ln1a.X;
2247 pqd.y := ln2a.Y - ln1a.Y;
2248 t := CrossProduct(pqd, s) / rs;
2249 if (t < -0.025) or (t > 1.025) then Exit;
2250 Result.X := ln1a.X + t * r.X;
2251 Result.Y := ln1a.Y + t * r.Y;
2252 // pqd.X := -pqd.X; pqd.Y := -pqd.Y;
2253 // u := CrossProduct(pqd, r) / rs;
2254 // if (u < -0.05) or (u > 1.05) then Exit;
2255 end;
2256 //------------------------------------------------------------------------------
2257
2258 function SegmentsIntersect(const ln1a, ln1b, ln2a, ln2b: TPointD;
2259 out ip: TPointD): Boolean;
2260 begin
2261 ip := SegmentIntersectPt(ln1a, ln1b, ln2a, ln2b);
2262 Result := IsValid(ip);
2263 end;
2264 //------------------------------------------------------------------------------
2265
2266 function CalcRoundingSteps(radius: double): double;
2267 begin
2268 //the results of this function have been derived empirically
2269 //and may need further adjustment
2270 if radius < 0.55 then result := 4
2271 else result := Pi * Sqrt(radius *2);
2272 end;
2273 //------------------------------------------------------------------------------
2274
2275 function Grow(const path, normals: TPathD; delta: double;
2276 joinStyle: TJoinStyle; miterLim: double; scale: double; isOpen: Boolean): TPathD;
2277 var
2278 resCnt, resCap : integer;
2279 norms : TPathD;
2280 stepsPerRadian : double;
2281 stepSin, stepCos : double;
2282 asin, acos : double;
2283
2284 procedure AddPoint(const pt: TPointD);
2285 begin
2286 if resCnt >= resCap then
2287 begin
2288 inc(resCap, 64);
2289 SetLengthUninit(result, resCap);
2290 end;
2291 result[resCnt] := pt;
2292 inc(resCnt);
2293 end;
2294
2295 procedure DoMiter(j, k: Integer; cosA: Double);
2296 var
2297 q: Double;
2298 begin
2299 q := delta / (cosA +1);
2300 AddPoint(PointD(
2301 path[j].X + (norms[k].X + norms[j].X) *q,
2302 path[j].Y + (norms[k].Y + norms[j].Y) *q));
2303 end;
2304
2305 procedure DoBevel(j, k: Integer);
2306 var
2307 absDelta: double;
2308 begin
2309 if k = j then
2310 begin
2311 absDelta := Abs(delta);
2312 AddPoint(PointD(
2313 path[j].x - absDelta * norms[j].x,
2314 path[j].y - absDelta * norms[j].y));
2315 AddPoint(PointD(
2316 path[j].x + absDelta * norms[j].x,
2317 path[j].y + absDelta * norms[j].y));
2318 end else
2319 begin
2320 AddPoint(PointD(
2321 path[j].x + delta * norms[k].x,
2322 path[j].y + delta * norms[k].y));
2323 AddPoint(PointD(
2324 path[j].x + delta * norms[j].x,
2325 path[j].y + delta * norms[j].y));
2326 end;
2327 end;
2328
2329 procedure DoSquare(j, k: Integer);
2330 var
2331 vec, ptQ, ptR, ptS, ptT, ptU, ip: TPointD;
2332 absDelta: double;
2333 begin
2334 if k = j then
2335 begin
2336 vec.X := norms[j].Y; //squaring a line end
2337 vec.Y := -norms[j].X;
2338 end else
2339 begin
2340 // using the reciprocal of unit normals (as unit vectors)
2341 // get the average unit vector ...
2342 vec := GetAvgUnitVector(
2343 PointD(-norms[k].Y, norms[k].X),
2344 PointD(norms[j].Y, -norms[j].X));
2345 end;
2346
2347 absDelta := Abs(delta);
2348 ptQ := PointD(path[j].X + absDelta * vec.X, path[j].Y + absDelta * vec.Y);
2349
2350 ptR := PointD(ptQ.X + delta * vec.Y, ptQ.Y + delta * -vec.X);
2351 ptS := ReflectPoint(ptR, ptQ);
2352
2353 // get 2 vertices along one edge offset
2354 ptT := PointD(
2355 path[k].X + norms[k].X * delta,
2356 path[k].Y + norms[k].Y * delta);
2357
2358 if (j = k) then
2359 begin
2360 ptU.X := ptT.X + vec.X * delta;
2361 ptU.Y := ptT.Y + vec.Y * delta;
2362 ip := IntersectPoint(ptR, ptS, ptT, ptU);
2363 AddPoint(ReflectPoint(ip, ptQ));
2364 AddPoint(ip);
2365 end else
2366 begin
2367 ptU := PointD(
2368 path[j].X + norms[k].X * delta,
2369 path[j].Y + norms[k].Y * delta);
2370 ip := IntersectPoint(ptR, ptS, ptT, ptU);
2371 AddPoint(ip);
2372 AddPoint(ReflectPoint(ip, ptQ));
2373 end;
2374 end;
2375
2376 procedure DoRound(j, k: Integer);
2377 var
2378 i, steps: Integer;
2379 pt: TPointD;
2380 dx, dy, oldDx: double;
2381 angle: double;
2382 begin
2383 // nb: angles may be negative but this will always be a convex join
2384 pt := path[j];
2385 if j = k then
2386 begin
2387 dx := -norms[k].X * delta;
2388 dy := -norms[k].Y * delta;
2389 end else
2390 begin
2391 dx := norms[k].X * delta;
2392 dy := norms[k].Y * delta;
2393 end;
2394 AddPoint(PointD(pt.X + dx, pt.Y + dy));
2395
2396 angle := ArcTan2(asin, acos);
2397 steps := Ceil(stepsPerRadian * abs(angle));
2398
2399 for i := 2 to steps do
2400 begin
2401 oldDx := dx;
2402 dx := oldDx * stepCos - stepSin * dy;
2403 dy := oldDx * stepSin + stepCos * dy;
2404 AddPoint(PointD(pt.X + dx, pt.Y + dy));
2405 end;
2406 AddPoint(PointD(
2407 pt.X + norms[j].X * delta,
2408 pt.Y + norms[j].Y * delta));
2409 end;
2410
2411 var
2412 j, k : cardinal;
2413 len : cardinal;
2414 steps : double;
2415 highI : cardinal;
2416 iLo,iHi : cardinal;
2417 absDelta : double;
2418 begin
2419 Result := nil;
2420 if not Assigned(path) then exit;
2421 len := Length(path);
2422 if not isOpen then
2423 while (len > 2) and
2424 PointsNearEqual(path[len -1], path[0], 0.001) do
2425 dec(len);
2426 if len < 2 then Exit;
2427
2428 if scale = 0 then scale := 1.0;
2429
2430 absDelta := Abs(delta);
2431 if absDelta * scale < 1 then
2432 joinStyle := jsButt
2433 else if joinStyle = jsAuto then
2434 begin
2435 if delta < AutoWidthThreshold / 2 then
2436 joinStyle := jsSquare else
2437 joinStyle := jsRound;
2438 end;
2439
2440 if absDelta < MinStrokeWidth/2 then
2441 begin
2442 if delta < 0 then
2443 delta := -MinStrokeWidth/2 else
2444 delta := MinStrokeWidth/2;
2445 end;
2446
2447
2448 if assigned(normals) then
2449 norms := normals else
2450 norms := GetNormals(path);
2451
2452 highI := len -1;
2453
2454 stepsPerRadian := 0;
2455 if joinStyle = jsRound then
2456 begin
2457 steps := CalcRoundingSteps(delta * scale);
2458 stepSin := sin(TwoPi/steps);
2459 stepCos := cos(TwoPi/steps);
2460 if (delta < 0) then stepSin := -stepSin;
2461 stepsPerRadian := steps / TwoPi;
2462 end;
2463
2464 if miterLim <= 0 then miterLim := DefaultMiterLimit
2465 else if miterLim < 2 then miterLim := 2;
2466 miterLim := 2 /(sqr(miterLim));
2467
2468 resCnt := 0;
2469 resCap := 0;
2470
2471 if isOpen then
2472 begin
2473 iLo := 1; iHi := highI -1;
2474 k := 0;
2475 AddPoint(PointD(
2476 path[0].X + norms[0].X * delta,
2477 path[0].Y + norms[0].Y * delta));
2478 end else
2479 begin
2480 iLo := 0; iHi := highI;
2481 k := highI;
2482 end;
2483
2484 for j := iLo to iHi do
2485 begin
2486
2487 if PointsNearEqual(path[j], path[k], 0.01) then
2488 begin
2489 k := j; // todo - check if needed
2490 Continue;
2491 end;
2492
2493 asin := CrossProduct(norms[k], norms[j]);
2494 if (asin > 1.0) then asin := 1.0
2495 else if (asin < -1.0) then asin := -1.0;
2496 acos := DotProduct(norms[k], norms[j]);
2497
2498 if (acos > -0.999) and (asin * delta < 0) then
2499 begin
2500 // is concave
2501 AddPoint(PointD(
2502 path[j].X + norms[k].X * delta, path[j].Y + norms[k].Y * delta));
2503 AddPoint(path[j]);
2504 AddPoint(PointD(
2505 path[j].X + norms[j].X * delta, path[j].Y + norms[j].Y * delta));
2506 end
2507 else if (acos > 0.999) and (joinStyle <> jsRound) then
2508 begin
2509 // almost straight - less than 2.5 degree, so miter
2510 DoMiter(j, k, acos);
2511 end
2512 else if (joinStyle = jsMiter) then
2513 begin
2514 if (1 + acos > miterLim) then
2515 DoMiter(j, k, acos) else
2516 DoSquare(j, k);
2517 end
2518 else if (joinStyle = jsRound) then
2519 DoRound(j, k)
2520 else if (joinStyle = jsSquare) then
2521 DoSquare(j, k)
2522 else
2523 DoBevel(j, k);
2524 k := j;
2525 end;
2526
2527 if isOpen then
2528 AddPoint(PointD(
2529 path[highI].X + norms[highI].X * delta, //todo - check this !!!
2530 path[highI].Y + norms[highI].Y * delta));
2531
2532 SetLength(Result, resCnt);
2533 end;
2534 //------------------------------------------------------------------------------
2535
2536 function GrowOpenLine(const line: TPathD; delta: double;
2537 joinStyle: TJoinStyle; endStyle: TEndStyle;
2538 miterLim: double = 0; scale: double = 1.0): TPathD;
2539 var
2540 len : integer;
2541 resCnt, resCap : integer;
2542 asin, acos : double;
2543 stepSin, stepCos : double;
2544 stepsPerRadian : double;
2545 path, norms : TPathD;
2546
2547 procedure AddPoint(const pt: TPointD);
2548 begin
2549 if resCnt >= resCap then
2550 begin
2551 inc(resCap, 64);
2552 SetLengthUninit(result, resCap);
2553 end;
2554 result[resCnt] := pt;
2555 inc(resCnt);
2556 end;
2557
2558 procedure DoMiter(j, k: Integer; cosA: Double);
2559 var
2560 q: Double;
2561 begin
2562 q := delta / (cosA +1);
2563 AddPoint(PointD(
2564 path[j].X + (norms[k].X + norms[j].X) *q,
2565 path[j].Y + (norms[k].Y + norms[j].Y) *q));
2566 end;
2567
2568 procedure DoBevel(j, k: Integer);
2569 var
2570 absDelta: double;
2571 begin
2572 if k = j then
2573 begin
2574 absDelta := Abs(delta);
2575 AddPoint(PointD(
2576 path[j].x - absDelta * norms[j].x,
2577 path[j].y - absDelta * norms[j].y));
2578 AddPoint(PointD(
2579 path[j].x + absDelta * norms[j].x,
2580 path[j].y + absDelta * norms[j].y));
2581 end else
2582 begin
2583 AddPoint(PointD(
2584 path[j].x + delta * norms[k].x,
2585 path[j].y + delta * norms[k].y));
2586 AddPoint(PointD(
2587 path[j].x + delta * norms[j].x,
2588 path[j].y + delta * norms[j].y));
2589 end;
2590 end;
2591
2592 procedure DoSquare(j, k: Integer);
2593 var
2594 vec, ptQ, ptR, ptS, ptT, ptU, ip: TPointD;
2595 absDelta: double;
2596 begin
2597 if k = j then
2598 begin
2599 vec.X := norms[j].Y; //squaring a line end
2600 vec.Y := -norms[j].X;
2601 end else
2602 begin
2603 // using the reciprocal of unit normals (as unit vectors)
2604 // get the average unit vector ...
2605 vec := GetAvgUnitVector(
2606 PointD(-norms[k].Y, norms[k].X),
2607 PointD(norms[j].Y, -norms[j].X));
2608 end;
2609
2610 absDelta := Abs(delta);
2611 ptQ := PointD(path[j].X + absDelta * vec.X, path[j].Y + absDelta * vec.Y);
2612
2613 ptR := PointD(ptQ.X + delta * vec.Y, ptQ.Y + delta * -vec.X);
2614 ptS := ReflectPoint(ptR, ptQ);
2615
2616 // get 2 vertices along one edge offset
2617 ptT := PointD(
2618 path[k].X + norms[k].X * delta,
2619 path[k].Y + norms[k].Y * delta);
2620
2621 if (j = k) then
2622 begin
2623 ptU.X := ptT.X + vec.X * delta;
2624 ptU.Y := ptT.Y + vec.Y * delta;
2625 ip := IntersectPoint(ptR, ptS, ptT, ptU);
2626 AddPoint(ReflectPoint(ip, ptQ));
2627 AddPoint(ip);
2628 end else
2629 begin
2630 ptU := PointD(
2631 path[j].X + norms[k].X * delta,
2632 path[j].Y + norms[k].Y * delta);
2633 ip := IntersectPoint(ptR, ptS, ptT, ptU);
2634 AddPoint(ip);
2635 AddPoint(ReflectPoint(ip, ptQ));
2636 end;
2637 end;
2638
2639 procedure DoRound(j, k: Integer);
2640 var
2641 i, steps: Integer;
2642 pt: TPointD;
2643 dx, dy, oldDx: double;
2644 angle: double;
2645 begin
2646 // nb: angles may be negative but this will always be a convex join
2647 pt := path[j];
2648 if j = k then
2649 begin
2650 dx := -norms[k].X * delta;
2651 dy := -norms[k].Y * delta;
2652 angle := PI;
2653 end else
2654 begin
2655 dx := norms[k].X * delta;
2656 dy := norms[k].Y * delta;
2657 angle := ArcTan2(asin, acos);
2658 end;
2659 AddPoint(PointD(pt.X + dx, pt.Y + dy));
2660
2661 steps := Ceil(stepsPerRadian * abs(angle));
2662 for i := 2 to steps do
2663 begin
2664 oldDx := dx;
2665 dx := oldDx * stepCos - stepSin * dy;
2666 dy := oldDx * stepSin + stepCos * dy;
2667 AddPoint(PointD(pt.X + dx, pt.Y + dy));
2668 end;
2669 AddPoint(PointD(
2670 pt.X + norms[j].X * delta,
2671 pt.Y + norms[j].Y * delta));
2672 end;
2673
2674 procedure DoPoint(j: Cardinal; var k: Cardinal);
2675 begin
2676 asin := CrossProduct(norms[k], norms[j]);
2677 if (asin > 1.0) then asin := 1.0
2678 else if (asin < -1.0) then asin := -1.0;
2679 acos := DotProduct(norms[k], norms[j]);
2680
2681 if (acos > -0.999) and (asin * delta < 0) then
2682 begin
2683 // is concave
2684 AddPoint(PointD(
2685 path[j].X + norms[k].X * delta, path[j].Y + norms[k].Y * delta));
2686 AddPoint(path[j]);
2687 AddPoint(PointD(
2688 path[j].X + norms[j].X * delta, path[j].Y + norms[j].Y * delta));
2689 end
2690 else if (acos > 0.999) and (joinStyle <> jsRound) then
2691 // almost straight - less than 2.5 degree, so miter
2692 DoMiter(j, k, acos)
2693 else if (joinStyle = jsMiter) then
2694 begin
2695 if (1 + acos > miterLim) then
2696 DoMiter(j, k, acos) else
2697 DoSquare(j, k);
2698 end
2699 else if (joinStyle = jsRound) then
2700 DoRound(j, k)
2701 else if (joinStyle = jsSquare) then
2702 DoSquare(j, k)
2703 else
2704 DoBevel(j, k);
2705 k := j;
2706 end;
2707
2708 var
2709 highJ : cardinal;
2710 j, k : cardinal;
2711 steps : double;
2712 begin
2713 Result := nil;
2714 path := StripNearDuplicates(line, 0.1, false);
2715 len := length(path);
2716 if (len = 0) or (delta <= 0) then Exit;
2717 // don't specify a minimum delta as this path may be scaled later
2718 // if delta < MinStrokeWidth then
2719 // delta := MinStrokeWidth;
2720 delta := delta * 0.5;
2721
2722 if len = 1 then
2723 begin
2724 with path[0] do
2725 result := Ellipse(RectD(x-delta, y-delta, x+delta, y+delta));
2726 Exit;
2727 end;
2728
2729 //Assert(endStyle <> esClosed);
2730
2731 //with very narrow lines, don't get fancy with joins and line ends
2732 if (delta <= 1) then
2733 begin
2734 if (joinStyle = jsRound) and (delta * scale <= 1) then
2735 joinStyle := jsButt;
2736 if (endStyle = esRound) and (delta * scale <= 1) then
2737 endStyle := esSquare;
2738 end
2739 else if joinStyle = jsAuto then
2740 begin
2741 if (endStyle = esRound) and (delta * scale >= AutoWidthThreshold) then
2742 joinStyle := jsRound else
2743 joinStyle := jsSquare;
2744 end;
2745
2746 stepsPerRadian := 0;
2747 if (joinStyle = jsRound) or (endStyle = esRound) then
2748 begin
2749 steps := CalcRoundingSteps(delta * scale);
2750 stepSin := sin(TwoPi/steps);
2751 stepCos := cos(TwoPi/steps);
2752 if (delta < 0) then stepSin := -stepSin;
2753 stepsPerRadian := steps / TwoPi;
2754 end;
2755
2756 if miterLim <= 0 then miterLim := DefaultMiterLimit
2757 else if miterLim < 2 then miterLim := 2;
2758 miterLim := 2 /(sqr(miterLim));
2759
2760 norms := GetNormals(path);
2761 resCnt := 0; resCap := 0;
2762
2763 case endStyle of
2764 esButt: DoBevel(0,0);
2765 esRound: DoRound(0,0);
2766 else DoSquare(0, 0);
2767 end;
2768
2769 // offset the left side going **forward**
2770 k := 0;
2771 highJ := len -1;
2772 for j := 1 to highJ -1 do DoPoint(j,k);
2773
2774 // reverse the normals ...
2775 for j := highJ downto 1 do
2776 begin
2777 norms[j].X := -norms[j-1].X;
2778 norms[j].Y := -norms[j-1].Y;
2779 end;
2780 norms[0] := norms[len -1];
2781
2782 case endStyle of
2783 esButt: DoBevel(highJ,highJ);
2784 esRound: DoRound(highJ,highJ);
2785 else DoSquare(highJ,highJ);
2786 end;
2787
2788 // offset the left side going **backward**
2789 k := highJ;
2790 for j := highJ -1 downto 1 do
2791 DoPoint(j, k);
2792
2793 SetLength(Result, resCnt);
2794 end;
2795 //------------------------------------------------------------------------------
2796
2797 function GrowClosedLine(const line: TPathD; width: double;
2798 joinStyle: TJoinStyle; miterLim: double = 0; scale: double = 1.0): TPathsD;
2799 var
2800 norms: TPathD;
2801 rec: TRectD;
2802 skipHole: Boolean;
2803 begin
2804 rec := GetBoundsD(line);
2805 skipHole := (rec.Width <= width) or (rec.Height <= width);
2806 if skipHole then
2807 begin
2808 SetLength(Result, 1);
2809 norms := GetNormals(line);
2810 Result[0] := Grow(line, norms, width/2, joinStyle, miterLim, scale, false);
2811 end else
2812 begin
2813 SetLength(Result, 2);
2814 norms := GetNormals(line);
2815 Result[0] := Grow(line, norms, width/2, joinStyle, miterLim, scale, false);
2816 Result[1] := ReversePath(
2817 Grow(line, norms, -width/2, joinStyle, miterLim, scale, false));
2818 end;
2819 end;
2820 //------------------------------------------------------------------------------
2821
2822 function RoughOutline(const line: TPathD; lineWidth: double;
2823 joinStyle: TJoinStyle; endStyle: TEndStyle;
2824 miterLim: double = 0; scale: double = 1.0): TPathsD;
2825 var
2826 lines: TPathsD;
2827 begin
2828 SetLength(lines,1);
2829 lines[0] := line;
2830 Result := RoughOutline(lines, lineWidth, joinStyle, endStyle, miterLim, scale);
2831 end;
2832 //------------------------------------------------------------------------------
2833
2834 function RoughOutline(const lines: TPathsD; lineWidth: double;
2835 joinStyle: TJoinStyle; endStyle: TEndStyle;
2836 miterLim: double = 0; scale: double = 1.0): TPathsD;
2837 var
2838 i: integer;
2839 lwDiv2: double;
2840 p: TPathD;
2841 begin
2842 result := nil;
2843 if not assigned(lines) then exit;
2844 if joinStyle = jsAuto then
2845 begin
2846 if endStyle in [esPolygon, esRound] then
2847 joinStyle := jsRound else
2848 joinStyle := jsSquare;
2849 end;
2850 if scale = 0 then scale := 1;
2851
2852 if endStyle = esPolygon then
2853 begin
2854 for i := 0 to high(lines) do
2855 begin
2856 if Length(lines[i]) = 1 then
2857 begin
2858 lwDiv2 := lineWidth/2;
2859 with lines[i][0] do
2860 AppendPath(Result,
2861 Ellipse(RectD(x-lwDiv2, y-lwDiv2, x+lwDiv2, y+lwDiv2)));
2862 end else
2863 begin
2864 p := StripNearDuplicates(lines[i], 0.1, true);
2865 if Length(p) = 2 then AppendPoint(p, p[0]);
2866 AppendPath(Result,
2867 GrowClosedLine(p, lineWidth, joinStyle, miterLim, scale));
2868 end;
2869 end;
2870 end
2871 else
2872 begin
2873 SetLength(Result, Length(lines));
2874 for i := 0 to high(lines) do
2875 Result[i] := GrowOpenLine(lines[i], lineWidth,
2876 joinStyle, endStyle, miterLim, scale);
2877 end;
2878 end;
2879 //------------------------------------------------------------------------------
2880
2881 function Rectangle(const rec: TRect): TPathD;
2882 begin
2883 NewPointDArray(Result, 4, True);
2884 with rec do
2885 begin
2886 result[0] := PointD(left, top);
2887 result[1] := PointD(right, top);
2888 result[2] := PointD(right, bottom);
2889 result[3] := PointD(left, bottom);
2890 end;
2891 end;
2892 //------------------------------------------------------------------------------
2893
2894 function Rectangle(const rec: TRectD): TPathD;
2895 begin
2896 NewPointDArray(Result, 4, True);
2897 with rec do
2898 begin
2899 result[0] := PointD(left, top);
2900 result[1] := PointD(right, top);
2901 result[2] := PointD(right, bottom);
2902 result[3] := PointD(left, bottom);
2903 end;
2904 end;
2905 //------------------------------------------------------------------------------
2906
2907 function Rectangle(l, t, r, b: double): TPathD;
2908 begin
2909 NewPointDArray(Result, 4, True);
2910 result[0] := PointD(l, t);
2911 result[1] := PointD(r, t);
2912 result[2] := PointD(r, b);
2913 result[3] := PointD(l, b);
2914 end;
2915 //------------------------------------------------------------------------------
2916
2917 procedure InflateRect(var rec: TRect; dx, dy: integer);
2918 begin
2919 rec.Left := rec.Left - dx;
2920 rec.Top := rec.Top - dy;
2921 rec.Right := rec.Right + dx;
2922 rec.Bottom := rec.Bottom + dy;
2923 end;
2924 //------------------------------------------------------------------------------
2925
2926 procedure InflateRect(var rec: TRectD; dx, dy: double);
2927 begin
2928 rec.Left := rec.Left - dx;
2929 rec.Top := rec.Top - dy;
2930 rec.Right := rec.Right + dx;
2931 rec.Bottom := rec.Bottom + dy;
2932 end;
2933 //------------------------------------------------------------------------------
2934
2935 function NormalizeRect(var rect: TRect): Boolean;
2936 var
2937 i: integer;
2938 begin
2939 Result := False;
2940 with rect do
2941 begin
2942 if Left > Right then
2943 begin
2944 i := Left;
2945 Left := Right;
2946 Right := i;
2947 Result := True;
2948 end;
2949 if Top > Bottom then
2950 begin
2951 i := Top;
2952 Top := Bottom;
2953 Bottom := i;
2954 Result := True;
2955 end;
2956 end;
2957 end;
2958 //------------------------------------------------------------------------------
2959
2960 function RoundRect(const rec: TRect; radius: integer): TPathD;
2961 begin
2962 Result := RoundRect(RectD(rec), PointD(radius, radius));
2963 end;
2964 //------------------------------------------------------------------------------
2965
2966 function RoundRect(const rec: TRect; radius: TPoint): TPathD;
2967 begin
2968 Result := RoundRect(RectD(rec), PointD(radius));
2969 end;
2970 //------------------------------------------------------------------------------
2971
2972 function RoundRect(const rec: TRectD; radius: double): TPathD;
2973 begin
2974 Result := RoundRect(rec, PointD(radius, radius));
2975 end;
2976 //------------------------------------------------------------------------------
2977
2978 function RoundRect(const rec: TRectD; radius: TPointD): TPathD;
2979 var
2980 i,j : integer;
2981 corners : TPathD;
2982 bezPts : TPathD;
2983 magic : TPointD;
2984 const
2985 magicC: double = 0.55228475; // =4/3 * (sqrt(2)-1)
2986 begin
2987 Result := nil;
2988 if rec.IsEmpty then Exit;
2989 radius.X := Min(radius.X, rec.Width/2);
2990 radius.Y := Min(radius.Y, rec.Height/2);
2991 if (radius.X < 1) and (radius.Y < 1) then
2992 begin
2993 Result := Rectangle(rec);
2994 Exit;
2995 end;
2996 magic.X := radius.X * magicC;
2997 magic.Y := radius.Y * magicC;
2998 NewPointDArray(Corners, 4, True);
2999 with rec do
3000 begin
3001 corners[0] := PointD(Right, Top);
3002 corners[1] := BottomRight;
3003 corners[2] := PointD(Left, Bottom);
3004 corners[3] := TopLeft;
3005 end;
3006 NewPointDArray(Result, 1, True);
3007 Result[0].X := corners[3].X + radius.X;
3008 Result[0].Y := corners[3].Y;
3009 NewPointDArray(bezPts, 4, True);
3010 for i := 0 to High(corners) do
3011 begin
3012 for j := 0 to 3 do bezPts[j] := corners[i];
3013 case i of
3014 3:
3015 begin
3016 bezPts[0].Y := bezPts[0].Y + radius.Y;
3017 bezPts[1].Y := bezPts[0].Y - magic.Y;
3018 bezPts[3].X := bezPts[3].X + radius.X;
3019 bezPts[2].X := bezPts[3].X - magic.X;
3020 end;
3021 0:
3022 begin
3023 bezPts[0].X := bezPts[0].X - radius.X;
3024 bezPts[1].X := bezPts[0].X + magic.X;
3025 bezPts[3].Y := bezPts[3].Y + radius.Y;
3026 bezPts[2].Y := bezPts[3].Y - magic.Y;
3027 end;
3028 1:
3029 begin
3030 bezPts[0].Y := bezPts[0].Y - radius.Y;
3031 bezPts[1].Y := bezPts[0].Y + magic.Y;
3032 bezPts[3].X := bezPts[3].X - radius.X;
3033 bezPts[2].X := bezPts[3].X + magic.X;
3034 end;
3035 2:
3036 begin
3037 bezPts[0].X := bezPts[0].X + radius.X;
3038 bezPts[1].X := bezPts[0].X - magic.X;
3039 bezPts[3].Y := bezPts[3].Y - radius.Y;
3040 bezPts[2].Y := bezPts[3].Y + magic.Y;
3041 end;
3042 end;
3043 ConcatPaths(Result, FlattenCBezier(bezPts));
3044 end;
3045 end;
3046 //------------------------------------------------------------------------------
3047
3048 function Circle(const pt: TPoint; radius: double): TPathD;
3049 var
3050 rec: TRectD;
3051 begin
3052 rec.Left := pt.X - radius;
3053 rec.Right := pt.X + radius;
3054 rec.Top := pt.Y - radius;
3055 rec.Bottom := pt.Y + radius;
3056 Result := Ellipse(rec);
3057 end;
3058 //------------------------------------------------------------------------------
3059
3060 function Circle(const pt: TPointD; radius: double): TPathD;
3061 var
3062 rec: TRectD;
3063 begin
3064 rec.Left := pt.X - radius;
3065 rec.Right := pt.X + radius;
3066 rec.Top := pt.Y - radius;
3067 rec.Bottom := pt.Y + radius;
3068 Result := Ellipse(rec);
3069 end;
3070 //------------------------------------------------------------------------------
3071
3072 function Circle(const pt: TPointD; radius: double; pendingScale: double): TPathD;
3073 var
3074 rec: TRectD;
3075 begin
3076 rec.Left := pt.X - radius;
3077 rec.Right := pt.X + radius;
3078 rec.Top := pt.Y - radius;
3079 rec.Bottom := pt.Y + radius;
3080 Result := Ellipse(rec, pendingScale);
3081 end;
3082 //------------------------------------------------------------------------------
3083
3084 function CalcCircleFrom3Points(const p1,p2,p3: TPointD;
3085 out centre: TPointD; out radius: double): Boolean;
3086 var
3087 mat11, mat12, mat13, mat14: TMatrixD;
3088 m11,m12,m13,m14: double;
3089 begin
3090 mat11 := Matrix(p1.X, p1.Y, 1, p2.X, p2.Y, 1, p3.X, p3.Y, 1);
3091 m11 := MatrixDeterminant(mat11);
3092 Result := m11 <> 0;
3093 if not Result then Exit;
3094 mat12 := Matrix(Sqr(p1.X)+Sqr(p1.Y), p1.Y, 1,
3095 Sqr(p2.X)+Sqr(p2.Y), p2.Y, 1, Sqr(p3.X)+Sqr(p3.Y), p3.Y, 1);
3096 mat12 := Matrix(2, 1, 1, 20, 4, 1, 34, 3, 1);
3097 m12 := MatrixDeterminant(mat12);
3098 mat13 := Matrix(Sqr(p1.X)+Sqr(p1.Y), p1.X, 1,
3099 Sqr(p2.X)+Sqr(p2.Y), p2.X, 1, Sqr(p3.X)+Sqr(p3.Y), p3.X, 1);
3100 m13 := MatrixDeterminant(mat13);
3101 mat14 := Matrix(Sqr(p1.X)+Sqr(p1.Y), p1.X, p1.Y,
3102 Sqr(p2.X)+Sqr(p2.Y), p2.X, p2.Y, Sqr(p3.X)+Sqr(p3.Y), p3.X, p3.Y);
3103 m14 := MatrixDeterminant(mat14);
3104 centre.X := 0.5 * m12/m11;
3105 centre.Y := -0.5 * m13/m11;
3106 radius := Sqrt(Sqr(centre.X) + Sqr(centre.Y) + m14/m11);
3107 end;
3108 //------------------------------------------------------------------------------
3109
3110 function Ellipse(const rec: TRectD; pendingScale: double): TPathD;
3111 var
3112 steps: integer;
3113 begin
3114 if pendingScale <= 0 then pendingScale := 1;
3115 steps := Round(CalcRoundingSteps((rec.width + rec.Height) * pendingScale));
3116 Result := Ellipse(rec, steps);
3117 end;
3118 //------------------------------------------------------------------------------
3119
3120
3121 function Ellipse(const rec: TRect; steps: integer): TPathD;
3122 begin
3123 Result := Ellipse(RectD(rec), steps);
3124 end;
3125 //------------------------------------------------------------------------------
3126
3127 function Ellipse(const rec: TRectD; steps: integer): TPathD;
3128 var
3129 i: Integer;
3130 sinA, cosA: double;
3131 centre, radius, delta: TPointD;
3132 begin
3133 result := nil;
3134 if rec.IsEmpty then Exit;
3135 with rec do
3136 begin
3137 centre := rec.MidPoint;
3138 radius := PointD(Width * 0.5, Height * 0.5);
3139 end;
3140 if steps < 4 then
3141 steps := Round(CalcRoundingSteps(rec.width + rec.height));
3142 GetSinCos(2 * Pi / Steps, sinA, cosA);
3143 delta.x := cosA; delta.y := sinA;
3144 NewPointDArray(Result, Steps, True);
3145 Result[0] := PointD(centre.X + radius.X, centre.Y);
3146 for i := 1 to steps -1 do
3147 begin
3148 Result[i] := PointD(centre.X + radius.X * delta.x,
3149 centre.Y + radius.y * delta.y);
3150 delta := PointD(delta.X * cosA - delta.Y * sinA,
3151 delta.Y * cosA + delta.X * sinA);
3152 end; //rotates clockwise
3153 end;
3154 //------------------------------------------------------------------------------
3155
3156 function RotatedEllipse(const rec: TRectD; angle: double; steps: integer = 0): TPathD;
3157 begin
3158 Result := Ellipse(rec, steps);
3159 if angle = 0 then Exit;
3160 Result := RotatePath(Result, rec.MidPoint, angle);
3161 end;
3162 //------------------------------------------------------------------------------
3163
3164 function RotatedEllipse(const rec: TRectD; angle: double; pendingScale: double): TPathD;
3165 begin
3166 Result := Ellipse(rec, pendingScale);
3167 if angle = 0 then Exit;
3168 Result := RotatePath(Result, rec.MidPoint, angle);
3169 end;
3170 //------------------------------------------------------------------------------
3171
3172 function AngleToEllipticalAngle(const ellRec: TRectD; angle: double): double;
3173 begin
3174 Result := arctan2(ellRec.Height/ellRec.Width * sin(angle), cos(angle));
3175 end;
3176 //------------------------------------------------------------------------------
3177
3178 function EllipticalAngleToAngle(const ellRec: TRectD; angle: double): double;
3179 begin
3180 Result := ArcTan2(sin(angle) *ellRec.Width, cos(angle) * ellRec.Height);
3181 end;
3182 //------------------------------------------------------------------------------
3183
3184 function Star(const rec: TRectD; points: integer; indentFrac: double): TPathD;
3185 var
3186 i: integer;
3187 innerOff: double;
3188 p, p2: TPathD;
3189 rec2: TRectD;
3190 begin
3191 Result := nil;
3192 if points < 5 then points := 5
3193 else if points > 15 then points := 15;
3194 if indentFrac < 0.2 then indentFrac := 0.2
3195 else if indentFrac > 0.8 then indentFrac := 0.8;
3196 innerOff := Min(rec.Width, rec.Height) * indentFrac * 0.5;
3197 if not Odd(points) then inc(points);
3198 p := Ellipse(rec, points);
3199 if not Assigned(p) then Exit;
3200 rec2 := rec;
3201 Img32.Vector.InflateRect(rec2, -innerOff, -innerOff);
3202 if rec2.IsEmpty then
3203 p2 := Ellipse(rec, points*2) else
3204 p2 := Ellipse(rec2, points*2);
3205 NewPointDArray(Result, points*2, True);
3206 for i := 0 to points -1 do
3207 begin
3208 Result[i*2] := p[i];
3209 Result[i*2+1] := p2[i*2+1];
3210 end;
3211 end;
3212 //------------------------------------------------------------------------------
3213
3214 function Star(const focalPt: TPointD;
3215 innerRadius, outerRadius: double; points: integer): TPathD;
3216 var
3217 i: Integer;
3218 sinA, cosA: double;
3219 delta: TPointD;
3220 begin
3221 result := nil;
3222 if (innerRadius <= 0) or (outerRadius <= 0) then Exit;
3223 if points <= 5 then points := 10
3224 else points := points * 2;
3225 GetSinCos(2 * Pi / points, sinA, cosA);
3226 delta.x := cosA; delta.y := sinA;
3227 NewPointDArray(Result, points, True);
3228 Result[0] := PointD(focalPt.X + innerRadius, focalPt.Y);
3229 for i := 1 to points -1 do
3230 begin
3231 if Odd(i) then
3232 Result[i] := PointD(focalPt.X + outerRadius * delta.x,
3233 focalPt.Y + outerRadius * delta.y)
3234 else
3235 Result[i] := PointD(focalPt.X + innerRadius * delta.x,
3236 focalPt.Y + innerRadius * delta.y);
3237 delta := PointD(delta.X * cosA - delta.Y * sinA,
3238 delta.Y * cosA + delta.X * sinA);
3239 end;
3240 end;
3241 //------------------------------------------------------------------------------
3242
3243 function Arc(const rec: TRectD;
3244 startAngle, endAngle: double; scale: double): TPathD;
3245 var
3246 i, steps: Integer;
3247 angle: double;
3248 sinA, cosA: double;
3249 centre, radius: TPointD;
3250 deltaX, deltaX2, deltaY: double;
3251 const
3252 qtrDeg = PI/1440;
3253 begin
3254 Result := nil;
3255 if (endAngle = startAngle) or IsEmptyRect(rec) then Exit;
3256 if scale <= 0 then scale := 4.0;
3257
3258 {$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES}
3259 startAngle := -startAngle;
3260 endAngle := -endAngle;
3261 {$ENDIF}
3262
3263 NormalizeAngle(startAngle, qtrDeg);
3264 NormalizeAngle(endAngle, qtrDeg);
3265 with rec do
3266 begin
3267 centre := MidPoint;
3268 radius := PointD(Width * 0.5, Height * 0.5);
3269 end;
3270 if endAngle < startAngle then
3271 angle := endAngle - startAngle + angle360 else
3272 angle := endAngle - startAngle;
3273 //steps = (No. steps for a whole ellipse) * angle/(2*Pi)
3274 steps := Round(CalcRoundingSteps((rec.width + rec.height)/2 * scale));
3275 steps := steps div 2; /////////////////////////////////
3276 if steps < 2 then steps := 2;
3277 NewPointDArray(Result, Steps +1, True);
3278 //angle of the first step ...
3279 GetSinCos(startAngle, deltaY, deltaX);
3280 Result[0].X := centre.X + radius.X * deltaX;
3281 Result[0].Y := centre.Y + radius.y * deltaY;
3282 //angle of each subsequent step ...
3283 GetSinCos(angle / Steps, sinA, cosA);
3284 for i := 1 to steps do
3285 begin
3286 deltaX2 := deltaX * cosA - deltaY * sinA;
3287 deltaY := deltaY * cosA + deltaX * sinA;
3288 deltaX := deltaX2;
3289 Result[i].X := centre.X + radius.X * deltaX;
3290 Result[i].Y := centre.Y + radius.y * deltaY;
3291 end; //progresses clockwise from start to end
3292 end;
3293 //------------------------------------------------------------------------------
3294
3295 function Pie(const rec: TRectD;
3296 StartAngle, EndAngle: double; scale: double): TPathD;
3297 var
3298 len: integer;
3299 begin
3300 result := Arc(rec, StartAngle, EndAngle, scale);
3301 len := length(result);
3302 SetLengthUninit(result, len +1);
3303 result[len] := PointD((rec.Left + rec.Right)/2, (rec.Top + rec.Bottom)/2);
3304 end;
3305 //------------------------------------------------------------------------------
3306
3307 function ArrowHead(const arrowTip, ctrlPt: TPointD; size: double;
3308 arrowStyle: TArrowStyle): TPathD;
3309 var
3310 unitVec, basePt: TPointD;
3311 sDiv40, sDiv50, sDiv60, sDiv120: double;
3312 begin
3313 result := nil;
3314 sDiv40 := size * 0.40;
3315 sDiv50 := size * 0.50;
3316 sDiv60 := size * 0.60;
3317 sDiv120 := sDiv60 * 2;
3318 unitVec := GetUnitVector(ctrlPt, arrowTip);
3319 case arrowStyle of
3320 asNone:
3321 Exit;
3322 asSimple:
3323 begin
3324 NewPointDArray(result, 3, True);
3325 basePt := TranslatePoint(arrowTip, -unitVec.X * size, -unitVec.Y * size);
3326 result[0] := arrowTip;
3327 result[1] := TranslatePoint(basePt, -unitVec.Y * sDiv50, unitVec.X * sDiv50);
3328 result[2] := TranslatePoint(basePt, unitVec.Y * sDiv50, -unitVec.X * sDiv50);
3329 end;
3330 asFancy:
3331 begin
3332 NewPointDArray(result, 4, True);
3333 basePt := TranslatePoint(arrowTip,
3334 -unitVec.X * sDiv120, -unitVec.Y * sDiv120);
3335 result[0] := TranslatePoint(basePt, -unitVec.Y *sDiv50, unitVec.X *sDiv50);
3336 result[1] := TranslatePoint(arrowTip, -unitVec.X *size, -unitVec.Y *size);
3337 result[2] := TranslatePoint(basePt, unitVec.Y *sDiv50, -unitVec.X *sDiv50);
3338 result[3] := arrowTip;
3339 end;
3340 asDiamond:
3341 begin
3342 NewPointDArray(result, 4, True);
3343 basePt := TranslatePoint(arrowTip, -unitVec.X * sDiv60, -unitVec.Y * sDiv60);
3344 result[0] := arrowTip;
3345 result[1] := TranslatePoint(basePt, -unitVec.Y * sDiv50, unitVec.X * sDiv50);
3346 result[2] := TranslatePoint(arrowTip, -unitVec.X * sDiv120, -unitVec.Y * sDiv120);
3347 result[3] := TranslatePoint(basePt, unitVec.Y * sDiv50, -unitVec.X * sDiv50);
3348 end;
3349 asCircle:
3350 begin
3351 basePt := TranslatePoint(arrowTip, -unitVec.X * sDiv50, -unitVec.Y * sDiv50);
3352 with Point(basePt) do
3353 result := Ellipse(RectD(x - sDiv50, y - sDiv50, x + sDiv50, y + sDiv50));
3354 end;
3355 asTail:
3356 begin
3357 NewPointDArray(result, 6, True);
3358 basePt := TranslatePoint(arrowTip, -unitVec.X * sDiv60, -unitVec.Y * sDiv60);
3359 result[0] := TranslatePoint(arrowTip, -unitVec.X * sDiv50, -unitVec.Y * sDiv50);
3360 result[1] := TranslatePoint(arrowTip, -unitVec.Y * sDiv40, unitVec.X * sDiv40);
3361 result[2] := TranslatePoint(basePt, -unitVec.Y * sDiv40, unitVec.X * sDiv40);
3362 result[3] := TranslatePoint(arrowTip, -unitVec.X * sDiv120, -unitVec.Y * sDiv120);
3363 result[4] := TranslatePoint(basePt, unitVec.Y * sDiv40, -unitVec.X * sDiv40);
3364 result[5] := TranslatePoint(arrowTip, unitVec.Y * sDiv40, -unitVec.X * sDiv40);
3365 end;
3366 end;
3367 end;
3368 //------------------------------------------------------------------------------
3369
3370 function GetDefaultArrowHeadSize(lineWidth: double): double;
3371 begin
3372 Result := lineWidth *3 + 7;
3373 end;
3374 //------------------------------------------------------------------------------
3375
3376 procedure AdjustPoint(var pt: TPointD; const referencePt: TPointD; delta: double);
3377 var
3378 vec: TPointD;
3379 begin
3380 //Positive delta moves pt away from referencePt, and
3381 //negative delta moves pt toward referencePt.
3382 vec := GetUnitVector(referencePt, pt);
3383 pt.X := pt.X + (vec.X * delta);
3384 pt.Y := pt.Y + (vec.Y * delta);
3385 end;
3386 //------------------------------------------------------------------------------
3387
3388 function ShortenPath(const path: TPathD;
3389 pathEnd: TPathEnd; amount: double): TPathD;
3390 var
3391 len, amount2: double;
3392 vec: TPointD;
3393 i, highPath: integer;
3394 begin
3395 result := path;
3396 highPath := high(path);
3397 if highPath < 1 then Exit;
3398 amount2 := amount;
3399 if pathEnd <> peEnd then
3400 begin
3401 //shorten start
3402 i := 0;
3403 while (i < highPath) do
3404 begin
3405 len := Distance(result[i], result[i+1]);
3406 if (len >= amount) then Break;
3407 amount := amount - len;
3408 inc(i);
3409 end;
3410 if i > 0 then
3411 begin
3412 Move(path[i], Result[0], (highPath - i +1) * SizeOf(TPointD));
3413 dec(highPath, i);
3414 SetLength(Result, highPath +1);
3415 end;
3416 if amount > 0 then
3417 begin
3418 vec := GetUnitVector(result[0], result[1]);
3419 result[0].X := result[0].X + vec.X * amount;
3420 result[0].Y := result[0].Y + vec.Y * amount;
3421 end;
3422 end;
3423 if pathEnd <> peStart then
3424 begin
3425 //shorten end
3426 while (highPath > 1) do
3427 begin
3428 len := Distance(result[highPath], result[highPath -1]);
3429 if (len >= amount2) then Break;
3430 amount2 := amount2 - len;
3431 dec(highPath);
3432 end;
3433 SetLength(Result, highPath +1);
3434 if amount2 > 0 then
3435 begin
3436 vec := GetUnitVector(result[highPath], result[highPath -1]);
3437 result[highPath].X := result[highPath].X + vec.X * amount2;
3438 result[highPath].Y := result[highPath].Y + vec.Y * amount2;
3439 end;
3440 end;
3441 end;
3442 //------------------------------------------------------------------------------
3443
3444 function GetDashedPath(const path: TPathD;
3445 closed: Boolean; const pattern: TArrayOfDouble;
3446 patternOffset: PDouble): TPathsD;
3447 var
3448 i, highI, paIdx: integer;
3449 vecs, path2, dash: TPathD;
3450 patCnt: integer;
3451 patLen: double;
3452 dashCapacity, dashCnt, ptsCapacity, ptsCnt: integer;
3453 segLen, residualPat, patOff: double;
3454 filling: Boolean;
3455 pt, pt2: TPointD;
3456
3457 procedure NewDash;
3458 begin
3459 if ptsCnt = 1 then ptsCnt := 0;
3460 if ptsCnt = 0 then Exit;
3461 if dashCnt = dashCapacity then
3462 begin
3463 inc(dashCapacity, BuffSize);
3464 setLength(result, dashCapacity);
3465 end;
3466 result[dashCnt] := Copy(dash, 0, ptsCnt);
3467 inc(dashCnt);
3468 ptsCapacity := BuffSize;
3469 setLength(dash, ptsCapacity);
3470 ptsCnt := 0;
3471 end;
3472
3473 procedure ExtendDash(const pt: TPointD);
3474 begin
3475 if ptsCnt = ptsCapacity then
3476 begin
3477 inc(ptsCapacity, BuffSize);
3478 setLength(dash, ptsCapacity);
3479 end;
3480 dash[ptsCnt] := pt;
3481 inc(ptsCnt);
3482 end;
3483
3484 begin
3485 Result := nil;
3486 paIdx := 0;
3487 patCnt := length(pattern);
3488 path2 := path;
3489 highI := high(path2);
3490 if (highI < 1) or (patCnt = 0) then Exit;
3491 if closed and
3492 ((path2[highI].X <> path2[0].X) or (path2[highI].Y <> path2[0].Y)) then
3493 begin
3494 inc(highI);
3495 setLength(path2, highI +2);
3496 path2[highI] := path2[0];
3497 end;
3498 vecs := GetVectors(path2);
3499 if (vecs[0].X = 0) and (vecs[0].Y = 0) then Exit; //not a line
3500 if not assigned(patternOffset) then
3501 patOff := 0 else
3502 patOff := patternOffset^;
3503 patLen := 0;
3504 for i := 0 to patCnt -1 do
3505 patLen := patLen + pattern[i];
3506 if patOff < 0 then
3507 begin
3508 patOff := patLen + patOff;
3509 while patOff < 0 do
3510 patOff := patOff + patLen;
3511 end
3512 else while patOff > patLen do
3513 patOff := patOff - patLen;
3514 //nb: each dash is made up of 2 or more pts
3515 dashCnt := 0;
3516 dashCapacity := 0;
3517 ptsCnt := 0;
3518 ptsCapacity := 0;
3519 filling := true;
3520 while patOff >= pattern[paIdx] do
3521 begin
3522 filling := not filling;
3523 patOff := patOff - pattern[paIdx];
3524 paIdx := (paIdx + 1) mod patCnt;
3525 end;
3526 residualPat := pattern[paIdx] - patOff;
3527 pt := path2[0];
3528 ExtendDash(pt);
3529 i := 0;
3530 while (i < highI) do
3531 begin
3532 segLen := Distance(pt, path2[i+1]);
3533 if residualPat > segLen then
3534 begin
3535 if filling then ExtendDash(path2[i+1]);
3536 residualPat := residualPat - segLen;
3537 pt := path2[i+1];
3538 inc(i);
3539 end else
3540 begin
3541 pt2.X := pt.X + vecs[i].X * residualPat;
3542 pt2.Y := pt.Y + vecs[i].Y * residualPat;
3543 if filling then ExtendDash(pt2);
3544 filling := not filling;
3545 NewDash;
3546 paIdx := (paIdx + 1) mod patCnt;
3547 residualPat := pattern[paIdx];
3548 pt := pt2;
3549 ExtendDash(pt);
3550 end;
3551 end;
3552 NewDash;
3553 SetLength(Result, dashCnt);
3554 if not assigned(patternOffset) then Exit;
3555 patOff := 0;
3556 for i := 0 to paIdx -1 do
3557 patOff := patOff + pattern[i];
3558 patternOffset^ := patOff + (pattern[paIdx] - residualPat);
3559 end;
3560 //------------------------------------------------------------------------------
3561
3562 function GetDashedOutLine(const path: TPathD;
3563 closed: Boolean; const pattern: TArrayOfDouble;
3564 patternOffset: PDouble; lineWidth: double;
3565 joinStyle: TJoinStyle; endStyle: TEndStyle): TPathsD;
3566 var
3567 i: integer;
3568 tmp: TPathsD;
3569 begin
3570 Result := nil;
3571 for i := 0 to High(pattern) do
3572 if pattern[i] <= 0 then pattern[i] := 1;
3573 tmp := GetDashedPath(path, closed, pattern, patternOffset);
3574 for i := 0 to high(tmp) do
3575 // AppendPath(Result, GrowOpenLine(tmp[i],
3576 // lineWidth, joinStyle, endStyle, 2));
3577 AppendPath(Result, GrowClosedLine(tmp[i], lineWidth, joinStyle, 2));
3578 end;
3579 //------------------------------------------------------------------------------
3580
3581 function GetBoundsD(const paths: TArrayOfPathsD): TRectD;
3582 var
3583 i, len: integer;
3584 rec: TRectD;
3585 begin
3586 len := Length(paths);
3587 i := 0;
3588 while (i < len) do
3589 begin
3590 rec := GetBoundsD(paths[i]);
3591 if not IsEmptyRect(rec) then Break;
3592 inc(i);
3593 end;
3594
3595 if i = len then
3596 begin
3597 Result := NullRectD;
3598 Exit;
3599 end;
3600 Result := rec;
3601
3602 for i := i + 1 to len -1 do
3603 begin
3604 rec := GetBoundsD(paths[i]);
3605 if IsEmptyRect(rec) then Continue;
3606 Result := UnionRect(Result, rec);
3607 end;
3608 end;
3609 //------------------------------------------------------------------------------
3610
3611 function GetBoundsD(const paths: TPathsD): TRectD;
3612 var
3613 i,j: integer;
3614 p: PPointD;
3615 {$IFDEF CPUX64}
3616 l,t,r,b,x,y: double;
3617 {$ENDIF CPUX64}
3618 begin
3619 if paths = nil then
3620 begin
3621 Result := NullRectD;
3622 Exit;
3623 end;
3624
3625 {$IFDEF CPUX64}
3626 l := MaxDouble; t := l;
3627 r := -MaxDouble; b := r;
3628 {$ELSE}
3629 Result.Left := MaxDouble;
3630 Result.Top := MaxDouble;
3631 Result.Right := -MaxDouble;
3632 Result.Bottom := -MaxDouble;
3633 {$ENDIF CPUX64}
3634 for i := 0 to high(paths) do
3635 begin
3636 p := PPointD(paths[i]);
3637 if not assigned(p) then Continue;
3638 for j := 0 to high(paths[i]) do
3639 begin
3640 {$IFDEF CPUX64}
3641 // load p.X and p.Y into xmm registers
3642 x := p.X;
3643 y := p.Y;
3644 if x < l then l := x;
3645 if x > r then r := x;
3646 if y < t then t := y;
3647 if y > b then b := y;
3648 {$ELSE}
3649 // If we must use the FPU and memory then we should write directly
3650 // to the target memory.
3651 if p.x < Result.Left then Result.Left := p.x;
3652 if p.x > Result.Right then Result.Right := p.x;
3653 if p.y < Result.Top then Result.Top := p.y;
3654 if p.y > Result.Bottom then Result.Bottom := p.y;
3655 {$ENDIF CPUX64}
3656 inc(p);
3657 end;
3658 end;
3659 {$IFDEF CPUX64}
3660 if r < l then
3661 Result := NullRectD
3662 else
3663 begin
3664 // Inline the RectD() call by hand
3665 Result.Left := l;
3666 Result.Top := t;
3667 Result.Right := r;
3668 Result.Bottom := b;
3669 end;
3670 {$ELSE}
3671 if Result.Right < Result.Left then
3672 Result := NullRectD;
3673 {$ENDIF CPUX64}
3674 end;
3675 //------------------------------------------------------------------------------
3676
3677 function GetBoundsD(const path: TPathD): TRectD;
3678 var
3679 i,highI: integer;
3680 p: PPointD;
3681 {$IFDEF CPUX64}
3682 l,t,r,b,x,y: double;
3683 {$ENDIF CPUX64}
3684 begin
3685 highI := High(path);
3686 if highI < 0 then
3687 begin
3688 Result := NullRectD;
3689 Exit;
3690 end;
3691
3692 {$IFDEF CPUX64}
3693 l := path[0].X; r := l;
3694 t := path[0].Y; b := t;
3695 p := PPointD(path);
3696 for i := 1 to highI do
3697 begin
3698 inc(p);
3699 // load p.X and p.Y into xmm registers
3700 x := p.X;
3701 y := p.Y;
3702 if x < l then l := x;
3703 if x > r then r := x;
3704 if y < t then t := y;
3705 if y > b then b := y;
3706 end;
3707 // Inline the RectD() call by hand
3708 Result.Left := l;
3709 Result.Top := t;
3710 Result.Right := r;
3711 Result.Bottom := b;
3712 {$ELSE}
3713 // If we must use the FPU and memory then we should write directly
3714 // to the target memory.
3715 {$IFDEF RECORD_METHODS}
3716 Result.TopLeft := path[0]; // uses "rep movsd"
3717 Result.BottomRight := Result.TopLeft;
3718 {$ELSE}
3719 Result.Left := path[0].X; // uses "fld" and "fstp"
3720 Result.Top := path[0].Y;
3721 Result.Right := Result.Left;
3722 Result.Bottom := Result.Right;
3723 {$ENDIF RECORD_METHODS}
3724 p := PPointD(path);
3725 for i := 1 to highI do
3726 begin
3727 inc(p);
3728 if p.x < Result.Left then Result.Left := p.x;
3729 if p.x > Result.Right then Result.Right := p.x;
3730 if p.y < Result.Top then Result.Top := p.y;
3731 if p.y > Result.Bottom then Result.Bottom := p.y;
3732 end;
3733 {$ENDIF CPUX64}
3734 end;
3735 //------------------------------------------------------------------------------
3736
3737 function GetBounds(const path: TPathD): TRect;
3738 var
3739 recD: TRectD;
3740 begin
3741 recD := GetBoundsD(path);
3742 Result := Rect(recD);
3743 end;
3744 //------------------------------------------------------------------------------
3745
3746 function GetBounds(const paths: TPathsD): TRect;
3747 var
3748 recD: TRectD;
3749 begin
3750 recD := GetBoundsD(paths);
3751 Result := Rect(recD);
3752 end;
3753 //------------------------------------------------------------------------------
3754
3755 procedure PrePendPoint(const pt: TPointD; const p: TPathD; var Result: TPathD);
3756 var
3757 len: integer;
3758 begin
3759 len := Length(p);
3760 SetLengthUninit(Result, len +1);
3761 Result[0] := pt;
3762 if len > 0 then Move(p[0], Result[1], len * SizeOf(TPointD));
3763 end;
3764 //------------------------------------------------------------------------------
3765
3766 function PrePendPoint(const pt: TPointD; const p: TPathD): TPathD;
3767 begin
3768 PrePendPoint(pt, p, Result);
3769 end;
3770 //------------------------------------------------------------------------------
3771
3772 function PrePendPoints(const pt1, pt2: TPointD; const p: TPathD): TPathD;
3773 var
3774 len: integer;
3775 begin
3776 len := Length(p);
3777 NewPointDArray(Result, len +2, True);
3778 Result[0] := pt1;
3779 Result[1] := pt2;
3780 if len > 0 then Move(p[0], Result[2], len * SizeOf(TPointD));
3781 end;
3782 //------------------------------------------------------------------------------
3783
3784 function GetPointInQuadBezier(const a,b,c: TPointD; t: double): TPointD;
3785 var
3786 omt: double;
3787 begin
3788 if t > 1 then t := 1
3789 else if t < 0 then t := 0;
3790 omt := 1 - t;
3791 Result.X := a.X*omt*omt + b.X*2*omt*t + c.X*t*t;
3792 Result.Y := a.Y*omt*omt + b.Y*2*omt*t + c.Y*t*t;
3793 end;
3794 //------------------------------------------------------------------------------
3795
3796 function FlattenQBezier(const firstPt: TPointD; const pts: TPathD;
3797 tolerance: double = 0.0): TPathD; overload;
3798 begin
3799 if tolerance <= 0.0 then tolerance := BezierTolerance;
3800 Result := FlattenQBezier(PrePendPoint(firstPt, pts), tolerance);
3801 end;
3802 //------------------------------------------------------------------------------
3803
3804 function FlattenQBezier(const pts: TPathD; tolerance: double = 0.0): TPathD;
3805 var
3806 i, highI: integer;
3807 p: TPathD;
3808 begin
3809 Result := nil;
3810 highI := high(pts);
3811 if highI < 0 then Exit;
3812 if (highI < 2) or Odd(highI) then
3813 raise Exception.CreateRes(@rsInvalidQBezier);
3814 if tolerance <= 0.0 then tolerance := BezierTolerance;
3815 NewPointDArray(Result, 1, True);
3816 Result[0] := pts[0];
3817 for i := 0 to (highI div 2) -1 do
3818 begin
3819 if PointsEqual(pts[i*2], pts[i*2+1]) and
3820 PointsEqual(pts[i*2+1], pts[i*2+2]) then
3821 begin
3822 AppendPoint(Result, pts[i*2]);
3823 AppendPoint(Result, pts[i*2 +2]);
3824 end else
3825 begin
3826 p := FlattenQBezier(pts[i*2], pts[i*2+1], pts[i*2+2], tolerance);
3827 ConcatPaths(Result, Copy(p, 1, Length(p) -1));
3828 end;
3829 end;
3830 end;
3831 //------------------------------------------------------------------------------
3832
3833 function FlattenQBezier(const pt1, pt2, pt3: TPointD;
3834 tolerance: double = 0.0): TPathD;
3835 var
3836 resultCnt, resultLen: integer;
3837
3838 procedure AddPoint(const pt: TPointD);
3839 begin
3840 if resultCnt = resultLen then
3841 begin
3842 inc(resultLen, BuffSize);
3843 SetLengthUninit(result, resultLen);
3844 end;
3845 result[resultCnt] := pt;
3846 inc(resultCnt);
3847 end;
3848
3849 procedure DoCurve(const p1, p2, p3: TPointD);
3850 var
3851 p12, p23, p123: TPointD;
3852 begin
3853 if (abs(p1.x + p3.x - 2 * p2.x) +
3854 abs(p1.y + p3.y - 2 * p2.y) < tolerance) then
3855 begin
3856 AddPoint(p3);
3857 end else
3858 begin
3859 P12.X := (P1.X + P2.X) * 0.5;
3860 P12.Y := (P1.Y + P2.Y) * 0.5;
3861 P23.X := (P2.X + P3.X) * 0.5;
3862 P23.Y := (P2.Y + P3.Y) * 0.5;
3863 P123.X := (P12.X + P23.X) * 0.5;
3864 P123.Y := (P12.Y + P23.Y) * 0.5;
3865 DoCurve(p1, p12, p123);
3866 DoCurve(p123, p23, p3);
3867 end;
3868 end;
3869
3870 begin
3871 resultLen := 0; resultCnt := 0;
3872 if tolerance <= 0.0 then tolerance := BezierTolerance;
3873 AddPoint(pt1);
3874 if ((pt1.X = pt2.X) and (pt1.Y = pt2.Y)) or
3875 ((pt2.X = pt3.X) and (pt2.Y = pt3.Y)) then
3876 begin
3877 AddPoint(pt3)
3878 end else
3879 DoCurve(pt1, pt2, pt3);
3880 SetLength(result, resultCnt);
3881 end;
3882 //------------------------------------------------------------------------------
3883
3884 function GetPointInCubicBezier(const a,b,c,d: TPointD; t: double): TPointD;
3885 var
3886 omt: double;
3887 begin
3888 if t > 1 then t := 1
3889 else if t < 0 then t := 0;
3890 omt := 1 - t;
3891 Result.X := a.X*omt*omt*omt +b.X*3*omt*omt*t +c.X*3*omt*t*t +d.X*t*t*t;
3892 Result.Y := a.Y*omt*omt*omt +b.Y*3*omt*omt*t +c.Y*3*omt*t*t +d.Y*t*t*t;
3893 end;
3894 //------------------------------------------------------------------------------
3895
3896 function FlattenCBezier(const firstPt: TPointD; const pts: TPathD;
3897 tolerance: double = 0.0): TPathD; overload;
3898 begin
3899 Result := FlattenCBezier(PrePendPoint(firstPt, pts), tolerance);
3900 end;
3901 //------------------------------------------------------------------------------
3902
3903 function FlattenCBezier(const path: TPathD; tolerance: double = 0.0): TPathD;
3904 var
3905 i, len: integer;
3906 p: TPathD;
3907 begin
3908 Result := nil;
3909 len := Length(path) -1;
3910 if len < 0 then Exit;
3911 if (len < 3) or (len mod 3 <> 0) then
3912 raise Exception.Create(rsInvalidCBezier);
3913 if tolerance <= 0.0 then tolerance := BezierTolerance;
3914 NewPointDArray(Result, 1, True);
3915 Result[0] := path[0];
3916 for i := 0 to (len div 3) -1 do
3917 begin
3918 if PointsEqual(path[i*3], path[i*3+1]) and
3919 PointsEqual(path[i*3+2], path[i*3+3]) then
3920 begin
3921 AppendPoint(Result, path[i*3]);
3922 AppendPoint(Result, path[i*3 +3]);
3923 end else
3924 begin
3925 p := FlattenCBezier(path[i*3], path[i*3+1],
3926 path[i*3+2], path[i*3+3], tolerance);
3927 ConcatPaths(Result, Copy(p, 1, Length(p) -1));
3928 end;
3929 end;
3930 end;
3931 //------------------------------------------------------------------------------
3932
3933 function FlattenCBezier(const paths: TPathsD; tolerance: double): TPathsD;
3934 var
3935 i, len: integer;
3936 begin
3937 len := Length(paths);
3938 SetLength(Result, len);
3939 for i := 0 to len -1 do
3940 Result[i] := FlattenCBezier(paths[i], tolerance);
3941 end;
3942 //------------------------------------------------------------------------------
3943
3944 function FlattenCBezier(const pt1, pt2, pt3, pt4: TPointD;
3945 tolerance: double = 0.0): TPathD;
3946 var
3947 resultCnt, resultLen: integer;
3948
3949 procedure AddPoint(const pt: TPointD);
3950 begin
3951 if resultCnt = resultLen then
3952 begin
3953 inc(resultLen, BuffSize);
3954 SetLengthUninit(result, resultLen);
3955 end;
3956 result[resultCnt] := pt;
3957 inc(resultCnt);
3958 end;
3959
3960 procedure DoCurve(const p1, p2, p3, p4: TPointD);
3961 var
3962 p12, p23, p34, p123, p234, p1234: TPointD;
3963 begin
3964 if ((abs(p1.x +p3.x - 2*p2.x) < tolerance) and
3965 (abs(p2.x +p4.x - 2*p3.x) < tolerance)) and
3966 ((abs(p1.y +p3.y - 2*p2.y) < tolerance) and
3967 (abs(p2.y +p4.y - 2*p3.y) < tolerance)) then
3968 begin
3969 AddPoint(p4);
3970 end else
3971 begin
3972 p12.X := (p1.X + p2.X) / 2;
3973 p12.Y := (p1.Y + p2.Y) / 2;
3974 p23.X := (p2.X + p3.X) / 2;
3975 p23.Y := (p2.Y + p3.Y) / 2;
3976 p34.X := (p3.X + p4.X) / 2;
3977 p34.Y := (p3.Y + p4.Y) / 2;
3978 p123.X := (p12.X + p23.X) / 2;
3979 p123.Y := (p12.Y + p23.Y) / 2;
3980 p234.X := (p23.X + p34.X) / 2;
3981 p234.Y := (p23.Y + p34.Y) / 2;
3982 p1234.X := (p123.X + p234.X) / 2;
3983 p1234.Y := (p123.Y + p234.Y) / 2;
3984 DoCurve(p1, p12, p123, p1234);
3985 DoCurve(p1234, p234, p34, p4);
3986 end;
3987 end;
3988
3989 begin
3990 result := nil;
3991 resultLen := 0; resultCnt := 0;
3992 if tolerance <= 0.0 then tolerance := BezierTolerance;
3993 AddPoint(pt1);
3994 if ValueAlmostZero(pt1.X - pt2.X) and ValueAlmostZero(pt1.Y - pt2.Y) and
3995 ValueAlmostZero(pt3.X - pt4.X) and ValueAlmostZero(pt3.Y - pt4.Y) then
3996 begin
3997 AddPoint(pt4)
3998 end else
3999 DoCurve(pt1, pt2, pt3, pt4);
4000 SetLength(result,resultCnt);
4001 end;
4002 //------------------------------------------------------------------------------
4003
4004 function ReflectPoint(const pt, pivot: TPointD): TPointD;
4005 begin
4006 Result.X := pivot.X + (pivot.X - pt.X);
4007 Result.Y := pivot.Y + (pivot.Y - pt.Y);
4008 end;
4009 //------------------------------------------------------------------------------
4010
4011 function FlattenCSpline(const priorCtrlPt, startPt: TPointD;
4012 const pts: TPathD; tolerance: double = 0.0): TPathD;
4013 var
4014 p: TPathD;
4015 len: integer;
4016 begin
4017 len := Length(pts);
4018 NewPointDArray(p, len + 2, True);
4019 p[0] := startPt;
4020 p[1] := ReflectPoint(priorCtrlPt, startPt);
4021 if len > 0 then
4022 Move(pts[0], p[2], len * SizeOf(TPointD));
4023 Result := FlattenCSpline(p, tolerance);
4024 end;
4025 //------------------------------------------------------------------------------
4026
4027 function FlattenCSpline(const pts: TPathD; tolerance: double = 0.0): TPathD;
4028 var
4029 resultCnt, resultLen: integer;
4030
4031 procedure AddPoint(const pt: TPointD);
4032 begin
4033 if resultCnt = resultLen then
4034 begin
4035 inc(resultLen, BuffSize);
4036 SetLengthUninit(result, resultLen);
4037 end;
4038 result[resultCnt] := pt;
4039 inc(resultCnt);
4040 end;
4041
4042 procedure DoCurve(const p1, p2, p3, p4: TPointD);
4043 var
4044 p12, p23, p34, p123, p234, p1234: TPointD;
4045 begin
4046 if (abs(p1.x + p3.x - 2*p2.x) + abs(p2.x + p4.x - 2*p3.x) +
4047 abs(p1.y + p3.y - 2*p2.y) + abs(p2.y + p4.y - 2*p3.y)) < tolerance then
4048 begin
4049 AddPoint(p4);
4050 end else
4051 begin
4052 p12.X := (p1.X + p2.X) / 2;
4053 p12.Y := (p1.Y + p2.Y) / 2;
4054 p23.X := (p2.X + p3.X) / 2;
4055 p23.Y := (p2.Y + p3.Y) / 2;
4056 p34.X := (p3.X + p4.X) / 2;
4057 p34.Y := (p3.Y + p4.Y) / 2;
4058 p123.X := (p12.X + p23.X) / 2;
4059 p123.Y := (p12.Y + p23.Y) / 2;
4060 p234.X := (p23.X + p34.X) / 2;
4061 p234.Y := (p23.Y + p34.Y) / 2;
4062 p1234.X := (p123.X + p234.X) / 2;
4063 p1234.Y := (p123.Y + p234.Y) / 2;
4064 DoCurve(p1, p12, p123, p1234);
4065 DoCurve(p1234, p234, p34, p4);
4066 end;
4067 end;
4068
4069 var
4070 i, len: integer;
4071 p: PPointD;
4072 pt1,pt2,pt3,pt4: TPointD;
4073 begin
4074 result := nil;
4075 len := Length(pts); resultLen := 0; resultCnt := 0;
4076 if (len < 4) then Exit;
4077 if tolerance <= 0.0 then tolerance := BezierTolerance;
4078 //ignore incomplete trailing control points
4079 if Odd(len) then dec(len);
4080 p := @pts[0];
4081 AddPoint(p^);
4082 pt1 := p^; inc(p);
4083 pt2 := p^; inc(p);
4084 for i := 0 to (len shr 1) - 2 do
4085 begin
4086 pt3 := p^; inc(p);
4087 pt4 := p^; inc(p);
4088 DoCurve(pt1, pt2, pt3, pt4);
4089 pt1 := pt4;
4090 pt2 := ReflectPoint(pt3, pt1);
4091 end;
4092 SetLength(result,resultCnt);
4093 end;
4094 //------------------------------------------------------------------------------
4095
4096 function FlattenQSpline(const priorCtrlPt, startPt: TPointD;
4097 const pts: TPathD; tolerance: double = 0.0): TPathD;
4098 var
4099 p: TPathD;
4100 len: integer;
4101 begin
4102 len := Length(pts);
4103 NewPointDArray(p, len + 2, True);
4104 p[0] := startPt;
4105 p[1] := ReflectPoint(priorCtrlPt, startPt);
4106 if len > 0 then
4107 Move(pts[0], p[2], len * SizeOf(TPointD));
4108 Result := FlattenQSpline(p, tolerance);
4109 end;
4110 //------------------------------------------------------------------------------
4111
4112 function FlattenQSpline(const pts: TPathD; tolerance: double = 0.0): TPathD;
4113 var
4114 resultCnt, resultLen: integer;
4115
4116 procedure AddPoint(const pt: TPointD);
4117 begin
4118 if resultCnt = resultLen then
4119 begin
4120 inc(resultLen, BuffSize);
4121 SetLengthUninit(result, resultLen);
4122 end;
4123 result[resultCnt] := pt;
4124 inc(resultCnt);
4125 end;
4126
4127 procedure DoCurve(const p1, p2, p3: TPointD);
4128 var
4129 p12, p23, p123: TPointD;
4130 begin
4131 if (abs(p1.x + p3.x - 2 * p2.x) +
4132 abs(p1.y + p3.y - 2 * p2.y) < tolerance) then
4133 begin
4134 AddPoint(p3);
4135 end else
4136 begin
4137 P12.X := (P1.X + P2.X) * 0.5;
4138 P12.Y := (P1.Y + P2.Y) * 0.5;
4139 P23.X := (P2.X + P3.X) * 0.5;
4140 P23.Y := (P2.Y + P3.Y) * 0.5;
4141 P123.X := (P12.X + P23.X) * 0.5;
4142 P123.Y := (P12.Y + P23.Y) * 0.5;
4143 DoCurve(p1, p12, p123);
4144 DoCurve(p123, p23, p3);
4145 end;
4146 end;
4147
4148 var
4149 i, len: integer;
4150 p: PPointD;
4151 pt1, pt2, pt3: TPointD;
4152 begin
4153 result := nil;
4154 len := Length(pts);
4155 if (len < 3) then Exit;
4156 resultLen := 0;
4157 resultCnt := 0;
4158 if tolerance <= 0.0 then tolerance := BezierTolerance;
4159 p := @pts[0];
4160 AddPoint(p^);
4161 pt1 := p^; inc(p);
4162 pt2 := p^; inc(p);
4163 for i := 0 to len - 3 do
4164 begin
4165 pt3 := p^; inc(p);
4166 DoCurve(pt1, pt2, pt3);
4167 pt1 := pt3;
4168 pt2 := ReflectPoint(pt2, pt1);
4169 end;
4170 SetLength(result,resultCnt);
4171 end;
4172 //------------------------------------------------------------------------------
4173
4174 function MakePath(const pts: array of double): TPathD;
4175 var
4176 i, len: Integer;
4177 x,y: double;
4178 begin
4179 Result := nil;
4180 len := length(pts) div 2;
4181 if len = 0 then Exit;
4182 NewPointDArray(Result, len, True);
4183 Result[0].X := pts[0];
4184 Result[0].Y := pts[1];
4185 for i := 1 to len -1 do
4186 begin
4187 x := pts[i*2];
4188 y := pts[i*2 +1];
4189 Result[i].X := x;
4190 Result[i].Y := y;
4191 end;
4192 end;
4193 //------------------------------------------------------------------------------
4194
4195 function MakePath(const pt: TPointD): TPathD;
4196 begin
4197 SetLengthUninit(Result, 1);
4198 Result[0] := pt;
4199 end;
4200 //------------------------------------------------------------------------------
4201
4202 end.