-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.c
More file actions
103 lines (83 loc) · 3.02 KB
/
math.c
File metadata and controls
103 lines (83 loc) · 3.02 KB
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
// SPDX-License-Identifier: MIT
// Copyright (C) 2026 p1k0chu
#include "my_math.h"
#include <SDL3/SDL_stdinc.h>
#include <assert.h>
#include <math.h>
double Vec2d_magnitude(const Vec2d *this) {
return SDL_sqrt(SDL_pow(this->x, 2) + SDL_pow(this->y, 2));
}
Vec2d Vec2d_get_normal(const Vec2d *this) {
const double direction = SDL_atan2(this->y, this->x) + SDL_PI_D / 2;
return (Vec2d){SDL_cos(direction), SDL_sin(direction)};
}
Vec2d Vec2d_project_on(const Vec2d *this, const Vec2d *other) {
const double ab = dot_product(this, other);
const double bb = dot_product(other, other);
const double i = ab / bb;
return (Vec2d){other->x * i, other->y * i};
}
Vec2d Vec2d_rotate(const Vec2d *this, double rotation_radian) {
const double c = SDL_cos(rotation_radian);
const double s = SDL_sin(rotation_radian);
return (Vec2d){
c * this->x - this->y * s,
this->x * s + this->y * c,
};
}
Vec2d Vec2d_add(const Vec2d *a, const Vec2d *b) {
return (Vec2d){a->x + b->x, a->y + b->y};
}
double dot_product(const Vec2d *left, const Vec2d *right) {
return left->x * right->x + left->y * right->y;
}
double Vec2d_angle2(const Vec2d *a, const Vec2d *b) {
return SDL_acos(dot_product(a, b) / Vec2d_magnitude(a) / Vec2d_magnitude(b));
}
double Vec2d_scalar_projection(const Vec2d *this, const Vec2d *onto) {
const double angle = Vec2d_angle2(this, onto);
// if angle is nan, one of the vectors is a zero vector,
// thus scalar projection is always 0
if (SDL_isnan(angle))
return 0;
assert(angle <= SDL_PI_D);
const Vec2d proj = Vec2d_project_on(this, onto);
const double sign = (angle <= SDL_PI_D / 2) ? 1 : -1;
return sign * Vec2d_magnitude(&proj);
}
int polygons_collide(const Vec2d *const normals,
const size_t normals_len,
const Vec2d *const dots_poly1,
const size_t dots_poly1_len,
const Vec2d *const dots_poly2,
const size_t dots_poly2_len) {
size_t i, j;
for (i = 0; i < normals_len; ++i) {
const Vec2d *normal = normals + i;
double poly1_min = INFINITY;
double poly1_max = -INFINITY;
for (j = 0; j < dots_poly1_len; ++j) {
const Vec2d *point = dots_poly1 + j;
const double m = Vec2d_scalar_projection(point, normal);
if (m > poly1_max)
poly1_max = m;
if (m < poly1_min)
poly1_min = m;
}
double poly2_min = INFINITY;
double poly2_max = -INFINITY;
for (j = 0; j < dots_poly2_len; ++j) {
const Vec2d *point = dots_poly2 + j;
const double m = Vec2d_scalar_projection(point, normal);
if (m > poly2_max)
poly2_max = m;
if (m < poly2_min)
poly2_min = m;
}
if ((poly1_min < poly2_min && poly1_max < poly2_min) ||
(poly1_min > poly2_max && poly1_max > poly2_max)) {
return 0;
}
}
return 1;
}