-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLimbSet.pas
244 lines (212 loc) · 5.83 KB
/
LimbSet.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
unit LimbSet;
interface
uses
VCL.Graphics, System.Math;
const
defArmLen = 30;
defForeArmLen = 20;
defHandLen = 10;
type
TLimb = class
procedure draw(const Canvas: TCanvas); virtual;
procedure setPos(const X, Y: Integer); virtual;
procedure setScale(const scale: Real); virtual;
procedure setAngle(const angle: Real); virtual;
constructor Create(const angle: Real; const X, Y: Integer; const Len: Integer);
public
Len: Integer;
protected
angle: Real;
anrX, anrY: Integer;
scale: Real;
public
property X: Integer read anrX;
property Y: Integer read anrY;
property Rotation: Real read angle write setAngle;
end;
TLongLimbOrientation = record
mainAngle, midAngle, finAngle: Real;
end;
TLongLimbSize = record
arm, foreArm, hand: Integer;
end;
TLongLimb = class(TLimb) //TLongLimb
private
foreArm, hand: TLimb;
fingerX, fingerY: Integer;
procedure setOrient(const savedPos: TLongLimbOrientation);
function getOrient: TLongLimbOrientation;
function getSize: TLongLimbSize;
procedure setSize(const size: TLongLimbSize);
public
procedure draw(const Canvas: TCanvas); override;
procedure setPos(const X, Y: Integer); override;
procedure setAngle(const angle: Real); override;
procedure setMidJoint(const angle: Real);
procedure setFinJoint(const angle: Real);
procedure setScale(const scale: Real); override;
constructor Create(const angle: Real; const X, Y: Integer);
property Orient: TLongLimbOrientation read getOrient write setOrient;
property Size: TLongLimbSize read getSize write setSize;
property midLimb: TLimb read foreArm;
property finLimb: TLimb read hand;
end;
THead = class(TLimb)
procedure draw(const Canvas: TCanvas); override;
procedure setScale(const scale: Real); override;
constructor Create(const X, Y: Integer; const neckLen, radius: Integer);
private
radius: Integer;
end;
implementation
constructor TLimb.Create(const angle: Real; const X, Y: Integer; const Len: Integer);
begin
anrX := X;
anrY := Y;
self.Len := Len;
setAngle(angle);
scale := 1;
end;
procedure TLimb.draw(const Canvas: TCanvas);
begin
with Canvas do
begin
MoveTo(anrX, anrY);
LineTo(anrX + Round(sin(angle) * Len), anrY + Round(cos(angle) * Len));
end;
end;
procedure TLimb.setPos(const X: Integer; const Y: Integer);
begin
anrX := X;
anrY := Y;
end;
procedure TLimb.setAngle(const angle: Real);
begin
self.angle := angle;
end;
procedure TLimb.setScale(const scale: Real);
var
tempLen: Integer;
begin
tempLen := round(Len * scale / self.scale);
if tempLen <> Len then
begin
self.scale := scale;
Len := tempLen;
end;
end;
//Arm implementation
constructor TLongLimb.Create(const angle: Real; const X, Y: Integer);
begin
foreArm := TLimb.Create(angle, X, Y, defforeArmLen);
hand := TLimb.Create(angle, X, Y, defHandLen);
inherited Create(angle, X, Y, defArmLen);
end;
procedure TLongLimb.draw(const Canvas: TCanvas);
begin
with Canvas do
begin
MoveTo(anrX, anrY);
LineTo(foreArm.anrX, foreArm.anrY);
LineTo(hand.anrX, hand.anrY);
LineTo(fingerX, fingerY);
end;
end;
procedure TLongLimb.setPos(const X: Integer; const Y: Integer);
var
changeX, changeY: Integer;
begin
changeX := X - anrX;
changeY := Y - anrY;
foreArm.setPos(foreArm.anrX + changeX, foreArm.anrY + changeY);
hand.setPos(hand.anrX + changeX, hand.anrY + changeY);
inc(fingerX, changeX);
inc(fingerY, changeY);
inherited setPos(X, Y);
end;
procedure TLongLimb.setAngle(const angle: Real);
begin
self.angle := angle;
foreArm.anrX := anrX + Round(Len * sin(angle));
foreArm.anrY := anrY + Round(Len * cos(angle));
setMidJoint(foreArm.angle);
end;
procedure TLongLimb.setMidJoint(const angle: Real);
begin
hand.anrX := foreArm.anrX + Round(foreArm.Len * sin(angle));
hand.anrY := foreArm.anrY + Round(foreArm.Len * cos(angle));
foreArm.setAngle(angle);
setFinJoint(hand.angle);
end;
procedure TLongLimb.setFinJoint(const angle: Real);
begin
fingerX := hand.anrX + Round(hand.Len * sin(angle));
fingerY := hand.anrY + Round(hand.Len * cos(angle));
hand.setAngle(angle);
end;
procedure TLongLimb.setScale(const scale: Real);
begin
inherited setScale(scale);
foreArm.setScale(scale);
hand.setScale(scale);
setAngle(angle);
setMidJoint(foreArm.angle);
// setWrist(wristAngle);
end;
procedure TLongLimb.setOrient(const savedPos: TLongLimbOrientation);
begin
setAngle(savedPos.mainAngle);
setMidJoint(savedPos.midAngle);
setFinJoint(savedPos.finAngle);
end;
function TLongLimb.getOrient: TLongLimbOrientation;
begin
Result.mainAngle := angle;
Result.midAngle := foreArm.angle;
Result.finAngle := hand.angle;
end;
function TLongLimb.getSize: TLongLimbSize;
begin
Result. arm := self.Len;
Result.foreArm := foreArm.Len;
Result.hand := hand.Len;
end;
procedure TLongLimb.setSize(const size: TLongLimbSize);
begin
Len:= size.arm;
foreArm.Len := size.foreArm;
hand.Len := size.hand;
end;
// head implementation
constructor THead.Create(const X: Integer; const Y: Integer; const neckLen: Integer; const radius: Integer);
begin
anrX := X;
anrY := Y;
Len := neckLen;
self.radius := radius;
angle := Pi;
scale := 1;
end;
procedure THead.draw(const Canvas: TCanvas);
var
centerX, centerY: Integer;
begin
inherited Draw(Canvas);
centerX := Round(anrX + (Len + radius) * sin(angle));
centerY := Round(anrY + (Len + radius) * cos(angle));
Canvas.Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
end;
procedure THead.setScale(const scale: Real);
var
tempLen, tempRadius: Integer;
begin
tempLen:= Round(Len * scale / self.scale);
tempRadius := Round(radius * scale / self.scale);
if (tempRadius <> radius) and (tempLen <> Len) then
begin
self.scale := scale;
radius := tempRadius;
Len:= tempLen;
end;
end;
end.