Gama C Library
Gama C API Documentation
position.h
Go to the documentation of this file.
1#pragma once
2
3#include "_math.h"
4
5/**
6 * @brief Represents a 2D position or vector.
7 */
8typedef struct {
9 double x, y; /**< The X and Y coordinates. */
10} gmPos;
11
12/**
13 * @brief Creates a new `gmPos` struct with the given coordinates.
14 * @param x The X-coordinate.
15 * @param y The Y-coordinate.
16 * @return A new `gmPos` instance.
17 */
18static inline gmPos gmpos(double x, double y) {
19 gmPos p = {x, y};
20 return p;
21}
22
23/**
24 * @brief Sets the coordinates of an existing `gmPos` struct.
25 * @param p A pointer to the `gmPos` struct to modify.
26 * @param x The new X-coordinate.
27 * @param y The new Y-coordinate.
28 */
29static inline void gm_pos_set(gmPos *p, double x, double y) {
30 p->x = x;
31 p->y = y;
32}
33
34/**
35 * @brief Resets the coordinates of a `gmPos` struct to (0, 0).
36 * @param p A pointer to the `gmPos` struct to reset.
37 */
38static inline void gm_pos_reset(gmPos *p) {
39 p->x = 0;
40 p->y = 0;
41}
42
43/**
44 * @brief Calculates the magnitude (length) of a `gmPos` vector.
45 * @param p The `gmPos` vector.
46 * @return The magnitude of the vector.
47 */
48static inline double gm_pos_magniture(gmPos p) {
49 return sqrt(p.x * p.x + p.y * p.y);
50}
51
52/**
53 * @brief Calculates the Euclidean distance between two `gmPos` points.
54 * @param a The first `gmPos` point.
55 * @param b The second `gmPos` point.
56 * @return The distance between the two points.
57 */
58static inline double gm_pos_distance(gmPos a, gmPos b) {
59 return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
60}
61
62/**
63 * Calculate the area of a triangle formed by 3 2D points using the cross
64 * product formula
65 *
66 * @param a First vertex of the triangle
67 * @param b Second vertex of the triangle
68 * @param c Third vertex of the triangle
69 * @return The area of the triangle (always positive)
70 */
72 double ab_x = b.x - a.x;
73 double ab_y = b.y - a.y;
74 double ac_x = c.x - a.x;
75 double ac_y = c.y - a.y;
76 return 0.5 * fabs(ab_x * ac_y - ab_y * ac_x);
77}
double pow(double base, double exp)
Calculates the base raised to the power of the exponent (base^exp).
Definition math.h:358
double fabs(double x)
Calculates the absolute value of a double.
Definition math.h:369
double sqrt(double x)
Calculates the square root of x.
Definition math.h:339
double gm_triangle_area(gmPos a, gmPos b, gmPos c)
Definition position.h:71
Represents a 2D position or vector.
Definition position.h:8
double x
Definition position.h:9
double y
Definition position.h:9