JacksEscape/Types.c

71 lines
2.0 KiB
C
Raw Normal View History

2024-02-29 11:22:52 +08:00
#include "Types.h"
static inline double dmin(double x, double y) {
return x < y ? x : y;
}
static inline double dmax(double x, double y) {
return x > y ? x : y;
}
Vec2 vec2_Add(Vec2 x, Vec2 y) {
Vec2 result = {
.x = x.x + y.x,
.y = x.y + y.y};
return result;
}
Vec2 vec2_Scale(Vec2 v, double scale) {
Vec2 result = {
.x = v.x * scale,
.y = v.y * scale};
return result;
}
bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection) {
// Compute the min and max of the first rectangle on both axes
const double r1MinX = dmin(x.lefttop.x, x.lefttop.x + x.size.x);
const double r1MaxX = dmax(x.lefttop.x, x.lefttop.x + x.size.x);
const double r1MinY = dmin(x.lefttop.y, x.lefttop.x + x.size.x);
const double r1MaxY = dmax(x.lefttop.y, x.lefttop.x + x.size.x);
// Compute the min and max of the second rectangle on both axes
const double r2MinX = dmin(y.lefttop.x, y.lefttop.x + y.size.x);
const double r2MaxX = dmax(y.lefttop.x, y.lefttop.x + y.size.x);
const double r2MinY = dmin(y.lefttop.y, y.lefttop.y + y.size.y);
const double r2MaxY = dmax(y.lefttop.y, y.lefttop.y + y.size.y);
// Compute the intersection boundaries
const double interLeft = dmax(r1MinX, r2MinX);
const double interTop = dmax(r1MinY, r2MinY);
const double interRight = dmin(r1MaxX, r2MaxX);
const double interBottom = dmin(r1MaxY, r2MaxY);
// If the intersection is valid (positive non zero area), then there is an intersection
if ((interLeft < interRight) && (interTop < interBottom)) {
if (out_intersection) {
out_intersection->lefttop.x = interLeft;
out_intersection->lefttop.y = interTop;
out_intersection->size.x = interRight - interLeft;
out_intersection->size.y = interBottom - interTop;
}
return true;
} else {
return false;
}
}
2024-02-29 16:51:46 +08:00
Box2 box2_Offset(Box2 box, Vec2 offset) {
box.lefttop = vec2_Add(box.lefttop, offset);
return box;
}
Box2 box2_OffsetX(Box2 box, double offsetX) {
box.lefttop.x += offsetX;
return box;
}
Box2 box2_OffsetY(Box2 box, double offsetY) {
box.lefttop.y += offsetY;
return box;
}