-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnitControl.pas
86 lines (66 loc) · 1.68 KB
/
UnitControl.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
unit UnitControl;
interface
uses
Math, TypeControl;
type
TUnit = class
private
FId: Int64;
FX: Double;
FY: Double;
protected
constructor Create(const id: Int64; const x: Double; const y: Double);
public
function GetId: Int64;
property Id: Int64 read GetId;
function GetX: Double;
property X: Double read GetX;
function GetY: Double;
property Y: Double read GetY;
function GetDistanceTo(x: Double; y: Double): Double; overload;
function GetDistanceTo(otherUnit: TUnit): Double; overload;
function GetSquaredDistanceTo(x: Double; y: Double): Double; overload;
function GetSquaredDistanceTo(otherUnit: TUnit): Double; overload;
destructor Destroy; override;
end;
TUnitArray = array of TUnit;
implementation
constructor TUnit.Create(const id: Int64; const x: Double; const y: Double);
begin
FId := id;
FX := x;
FY := y;
end;
function TUnit.GetId: Int64;
begin
result := FId;
end;
function TUnit.GetX: Double;
begin
result := FX;
end;
function TUnit.GetY: Double;
begin
result := FY;
end;
function TUnit.getDistanceTo(x: Double; y: Double): Double;
begin
result := Sqrt(Sqr(FX - x) + Sqr(FY - y));
end;
function TUnit.getDistanceTo(otherUnit: TUnit): Double;
begin
result := GetDistanceTo(otherUnit.FX, otherUnit.FY);
end;
function TUnit.getSquaredDistanceTo(x: Double; y: Double): Double;
begin
result := Sqr(FX - x) + Sqr(FY - y);
end;
function TUnit.getSquaredDistanceTo(otherUnit: TUnit): Double;
begin
result := GetSquaredDistanceTo(otherUnit.FX, otherUnit.FY);
end;
destructor TUnit.Destroy;
begin
inherited;
end;
end.