-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.h
128 lines (97 loc) · 2.17 KB
/
math.h
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
//
// V2 vector type
//
internal inline v2 operator+(v2 A, v2 B)
{
return v2{ A.X + B.X, A.Y + B.Y };
}
internal inline v2 operator-(v2 A, v2 B)
{
return v2{ A.X - B.X, A.Y - B.Y };
}
internal inline v2 operator-(v2 A)
{
return v2{ -A.X, -A.Y };
}
internal inline v2 operator*(v2 A, v2 B)
{
return v2{ A.X * B.X, A.Y * B.Y };
}
internal inline v2 operator*(f32 A, v2 B)
{
return v2{ A * B.X, A * B.Y };
}
internal inline v2 operator/(v2 A, v2 B)
{
return v2{ A.X / B.X, A.Y / B.Y };
}
internal inline v2 operator/(v2 A, f32 B)
{
return v2{ A.X / B, A.Y / B };
}
internal inline v2 operator/(f32 A, v2 B)
{
return v2{ A / B.X, A / B.Y };
}
internal inline v2 &operator+=(v2 &A, v2 B)
{
A = A + B;
return A;
}
internal inline v2 &operator-=(v2 &A, v2 B)
{
A = A - B;
return A;
}
internal inline v2 &operator*=(v2 &A, f32 B)
{
A = B * A;
return A;
}
internal inline v2 &operator*=(v2 &A, v2 B)
{
A = B * A;
return A;
}
//
// @Warn floor(7.999) gives 8 which is dangerous
// It happens because of rounding during the fp-add
//
internal inline f32 DotV2(v2 A, v2 B)
{
f32 Result = A.X * B.X + A.Y * B.Y;
return Result;
}
internal inline f32 DistanceBetweenTwoPointsSq(v2 A, v2 B)
{
f32 Result = Square(B.X - A.X) + Square(B.Y - A.Y);
return Result;
}
internal inline bmm PointInRectangle(v2 Position, v4 Rectangle)
{
bmm Result = ((Position.X >= Rectangle.Left) && (Position.Y >= Rectangle.Bottom) &&
(Position.X < Rectangle.Right) && (Position.Y < Rectangle.Top));
return Result;
}
internal inline bmm PointInRange(f32 Value, f32 Start, f32 End)
{
bmm Result = ((Value >= Start) &&
(Value < End));
return Result;
}
internal inline bmm LineInRange(f32 LineStart, f32 LineEnd, f32 RangeStart, f32 RangeEnd)
{
bmm Result = ((LineEnd >= RangeStart) &&
(LineStart < RangeEnd));
return Result;
}
internal inline s32 FloorF32(f32 Value)
{
s32 Result = (s32)(Value + 32768.0f) - 32768;
return Result;
}
internal inline s32 CeilF32(f32 Value)
{
s32 Result = 32768 - (s32)(32768.0f - Value);
return Result;
}