-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.h
More file actions
84 lines (67 loc) · 1.97 KB
/
shape.h
File metadata and controls
84 lines (67 loc) · 1.97 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
#ifndef SHAPE_H
#define SHAPE_H
#include <stdbool.h>
#include <math.h>
#include <raylib.h>
enum Shape {
S_SQUARE, S_TRIANGLE, S_CIRCLE
};
typedef int Shape;
typedef struct Triangle {
Vector2 p1, p2, p3;
} Triangle;
typedef struct Circle {
Vector2 center;
float radius;
} Circle;
/* ... */
static Triangle triangle(Vector2 p1, Vector2 p2, Vector2 p3) {
return (Triangle) { p1, p2, p3 };
}
/* Calculates the area of any triangle. */
static float triangle_area(Triangle tr) {
return 0.5f * fabs(
tr.p1.x * (tr.p2.y - tr.p3.y) +
tr.p2.x * (tr.p3.y - tr.p1.y) +
tr.p3.x * (tr.p1.y - tr.p2.y)
);
}
/* Returns true if `point` is within `tr`. */
static bool triangle_point(Triangle tr, Vector2 point) {
float
areaABC = triangle_area(tr),
areaPAB = triangle_area(triangle(point, tr.p1, tr.p2)),
areaPBC = triangle_area(triangle(point, tr.p2, tr.p3)),
areaPCA = triangle_area(triangle(point, tr.p3, tr.p1));
return (areaABC == areaPAB + areaPBC + areaPCA);
}
/* ... */
static Circle circle(Vector2 center, float radius) {
return (Circle) { .center = center, .radius = radius };
}
/* Calculates the area of a circle. */
static float circle_area(Circle cl) {
float t = PI * (cl.radius);
return t * t;
}
/* Returns true if `point` is within `cl`. */
static bool circle_point(Circle cl, Vector2 point) {
float dist_p2 =
(point.x - cl.center.x) * (point.x - cl.center.x)
+ (point.y - cl.center.y) * (point.y - cl.center.y);
return dist_p2 <= cl.radius * cl.radius;
}
// Vector2, Vector3, etc.
static float vec2_cross(Vector2 v1, Vector2 v2) {
return v1.x * v2.y - v1.y * v2.x;
}
static Vector2 vec2_add(Vector2 v1, Vector2 v2) {
return (Vector2) { v1.x + v2.x, v1.y + v2.y };
}
static Vector2 vec2_sub(Vector2 v1, Vector2 v2) {
return (Vector2) { v1.x - v2.x, v1.y - v2.y };
}
static float vec2_dot(Vector2 v1, Vector2 v2) {
return v1.x * v2.x + v1.y * v2.y;
}
#endif