2D_Engine_Casio/src/vector2d.h

76 lines
2.4 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* All of the functions declared in this file and *
* defined in vector2d.c are from the SDL2 source code. *
* They are just renamed. *
* => https://github.com/libsdl-org/SDL *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <gint/defs/types.h>
/* vector2d_t: A point in space
This struct is the base for storing 2D positions.
@x X pos of the vector
@y Y pos of the vector */
typedef struct vector2d_t {
float x, y;
} vector2d_t;
/* vector2d_t: A point in space
This struct is the base for storing 2D positions.
@x X pos of the rectangle
@y Y pos of the rectangle
@w Width of the rectangle
@h Height of the rectangle */
typedef struct rect_t {
float x, y;
float w, h;
} rect_t;
#define EPSILON 0.000001f
__attribute__((const)) float fabsf(float x);
#define is_equal_to_zero(X) (fabsf(X) <= EPSILON)
#define is_not_zero(X) (fabsf(X) > EPSILON)
/* point_in_rect(): Verify if a point is in a rectangle
This function is useful for verify if a point is in a rectangle.
@P A Vector2d
@R A rectangle
Return true if the condition is validated else false. */
#define point_in_rect(P, R) (((P)->x >= (R)->x) && ((P)->x < ((R)->x + (R)->w)) && \
((P)->y >= (R)->y) && ((P)->y < ((R)->y + (R)->h)))
/* rect_empty(): Verify if a rectangle is empty
This function is useful for verify if a rectangle exists.
@R A rectangle
Return true if the condition is validated else false. */
#define rect_empty(R) ((!(R)) || (is_equal_to_zero((R)->w)) || (is_equal_to_zero((R)->h)))
/* has_intersection(): Verify if there is a intersction between two rectangles
This function is useful for verify intersection between two rectangles.
@A A rectangle
@B Another rectangle
Return true if the condition is validated else false. */
bool has_intersection(const rect_t *, const rect_t *);
/* intersect_rect(): Like has_intersection but has a result rectangle
This function is useful for verify intersection between two rectangles
and get the intersection rectangle.
@A A rectangle
@B Another rectangle
@result The intersection rectangle
Return true if the condition is validated else false. */
bool intersect_rect(const rect_t *, const rect_t *, rect_t *);
#endif // VECTOR2D_H