-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwg.pas
More file actions
1270 lines (1122 loc) · 33 KB
/
dwg.pas
File metadata and controls
1270 lines (1122 loc) · 33 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
{
/***************************************************************************
dwg.pas
-------
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus packages by Andreas Jakobsche
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit Dwg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, ExtCtrls;
type
{ TVector }
TVector = class(TComponent)
private
FEinheitsvektor: TVector;
FX, FY: Extended;
FT: TDateTime;
function GetAbs: Extended;
function GetEinheitsvektor: TVector;
protected
procedure AssignTo(Dest: TPersistent); override;
public
property Abs: Extended read GetAbs;
property Einheitsvektor: TVector read GetEinheitsvektor;
published
property X: Extended read FX write FX;
property Y: Extended read FY write FY;
property T: TDateTime read FT write FT;
end;
{ TRectangle }
TRectangle = class(TComponent)
private
FLeftBottom, FExtent, FRightTop: TVector;
function GetExtent: TVector;
function GetLeftBottom: TVector;
function GetRightTop: TVector;
procedure SetExtent(AValue: TVector);
procedure SetLeftBottom(AValue: TVector);
procedure SetRightTop(AValue: TVector);
protected
procedure AssignTo(Dest: TPersistent); override;
published
property LeftBottom: TVector read GetLeftBottom write SetLeftBottom;
property RightTop: TVector read GetRightTop write SetRightTop;
property Extent: TVector read GetExtent write SetExtent;
end;
TDrawing = class;
{ TDrawingItem }
TDrawingItem = class(TComponent)
private
FArea: TRectangle;
FColor: TColor;
FSelected: Boolean;
FSelectedColor: TColor;
function GetArea: TRectangle; virtual;
function GetDrawing: TDrawing;
procedure SetColor(AValue: TColor); virtual;
procedure SetSelected(AValue: Boolean); virtual;
procedure SetSelectedColor(AValue: TColor); virtual;
protected
{procedure LineTo(AVector: TVector); virtual;
procedure MoveTo(AVector: TVector); virtual;}
function GetPosition: TVector; virtual;
procedure SetPosition(AValue: TVector); virtual;
property Drawing: TDrawing read GetDrawing;
public
constructor Create(AnOwner: TComponent); override;
destructor Destroy; override;
function Contains(AVector: TVector): Boolean; virtual;
procedure Draw; virtual; abstract;
procedure Print; virtual; abstract;
property Area: TRectangle read GetArea;
property Selected: Boolean read FSelected write SetSelected;
published
property SelectedColor: TColor read FSelectedColor write SetSelectedColor;
property Color: TColor read FColor write SetColor;
property Position: TVector read GetPosition write SetPosition;
end;
{ Geradenabschnitt der alles zum Zeichnen seiner selbst enthält aber auf
externe Daten (Vektoren für Anfang und Ende) angewendet wird }
{ TStraightLineSection }
TStraightLineSection = class(TDrawingItem)
private
function GetDiff: TVector;
private
FA, FB, FDiff: TVector;
function GetLength: Extended;
{function GetArea: TRectangle; override;} {kein GetArea, weil nie als TFigure.Items[i] verwendet}
property Diff: TVector read GetDiff;
public
procedure Draw; override;
procedure Print; override;
property Length: Extended read GetLength;
published
{ Begrenzungen }
property A: TVector read FA write FA;
property B: TVector read FB write FB;
end;
{ TFigure }
TFigure = class(TDrawingItem)
private
FItemList, FSelectedList: TList;
function GetItemList: TList;
function GetSelectedList: TList;
procedure SetColor(AValue: TColor); override;
procedure SetSelectedColor(AValue: TColor); override;
private
function GetItemCount: Integer;
function GetItems(i: Integer): TDrawingItem;
function GetSelectedItems(i: Integer): TDrawingItem;
function GetSelectedCount: Integer;
procedure ReadItems(Reader: TReader);
procedure SetSelected(AValue: Boolean); override;
procedure WriteItems(Writer: TWriter);
property ItemList: TList read GetItemList;
property SelectedList: TList read GetSelectedList;
protected
procedure DefineProperties(Filer: TFiler); override;
function GetArea: TRectangle; override;
public
destructor Destroy; override;
procedure Append(AnItem: TDrawingItem);
procedure Clear;
procedure Deselect(AnItem: TDrawingItem);
procedure Draw; override;
procedure Print; override;
procedure Select(AnItem: TDrawingItem);
procedure ToggleSelection(AnItem: TDrawingItem);
property ItemCount: Integer read GetItemCount;
property Items[i: Integer]: TDrawingItem read GetItems;
property SelectedItems[i: Integer]: TDrawingItem read GetSelectedItems;
property SelectedCount: Integer read GetSelectedCount;
end;
TFrameBorderIndex = (fbBottom, fbRight, fbTop, fbLeft);
{ TFrame }
TFrame = class(TFigure) {rechteckiger Rahmen mit Seiten parallel zu den
Koordinatenachsen}
private
FBorders: array[TFrameBorderIndex] of TStraightLineSection;
FHeight: Extended;
FWidth: Extended;
function GetBorders(AnIndex: TFrameBorderIndex): TStraightLineSection;
function GetLeftBottom: TVector;
procedure SetBorders(AnIndex: TFrameBorderIndex;
AValue: TStraightLineSection);
procedure SetHeight(AValue: Extended);
procedure SetLeftBottom(AValue: TVector);
procedure SetWidth(AValue: Extended);
public
procedure Draw; override;
property Borders[AnIndex: TFrameBorderIndex]: TStraightLineSection read GetBorders write SetBorders; {Seiten des Rechtecks}
published
property Height: Extended read FHeight write SetHeight;
property Width: Extended read FWidth write SetWidth;
property LeftBottom: TVector read GetLeftBottom write SetLeftBottom;
end;
{ TTextArea }
TTextArea = class(TDrawingItem)
private
{FFont: TFont;}
FHeight: Extended;
FText: string;
FWidth: Extended;
function GetArea: TRectangle; override;
{function GetFont: TFont;}
{function GetHeight: Extended;}
function GetWidth: Extended;
{procedure SetFont(AValue: TFont);
procedure SetHeight(AValue: Extended);}
procedure SetText(AValue: string);
{procedure SetWidth(AValue: Extended);}
protected
function GetPosition: TVector; override;
{procedure SetColor(AValue: TColor); override;}
procedure SetPosition(AValue: TVector); override;
public
{destructor Destroy; override;}
procedure Draw; override;
procedure Print; override;
published
{property Font: TFont read GetFont write SetFont;}
property Text: string read FText write SetText;
property Height: Extended read FHeight write FHeight;
property Width: Extended read GetWidth write FWidth;
end;
TDrawing = class(TFigure)
private
FPaintBox: TPaintBox;
FLeftBottom, FPosition: TVector;
FPixelsPerMillimeter: Extended;
{FYPixelsPerMillimeter: Extended;}
function GetLeftBottom: TVector;
function GetPixelsPerInch: Integer;
procedure SetLeftBottom(AValue: TVector);
procedure SetPixelsPerInch(AValue: Integer);
procedure SetPixelsPerMillimeter(AValue: Extended);
{function GetPosition: TVector;
procedure SetPosition(AValue: TVector);}
protected
function FindBorder(A, B: TVector): TVector;
function IsInPaintRect(AVector: TVector): Boolean;
function PointToVector(AVector: TVector; APoint: TPoint): TVector;
function VectorToPoint(AVector: TVector): TPoint;
public
{procedure LineTo(AVector: TVector); override;
procedure MoveTo(AVector: TVector); override;}
procedure Draw; override;
procedure Focus;
function PointToVector(AVector: TVector; X, Y: Integer): TVector;
property PaintBox: TPaintBox read FPaintBox write FPaintBox;
{property Position: TVector read GetPosition write SetPosition;}
property PixelsPerInch: Integer read GetPixelsPerInch write SetPixelsPerInch;
property PixelsPerMillimeter: Extended read FPixelsPerMillimeter write SetPixelsPerMillimeter;
{property XPixelsPerMillimeter: Extended read FXPixelsPerMillimeter write FXPixelsPerMillimeter;
property YPixelsPerMillimeter: Extended read FYPixelsPerMillimeter write FYPixelsPerMillimeter;}
published
property LeftBottom: TVector read GetLeftBottom write SetLeftBottom;
end;
{ TPolygon }
TPolygon = class(TDrawingItem)
private
FAcceleration: TVector;
FEdgeList: TList;
FVelocity: TVector;
function GetEdgeList: TList;
private
FClosed: Boolean;
FPrecision: Extended;
function GetAcceleration(i: Integer): TVector;
function GetArea: TRectangle; override;
function GetEdgeCount: Integer;
function GetEdges(i: Integer): TVector;
function GetVelocities(i: Integer): TVector;
procedure ReadEdges(Reader: TReader);
procedure WriteEdges(Writer: TWriter);
property EdgeList: TList read GetEdgeList;
protected
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create(AnOwner: TComponent); override;
destructor Destroy; override;
procedure Append(x, y, t: Extended);
procedure Clear;
procedure Draw; override;
procedure Print; override;
property EdgeCount: Integer read GetEdgeCount;
property Edges[i: Integer]: TVector read GetEdges;
property Acceleration[i: Integer]: TVector read GetAcceleration;
property Velocities[i: Integer]: TVector read GetVelocities;
published
property Closed: Boolean read FClosed write FClosed;
property Precision: Extended read FPrecision write FPrecision;
end;
TTrack = class(TFigure)
public
end;
function Difference(Ref, A, B: TVector): TVector;
function Sum(Ref, A, B: TVector): TVector;
function Product(Ref, A: TVector; B: Extended): TVector;
implementation
uses
{Nur zum Debuggen:} Dialogs,
Streaming2, Printers;
function Difference(Ref, A, B: TVector): TVector;
begin
if Ref = nil then Ref := TVector.Create(nil);
if (A <> nil) and (B <> nil) then begin
Ref.X := A.X - B.X;
Ref.Y := A.Y - B.Y;
Ref.T := A.T - B.T;
end;
Result := Ref
end;
function Sum(Ref, A, B: TVector): TVector;
begin
if not Assigned(Ref) then Ref := TVector.Create(nil);
Ref.X := A.X + B.X;
Ref.Y := A.Y + B.Y;
Ref.T := A.T + B.T;
Result := Ref
end;
function Product(Ref, A: TVector; B: Extended): TVector;
begin
Ref.X := A.X * B;
Ref.Y := A.Y * B;
Result := Ref
end;
{ TTextArea }
function TTextArea.GetArea: TRectangle;
begin
Result:=inherited GetArea;
Result.Extent.Y := Height;
Result.Extent.X := Width;
end;
{function TTextArea.GetFont: TFont;
begin
if not Assigned(FFont) then begin
FFont := TFont.Create;
if Drawing <> nil then
if Drawing.PaintBox <> nil then FFont.Assign(Drawing.PaintBox.Canvas.Font)
end;
Result := FFont
end;}
function TTextArea.GetWidth: Extended;
begin
if Drawing <> nil then
if Drawing.PaintBox <> nil then begin
Drawing.PaintBox.Canvas.Font.Height := -Round(Height * Drawing.PixelsPerMillimeter);
Result := Drawing.PaintBox.Canvas.TextWidth(Text) / Drawing.PixelsPerMillimeter;
FWidth := Result;
Exit
end;
Result := 0
end;
function TTextArea.GetPosition: TVector;
begin
Result := Area.LeftBottom
end;
{procedure TTextArea.SetColor(AValue: TColor);
begin
inherited SetColor(AValue);
Font.Color := AValue
end;}
{procedure TTextArea.SetFont(AValue: TFont);
begin
GetFont.Assign(AValue)
end;}
{procedure TTextArea.SetHeight(AValue: Extended);
begin
FHeight := AValue
end;}
procedure TTextArea.SetPosition(AValue: TVector);
begin
Area.LeftBottom.Assign(AValue);
Area.Extent.X := Width;
Area.Extent.Y := Height
end;
procedure TTextArea.SetText(AValue: string);
begin
if FText=AValue then Exit;
FText:=AValue;
GetWidth;
end;
{procedure TTextArea.SetWidth(AValue: Extended);
begin
FWidth := AValue
end;}
{destructor TTextArea.Destroy;
begin
inherited Destroy;
end;}
procedure TTextArea.Draw;
var
P: TPoint;
begin
if Drawing = nil then Exit;
if Drawing.PaintBox = nil then Exit;
P := Drawing.VectorToPoint(Position);
if Selected then Drawing.PaintBox.Canvas.Font.Color := SelectedColor
else Drawing.PaintBox.Canvas.Font.Color := Color;
Drawing.PaintBox.Canvas.Font.PixelsPerInch := Drawing.PixelsPerInch;
Drawing.PaintBox.Canvas.Font.Height := -Round(Height * Drawing.PixelsPerMillimeter);
with Drawing.PaintBox.Canvas do begin
TextOut(P.X, P.Y - TextHeight(Text), Text)
end;
end;
procedure TTextArea.Print;
var
P: TPoint;
begin
P := Drawing.VectorToPoint(Position);
Printer.Canvas.Font.Color := Color;
Printer.Canvas.Font.PixelsPerInch := Drawing.PixelsPerInch;
Printer.Canvas.Font.Height := -Round(Height * Drawing.PixelsPerMillimeter);
with Printer.Canvas do
TextOut(P.X, P.Y - TextHeight(Text), Text)
end;
{ TFrame }
function TFrame.GetBorders(AnIndex: TFrameBorderIndex): TStraightLineSection;
begin
if not Assigned(FBorders[AnIndex]) then
FBorders[AnIndex] := TStraightLineSection.Create(Self);
Result := FBorders[AnIndex];
case AnIndex of
fbBottom: begin
Result.A := LeftBottom;
Result.B := LeftBottom;
Result.B.X := Result.B.X + Width;
end;
fbRight:;
fbTop:;
fbLeft:;
end;
end;
function TFrame.GetLeftBottom: TVector;
begin
Result := Area.LeftBottom
end;
procedure TFrame.SetBorders(AnIndex: TFrameBorderIndex;
AValue: TStraightLineSection);
begin
end;
procedure TFrame.SetHeight(AValue: Extended);
begin
if FHeight=AValue then Exit;
FHeight:=AValue;
end;
procedure TFrame.SetLeftBottom(AValue: TVector);
begin
Area.LeftBottom.Assign(AValue)
end;
procedure TFrame.SetWidth(AValue: Extended);
begin
if FWidth=AValue then Exit;
FWidth:=AValue;
end;
procedure TFrame.Draw;
begin
inherited Draw;
end;
{ TRectangle }
function TRectangle.GetExtent: TVector;
begin
if not Assigned(FExtent) then FExtent := TVector.Create(Self);
Result := FExtent
end;
function TRectangle.GetLeftBottom: TVector;
begin
if not Assigned(FLeftBottom) then FLeftBottom := TVector.Create(Self);
Result := FLeftBottom
end;
function TRectangle.GetRightTop: TVector;
begin
Result := Sum(FRightTop, LeftBottom, Extent)
end;
procedure TRectangle.SetExtent(AValue: TVector);
var
LB: Extended;
begin
if AValue.X >= 0 then GetExtent.X := AValue.X
else begin
LB := LeftBottom.X + AValue.X;
GetExtent.X := - AValue.X;
LeftBottom.X := LB
end;
if AValue.Y >= 0 then GetExtent.Y := AValue.Y
else begin
LB := LeftBottom.Y + AValue.Y;
GetExtent.Y := -AValue.Y;
LeftBottom.Y := LB
end;
end;
procedure TRectangle.SetLeftBottom(AValue: TVector);
begin
GetLeftBottom.Assign(AValue)
end;
procedure TRectangle.SetRightTop(AValue: TVector);
begin
if LeftBottom.X > AValue.X then begin
LeftBottom.X := AValue.X;
Extent.X := 0
end
else Extent.X := AValue.X - LeftBottom.X;
if LeftBottom.Y > AValue.Y then begin
LeftBottom.Y := AValue.Y;
Extent.Y := 0
end
else Extent.Y := AValue.Y - LeftBottom.Y
end;
procedure TRectangle.AssignTo(Dest: TPersistent);
begin
if Dest is TRectangle then begin
(Dest as TRectangle).LeftBottom := LeftBottom;
(Dest as TRectangle).Extent := Extent
end;
end;
{ TStraightLineSection }
{function TStraightLineSection.GetArea: TRectangle;
begin
Result := inherited GetArea;
if A.X < B.X then begin
Result.LeftBottom.X := A.X;
Result.RightTop.X := B.X
end
else begin
Result.LeftBottom.X := B.X;
Result.RightTop.X := A.X
end;
if A.Y < B.Y then begin
Result.LeftBottom.Y := A.Y;
Result.RightTop.Y := B.Y
end
else begin
Result.LeftBottom.Y := B.Y;
Result.RightTop.Y := A.Y
end;
end;}
function TStraightLineSection.GetDiff: TVector;
begin
if not Assigned(FDiff) then FDiff := TVector.Create(Self);
if (A <> nil) and (B <> nil) then Result := Difference(FDiff, B, A)
else Result := FDiff
end;
function TStraightLineSection.GetLength: Extended;
begin
Result := Diff.Abs
end;
procedure TStraightLineSection.Draw;
begin
if Drawing <> nil then
if Drawing.PaintBox <> nil then
if Drawing.IsInPaintRect(A) and Drawing.IsInPaintRect(B) then begin
if Selected then Drawing.PaintBox.Canvas.Pen.Color := SelectedColor
else Drawing.PaintBox.Canvas.Pen.Color := Color;
with Drawing do begin
PaintBox.Canvas.MoveTo(VectorToPoint(A));
PaintBox.Canvas.LineTo(VectorToPoint(B))
end;
end;
end;
procedure TStraightLineSection.Print;
begin
if Drawing <> nil then
if Drawing.IsInPaintRect(A) and Drawing.IsInPaintRect(B) then begin
if Selected then Printer.Canvas.Pen.Color := SelectedColor
else Printer.Canvas.Pen.Color := Color;
with Drawing do begin
Printer.Canvas.MoveTo(VectorToPoint(A));
Printer.Canvas.LineTo(VectorToPoint(B))
end;
end;
end;
{ TVector }
function TVector.GetAbs: Extended;
begin
Result := Sqrt(Sqr(X) + Sqr(Y))
end;
function TVector.GetEinheitsvektor: TVector;
begin
if not Assigned(FEinheitsvektor) then FEinheitsvektor := TVector.Create(Self);
FEinheitsvektor.X := X / Abs;
FEinheitsvektor.Y := Y / Abs;
Result := FEinheitsvektor
end;
procedure TVector.AssignTo(Dest: TPersistent);
var
D: TVector absolute Dest;
begin
if Dest is TVector then begin
D.X := X;
D.Y := Y;
end
else inherited AssignTo(Dest)
end;
{ TDrawing }
{function TDrawing.GetPosition: TVector;
begin
if not Assigned(FPosition) then FPosition := TVector.Create(Self);
Result := FPosition
end;}
function TDrawing.GetLeftBottom: TVector;
begin
if not Assigned(FLeftBottom) then FLeftBottom := TVector.Create(Self);
Result := FLeftBottom
end;
function TDrawing.GetPixelsPerInch: Integer;
begin
Result := Round(PixelsPerMillimeter * 25.4)
end;
procedure TDrawing.SetLeftBottom(AValue: TVector);
begin
GetLeftBottom.Assign(AValue)
end;
procedure TDrawing.SetPixelsPerInch(AValue: Integer);
begin
PixelsPerMillimeter := AValue / 25.4
end;
procedure TDrawing.SetPixelsPerMillimeter(AValue: Extended);
begin
if FPixelsPerMillimeter=AValue then Exit;
FPixelsPerMillimeter:=AValue;
end;
function TDrawing.FindBorder(A, B: TVector): TVector;
begin
end;
function TDrawing.IsInPaintRect(AVector: TVector): Boolean;
var
P: TPoint;
begin
if PaintBox <> nil then begin
P := VectorToPoint(AVector);
Result := (P.X >= 0) and (P.X < PaintBox.Width) and (P.Y >= 0) and (P.Y < PaintBox.Height)
end
else Result := False;
end;
function TDrawing.PointToVector(AVector: TVector; APoint: TPoint): TVector;
var
x, y: Integer;
begin
if PaintBox <> nil then begin
if AVector = nil then AVector := TVector.Create(nil);
AVector.X := LeftBottom.X + APoint.X / PixelsPerMillimeter;
AVector.Y := LeftBottom.Y + (PaintBox.Height - APoint.Y) / PixelsPerMillimeter
end;
Result := AVector
end;
function TDrawing.PointToVector(AVector: TVector; X, Y: Integer): TVector;
begin
Result := PointToVector(AVector, Point(X, Y))
end;
function TDrawing.VectorToPoint(AVector: TVector): TPoint;
var
C: TVector;
begin
if AVector = nil then Result := Point(-1, -1)
else begin
C := Difference(nil, AVector, LeftBottom);
try
Result.X := Round(C.X * PixelsPerMillimeter);
Result.Y := PaintBox.Height - Round(C.Y * PixelsPerMillimeter);
finally
C.Free
end
end;
end;
procedure TDrawing.Draw;
begin
if PaintBox <> nil then with PaintBox.Canvas do begin
FillRect(PaintBox.ClientRect);
end;
inherited Draw;
end;
{ TDrawingItem }
procedure TDrawingItem.SetColor(AValue: TColor);
begin
if FColor=AValue then Exit;
FColor:=AValue;
if not Selected then Draw
end;
procedure TDrawingItem.SetSelected(AValue: Boolean);
begin
if FSelected=AValue then Exit;
FSelected:=AValue;
Draw
end;
procedure TDrawingItem.SetSelectedColor(AValue: TColor);
begin
if FSelectedColor=AValue then Exit;
FSelectedColor:=AValue;
if Selected then Draw
end;
function TDrawingItem.GetPosition: TVector;
begin
Result := Area.LeftBottom;
end;
procedure TDrawingItem.SetPosition(AValue: TVector);
begin
Area.LeftBottom.Assign(AValue);
end;
constructor TDrawingItem.Create(AnOwner: TComponent);
begin
inherited Create(AnOwner);
if AnOwner is TFigure then begin
TFigure(AnOwner).Append(Self);
Color := TFigure(AnOwner).Color;
SelectedColor := TFigure(AnOwner).SelectedColor;
if TFigure(Owner).Selected then TFigure(Owner).Select(Self)
end;
end;
destructor TDrawingItem.Destroy;
var
x: Integer;
Fig: TFigure;
begin
if Owner <> nil then
if Owner is TFigure then begin
TComponent(Fig) := Owner;
if Fig.ItemCount > 0 then begin
x := Fig.ItemList.IndexOf(Self);
if x >= 0 then Fig.ItemList.Delete(x);
end
end;
inherited Destroy
end;
function TDrawingItem.Contains(AVector: TVector): Boolean;
begin
Result := (AVector.X >= Area.LeftBottom.X) and (AVector.X <= Area.RightTop.X)
and (AVector.Y >= Area.LeftBottom.Y) and (AVector.Y <= Area.RightTop.Y)
end;
procedure TDrawing.Focus;
var
x, y: Extended;
begin
if PaintBox <> nil then begin
LeftBottom := Area.LeftBottom;
if Area.Extent.X > 0 then x := PaintBox.Width / Area.Extent.X
else x := PixelsPerMillimeter;
if Area.Extent.Y > 0 then y := PaintBox.Height / Area.Extent.Y
else y := PixelsPerMillimeter;
if y < x then x := y;
PixelsPerMillimeter := x;
Draw
end;
end;
function TDrawingItem.GetArea: TRectangle;
begin
if not Assigned(FArea) then FArea := TRectangle.Create(Self);
Result := FArea;
end;
function TDrawingItem.GetDrawing: TDrawing;
var
x: TComponent;
begin
x := Self;
while x <> nil do begin
if x is TDrawing then begin
Result := TDrawing(x);
Exit
end;
x := x.Owner
end;
Result := nil
end;
{ TPolygon }
function TPolygon.GetEdgeList: TList;
begin
if not Assigned(FEdgeList) then FEdgeList := TList.Create;
Result := FEdgeList;
end;
function TPolygon.GetArea: TRectangle;
var
i: Integer;
begin
Result:=inherited GetArea;
if EdgeCount > 0 then begin
Result.LeftBottom := Edges[0];
Result.RightTop := Edges[0];
if EdgeCount > 1 then
for i := 1 to EdgeCount - 1 do begin
if Result.LeftBottom.X > Edges[i].X then Result.LeftBottom.X := Edges[i].X;
if Result.LeftBottom.Y > Edges[i].Y then Result.LeftBottom.Y := Edges[i].Y;
if Result.RightTop.X < Edges[i].X then Result.RightTop.X := Edges[i].X;
if Result.RightTop.Y < Edges[i].Y then Result.RightTop.Y := Edges[i].Y
end;
end;
end;
function TPolygon.GetAcceleration(i: Integer): TVector;
begin
if not Assigned(FAcceleration) then FAcceleration := TVector.Create(Self);
Result := FAcceleration;
if (i > 0) and (i < EdgeCount) then begin
if Edges[i].T <> Edges[i-1].T then begin
Result.X := (Velocities[i].X - Velocities[i-1].X) / (Edges[i].T - Edges[i-1].T);
Result.Y := (Velocities[i].Y - Velocities[i-1].Y) / (Edges[i].T - Edges[i-1].T);
end
else Result := Acceleration[i-1]
end
else begin
Result.X := 0;
Result.Y := 0
end;
end;
function TPolygon.GetEdgeCount: Integer;
begin
if not Assigned(FEdgeList) then Result := 0
else Result := EdgeList.Count
end;
function TPolygon.GetEdges(i: Integer): TVector;
begin
Result := TVector(EdgeList[i])
end;
function TPolygon.GetVelocities(i: Integer): TVector;
begin
if not Assigned(FVelocity) then FVelocity := TVector.Create(Self);
Result := FVelocity;
if (i > 0) and (i < EdgeCount) then
if Edges[i].T > Edges[i-1].T then begin
Result.X := (Edges[i].X - Edges[i-1].X) / (Edges[i].T - Edges[i-1].T);
Result.Y := (Edges[i].Y - Edges[i-1].Y) / (Edges[i].T - Edges[i-1].T);
end
else Result := Velocities[i - 1]
else begin
Result.X := 0;
Result.Y := 0
end;
end;
procedure TPolygon.ReadEdges(Reader: TReader);
var
i, n: Integer;
x: TVector;
begin
n := Reader.ReadInteger;
if n > 0 then begin
Reader.ReadListBegin;
for i := 0 to n - 1 do begin
x := TVector.Create(Self);
EdgeList.Add(x);
Reader.ReadComponent(x)
end;
Reader.ReadListEnd;
end;
end;
procedure TPolygon.WriteEdges(Writer: TWriter);
var i: Integer;
begin
Writer.WriteInteger(EdgeCount);
if EdgeCount > 0 then begin
Writer.WriteListBegin;
for i := 0 to EdgeCount - 1 do Writer.WriteComponent(Edges[i]);
Writer.WriteListEnd;
end;
end;
procedure TPolygon.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('Edges', @ReadEdges, @WriteEdges, True)
end;
constructor TPolygon.Create(AnOwner: TComponent);
begin
inherited Create(AnOwner);
FPrecision := 0.1;
end;
destructor TPolygon.Destroy;
begin
FEdgeList.Free;
FEdgeList := nil;
inherited Destroy;
end;
procedure TPolygon.Append(x, y, t: Extended);
var
C, D, Edge: TVector;
begin
Edge := TVector.Create(Self);
Edge.X := x;
Edge.Y := y;
Edge.T := t;
if EdgeCount = 0 then EdgeList.Add(Edge)
else begin
C := TVector.Create(Self);
D := TVector.Create(Self);
try
if Difference(D, Edge, Edges[EdgeCount - 1]).Abs >= Precision then begin
if EdgeCount <= 1 then EdgeList.Add(Edge)
else begin
Difference(C, Edges[EdgeCount - 2], Edges[EdgeCount - 1]);
if C.Einheitsvektor = D.Einheitsvektor then begin
Edges[EdgeCount - 1].Assign(Edge);
Edge.Free
end
else EdgeList.Add(Edge)
end
end
else
Edge.Free
finally
D.Free;
C.Free
end
end
end;
procedure TPolygon.Clear;
var i: Integer;
begin
if EdgeCount > 0 then begin
for i := 0 to EdgeCount - 1 do Edges[i].Free;
EdgeList.Clear
end;
end;
procedure TPolygon.Draw;
var
i: Integer;
LS: TStraightLineSection;
begin