diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-04-09 23:36:55 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-04-09 23:36:55 +0300 |
commit | 80ead50951c8b0d2c60c9fd57b8a4c943634b084 (patch) | |
tree | 635d8479b7e862d3cd7b3bc69b0cb64e31c38c2e /src/common.h | |
parent | fc027f7d214028086178a7328926c9b14ead464d (diff) |
things...
Diffstat (limited to 'src/common.h')
-rw-r--r-- | src/common.h | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/common.h b/src/common.h index d9a222f..4d0df29 100644 --- a/src/common.h +++ b/src/common.h @@ -1,14 +1,26 @@ #ifndef COMMON_H #define COMMON_H -#include <stdint.h> // for size_t +#include <stddef.h> // for size_t typedef unsigned int uint; -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#define max(a, b) (((a) > (b)) ? (a) : (b)) -#define clamp(x, l, h) (min(max((x), (l)), (h))) -#define cross(x1, y1, x2, y2) ((x1)*(y2) - (x2)*(y1)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) // choose smaller +#define max(a, b) (((a) > (b)) ? (a) : (b)) // choose bigger +#define clamp(x, l, h) (min(max((x), (l)), (h))) // clamp x between a range +#define vxs(x0,y0, x1,y1) ((x0)*(y1) - (x1)*(y0)) // vector cross product + +// Overlap: Determine whether the two number ranges overlap. +#define overlap(a0,a1,b0,b1) (min(a0,a1) <= max(b0,b1) && min(b0,b1) <= max(a0,a1)) +// IntersectBox: Determine whether two 2D-boxes intersect. +#define intersectbox(x0,y0, x1,y1, x2,y2, x3,y3) (overlap(x0,x1,x2,x3) && overlap(y0,y1,y2,y3)) +// PointSide: Determine which side of a line the point is on. Return value: <0, =0 or >0. +#define pointside(px,py, x0,y0, x1,y1) vxs((x1)-(x0), (y1)-(y0), (px)-(x0), (py)-(y0)) +// Intersect: Calculate the point of intersection between two lines. +#define intersect(x1,y1, x2,y2, x3,y3, x4,y4) ((struct xy) { \ + vxs(vxs(x1,y1, x2,y2), (x1)-(x2), vxs(x3,y3, x4,y4), (x3)-(x4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)), \ + vxs(vxs(x1,y1, x2,y2), (y1)-(y2), vxs(x3,y3, x4,y4), (y3)-(y4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)) }) + struct xy { float x, y; @@ -23,7 +35,7 @@ struct xyz { #define COLOR_RED 0xFF0000 #define COLOR_GREEN 0x00FF00 #define COLOR_BLUE 0x0000FF -#define COLOR_LIGHTWHITE 0xF5F5F5FF +#define COLOR_LIGHTWHITE 0xF5F5F5 #define color(col) (((col)>>16) & 0xFF), (((col)>>8) & 0xFF), ((col) & 0xFF) // macro wrapper for the sdl logging system |