-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvancedShapes.pas
More file actions
1554 lines (1309 loc) · 36.7 KB
/
Copy pathAdvancedShapes.pas
File metadata and controls
1554 lines (1309 loc) · 36.7 KB
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 AdvancedShapes;
{ *******************************************************
Digital Omerta 3D Engine
Unit Author: Pawel Piotrowski
Copyright Pawel Piotrowski
Version: 3.9
History
2018-10-06: shapes can be cloned now
2013-10-11L The Ellipse now uses a bit matrix to store its area, previously it was stored in a monochrome bitmap. The Bitmatrix ist around 1/2 of the memory size and around 30 times faster.
2013-10-09:
- Polygons can now be open, see the Closed property.
- The Point in non convex polygon function is improved and faster
2013-07-22:
* Added Properties: name, Userobject, UserInterface
* fixed a bug in calculating the center of the shape
2013-06-18: added a function OverlapWithRect, to implement a fast rect overlaping check
2013-06-05: aded TadvShape.tag: integer property }
{$I JEDI.INC}
{ Version: 1
History:
2013-05-22: moved all geometry related code from the ExperimentData.pas here }
interface
uses
Windows, Classes, SysUtils,
XMLLib,
Graphics, D3DX9, Generics.Collections, max3DMath;
type
TControlPoint = (cpNone, cpMove, cpTop, cpLeft, cpBottom, cpRight, cpTopLeft, cpTopRight, cpBottomleft, cpBottomRight, cpPolyPoint, cpPolyHalfPoint);
TPointArray = TArray<Tpoint>;
TShapeKind = (skRect = 0, skEllipse = 1, skPolygon = 2);
// forward declrations
TBitMatrix = class;
TEllipse = class;
TAdvPolygon = class;
TAdvShape = class
private
FTag: integer;
Fname: string;
FUserInterface: iInterface;
FOwnsUserObject: Boolean;
FUserObject: TObject;
function GetCenter: TD3DXVector2;
procedure SetTyp(const Value: TShapeKind);
procedure SetTag(const Value: integer);
procedure Setname(const Value: string);
procedure SetOwnsUserObject(const Value: Boolean);
procedure SetUserInterface(const Value: iInterface);
procedure SetUserObject(const Value: TObject);
function GetControlPoint(cp: TControlPoint): Tpoint;
function GetCenter2d: Tpoint;
protected
fTyp: TShapeKind;
fMinMax: Trect;
procedure SetRect(const Value: Trect); virtual;
procedure LoadFromNode(node: TXMLNode); virtual;
public
constructor Create;
destructor Destroy; override;
class Function CreateRectShape(const r: Trect): TAdvShape;
class Function CreateEllipseShape(const r: Trect): TEllipse;
class Function CreatePolygonShape(const aPoints: TPointArray; PolyIsClosed: Boolean = true): TAdvPolygon;
// note: will clone the userObject oly if it is not owned
function Clone: TAdvShape; virtual;
procedure SaveToXMLNode(node: TXMLNode); virtual;
class function CreateFromXMLNode(node: TXMLNode): TAdvShape;
Function PointInside(const p: Tpoint): Boolean; virtual;
// this functions returns the smallest distance, or 0 if the point or poly is inside the bounds
Function Distance(const p: Tpoint): single; overload;
Function Distance(Bounds: TAdvShape): single; overload;
function DistanceToPoint(const p: Tpoint): single; virtual;
Function DistanceToShape(Shape: TAdvShape): single;
Function GetNearestPoint2d(const p: Tpoint): TD3DXVector2; virtual;
Function GetNearestPoint(const p: Tpoint): Tpoint;
Function OverlapWithRect(const r: Trect): Boolean;
function CalcDistanceToRect(const r: Trect): single; virtual;
function CalcDistanceToEllipse(const aEllipse: TEllipse): single; virtual;
function CalcDistanceToPolygon(Poly: TAdvPolygon): single; virtual;
property Typ: TShapeKind Read fTyp write SetTyp;
// THIS ONE WILL RETURN A BOUNDING RECT. depending on the type of the Zone, it will either will be the rect itself or the MinMax Rect of the circle or poly
property Rect: Trect Read fMinMax Write SetRect;
property Center: TD3DXVector2 read GetCenter;
property Center2d: Tpoint read GetCenter2d;
Property ControlPoint[cp: TControlPoint]: Tpoint read GetControlPoint;
// for storing of custom user data
property Tag: integer read FTag write SetTag;
property name: string read Fname write Setname;
property UserObject: TObject read FUserObject write SetUserObject;
property OwnsUserObject: Boolean read FOwnsUserObject write SetOwnsUserObject;
property UserInterface: iInterface read FUserInterface write SetUserInterface;
end;
TAdvPolygon = class(TAdvShape)
private
fPoints: TPointArray;
fClosed: Boolean;
procedure SetClosed(const Value: Boolean);
function GetClosed: Boolean;
protected
procedure SetRect(const Value: Trect); override;
procedure LoadFromNode(node: TXMLNode); override;
public
Constructor Create;
Destructor Destroy; override;
Procedure AfterChanged;
procedure SaveToXMLNode(node: TXMLNode); override;
procedure InsertPoint(index: integer; const p: Tpoint);
procedure DeletePoint(index: integer);
Function PointInside(const p: Tpoint): Boolean; override;
Function DistanceToPoint(const p: Tpoint): single; override;
function CalcDistanceToRect(const r: Trect): single; override;
function CalcDistanceToEllipse(const aEllipse: TEllipse): single; override;
function CalcDistanceToPolygon(Poly: TAdvPolygon): single; override;
Function GetNearestPoint2d(const p: Tpoint): TD3DXVector2; override;
property Points: TPointArray read fPoints;
property Closed: Boolean read GetClosed write SetClosed;
end;
TEllipse = class(TAdvShape)
private
fBitMatrix: TBitMatrix;
fAsPolygon: TAdvPolygon;
procedure CalculateValuesFromRect(const r: Trect);
procedure PrepareMap;
procedure PreparePolygon;
protected
procedure SetRect(const Value: Trect); override;
public
// semi major axis
a: single;
// major axis
_2a: integer;
// semi minor axis
b: single;
// semi axis
_2b: integer;
Center: TD3DXVector2;
// distance from center to the foci points
c: single;
// eccentricity
e: single;
constructor Create;
destructor Destroy; override;
function Width: integer;
function height: integer;
function IsUnitCircle: Boolean;
function Foci1: TD3DXVector2;
function foci2: TD3DXVector2;
Function Vertex1: TD3DXVector2;
Function Vertex2: TD3DXVector2;
function coVertex1: TD3DXVector2;
Function CoVertex2: TD3DXVector2;
Function Radius: single;
function RatioWidthToHeight: single;
Function PointInside(const WorldCoordPoint: Tpoint): Boolean; override;
Function PointInside2d(const WorldCoordPoint: TD3DXVector2): Boolean;
Function DistanceToPoint(const p: Tpoint): single; override;
function CalcDistanceToRect(const r: Trect): single; override;
function CalcDistanceToEllipse(const aEllipse: TEllipse): single; override;
function CalcDistanceToPolygon(Poly: TAdvPolygon): single; override;
Function GetNearestPoint2d(const p: Tpoint): TD3DXVector2; override;
property AsPolygon: TAdvPolygon read fAsPolygon;
end;
TBitMatrix = class
private
fBuffer: pByte;
fSize: integer;
fWidth: integer;
fHeight: integer;
function GetValue(x, y: integer): Boolean;
procedure SetValue(x, y: integer; const Value: Boolean);
Function RoundUp(const Value: double): integer;
function GetByte(x, y: integer; out pb: pByte; out iBit: byte): Boolean;
public
Constructor Create;
Destructor Destroy; override;
// black is false, any other color is true
Procedure BuildFromBitMap(bmp: TBitMap; TruColor: tcolor);
procedure Clear(NewWidth, NewHeight: integer);
property Value[x, y: integer]: Boolean read GetValue write SetValue;
end;
TLine = record
StartPoint, EndPoint: Tpoint;
end;
Procedure RectToPoints(const r: Trect; out Points: TPointArray);
Function DistanceToRect(const p: Tpoint; const r: Trect): single;
Function DistanceToSegment(const p: Tpoint; const LineStartPoint, LineEndPoint: Tpoint): single; overload;
Function DistanceToSegment(const vp, LineStartPoint, LineEndPoint: TD3DXVector2): single; overload;
// will return 0 if there is an overlaping or if the one is in the other
Function DistanceSegment2Segment(const Seg1Start, Seg1End, Seg2Start, Seg2End: Tpoint): single;
Function DistanceSegmentToSegments(const s1, s2: Tpoint; const Points: TPointArray): single;
Function DistanceSegmentsToSegments(const Points1, Points2: TPointArray; Poly1IsClosed, Poly2isClosed: Boolean): single;
function distPointToSegments(const p: TD3DXVector2; const Points: TPointArray; PolyIsClosed: Boolean): single;
function DistRect2Rect(const r1, r2: Trect): single;
function ClosestPointOnLine(Const LineStart, LineEnd: Tpoint; const vPoint: TD3DXVector2): TD3DXVector2; overload;
function ClosestPointOnLine(Const LineStart, LineEnd, vPoint: Tpoint): TD3DXVector2; overload;
Function Vector2(const p: Tpoint): TD3DXVector2; overload;
Function Vector2(const x, y: integer): TD3DXVector2; overload;
Function Vector2(const x, y: single): TD3DXVector2; overload;
function Vector2ToPoint(const p: TD3DXVector2): Tpoint;
// It returns 1 for strictly interior points, 0 for strictly exterior, and 0 or 1 for points on the boundary.
function PointInPolygon(var xp, yp: array of single; x, y: single): Boolean;
function PointInRect(const Point: Tpoint; const Rect: Trect): Boolean;
// this will calculate the angle in degrees between the View point and the object. Or in other words the angle between the two lines Body <-> head and body <-> Object
function getViewpoint(ptBody, ptHead: Tpoint; const ptObjectCenter: TD3DXVector2): single;
Function Equal(const p1, p2: Tpoint): Boolean;
implementation
uses
GDIPOBJ, GDIPAPI,
pawel1, bsUTILS, Dialogs, zLib, math, max3DCollision, ClipBrd, XmlLibHelper, MaxLogic.QuickPixel, mmsystem;
function getViewpoint(ptBody, ptHead: Tpoint; const ptObjectCenter: TD3DXVector2): single;
begin
// if one of the lines (head <-> body or body <-> objectCenter) is invalid, then we can't calculate the angle
if (ptHead = ptBody) or
D3DXVector2Equal(D3DXVector2(ptBody.x, ptBody.y), ptObjectCenter) then
exit(0);
result := max3DMath.VertexAngle(
ptHead.x, ptHead.y,
ptBody.x, ptBody.y,
ptObjectCenter.x, ptObjectCenter.y);
end;
function DistRect2Rect(const r1, r2: Trect): single;
var
Points1, Points2: TPointArray;
x: integer;
begin
result := 0;
if RectIntersection(r1, r2) then
exit;
RectToPoints(r1, Points1);
RectToPoints(r2, Points2);
result := DistanceSegmentsToSegments(Points1, Points2, true, true);
Points1 := NIL;
Points2 := NIL;
end;
function distPointToSegments(const p: TD3DXVector2; const Points: TPointArray; PolyIsClosed: Boolean): single;
var
x: integer;
dist: single;
v: TD3DXVector2;
s1, s2: Tpoint;
begin
result := 0;
for x := 0 to Length(Points) - 1 do
begin
s1 := Points[x];
if x + 1 < Length(Points) then
s2 := Points[x + 1]
else
begin
if not PolyIsClosed then
break
else
s2 := Points[0];
end;
v := ClosestPointOnLine(s1, s2, p);
dist := VectorDistance(v, p);
if (x = 0) or (result > dist) then
result := dist;
end;
end;
Function DistanceSegmentsToSegments(const Points1, Points2: TPointArray; Poly1IsClosed, Poly2isClosed: Boolean): single;
var
x, y: integer;
dist: single;
s1, s2, s3, s4: Tpoint;
begin
result := 0;
for x := 0 to Length(Points1) - 1 do
begin
s1 := Points1[x];
if x + 1 < Length(Points1) then
s2 := Points1[x + 1]
else
begin
if not Poly1IsClosed then
break
else
s2 := Points1[0];
end;
for y := 0 to Length(Points2) - 1 do
begin
s3 := Points2[y];
if y + 1 < Length(Points2) then
s4 := Points2[y + 1]
else
begin
if not Poly2isClosed then
break
else
s4 := Points2[0];
end;
dist := DistanceSegment2Segment(s1, s2, s3, s4);
if ((x = 0) and (y = 0)) OR (dist < result) then
result := dist;
end;
end;
end;
Function DistanceSegmentToSegments(const s1, s2: Tpoint; const Points: TPointArray): single;
var
y: integer;
dist: single;
s3, s4: Tpoint;
begin
result := 0;
for y := 0 to Length(Points) - 1 do
begin
s3 := Points[y];
if y + 1 < Length(Points) then
s4 := Points[y + 1]
else
s4 := Points[0];
dist := DistanceSegment2Segment(s1, s2, s3, s4);
if (y = 0) OR (dist < result) then
result := dist;
end;
end;
Function DistanceSegment2Segment(const Seg1Start, Seg1End, Seg2Start, Seg2End: Tpoint): single;
var
s1, s2, s3, s4, v1: TD3DXVector2;
dist: single;
begin
s1 := Vector2(Seg1Start);
s2 := Vector2(Seg1End);
s3 := Vector2(Seg2Start);
s4 := Vector2(Seg2End);
result := 0;
if Intersect(s1, s2, s3, s4) then
exit;
v1 := ClosestPointOnLine(s1, s2, s3);
result := VectorDistance(v1, s3);
v1 := ClosestPointOnLine(s1, s2, s4);
dist := VectorDistance(v1, s4);
if dist < result then
result := dist;
// check the other line with the end point of the first one
v1 := ClosestPointOnLine(s3, s4, s1);
dist := VectorDistance(v1, s1);
if dist < result then
result := dist;
v1 := ClosestPointOnLine(s3, s4, s2);
dist := VectorDistance(v1, s2);
if dist < result then
result := dist;
end;
Procedure RectToPoints(const r: Trect; out Points: TPointArray);
begin
setlength(Points, 4);
Points[0] := r.TopLeft;
Points[1] := Point(r.Right, r.Top);
Points[2] := r.BottomRight;
Points[3] := Point(r.left, r.Bottom);
end;
Function Equal(const p1, p2: Tpoint): Boolean;
begin
result := (p1.x = p2.x) and
(p1.y = p2.y)
end;
Function DistanceToRect(const p: Tpoint; const r: Trect): single;
var
dist: single;
x: integer;
begin
if PointInRect(p, r) then
result := 0
else
begin
result := DistanceToSegment(p,
Point(r.left, r.Top),
Point(r.Right, r.Top));
dist := DistanceToSegment(p,
Point(r.Right, r.Top),
Point(r.Right, r.Bottom));
if dist < result then
result := dist;
dist := DistanceToSegment(p,
Point(r.Right, r.Bottom),
Point(r.left, r.Bottom));
if dist < result then
result := dist;
dist := DistanceToSegment(p,
Point(r.left, r.Bottom),
Point(r.left, r.Top));
if dist < result then
result := dist;
end;
end;
function PointInRect(const Point: Tpoint; const Rect: Trect): Boolean;
begin
result := (Point.x >= Rect.left) and
(Point.x <= Rect.Right) and
(Point.y >= Rect.Top) and
(Point.y <= Rect.Bottom);
end;
Function DistanceToSegment(const p: Tpoint; const LineStartPoint, LineEndPoint: Tpoint): single;
begin
result := DistanceToSegment(Vector2(p), Vector2(LineStartPoint), Vector2(LineEndPoint));
end;
Function DistanceToSegment(const vp, LineStartPoint, LineEndPoint: TD3DXVector2): single;
var
NearestPoint: TD3DXVector2;
begin
NearestPoint := ClosestPointOnLine(LineStartPoint, LineEndPoint, vp);
result := VectorDistance(NearestPoint, vp);
end;
function ClosestPointOnLine(Const LineStart, LineEnd, vPoint: Tpoint): TD3DXVector2;
begin
result := max3DMath.ClosestPointOnLine(Vector2(LineStart), Vector2(LineEnd),
Vector2(vPoint));
end;
function ClosestPointOnLine(Const LineStart, LineEnd: Tpoint; const vPoint: TD3DXVector2): TD3DXVector2;
begin
result := max3DMath.ClosestPointOnLine(Vector2(LineStart), Vector2(LineEnd),
vPoint);
end;
function PointInPolygon(var xp, yp: array of single; x, y: single): Boolean;
// It returns 1 for strictly interior points, 0 for strictly exterior, and 0 or 1 for points on the boundary.
var
I, J: integer;
begin
result := False;
if High(xp) = High(yp) then
begin
J := High(xp);
for I := 0 to High(xp) do
begin
if ((((yp[I] <= y) and (y < yp[J])) or ((yp[J] <= y) and (y < yp[I])))
and (x < (xp[J] - xp[I]) * (y - yp[I]) / (yp[J] - yp[I]) + xp[I])) then
result := not result;
J := I;
end;
end;
end;
Function Vector2(const p: Tpoint): TD3DXVector2; overload;
begin
with p do
begin
result.x := x;
result.y := y;
end;
end;
function Vector2ToPoint(const p: TD3DXVector2): Tpoint;
begin
result.x := round(p.x);
result.y := round(p.y);
end;
Function Vector2(const x, y: single): TD3DXVector2;
begin
result.x := x;
result.y := y;
end;
Function Vector2(const x, y: integer): TD3DXVector2; overload;
begin
result.x := x;
result.y := y;
end;
{ TEllipse }
function TEllipse.CalcDistanceToEllipse(const aEllipse: TEllipse): single;
begin
result := 0;
if self.IsUnitCircle and aEllipse.IsUnitCircle then
begin
result := VectorDistance(self.Center, aEllipse.Center);
result := result - (self.Radius + aEllipse.Radius);
if result < 0 then
result := 0;
end
else
if (not self.IsUnitCircle) and (not aEllipse.IsUnitCircle) then
result := self.AsPolygon.CalcDistanceToPolygon(aEllipse.AsPolygon)
else
if (self.IsUnitCircle) and (not aEllipse.IsUnitCircle) then
result := self.CalcDistanceToPolygon(aEllipse.AsPolygon)
else
if (not self.IsUnitCircle) and (aEllipse.IsUnitCircle) then
result := aEllipse.CalcDistanceToPolygon(self.AsPolygon);
end;
function TEllipse.CalcDistanceToPolygon(Poly: TAdvPolygon): single;
begin
result := Poly.CalcDistanceToEllipse(self)
end;
function TEllipse.CalcDistanceToRect(const r: Trect): single;
var
Poly: TAdvPolygon;
dist: single;
begin
result := 0;
if PointInRect(Center2d, r) then
exit;
Poly := TAdvPolygon.Create;
result := CalcDistanceToPolygon(Poly);
Poly.Free;
end;
procedure TEllipse.CalculateValuesFromRect;
var
c2: single;
begin
if Width > height then
begin
_2a := Width;
_2b := height;
end else begin
_2a := height;
_2b := Width;
end;
a := _2a / 2;
b := _2b / 2;
Center.x := r.left + Width / 2;
Center.y := r.Top + height / 2;
// calculate now c
// a2 – c2 = b2 or
// b2 + c2 = a2 or
// c2 = a2 - b2
c2 := (a * a) - (b * b);
c := sqrt(c2);
// eccentricity
// e = c/a.
e := c / a;
end;
function TEllipse.coVertex1: TD3DXVector2;
begin
result := Center;
if Width > height then
result.y := result.y - b
else
result.x := result.x - b
end;
function TEllipse.CoVertex2: TD3DXVector2;
begin
result := Center;
if Width > height then
result.y := result.y + b
else
result.x := result.x + b
end;
constructor TEllipse.Create;
begin
inherited;
fTyp := skEllipse;
end;
destructor TEllipse.Destroy;
begin
fAsPolygon.Free;
fBitMatrix.Free;
inherited;
end;
procedure TEllipse.PreparePolygon;
var
CirclePoints: array of TD3DXVector2;
x, y: integer;
v: TD3DXVector2;
Angle: double;
pt: Tpoint;
EllipsePointCount: integer;
ratio: TD3DXVector2;
procedure Add(v: TD3DXVector2);
var
len: integer;
begin
// scale, to squich it into the elipse
v := Vectormul(v, ratio);
// now move it from the 0,0 origin to the position of the ellipse
v := Vectoradd(v, self.Center);
pt := Vector2ToPoint(v);
len := Length(fAsPolygon.fPoints);
if len = 0 then
begin
setlength(fAsPolygon.fPoints, 1);
fAsPolygon.fPoints[0] := pt;
end else begin
if not Equal(pt, fAsPolygon.fPoints[len - 1]) then
if not Equal(pt, fAsPolygon.fPoints[0]) then
begin
setlength(fAsPolygon.fPoints, len + 1);
fAsPolygon.fPoints[len] := pt;
end;
end;
end;
var
d: dword;
ia, StepCount: integer;
IncStep: double;
begin
d := GetTickCount;
if not Assigned(fAsPolygon) then
fAsPolygon := TAdvPolygon.Create;
setlength(fAsPolygon.fPoints, 0);
if Width > height then
begin
ratio.x := 1;
ratio.y := height / Width;
end else begin
ratio.y := 1;
ratio.x := Width / height;
end;
StepCount := Max(Width, height) * 2;
if StepCount < 360 then
StepCount := 360;
IncStep := 360 / StepCount;
for ia := 0 to StepCount do
begin
Angle := IncStep * ia;
v := Vector2(0, a);
v := Rotate(Angle, v);
Add(v);
end;
d := GetTickCount - d;
// Clipboard.AsText := IntToStr(d);
fAsPolygon.AfterChanged;
end;
procedure TEllipse.PrepareMap;
var
bmp: TBitMap;
begin
bmp := TBitMap.Create;
bmp.Monochrome := true;
bmp.SetSize(Width, height);
fBitMatrix := TBitMatrix.Create;
bmp.Canvas.Pen.Color := clwhite;
bmp.Canvas.Brush.Color := clwhite;
bmp.Canvas.FillRect(Trect.Create(0, 0, bmp.Width, bmp.height));
bmp.Canvas.Pen.Color := clBlack;
bmp.Canvas.Brush.Color := clBlack;
bmp.Canvas.Ellipse(Trect.Create(0, 0, Width, height));
fBitMatrix.BuildFromBitMap(bmp, clBlack);
bmp.Free;
end;
function TEllipse.Foci1: TD3DXVector2;
begin
result := Center;
if Width > height then
result.x := result.x - c
else
result.y := result.y - c;
end;
function TEllipse.foci2: TD3DXVector2;
begin
result := Center;
if Width > height then
result.x := result.x + c
else
result.y := result.y + c;
end;
function TEllipse.GetNearestPoint2d(const p: Tpoint): TD3DXVector2;
begin
result := AsPolygon.GetNearestPoint2d(p);
end;
function TEllipse.height: integer;
begin
result := fMinMax.height;
end;
function TEllipse.IsUnitCircle: Boolean;
begin
result := Width = height;
end;
function TEllipse.PointInside(const WorldCoordPoint: Tpoint): Boolean;
var
x, y: integer;
begin
result := inherited PointInside(WorldCoordPoint);
if result then
begin
x := WorldCoordPoint.x - fMinMax.left;
y := WorldCoordPoint.y - fMinMax.Top;
if (x >= 0) and (x < Width) and (y >= 0) and (y < height) then
result := fBitMatrix.Value[x, y]
else
result := False;
end;
end;
Function TEllipse.DistanceToPoint(const p: Tpoint): single;
begin
result := 0;
if PointInside(p) then
exit(0);
if IsUnitCircle then
begin
result := VectorDistance(Center, Vector2(p));
result := result - Radius;
if result < 0 then
result := 0;
end else begin
// it is not a unit circle, now it is an ellipse
result := AsPolygon.DistanceToPoint(p);
end;
end;
function TEllipse.PointInside2d(const WorldCoordPoint: TD3DXVector2): Boolean;
var
p: Tpoint;
begin
with WorldCoordPoint do
begin
p.x := round(x);
p.y := round(y);
end;
result := PointInside(p);
end;
function TEllipse.Radius: single;
begin
result := Width / 2;
end;
function TEllipse.RatioWidthToHeight: single;
begin
result := Width / height;
end;
procedure TEllipse.SetRect(const Value: Trect);
begin
inherited;
CalculateValuesFromRect(Value);
PrepareMap;
PreparePolygon;
end;
function TEllipse.Vertex1: TD3DXVector2;
begin
result := Center;
if Width > height then
result.x := result.x - a
else
result.y := result.y - a
end;
function TEllipse.Vertex2: TD3DXVector2;
begin
result := Center;
if Width > height then
result.x := result.x + a
else
result.y := result.y + a
end;
function TEllipse.Width: integer;
begin
result := fMinMax.Width;
end;
{ TZoneBounds2 }
function TAdvShape.CalcDistanceToPolygon(Poly: TAdvPolygon): single;
begin
result := Poly.CalcDistanceToRect(self.Rect)
end;
function TAdvShape.CalcDistanceToRect(const r: Trect): single;
begin
result := DistRect2Rect(r, self.Rect);
end;
function TAdvShape.CalcDistanceToEllipse(const aEllipse: TEllipse): single;
begin
result := aEllipse.CalcDistanceToRect(self.Rect);
end;
constructor TAdvShape.Create;
begin
inherited;
fTyp := skRect;
end;
class function TAdvShape.CreateEllipseShape(const r: Trect): TEllipse;
begin
result := TEllipse.Create;
result.Rect := r;
end;
class function TAdvShape.CreatePolygonShape(const aPoints: TPointArray; PolyIsClosed: Boolean = true): TAdvPolygon;
var
x: integer;
begin
result := TAdvPolygon.Create;
setlength(result.fPoints, Length(aPoints));
for x := 0 to Length(aPoints) - 1 do
result.fPoints[x] := aPoints[x];
if result.fPoints[0] = result.fPoints[Length(result.fPoints) - 1] then
setlength(result.fPoints, Length(result.fPoints) - 1);
result.Closed := PolyIsClosed;
result.AfterChanged;
end;
class function TAdvShape.CreateRectShape(const r: Trect): TAdvShape;
begin
result := TAdvShape.Create;
result.Rect := r;
end;
destructor TAdvShape.Destroy;
begin
if Assigned(FUserObject) then
if FOwnsUserObject then
FreeAndNIL(FUserObject);
inherited;
end;
function TAdvShape.DistanceToPoint(const p: Tpoint): single;
begin
result := DistanceToRect(p, self.Rect);
end;
function TAdvShape.DistanceToShape(Shape: TAdvShape): single;
begin
case Shape.Typ of
skRect:
result := CalcDistanceToRect(Shape.Rect);
skEllipse:
result := CalcDistanceToEllipse(TEllipse(Shape));
skPolygon:
result := CalcDistanceToPolygon(TAdvPolygon(Shape));
else
begin
result := 0;
raise Exception.Create('unknown bounding type in function TZoneBounds2.Distance(Bounds: TZoneBounds2): single');
end;
end;
end;
function TAdvShape.Distance(const p: Tpoint): single;
begin
result := DistanceToPoint(p);
end;
function TAdvShape.Distance(Bounds: TAdvShape): single;
begin
result := DistanceToShape(Bounds);
end;
function TAdvShape.GetCenter: TD3DXVector2;
begin
result.x := fMinMax.left + fMinMax.Width / 2;
result.y := fMinMax.Top + fMinMax.height / 2;
end;
function TAdvShape.GetCenter2d: Tpoint;
begin
with Center do
begin
result.x := round(x);
result.y := round(y);
end;
end;
function TAdvShape.GetControlPoint(cp: TControlPoint): Tpoint;
var
r: Trect;
begin
r := self.Rect;
case cp of
cpTop:
result := Point(r.left + r.Width div 2, r.Top);
cpLeft:
result := Point(r.left, r.Top + r.height div 2);
cpBottom:
result := Point(r.left + r.Width div 2, r.Bottom);
cpRight:
result := Point(r.Right, r.Top + r.height div 2);