22 #ifndef BLADERUNNER_VECTOR_H 23 #define BLADERUNNER_VECTOR_H 25 #include "common/types.h" 36 Vector2(
float ax,
float ay) : x(ax), y(ay) {}
40 return a.x == b.x && a.y == b.y;
53 Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
55 Vector3(
float ax,
float ay,
float az) : x(ax), y(ay), z(az) {}
57 inline float length() {
58 return sqrt(x * x + y * y + z * z);
64 return Vector3(0.0f, 0.0f, 0.0f);
66 return Vector3(x / len, y / len, z / len);
71 a.y * b.z - a.z * b.y,
72 a.z * b.x - a.x * b.z,
73 a.x * b.y - a.y * b.x);
77 return a.x * b.x + a.y * b.y + a.z * b.z;
86 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
90 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
94 return Vector3(f * v.x, f * v.y, f * v.z);
104 Vector4() : x(0.0), y(0.0), z(0.0), w(0.0) {}
106 Vector4(
float ax,
float ay,
float az,
float aw) : x(ax), y(ay), z(az), w(aw) {}
110 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
114 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
118 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
122 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
126 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
129 inline int angle_1024(
float x1,
float z1,
float x2,
float z2) {
130 float angle_rad = atan2(x2 - x1, z1 - z2);
131 int a = int(512.0 * angle_rad / M_PI);
132 return (a + 1024) % 1024;
136 return angle_1024(v1.x, v1.z, v2.x, v2.z);
139 inline float distance(
float x1,
float z1,
float x2,
float z2) {
142 float d = sqrt(dx * dx + dz * dz);
144 float int_part = (int)d;
145 float frac_part = d - int_part;
147 if (frac_part < 0.001)
150 return int_part + frac_part;
154 return distance(v1.x, v1.y, v2.x, v2.y);
158 return distance(v1.x, v1.z, v2.x, v2.z);
162 Vector2 s1(a2.x - a1.x, a2.y - a1.y);
163 Vector2 s2(b2.x - b1.x, b2.y - b1.y);
165 float s = (s1.x * (a1.y - b1.y) - s1.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
166 float t = (s2.x * (a1.y - b1.y) - s2.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
168 if (s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f) {
169 intersection->x = a1.x + (t * s1.x);
170 intersection->y = a1.y + (t * s1.y);