-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil_math.pas
295 lines (260 loc) · 6.23 KB
/
util_math.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
unit util_math;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math;
procedure HSL2RGB(Hue, Saturation, Lightness: Double; out R, G, B: Byte);
procedure BeatifulTick(const ScreenWidth, MinTickWidth: Integer; const V0, V1: Double; out Start, Step: Double);
function AtoF(const S: string): Double;
function AtoI(const S: string): Int64;
function HexToInt(HexStr: string): Int64;
function FindNearest(L: array of Integer; const V, Def: Integer): Integer;
function IsPtInRect(const APt: TPoint; const ARect: TRect): Boolean;
function GCD(X, Y: Cardinal): Cardinal;
function DBtoMultiplier(const Db: Double): Integer;
function FloatAsInt(const F: Single): Integer;
function IntAsFloat(const N: Integer): Float;
procedure QuickSort(AList: PPointer; const N: Integer; Compare: TListSortCompare);
implementation
procedure QuickSort0(FList: PPointer; L, R : Longint;
Compare: TListSortCompare);
var
I, J : Longint;
P, Q : Pointer;
begin
repeat
I := L;
J := R;
P := FList[ (L + R) div 2 ];
repeat
while Compare(P, FList[i]) > 0 do
I := I + 1;
while Compare(P, FList[J]) < 0 do
J := J - 1;
If I <= J then
begin
Q := FList[I];
Flist[I] := FList[J];
FList[J] := Q;
I := I + 1;
J := J - 1;
end;
until I > J;
// sort the smaller range recursively
// sort the bigger range via the loop
// Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
if J - L < R - I then
begin
if L < J then
QuickSort0(FList, L, J, Compare);
L := I;
end
else
begin
if I < R then
QuickSort0(FList, I, R, Compare);
R := J;
end;
until L >= R;
end;
procedure QuickSort(AList: PPointer; const N: Integer; Compare: TListSortCompare);
begin
QuickSort0(AList, 0, N - 1, Compare);
end;
procedure HSL2RGB(Hue, Saturation, Lightness: Double; out R, G, B: Byte);
var
P, Q: Double;
function hue2rgb(P, Q, T: Double): Double;
begin
if T < 0 then T := T + 1;
if T > 1 then T := T - 1;
if T < 1/6 then Result := P + (Q - P) * 6 * T
else if T < 1/2 then Result := Q
else if T < 2/3 then Result := P + (Q - P) * (2/3 - T) * 6
else Result := P;
end;
begin
if Saturation = 0 then
begin
R := 255; G := 255; B := 255;
Exit;
end;
q := IfThen(Lightness < 0.5, Lightness * (1 + Saturation), Lightness + Saturation - Lightness * Saturation);
p := 2 * Lightness - q;
R := Trunc(255 * hue2rgb(p, q, Hue + 1/3));
G := Trunc(255 * hue2rgb(p, q, Hue));
B := Trunc(255 * hue2rgb(p, q, Hue - 1/3));
end;
procedure BeatifulTick(const ScreenWidth, MinTickWidth: Integer; const V0, V1: Double; out Start, Step: Double);
var
I: Integer;
function Simplify(V: Double): Double;
var
S: string;
K: Integer;
T: Integer = 0;
procedure ClearS(const Start: Integer);
var
L: Integer;
begin
for L := Start to Length(S) do
if S[L] in ['0'..'9'] then S[L] := '0';
end;
begin
S := FloatToStr(V);
for K := 1 to Length(S) do
begin
if S[K] in ['1'..'9'] then
begin
if I < Length(S) then
if StrToIntDef(S[K + 1], 0) >= 5 then T := 1;
Inc(T, StrToInt(S[K]));
case T of
8, 9:
T := 10;
4, 6, 7:
T := 5;
3:
T := 2;
end;
if T < 10 then
begin
S[K] := IntToStr(T)[1];
ClearS(K + 1);
end
else begin
if K > 1 then
begin
S[K - 1] := '1';
ClearS(K);
end
else begin
ClearS(1);
S := '1' + S;
end;
end;
end;
end;
Result := StrToFloat(S);
end;
begin
I := Trunc(ScreenWidth / MinTickWidth);
Start := V0;
Step := V1 - V0;
if I <= 0 then Exit;
if Step <= 0 then Exit;
Step := Simplify((V1 - V0) / I);
I := Round(V0 / Step);
Start := I * Step;
if Start > V0 then Start := Start - Step;
end;
function AtoF(const S: string): Double;
var
I, J: Integer;
begin
Result := 0;
for I := 1 to Length(S) do
if S[I] in ['0'..'9', '.'] then
begin
for J := I + 1 to Length(S) do
if not (S[J] in ['0'..'9', '.']) then
begin
Result := StrToFloatDef(Copy(S, I, J - I), 0.0);
Exit;
end;
Result := StrToFloatDef(Copy(S, I, Length(S) + 1 - I), 0.0);
Break;
end;
end;
function AtoI(const S: string): Int64;
var
T: string;
begin
T := S;
Result := 0;
if T = '' then
Exit;
if T[1] = '$' then
begin
Delete(T, 1, 1);
Result := HexToInt(T);
end
else if (Length(T) > 2) and (T[1] in ['X', 'x']) then
begin
Delete(T, 1, 2);
Result := HexToInt(T);
end
else
Result := StrToInt64Def(T, 0);
end;
function HexToInt(HexStr: string): Int64;
var
I: integer;
begin
HexStr := UpperCase(HexStr);
Result := 0;
for i := 1 to length(HexStr) do
begin
Result := Result shl 4;
if HexStr[i] in ['0'..'9'] then
Result := Result + DWord(byte(HexStr[i]) - 48)
else
if HexStr[i] in ['A'..'F'] then
Result := Result + DWord(byte(HexStr[i]) - 55)
else
begin
Break;
end;
end;
end;
function FindNearest(L: array of Integer; const V, Def: Integer): Integer;
var
I: Integer;
O: Integer;
begin
Result := Def;
if Low(L) > High(L) then Exit;
Result := L[Low(L)];
O := Abs(Result - V);
for I := Low(L) + 1 to High(L) do
if Abs(L[I] - V) < O then
begin
O := Abs(L[I] - V);
Result := L[I];
end;
end;
function IsPtInRect(const APt: TPoint; const ARect: TRect): Boolean;
begin
Result := InRange(APt.x, ARect.Left, ARect.Right)
and InRange(APt.y, ARect.Top, ARect.Bottom);
end;
function GCD(X, Y: Cardinal): Cardinal;
begin
Result := 0;
if (X = 0) or (Y = 0) then Exit;
Result := X mod Y;
while True do
begin
if Result = 0 then
begin
Result := Y;
Break;
end;
X := Y;
Y := Result;
Result := X mod Y;
end;
end;
function DBtoMultiplier(const Db: Double): Integer;
begin
Result := Round(Power(10, Db / 20));
end;
function FloatAsInt(const F: Single): Integer;
begin
Result := PInteger(@F)^;
end;
function IntAsFloat(const N: Integer): Float;
begin
Result := PSingle(@N)^;
end;
end.