added new functions to rect such as CenterOn and intersections for ARC_FRect

This commit is contained in:
herbglitch 2023-06-24 02:03:55 -06:00
parent 3565a5cf15
commit 67762e8ab2
2 changed files with 77 additions and 4 deletions

View file

@ -2,6 +2,7 @@
#define ARC_MATH_RECT_H_ #define ARC_MATH_RECT_H_
#include <stdint.h> #include <stdint.h>
#include "point.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -44,6 +45,15 @@ void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds);
*/ */
void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds); void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds);
/**
* @brief casts Rect to FRect
*
* @param rect ARC_Rect to be casted
*
* @return ARC_FRect
*/
ARC_FRect ARC_Rect_CastToFRect(ARC_Rect *rect);
/** /**
* @brief casts FRect to Rect * @brief casts FRect to Rect
* *
@ -63,6 +73,36 @@ ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect);
*/ */
int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2); int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2);
/**
* @brief checks if two ARC_FRects intersect
*
* @param rect1 ARC_FRect that will be checked against rect2
* @param rect2 ARC_FRect that will be checked against rect1
*
* @return 1 if they intersect, 0 if they don't intersect
*/
int32_t ARC_FRect_Intersects(ARC_FRect *rect1, ARC_FRect *rect2);
/**
* @brief checks if ARC_Rect intersects with point
*
* @param rect ARC_Rect that will be checked against point
* @param point ARC_Point that will be checked against rect
*
* @return 1 if they intersect, 0 if they don't intersect
*/
int32_t ARC_Rect_IntersectsPoint(ARC_Rect *rect, ARC_Point *point);
/**
* @brief checks if ARC_FRect intersects with point
*
* @param rect ARC_FRect that will be checked against point
* @param point ARC_Point that will be checked against rect
*
* @return 1 if they intersect, 0 if they don't intersect
*/
int32_t ARC_FRect_IntersectsPoint(ARC_FRect *rect, ARC_Point *point);
/** /**
* @brief checks if ARC_Rect intersects a line * @brief checks if ARC_Rect intersects a line
* *

View file

@ -4,13 +4,22 @@
// #include <SDL.h> // #include <SDL.h>
void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds){ void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds){
rect->x = ((bounds->x + bounds->w) / 2) - (rect->w / 2); rect->x = (bounds->x + (bounds->w / 2)) - (rect->w / 2);
rect->y = ((bounds->y + bounds->h) / 2) - (rect->h / 2); rect->y = (bounds->y + (bounds->h / 2)) - (rect->h / 2);
} }
void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds){ void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds){
rect->x = ((bounds->x + bounds->w) / 2.0f) - (rect->w / 2.0f); rect->x = (bounds->x + (bounds->w / 2.0f)) - (rect->w / 2.0f);
rect->y = ((bounds->y + bounds->h) / 2.0f) - (rect->h / 2.0f); rect->y = (bounds->y + (bounds->h / 2.0f)) - (rect->h / 2.0f);
}
ARC_FRect ARC_Rect_CastToFRect(ARC_Rect *rect){
return (ARC_FRect){
.x = (float)rect->x,
.y = (float)rect->y,
.w = (float)rect->w,
.h = (float)rect->h,
};
} }
ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect){ ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect){
@ -30,6 +39,30 @@ int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2){
return 0; return 0;
} }
int32_t ARC_FRect_Intersects(ARC_FRect *rect1, ARC_FRect *rect2){
if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x &&
rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y){
return 1;
}
return 0;
}
int32_t ARC_Rect_IntersectsPoint(ARC_Rect *rect, ARC_Point *point){
if(rect->x <= point->x && rect->x + rect->w >= point->x &&
rect->y <= point->y && rect->y + rect->h >= point->y){
return 1;
}
return 0;
}
int32_t ARC_FRect_IntersectsPoint(ARC_FRect *rect, ARC_Point *point){
if(rect->x <= point->x && rect->x + rect->w >= point->x &&
rect->y <= point->y && rect->y + rect->h >= point->y){
return 1;
}
return 0;
}
int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2){ int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2){
//TODO: Replace soon //TODO: Replace soon
// return SDL_IntersectRectAndLine((SDL_Rect *) rect, x1, y1, x2, y2); // return SDL_IntersectRectAndLine((SDL_Rect *) rect, x1, y1, x2, y2);