-
Notifications
You must be signed in to change notification settings - Fork 2
/
GR32_PathsEx.pas
1886 lines (1634 loc) · 47.8 KB
/
GR32_PathsEx.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit GR32_PathsEx;
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Graphics32
*
* The Initial Developer of the Original Code is
* Mattias Andersson
*
* Portions created by the Initial Developer are Copyright (C) 2000-2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
interface
{$I GR32.inc}
{$IFNDEF PUREPASCAL}
{$IFDEF TARGET_X86}
{$DEFINE USESTACKALLOC}
{$ENDIF}
{$ENDIF}
uses
Windows, Classes, Math, GR32, GR32_Polygons, GR32_Resamplers, GR32_Transforms
{ GR32_PolygonsEx };
const
GGO_UNHINTED = $0100;
type
{ TCustomShape }
TCustomShape = class(TPersistent)
private
{ procedure SetPoint(Index: Integer; Value: TFloatPoint);
function GetEndPoint: PFloatPoint;
function GetStartPoint: PFloatPoint;
function GetPoint(Index: Integer): TFloatPoint; }
//function GetPointPtr(Index: Integer): PFloatPoint;
protected
{ function GetPointPtr(Index: Integer): PFloatPoint; virtual; abstract;
function GetNumPoints: Integer; virtual; abstract; }
procedure AssignTo(Dest: TPersistent); override;
public
function AsPolygon: TPolygon32;
//function FindClosestPoint(const P: TFloatPoint): PFloatPoint;
//function ClosestPointOnCurve(const P: TFloatPoint): TFloatPoint; virtual; abstract;
procedure AppendToPolygon(Polygon: TPolygon32); virtual; abstract;
{ procedure AddPoint(const P: TFloatPoint); virtual; abstract;
procedure RemovePoint(Index: Integer); virtual; abstract;
property StartPoint: PFloatPoint read GetStartPoint;
property EndPoint: PFloatPoint read GetEndPoint;
property PointPtr[Index: Integer]: PFloatPoint read GetPointPtr;
property Point[Index: Integer]: TFloatPoint read GetPoint write SetPoint;
property NumPoints: Integer read GetNumPoints; }
end;
{ TCustomCurve }
TCustomCurve = class(TCustomShape)
public
procedure Delete; virtual;
end;
{ TBezierCurve }
PBezierVertex = ^TBezierVertex;
TBezierVertex = record
case Integer of
0: (Point: TFloatPoint; ControlPoints: array [0..1] of TFloatPoint);
1: (Points: array [0..2] of TFloatPoint);
end;
TBezierSegment = record
P1, P2, P3, P4: TFloatPoint;
end;
TArrayOfBezierVertex = array of TBezierVertex;
TArrayOfArrayOfBezierVertex = array of TArrayOfBezierVertex;
TBezierCurve = class(TCustomCurve)
private
FVertices: TArrayOfArrayOfBezierVertex;
FClosed: Boolean;
{ protected
function GetPointPtr(Index: Integer): PFloatPoint; override;
function GetNumPoints: Integer; override;
procedure AssignTo(Dest: TPersistent); override; }
public
constructor Create;
procedure Delete; override;
procedure NewContour;
{ procedure AddPoint(const P: TFloatPoint); override;
procedure RemovePoint(Index: Integer); override; }
procedure AddVertex(const V: TBezierVertex);
procedure AppendToPolygon(Polygon: TPolygon32); override;
//function ClosestPointOnCurve(const P: TFloatPoint): TFloatPoint; override;
property Vertices: TArrayOfArrayOfBezierVertex read FVertices write FVertices;
property Closed: Boolean read FClosed write FClosed;
end;
{ TSplineCurve }
TSplineCurve = class(TCustomShape)
private
FPoints: TArrayOfArrayOfFloatPoint;
FKernel: TCustomKernel;
procedure SetKernel(const Value: TCustomKernel);
public
constructor Create;
procedure NewContour;
procedure AddPoint(const Point: TFloatPoint);
procedure AppendToPolygon(Polygon: TPolygon32); override;
property Points: TArrayOfArrayOfFloatPoint read FPoints write FPoints;
property Kernel: TCustomKernel read FKernel write SetKernel;
end;
{ TEllipse }
TEllipse = class(TCustomShape)
private
FBoundsRect: TFloatRect;
public
procedure AppendToPolygon(Polygon: TPolygon32); override;
property BoundsRect: TFloatRect read FBoundsRect write FBoundsRect;
end;
{ TRegularPolygon = class(TCustomShape)
public
public
property Corners: Integer;
property BoundsRect: TFloatRect;
end; }
(*
TPolyLine = class(TCustomShape)
private
FPoints: TArrayOfFloatPoint;
protected
constructor Create;
function GetPointPtr(Index: Integer): PFloatPoint; override;
function GetNumPoints: Integer; override;
public
procedure AddPoint(const P: TFloatPoint); override;
procedure RemovePoint(Index: Integer); override;
procedure AppendToPolygon(Polygon: TPolygon32); override;
end;
*)
// TParametricCurve = class(TCustomShape)
// end;
//============================================================================//
(*
TCustomSegment = class(TPersistent)
protected
procedure BreakSegment(Path: TCustomPath; const P1, P2: TFloatPoint); virtual; abstract;
function FindClosestPoint(const P1, P2, P: TFloatPoint): TFloatPoint; virtual; abstract;
end;
TLineSegment = class(TCustomSegment)
protected
procedure BreakSegment(Path: TCustomPath; const P1, P2: TFloatPoint); override;
function FindClosestPoint(const P1, P2, P: TFloatPoint): TFloatPoint; override;
end;
PControlPoints = ^TControlPoints;
TControlPoints = array [0..1] of TFloatPoint;
TBezierSegment = class(TCustomSegment)
private
FControlPoints: TControlPoints;
function GetControlPoints: PControlPoints;
protected
procedure BreakSegment(Path: TCustomPath; const P1, P2: TFloatPoint); override;
function FindClosestPoint(const P1, P2, P: TFloatPoint): TFloatPoint; override;
public
constructor Create; overload;
constructor Create(const C1, C2: TFloatPoint); overload;
function GetCoefficient(const P1, P2, P: TFloatPoint): TFloat;
procedure CurveThroughPoint(Path: TCustomPath; Index: Integer; P: TFloatPoint;
Coeff: TFloat);
property ControlPoints: PControlPoints read GetControlPoints;
end;
TSegmentList = class(TList)
private
function GetCustomSegment(Index: Integer): TCustomSegment;
procedure SetCustomSegment(Index: Integer;
const Value: TCustomSegment);
public
property Items[Index: Integer]: TCustomSegment read GetCustomSegment write SetCustomSegment; default;
end;
TCustomPath = class(TPersistent)
private
FVertices: TArrayOfFloatPoint;
FOutput: TArrayOfFloatPoint;
FSegmentList: TSegmentList;
FVertCount: Integer;
FOutCount: Integer;
function GetOutputCapacity: Integer;
function GetVertexCapacity: Integer;
procedure SetOutputCapacity(const Value: Integer);
procedure SetVertexCapacity(const Value: Integer);
function GetEndPoint: TFloatPoint;
function GetStartPoint: TFloatPoint;
procedure SetEndPoint(const Value: TFloatPoint);
procedure SetStartPoint(const Value: TFloatPoint);
function GetSegment(Index: Integer): TCustomSegment;
function GetVertex(Index: Integer): TFloatPoint;
function GetLastSegment: TCustomSegment;
function GetSegmentCount: Integer;
protected
procedure AppendPoint(const Point: TFloatPoint);
public
constructor Create;
destructor Destroy; override;
function AsPolygon: TPolygon32;
function FindClosestVertex(const P: TFloatPoint): PFloatPoint;
function FindClosestSegment(const P: TFloatPoint;
out Segment: TCustomSegment; out OutPoint: TFloatPoint): Integer;
function GetCoefficient(const P: TFloatPoint; Index: Integer): TFloat;
procedure CurveThroughPoint(const P: TFloatPoint; Index: Integer; Coeff: TFloat);
procedure AddStartPoint(const P: TFloatPoint);
procedure AddSegment(const P: TFloatPoint; Segment: TCustomSegment);
procedure InsertSegment(const P: TFloatPoint; Segment: TCustomSegment; Index: Integer);
procedure DeleteSegment(Index: Integer);
procedure RemoveSegment(Segment: TCustomSegment);
function IndexOf(Segment: TCustomSegment): Integer;
procedure Offset(const Dx, Dy: TFloat);
property VertexCapacity: Integer read GetVertexCapacity write SetVertexCapacity;
property OutputCapacity: Integer read GetOutputCapacity write SetOutputCapacity;
property StartPoint: TFloatPoint read GetStartPoint write SetStartPoint;
property EndPoint: TFloatPoint read GetEndPoint write SetEndPoint;
property Segments[Index: Integer]: TCustomSegment read GetSegment;
property Vertices[Index: Integer]: TFloatPoint read GetVertex;
property VertexCount: Integer read FVertCount;
property SegmentCount: Integer read GetSegmentCount;
property LastSegment: TCustomSegment read GetLastSegment;
end;
*)
const
Epsilon = 1;
var
BezierTolerance: Single = 0.25;
function SqrDistance(const A, B: TFloatPoint): TFloat;
function DotProduct(const A, B: TFloatPoint): TFloat;
function AddPoints(const A, B: TFloatPoint): TFloatPoint;
function SubPoints(const A, B: TFloatPoint): TFloatPoint;
{ Bezier curve routines }
function BezierCurveToPolygonX(const Vertices: TArrayOfBezierVertex;
Closed: Boolean = True): TArrayOfFixedPoint; overload;
function BezierCurveToPolygonF(const Vertices: TArrayOfBezierVertex;
Closed: Boolean = True): TArrayOfFloatPoint; overload;
procedure OffsetVertex(var Vertex: TBezierVertex; const Dx, Dy: TFloat);
function ZeroVertex(const Point: TFloatPoint): TBezierVertex;
function BezierSegment(const Vertices: TArrayOfBezierVertex; Index: Integer): TBezierSegment;
{ Text routines }
procedure DrawText(Dst: TBitmap32; X, Y: TFloat; const Text: WideString;
OutlineColor, FillColor: TColor32; Transformation: TTransformation = nil); overload;
procedure DrawText(Dst: TBitmap32; X, Y: TFloat; const Text: WideString;
OutlineColor: TColor32; Filler: TCustomPolygonFiller;
Transformation: TTransformation = nil); overload;
procedure DrawTextOutline(Dst: TBitmap32; X, Y: TFloat; const Text: WideString;
Color: TColor32; Transformation: TTransformation = nil);
procedure FillText(Dst: TBitmap32; X, Y: TFloat; const Text: WideString;
Filler: TCustomPolygonFiller; Transformation: TTransformation = nil);
function TextToPolygon(Dst: TBitmap32; DstX, DstY: TFloat; const Text: WideString): TPolygon32;
function TextToPolygonF(Dst: TBitmap32; DstX, DstY: TFloat; const Text: WideString): TArrayOfArrayOfFloatPoint;
// function EllipseToPolygon
function MakeCurve(const Points: TArrayOfFloatPoint; Kernel: TCustomKernel;
Closed: Boolean; StepSize: Integer): TArrayOfFixedPoint; overload;
var
GGODefaultFlags: Integer = GGO_NATIVE or GGO_UNHINTED;
implementation
uses
SysUtils, Types, GR32_LowLevel, GR32_Backends;
function AddPoints(const A, B: TFloatPoint): TFloatPoint;
begin
Result.X := A.X + B.X;
Result.Y := A.Y + B.Y;
end;
function SubPoints(const A, B: TFloatPoint): TFloatPoint;
begin
Result.X := A.X - B.X;
Result.Y := A.Y - B.Y;
end;
// Returns square of the distance between points A and B.
function SqrDistance(const A, B: TFloatPoint): TFloat;
begin
Result := Sqr(A.X - B.X) + Sqr(A.Y - B.Y);
end;
function DotProduct(const A, B: TFloatPoint): TFloat;
begin
Result := A.X * B.X + A.Y * B.Y;
end;
// Returns a point on the line from A to B perpendicular to C.
function PointOnLine(const A, B, C: TFloatPoint): TFloatPoint;
var
dx, dy, r: Single;
begin
dx := B.X - A.X;
dy := B.Y - A.Y;
r := ((C.X - A.X) * dx + (C.Y - A.Y) * dy) / (Sqr(dx) + Sqr(dy));
if InRange(r, 0, 1) then
begin
Result.X := A.X + r * dx;
Result.Y := A.Y + r * dy;
end
else
begin
if SqrDistance(A, C) < SqrDistance(B, C) then
Result := A
else
Result := B;
end;
end;
function Flatness(P1, P2, P3, P4: TFloatPoint): Single;
begin
Result :=
Abs(P1.X + P3.X - 2*P2.X) +
Abs(P1.Y + P3.Y - 2*P2.Y) +
Abs(P2.X + P4.X - 2*P3.X) +
Abs(P2.Y + P4.Y - 2*P3.Y);
end;
procedure OffsetVertex(var Vertex: TBezierVertex; const Dx, Dy: TFloat);
begin
Vertex.Points[0].X := Vertex.Points[0].X + Dx;
Vertex.Points[0].Y := Vertex.Points[0].Y + Dy;
Vertex.Points[1].X := Vertex.Points[1].X + Dx;
Vertex.Points[1].Y := Vertex.Points[1].Y + Dy;
Vertex.Points[2].X := Vertex.Points[2].X + Dx;
Vertex.Points[2].Y := Vertex.Points[2].Y + Dy;
end;
function ZeroVertex(const Point: TFloatPoint): TBezierVertex;
begin
Result.Points[0] := Point;
Result.Points[1] := Point;
Result.Points[2] := Point;
end;
function BezierSegment(const Vertices: TArrayOfBezierVertex; Index: Integer): TBezierSegment;
begin
if Index = High(Vertices) then
begin
Result.P1 := Vertices[Index].Points[0];
Result.P2 := Vertices[Index].Points[2];
Result.P3 := Vertices[0].Points[1];
Result.P4 := Vertices[0].Points[0];
end
else
begin
Result.P1 := Vertices[Index].Points[0];
Result.P2 := Vertices[Index].Points[2];
Result.P3 := Vertices[Index + 1].Points[1];
Result.P4 := Vertices[Index + 1].Points[0];
end;
end;
(*
{ TLineSegment }
procedure TLineSegment.BreakSegment(Path: TCustomPath; const P1, P2: TFloatPoint);
begin
Path.AppendPoint(P2);
end;
function TLineSegment.FindClosestPoint(const P1, P2, P: TFloatPoint): TFloatPoint;
begin
Result := PointOnLine(P1, P2, P);
end;
{ TBezierSegment }
procedure TBezierSegment.BreakSegment(Path: TCustomPath; const P1, P2: TFloatPoint);
var
C1, C2: TFloatPoint;
procedure Recurse(const P1, P2, P3, P4: TFloatPoint);
var
P12, P23, P34, P123, P234, P1234: TFloatPoint;
begin
if Flatness(P1, P2, P3, P4) < BezierTolerance then
begin
Path.AppendPoint(P4);
end
else
begin
P12.X := (P1.X + P2.X) * 0.5;
P12.Y := (P1.Y + P2.Y) * 0.5;
P23.X := (P2.X + P3.X) * 0.5;
P23.Y := (P2.Y + P3.Y) * 0.5;
P34.X := (P3.X + P4.X) * 0.5;
P34.Y := (P3.Y + P4.Y) * 0.5;
P123.X := (P12.X + P23.X) * 0.5;
P123.Y := (P12.Y + P23.Y) * 0.5;
P234.X := (P23.X + P34.X) * 0.5;
P234.Y := (P23.Y + P34.Y) * 0.5;
P1234.X := (P123.X + P234.X) * 0.5;
P1234.Y := (P123.Y + P234.Y) * 0.5;
Recurse(P1, P12, P123, P1234);
Recurse(P1234, P234, P34, P4);
end;
end;
begin
C1 := AddPoints(FControlPoints[0], P1);
C2 := AddPointS(FControlPoints[1], P2);
Recurse(P1, C1, C2, P2);
end;
constructor TBezierSegment.Create(const C1, C2: TFloatPoint);
begin
FControlPoints[0] := C1;
FControlPoints[1] := C2;
end;
constructor TBezierSegment.Create;
begin
//
end;
function TBezierSegment.FindClosestPoint(const P1, P2, P: TFloatPoint): TFloatPoint;
var
C1, C2: TFloatPoint;
function FindPoint(const P1, P2, P3, P4: TFloatPoint; D1, D2: TFloat): TFloatPoint;
var
P12, P23, P34, P123, P234, P1234: TFloatPoint;
NewD: TFloat;
begin
P12.X := (P1.X + P2.X) * 0.5;
P12.Y := (P1.Y + P2.Y) * 0.5;
P23.X := (P2.X + P3.X) * 0.5;
P23.Y := (P2.Y + P3.Y) * 0.5;
P34.X := (P3.X + P4.X) * 0.5;
P34.Y := (P3.Y + P4.Y) * 0.5;
P123.X := (P12.X + P23.X) * 0.5;
P123.Y := (P12.Y + P23.Y) * 0.5;
P234.X := (P23.X + P34.X) * 0.5;
P234.Y := (P23.Y + P34.Y) * 0.5;
P1234.X := (P123.X + P234.X) * 0.5;
P1234.Y := (P123.Y + P234.Y) * 0.5;
NewD := SqrDistance(P, P1234);
if (NewD < D1) and (NewD < D2) then
begin
P12 := FindPoint(P1, P12, P123, P1234, D1, NewD);
P23 := FindPoint(P1234, P234, P34, P4, NewD, D2);
if SqrDistance(P, P12) < SqrDistance(P, P23) then
Result := P12
else
Result := P23;
end
else
case CompareValue(D1, D2, Epsilon) of
-1: Result := FindPoint(P1, P12, P123, P1234, D1, NewD);
1: Result := FindPoint(P1234, P234, P34, P4, NewD, D2);
0: Result := P1234;
end;
end;
begin
C1 := AddPoints(FControlPoints[0], P1);
C2 := AddPointS(FControlPoints[1], P2);
Result := FindPoint(P1, C1, C2, P2, Infinity, Infinity);
end;
procedure TBezierSegment.CurveThroughPoint(Path: TCustomPath; Index: Integer;
P: TFloatPoint; Coeff: TFloat);
var
P1, P2, C1, C2, D1, D2: TFloatPoint;
t, t3, s, s3, w, a, ax, ay, a1, a2: TFloat;
begin
P1 := Path.Vertices[Index];
P2 := Path.Vertices[Index + 1];
C1 := FControlPoints[0];
C2 := FControlPoints[1];
t := Coeff;
t3 := t * t * t;
s := 1 - t;
s3 := s * s * s;
w := (P.X - s3 * P1.X - t3 * P2.X) / (3 * s * t);
ax := ((w - P1.X - t * (P2.X - P1.X)) / (C1.X + t * (C2.X - C1.X)));
w := (P.Y - s3 * P1.Y - t3 * P2.Y) / (3 * s * t);
ay := ((w - P1.Y - t * (P2.Y - P1.Y)) / (C1.Y + t * (C2.Y - C1.Y)));
C1.X := ax * C1.X;
C1.Y := ay * C1.Y;
C2.X := ax * C2.X;
C2.Y := ay * C2.Y;
FControlPoints[0] := C1;
FControlPoints[1] := C2;
if (Index > 0) and (Path.Segments[Index - 1] is TBezierSegment) then
begin
D1 := TBezierSegment(Path.Segments[Index - 1]).ControlPoints[1];
a1 := Sqrt(Sqr(D1.X) + Sqr(D1.Y));
D1.X := ax * D1.X;
D1.Y := ay * D1.Y;
a := Sqrt(Sqr(D1.X) + Sqr(D1.Y));
D1.X := a1 / a * D1.X;
D1.Y := a1 / a * D1.Y;
(Path.Segments[Index - 1] as TBezierSegment).ControlPoints[1] := D1;
end;
if (Index < Path.SegmentCount - 1) and (Path.Segments[Index + 1] is TBezierSegment) then
begin
D2 := TBezierSegment(Path.Segments[Index + 1]).ControlPoints[0];
a2 := Sqrt(Sqr(D2.X) + Sqr(D2.Y));
D2.X := ax * D2.X;
D2.Y := ay * D2.Y;
a := Sqrt(Sqr(D2.X) + Sqr(D2.Y));
D2.X := a2 / a * D2.X;
D2.Y := a2 / a * D2.Y;
(Path.Segments[Index + 1] as TBezierSegment).ControlPoints[0] := D2;
end;
end;
function TBezierSegment.GetControlPoints: PControlPoints;
begin
Result := @FControlPoints;
end;
function TBezierSegment.GetCoefficient(const P1, P2, P: TFloatPoint): TFloat;
var
C1, C2, OutPoint: TFloatPoint;
function FindPoint(const P1, P2, P3, P4: TFloatPoint; D1, D2, Delta, Pos: TFloat; out POut: TFloatPoint): TFloat;
var
P12, P23, P34, P123, P234, P1234: TFloatPoint;
R1, R2, NewD: TFloat;
begin
P12.X := (P1.X + P2.X) * 0.5;
P12.Y := (P1.Y + P2.Y) * 0.5;
P23.X := (P2.X + P3.X) * 0.5;
P23.Y := (P2.Y + P3.Y) * 0.5;
P34.X := (P3.X + P4.X) * 0.5;
P34.Y := (P3.Y + P4.Y) * 0.5;
P123.X := (P12.X + P23.X) * 0.5;
P123.Y := (P12.Y + P23.Y) * 0.5;
P234.X := (P23.X + P34.X) * 0.5;
P234.Y := (P23.Y + P34.Y) * 0.5;
P1234.X := (P123.X + P234.X) * 0.5;
P1234.Y := (P123.Y + P234.Y) * 0.5;
Delta := Delta * 0.5;
NewD := SqrDistance(P, P1234);
if (NewD < D1) and (NewD < D2) then
begin
R1 := FindPoint(P1, P12, P123, P1234, D1, NewD, Delta, Pos - Delta, P12);
R2 := FindPoint(P1234, P234, P34, P4, NewD, D2, Delta, Pos + Delta, P23);
if SqrDistance(P, P12) < SqrDistance(P, P23) then
begin
POut := P12;
Result := R1;
end
else
begin
POut := P23;
Result := R2;
end;
end
else
case CompareValue(D1, D2, Epsilon) of
-1: Result := FindPoint(P1, P12, P123, P1234, D1, NewD, Delta, Pos - Delta, POut);
1: Result := FindPoint(P1234, P234, P34, P4, NewD, D2, Delta, Pos + Delta, POut);
0:
begin
POut := P1234;
Result := Pos;
end;
end;
end;
begin
C1 := AddPoints(P1, FControlPoints[0]);
C2 := AddPoints(P2, FControlPoints[1]);
Result := FindPoint(P1, C1, C2, P2, Infinity, Infinity, 0.5, 0.5, OutPoint);
end;
{ TCustomPath }
procedure TCustomPath.AddSegment(const P: TFloatPoint; Segment: TCustomSegment);
begin
FSegmentList.Add(Segment);
if High(FVertices) < FVertCount then
SetLength(FVertices, Length(FVertices) * 2);
FVertices[FVertCount] := P;
Inc(FVertCount);
end;
procedure TCustomPath.AddStartPoint(const P: TFloatPoint);
begin
FVertices[0] := P;
Inc(FVertCount);
end;
procedure TCustomPath.AppendPoint(const Point: TFloatPoint);
begin
if High(FOutput) < FOutCount then
SetLength(FOutput, Length(FOutput) * 2);
FOutput[FOutCount] := Point;
Inc(FOutCount);
end;
function TCustomPath.AsPolygon: TPolygon32;
var
I: Integer;
FixedPoints: TArrayOfFixedPoint;
begin
FOutput := nil;
SetLength(FOutput, 4);
FOutCount := 0;
AppendPoint(StartPoint);
for I := 0 to FSegmentList.Count - 1 do
FSegmentList[I].BreakSegment(Self, FVertices[I], FVertices[I + 1]);
Result := TPolygon32.Create;
SetLength(FixedPoints, FOutCount);
for I := 0 to FOutCount - 1 do
FixedPoints[I] := FixedPoint(FOutput[I]);
Result.Points[0] := FixedPoints;
end;
constructor TCustomPath.Create;
begin
FVertCount := 0;
FOutCount := 0;
SetLength(FVertices, 4);
SetLengtH(FOutput, 4);
FSegmentList := TSegmentList.Create;
end;
procedure TCustomPath.CurveThroughPoint(const P: TFloatPoint; Index: Integer; Coeff: TFloat);
var
S: TCustomSegment;
begin
S := FSegmentList[Index];
if S is TBezierSegment then
TBezierSegment(S).CurveThroughPoint(Self, Index, P, Coeff);
end;
procedure TCustomPath.DeleteSegment(Index: Integer);
var
S: TCustomSegment;
begin
S := FSegmentList[Index];
if Assigned(S) then
begin
FreeAndNil(S);
MoveLongWord(FVertices[Index+1], FVertices[Index], FVertCount - Index);
Dec(FVertCount);
end;
end;
destructor TCustomPath.Destroy;
var
I: Integer;
begin
FVertices := nil;
FOutput := nil;
for I := 0 to FSegmentList.Count - 1 do
FSegmentList[I].Free;
FSegmentList.Clear;
FSegmentList.Free;
inherited;
end;
function TCustomPath.FindClosestSegment(const P: TFloatPoint;
out Segment: TCustomSegment; out OutPoint: TFloatPoint): Integer;
var
S: TCustomSegment;
Q: TFloatPoint;
I: Integer;
d, d_min: TFloat;
begin
d_min := MaxSingle;
Segment := nil;
for I := 0 to FSegmentList.Count - 1 do
begin
S := FSegmentList[I];
Q := S.FindClosestPoint(FVertices[I], FVertices[I + 1], P);
d := SqrDistance(P, Q);
if d < d_min then
begin
d_min := d;
Segment := S;
OutPoint := Q;
Result := I;
end;
end;
end;
function TCustomPath.FindClosestVertex(const P: TFloatPoint): PFloatPoint;
var
I: Integer;
D, MinD: TFloat;
begin
MinD := Infinity;
for I := 0 to FVertCount - 1 do
begin
D := SqrDistance(P, FVertices[I]);
if D < MinD then
begin
MinD := D;
Result := @FVertices[I];
end;
end;
end;
function TCustomPath.GetCoefficient(const P: TFloatPoint;
Index: Integer): TFloat;
var
S: TCustomSegment;
begin
Result := 0;
S := FSegmentList[Index];
if S is TBezierSegment then
Result := TBezierSegment(S).GetCoefficient(FVertices[Index], FVertices[Index + 1], P);
end;
function TCustomPath.GetEndPoint: TFloatPoint;
begin
Result := FVertices[FVertCount - 1];
end;
function TCustomPath.GetLastSegment: TCustomSegment;
begin
Result := FSegmentList.Last;
end;
function TCustomPath.GetOutputCapacity: Integer;
begin
Result := Length(FOutput);
end;
function TCustomPath.GetSegment(Index: Integer): TCustomSegment;
begin
Result := FSegmentList[Index];
end;
function TCustomPath.GetSegmentCount: Integer;
begin
Result := FSegmentList.Count;
end;
function TCustomPath.GetStartPoint: TFloatPoint;
begin
Result := FVertices[0];
end;
function TCustomPath.GetVertex(Index: Integer): TFloatPoint;
begin
Result := FVertices[Index];
end;
function TCustomPath.GetVertexCapacity: Integer;
begin
Result := Length(FVertices);
end;
function TCustomPath.IndexOf(Segment: TCustomSegment): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to FSegmentList.Count - 1 do
if FSegmentList[I] = Segment then
begin
Result := I;
Exit;
end;
end;
procedure TCustomPath.InsertSegment(const P: TFloatPoint;
Segment: TCustomSegment; Index: Integer);
begin
FSegmentList.Insert(Index, Segment);
if High(FVertices) < FVertCount then
SetLength(FVertices, Length(FVertices) * 2);
Move(FVertices[Index], FVertices[Index + 1], (FVertCount - Index) * SizeOf(TFloatPoint));
FVertices[Index] := P;
Inc(FVertCount);
end;
procedure TCustomPath.Offset(const Dx, Dy: TFloat);
var
I: Integer;
begin
for I := 0 to FVertCount - 1 do
with FVertices[I] do
begin
X := X + Dx;
Y := Y + Dy;
end;
end;
procedure TCustomPath.RemoveSegment(Segment: TCustomSegment);
begin
DeleteSegment(IndexOf(Segment));
end;
procedure TCustomPath.SetEndPoint(const Value: TFloatPoint);
begin
FVertices[FVertCount - 1] := Value;
end;
procedure TCustomPath.SetOutputCapacity(const Value: Integer);
begin
SetLength(FOutput, Value);
end;
procedure TCustomPath.SetStartPoint(const Value: TFloatPoint);
begin
FVertices[0] := Value;
end;
procedure TCustomPath.SetVertexCapacity(const Value: Integer);
begin
SetLength(FVertices, Value);
end;
{ TSegmentList }
function TSegmentList.GetCustomSegment(Index: Integer): TCustomSegment;
begin
Result := List[Index];
end;
procedure TSegmentList.SetCustomSegment(Index: Integer;
const Value: TCustomSegment);
begin
List[Index] := Value;
end;
*)
{ TBezierCurve }
{ procedure TBezierCurve.AddPoint(const P: TFloatPoint);
var
V: TBezierVertex;
begin
V.Point := P;
V.ControlPoints[0] := P;
V.ControlPoints[1] := P;
AddVertex(V);
end; }
procedure TBezierCurve.AddVertex(const V: TBezierVertex);
var
H, L: Integer;
begin
H := High(FVertices);
L := Length(FVertices[H]);
SetLength(FVertices[H], L + 1);
FVertices[H][L] := V;
end;
function BezierCurveToPolygonX(const Vertices: TArrayOfBezierVertex;
Closed: Boolean = True): TArrayOfFixedPoint;
var
I, Index: Integer;
procedure Recurse(const P1, P2, P3, P4: TFloatPoint);
var
P12, P23, P34, P123, P234, P1234: TFloatPoint;
begin
if Flatness(P1, P2, P3, P4) < BezierTolerance then
begin
if High(Result) < Index then
SetLength(Result, Length(Result) * 2);
Result[Index] := FixedPoint(P4);
Inc(Index);
end
else
begin
P12.X := (P1.X + P2.X) * 0.5;
P12.Y := (P1.Y + P2.Y) * 0.5;
P23.X := (P2.X + P3.X) * 0.5;
P23.Y := (P2.Y + P3.Y) * 0.5;
P34.X := (P3.X + P4.X) * 0.5;
P34.Y := (P3.Y + P4.Y) * 0.5;
P123.X := (P12.X + P23.X) * 0.5;
P123.Y := (P12.Y + P23.Y) * 0.5;
P234.X := (P23.X + P34.X) * 0.5;
P234.Y := (P23.Y + P34.Y) * 0.5;
P1234.X := (P123.X + P234.X) * 0.5;
P1234.Y := (P123.Y + P234.Y) * 0.5;
Recurse(P1, P12, P123, P1234);
Recurse(P1234, P234, P34, P4);
end;
end;
begin
Index := 0;
SetLength(Result, 8);
Result[Index] := FixedPoint(Vertices[0].Point);
Inc(Index);
for I := 0 to High(Vertices) + Ord(Closed) - 1 do
with BezierSegment(Vertices, I) do
Recurse(P1, P2, P3, P4);
SetLength(Result, Index);
end;
function BezierCurveToPolygonF(const Vertices: TArrayOfBezierVertex;
Closed: Boolean = True): TArrayOfFloatPoint;
var
I, Index: Integer;
procedure Recurse(const P1, P2, P3, P4: TFloatPoint);
var
P12, P23, P34, P123, P234, P1234: TFloatPoint;
begin
if Flatness(P1, P2, P3, P4) < BezierTolerance then
begin
if High(Result) < Index then
SetLength(Result, Length(Result) * 2);
Result[Index] := P4;
Inc(Index);
end
else
begin
P12.X := (P1.X + P2.X) * 0.5;
P12.Y := (P1.Y + P2.Y) * 0.5;
P23.X := (P2.X + P3.X) * 0.5;
P23.Y := (P2.Y + P3.Y) * 0.5;
P34.X := (P3.X + P4.X) * 0.5;
P34.Y := (P3.Y + P4.Y) * 0.5;
P123.X := (P12.X + P23.X) * 0.5;
P123.Y := (P12.Y + P23.Y) * 0.5;
P234.X := (P23.X + P34.X) * 0.5;
P234.Y := (P23.Y + P34.Y) * 0.5;
P1234.X := (P123.X + P234.X) * 0.5;
P1234.Y := (P123.Y + P234.Y) * 0.5;
Recurse(P1, P12, P123, P1234);
Recurse(P1234, P234, P34, P4);
end;
end;
begin
Index := 0;
SetLength(Result, 8);
for I := 0 to High(Vertices) + Ord(Closed) - 1 do
with BezierSegment(Vertices, I) do
Recurse(P1, P2, P3, P4);