Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
geometry.h File Reference

Detailed Description

Design Philosophy

These utility functions are can be used for geometric math.

Much of this file was adapted from https://www.jeffreythompson.org/collision-detection/

Usage

No initialization or deinitialization is required. Each function will not modify the given shapes. See the functions below for what is provided and how to use them.

Example

circle_t circ = {
.pos.x = 2,
.pos.y = 5,
.radius = 8,
};
rectangle_t rect = {
.pos.x = -2,
.pos.y = -4,
.width = 6,
.height = 9,
};
if (circleRectIntersection(circ, rect, NULL))
{
printf("Shapes intersect\n");
}
else
{
printf("Shapes do not intersect\n");
}
bool circleRectIntersection(circle_t circle, rectangle_t rect, vec_t *collisionVec)
Check if a circle and a rectangle intersect.
Definition geometry.c:116
vec_t pos
The position of the center of the circle.
Definition geometry.h:51
Signed integer representation of a circle.
Definition geometry.h:50
Signed integer representation of a rectangle.
Definition geometry.h:59
int32_t x
The signed integer X component of the vector.
Definition vector2d.h:38

Go to the source code of this file.

Data Structures

struct  circle_t
 Signed integer representation of a circle. More...
 
struct  rectangle_t
 Signed integer representation of a rectangle. More...
 
struct  line_t
 Signed integer representation of a line segment. More...
 
struct  arrow_t
 

Functions

bool circleCircleIntersection (circle_t circle1, circle_t circle2, vec_t *collisionVec)
 Check if two circles intersect.
 
bool circlePointIntersection (circle_t circle, vec_t point, vec_t *collisionVec)
 Check if a point and a circle intersect.
 
bool circleRectIntersection (circle_t circle, rectangle_t rect, vec_t *collisionVec)
 Check if a circle and a rectangle intersect.
 
bool circleLineIntersection (circle_t circle, line_t line, vec_t *collisionVec)
 Check if a circle and a line intersect.
 
bool rectRectIntersection (rectangle_t rect1, rectangle_t rect2, vec_t *collisionVec)
 Check if two rectangles intersect.
 
bool rectLineIntersection (rectangle_t rect, line_t line, vec_t *collisionVec)
 Check if a line intersects with a rectangle.
 
bool lineLineIntersection (line_t line1, line_t line2)
 Check if two lines intersect.
 
arrow_t initArrow (vec_t base, vec_t tip, int32_t wingLen)
 Initialize an arrow pointing from base to tip.
 

Data Structure Documentation

◆ circle_t

struct circle_t
Data Fields
vec_t pos The position of the center of the circle.
int32_t radius The radius of the circle.

◆ rectangle_t

struct rectangle_t
Data Fields
vec_t pos The position the top left corner of the rectangle.
int32_t width The width of the rectangle.
int32_t height The height of the rectangle.

◆ line_t

struct line_t
Data Fields
vec_t p1 One end point of the line segment.
vec_t p2 The other end point of the line segment.

◆ arrow_t

struct arrow_t
Data Fields
vec_t base The base of the arrow.
vec_t tip The tip of the arrow.
vec_t wing1 The base of one of the arrowhead wings.
vec_t wing2 The base of one of the other arrowhead wing.

Function Documentation

◆ circleCircleIntersection()

bool circleCircleIntersection ( circle_t circle1,
circle_t circle2,
vec_t * collisionVec )

Check if two circles intersect.

Parameters
circle1[IN] One circle to check for intersection
circle2[IN] The other circle to check for intersection
collisionVec[OUT] A vector pointing from the second circle to the first in the direction of the collision. May be NULL.
Returns
true if the circles intersect or touch, false if they do not

◆ circlePointIntersection()

bool circlePointIntersection ( circle_t circle,
vec_t point,
vec_t * collisionVec )

Check if a point and a circle intersect.

Parameters
circle[IN] A circle to check for intersection
point[IN] A point to check for intersection
collisionVec[OUT] A vector pointing from the point to the circle in the direction of the collision. May be NULL.
Returns
true if the point intersects or touches the circle, false if it doesn't

◆ circleRectIntersection()

bool circleRectIntersection ( circle_t circle,
rectangle_t rect,
vec_t * collisionVec )

Check if a circle and a rectangle intersect.

Adapted from https://www.jeffreythompson.org/collision-detection/circle-rect.php

Parameters
circle[IN] The circle to check for intersection
rect[IN] The rectangle to check for intersection
collisionVec[OUT] A vector pointing from the rectangle to the circle in the direction of the collision. May be NULL.
Returns
true if the shapes intersect or touch, false if they do not

◆ circleLineIntersection()

bool circleLineIntersection ( circle_t circle,
line_t line,
vec_t * collisionVec )

Check if a circle and a line intersect.

Adapted from https://www.jeffreythompson.org/collision-detection/line-circle.php

Parameters
circle[IN] A circle to test intersection
line[IN] A line to test intersection
collisionVec[OUT] A vector pointing from the line to the circle in the direction of the collision. This may be NULL.
Returns
true if the circle intersects the line, false if it doesn't

◆ rectRectIntersection()

bool rectRectIntersection ( rectangle_t rect1,
rectangle_t rect2,
vec_t * collisionVec )

Check if two rectangles intersect.

Adapted from https://www.jeffreythompson.org/collision-detection/rect-rect.php

Parameters
rect1[IN] One rectangle to check for intersection
rect2[IN] The other rectangle to check for intersection
collisionVec[OUT] A vector pointing from the second rectangle to the first in the direction of the collision. May be NULL.
Returns
true if the rectangles intersect or touch, false if they do not

◆ rectLineIntersection()

bool rectLineIntersection ( rectangle_t rect,
line_t line,
vec_t * collisionVec )

Check if a line intersects with a rectangle.

Adapted from https://www.jeffreythompson.org/collision-detection/line-rect.php

Parameters
rect[IN] A rectangle to check for intersection
line[IN] A line to check for intersection
collisionVec[OUT] A vector pointing from the rectangle to the line in the direction of the collision. May be NULL.
Returns
true if the line intersects the rectangle, false if it doesn't

◆ lineLineIntersection()

bool lineLineIntersection ( line_t line1,
line_t line2 )

Check if two lines intersect.

Adapted from https://www.jeffreythompson.org/collision-detection/line-line.php

Parameters
line1[IN] One line to check for intersection
line2[IN] Another line to check for intersection
Returns
true if the lines intersect, false if they do not

◆ initArrow()

arrow_t initArrow ( vec_t base,
vec_t tip,
int32_t wingLen )

Initialize an arrow pointing from base to tip.

Parameters
baseThe point of the base of the arrow
tipThe point of the tip of the arrow
wingLenThe length of the wings of the arrowhead
Returns
The initialized arrow