-
Notifications
You must be signed in to change notification settings - Fork 0
/
MATH.txt
74 lines (54 loc) · 1.83 KB
/
MATH.txt
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
-------------------------------------------------------------------------------------------
-- Line equations.
-------------------------------------------------------------------------------------------
ax + by + c = 0;
a = p2y - p1y
b = p1x - p2x
c = p1y * p2x - p1x * p2y
Ay = -b/a
By = -c/a
Ax = -a/b
Bx = -c/b
x = f(y) = Ay * y + By
y = f(x) = Ax * x + Bx
f(y+1) = f(y) + Ay
f(x+1) = f(x) + Ax
fl = floor(x)
fl + round(x - fl)
-------------------------------------------------------------------------------------------
-- Barycentric formulas.
-------------------------------------------------------------------------------------------
aria = (v2x - v1x) * (v3y - v1y) - (v2y - v1y) * (v3x - v1x)
a1 = v2y - v3y
b1 = v3x - v2x
c1 = v2x * v3y - v2y * v3x
a2 = v3y - v1y
b2 = v1x - v3x
c2 = v3x * v1y - v3y * v1x
a3 = v1y - v2y
b3 = v2x - v1x
c3 = v1x * v2y - v1y * v2x
l1 = a1 / aria * x + b1 / aria * y + c1 / aria
l2 = a2 / aria * x + b2 / aria * y + c2 / aria
l3 = a3 / aria * x + b3 / aria * y + c3 / aria
-------------------------------------------------------------------------------------------
-- Interpolant formulas.
-------------------------------------------------------------------------------------------
t_dx = (v1y - v2y) * (i3 - i1) + (v3y - v1y) * (i2 - i1)
t_dy = (v2x - v1x) * (i3 - i1) + (v1x - v3x) * (i2 - i1)
idx = t_dx / aria
idy = t_dy / aria
-------------------------------------------------------------------------------------------
-- Fast log2.
-------------------------------------------------------------------------------------------
inline float fast_log2 (float val)
{
assert (val > 0);
int * const exp_ptr = reinterpret_cast <int *> (&val);
int x = *exp_ptr;
const int log_2 = ((x >> 23) & 255) - 128;
x &= ~(255 << 23);
x += 127 << 23;
*exp_ptr = x;
return (val + log_2);
}