njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Loading...
Searching...
No Matches
_math.h
1#pragma once
2#include "_types.h"
3#include <cmath>
4
5namespace njin {
8
10inline constexpr f32 pi = 3.14159265358979f;
11
13struct rect {
16};
17
19struct circle {
21 f32 radius = 0.0f;
22};
23
24// Phép toán vec2: + - * / theo từng thành phần, * / với một số, đổi dấu,
25// += -= *= /=, == và !=. Mô tả trong trang Toán học của wiki.
27constexpr vec2 operator+(vec2 a, vec2 b) { return {a.x + b.x, a.y + b.y}; }
28constexpr vec2 operator-(vec2 a, vec2 b) { return {a.x - b.x, a.y - b.y}; }
29constexpr vec2 operator*(vec2 a, vec2 b) { return {a.x * b.x, a.y * b.y}; }
30constexpr vec2 operator/(vec2 a, vec2 b) { return {a.x / b.x, a.y / b.y}; }
31constexpr vec2 operator*(vec2 a, f32 s) { return {a.x * s, a.y * s}; }
32constexpr vec2 operator*(f32 s, vec2 a) { return {a.x * s, a.y * s}; }
33constexpr vec2 operator/(vec2 a, f32 s) { return {a.x / s, a.y / s}; }
34constexpr vec2 operator-(vec2 a) { return {-a.x, -a.y}; }
35constexpr vec2 &operator+=(vec2 &a, vec2 b) { return a = a + b; }
36constexpr vec2 &operator-=(vec2 &a, vec2 b) { return a = a - b; }
37constexpr vec2 &operator*=(vec2 &a, f32 s) { return a = a * s; }
38constexpr vec2 &operator/=(vec2 &a, f32 s) { return a = a / s; }
39constexpr bool operator==(vec2 a, vec2 b) { return a.x == b.x && a.y == b.y; }
40constexpr bool operator!=(vec2 a, vec2 b) { return !(a == b); }
42
47constexpr f32 dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; }
48
53constexpr f32 cross(vec2 a, vec2 b) { return a.x * b.y - a.y * b.x; }
54
58constexpr f32 length_sq(vec2 v) { return dot(v, v); }
59
63inline f32 length(vec2 v) { return std::sqrt(length_sq(v)); }
64
69inline f32 distance(vec2 a, vec2 b) { return length(b - a); }
70
74inline vec2 normalize(vec2 v) {
75 const f32 len = length(v);
76 return len > 0.0f ? v / len : vec2{0.0f, 0.0f};
77}
78
83inline vec2 rotate(vec2 v, f32 degrees) {
84 const f32 r = degrees * (pi / 180.0f);
85 const f32 c = std::cos(r);
86 const f32 s = std::sin(r);
87 return {v.x * c - v.y * s, v.x * s + v.y * c};
88}
89
94inline vec2 from_angle(f32 degrees) { return rotate({1.0f, 0.0f}, degrees); }
95
99inline f32 angle_of(vec2 v) { return std::atan2(v.y, v.x) * (180.0f / pi); }
100
102constexpr vec3 operator+(vec3 a, vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
103constexpr vec3 operator-(vec3 a, vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
104constexpr vec3 operator*(vec3 a, f32 s) { return {a.x * s, a.y * s, a.z * s}; }
105constexpr vec3 operator*(f32 s, vec3 a) { return {a.x * s, a.y * s, a.z * s}; }
106constexpr vec3 operator/(vec3 a, f32 s) { return {a.x / s, a.y / s, a.z / s}; }
107constexpr vec3 operator-(vec3 a) { return {-a.x, -a.y, -a.z}; }
108constexpr vec3 &operator+=(vec3 &a, vec3 b) { return a = a + b; }
109constexpr vec3 &operator-=(vec3 &a, vec3 b) { return a = a - b; }
110constexpr vec3 &operator*=(vec3 &a, f32 s) { return a = a * s; }
111constexpr bool operator==(vec3 a, vec3 b) { return a.x == b.x && a.y == b.y && a.z == b.z; }
112constexpr bool operator!=(vec3 a, vec3 b) { return !(a == b); }
114
119constexpr f32 dot(vec3 a, vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
120
125constexpr vec3 cross(vec3 a, vec3 b) {
126 return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
127}
128
132constexpr f32 length_sq(vec3 v) { return dot(v, v); }
133
137inline f32 length(vec3 v) { return std::sqrt(length_sq(v)); }
138
143inline f32 distance(vec3 a, vec3 b) { return length(b - a); }
144
148inline vec3 normalize(vec3 v) {
149 const f32 len = length(v);
150 return len > 0.0f ? v / len : vec3{0.0f, 0.0f, 0.0f};
151}
152
158constexpr vec3 lerp(vec3 a, vec3 b, f32 t) { return a + (b - a) * t; }
159
165constexpr f32 clamp(f32 v, f32 lo, f32 hi) { return v < lo ? lo : (v > hi ? hi : v); }
166
172constexpr vec2 clamp(vec2 v, vec2 lo, vec2 hi) {
173 return {clamp(v.x, lo.x, hi.x), clamp(v.y, lo.y, hi.y)};
174}
175
181constexpr f32 lerp(f32 a, f32 b, f32 t) { return a + (b - a) * t; }
187constexpr vec2 lerp(vec2 a, vec2 b, f32 t) { return a + (b - a) * t; }
193constexpr rgba lerp(rgba a, rgba b, f32 t) {
194 return {lerp(a.r, b.r, t), lerp(a.g, b.g, t), lerp(a.b, b.b, t),
195 lerp(a.a, b.a, t)};
196}
197
203constexpr f32 move_toward(f32 from, f32 to, f32 max_step) {
204 return to > from ? (from + max_step > to ? to : from + max_step)
205 : (from - max_step < to ? to : from - max_step);
206}
207
213inline vec2 move_toward(vec2 from, vec2 to, f32 max_step) {
214 const vec2 d = to - from;
215 const f32 len = length(d);
216 return len <= max_step || len == 0.0f ? to : from + d / len * max_step;
217}
218
222constexpr vec2 rect_center(rect r) { return r.pos + r.size * 0.5f; }
223
228constexpr rect rect_from_center(vec2 center, vec2 size) {
229 return {center - size * 0.5f, size};
230}
231
233namespace colors {
234inline constexpr rgba white{1.0f, 1.0f, 1.0f, 1.0f};
235inline constexpr rgba black{0.0f, 0.0f, 0.0f, 1.0f};
236inline constexpr rgba red{0.9f, 0.16f, 0.22f, 1.0f};
237inline constexpr rgba green{0.0f, 0.89f, 0.19f, 1.0f};
238inline constexpr rgba blue{0.0f, 0.47f, 0.95f, 1.0f};
239inline constexpr rgba yellow{0.99f, 0.98f, 0.0f, 1.0f};
240inline constexpr rgba gray{0.51f, 0.51f, 0.51f, 1.0f};
241inline constexpr rgba transparent{0.0f, 0.0f, 0.0f, 0.0f};
242} // namespace colors
243
244} // namespace njin
constexpr f32 length_sq(vec2 v)
Bình phương độ dài.
Definition _math.h:58
constexpr f32 clamp(f32 v, f32 lo, f32 hi)
Giới hạn v trong khoảng [lo, hi].
Definition _math.h:165
f32 distance(vec2 a, vec2 b)
Khoảng cách giữa hai điểm.
Definition _math.h:69
constexpr f32 move_toward(f32 from, f32 to, f32 max_step)
Di chuyển from về phía to một đoạn tối đa max_step, không vượt quá.
Definition _math.h:203
vec2 rotate(vec2 v, f32 degrees)
Xoay vector theo chiều kim đồng hồ trên màn hình (trục y hướng xuống).
Definition _math.h:83
constexpr vec3 lerp(vec3 a, vec3 b, f32 t)
Nội suy tuyến tính giữa hai vector.
Definition _math.h:158
constexpr f32 dot(vec2 a, vec2 b)
Tích vô hướng.
Definition _math.h:47
constexpr vec2 rect_center(rect r)
Tâm của hình chữ nhật.
Definition _math.h:222
f32 length(vec2 v)
Độ dài.
Definition _math.h:63
constexpr f32 pi
Số pi.
Definition _math.h:10
constexpr f32 cross(vec2 a, vec2 b)
Tích có hướng 2D (thành phần z của tích có hướng 3D).
Definition _math.h:53
vec2 from_angle(f32 degrees)
Vector độ dài 1 theo một góc, 0 độ là hướng sang phải, tăng theo chiều kim đồng hồ trên màn hình.
Definition _math.h:94
f32 angle_of(vec2 v)
Góc của vector, cùng quy ước với from_angle().
Definition _math.h:99
vec2 normalize(vec2 v)
Vector cùng hướng, độ dài 1.
Definition _math.h:74
constexpr rect rect_from_center(vec2 center, vec2 size)
Tạo hình chữ nhật từ tâm và kích thước.
Definition _math.h:228
float f32
Số thực 32 bit.
Definition _types.h:28
Các màu hay dùng.
Definition _math.h:233
constexpr rgba yellow
Vàng.
Definition _math.h:239
constexpr rgba white
Trắng.
Definition _math.h:234
constexpr rgba red
Đỏ.
Definition _math.h:236
constexpr rgba black
Đen.
Definition _math.h:235
constexpr rgba green
Xanh lá.
Definition _math.h:237
constexpr rgba transparent
Trong suốt.
Definition _math.h:241
constexpr rgba blue
Xanh dương.
Definition _math.h:238
constexpr rgba gray
Xám.
Definition _math.h:240
Hình tròn.
Definition _math.h:19
vec2 center
Tâm.
Definition _math.h:20
f32 radius
Bán kính.
Definition _math.h:21
Hình chữ nhật thẳng trục: pos là góc trên trái, size là rộng và cao.
Definition _math.h:13
vec2 pos
Góc trên trái.
Definition _math.h:14
vec2 size
Chiều rộng (x) và chiều cao (y).
Definition _math.h:15
Màu gồm đỏ, xanh lá, xanh dương và alpha.
Definition _types.h:64
f32 r
Đỏ.
Definition _types.h:65
f32 a
Độ đục (0 = trong suốt, 1 = đục hoàn toàn).
Definition _types.h:68
f32 g
Xanh lá.
Definition _types.h:66
f32 b
Xanh dương.
Definition _types.h:67
Vector 2 chiều. Dùng cho vị trí, kích thước, độ dời.
Definition _types.h:39
f32 y
Thành phần dọc (trong không gian màn hình, y tăng khi đi xuống).
Definition _types.h:41
f32 x
Thành phần ngang.
Definition _types.h:40
Vector 3 chiều.
Definition _types.h:47
f32 x
Thành phần thứ nhất.
Definition _types.h:48
f32 z
Thành phần thứ ba.
Definition _types.h:50
f32 y
Thành phần thứ hai.
Definition _types.h:49