Phyzix
Loading...
Searching...
No Matches
BoundaryCalculations.h
1//
2// Created by shams on 9/12/24.
3//
4
5#ifndef PHYZIX_BOUNDARYCALCULATIONS_H
6#define PHYZIX_BOUNDARYCALCULATIONS_H
7
13public:
22 static BoundaryIntersectionResult intersects(LineSegmentBoundary * line, CircleBoundary * circle, bool flip = false) {
23 double A = line->y2 - line->y1;
24 double B = line->x1 - line->x2;
25 double C = line->x2 * line->y1 - line->x1 * line->y2;
26
27 if (A == 0) {
28 // horizontal line
29 if (Math::fabs(circle->y - (-C / B)) <= circle->r) {
30 if (Math::min(line->x1, line->x2) <= circle->x && circle->x <= Math::max(line->x1, line->x2)) {
31 // Intersects
32 if (flip)
33 return {true, Vector(0, circle->y - (-C / B)).getNormal(), circle, line};
34 else
35 return {true, Vector(0, circle->y - (-C / B)).getNormal(), line, circle};
36 }
37 }
38 }
39
40 if (B == 0) {
41 // vertical line
42 if (Math::fabs(circle->x - (-C / A)) <= circle->r) {
43 if (Math::min(line->y1, line->x2) <= circle->y && circle->y <= Math::max(line->y1, line->y2)) {
44 // Intersects
45 if (flip)
46 return {true, Vector(circle->x - (-C / A), 0).getNormal(), circle, line};
47 else
48 return {true, Vector(circle->x - (-C / A), 0).getNormal(), line, circle};
49 }
50 }
51 }
52
53 float D = C / B - circle->y;
54 float a = 1 + A*A / (B * B);
55 float b = -2 * circle->x + 2 * A * D / B;
56 float c = circle->x * circle->x + D * D - circle->r * circle->r;
57
58 float discriminant = b * b - 4 * a * c;
59 if (discriminant >= 0) {
60 // Intersects
61 float sqrtDiscriminant = Math::sqrt(discriminant);
62 float x1 = (-b + sqrtDiscriminant) / (2 * a);
63 float x2 = (-b - sqrtDiscriminant) / (2 * a);
64 float y1 = -A/B * x1 - C/B;
65 float y2 = -A/B * x2 - C/B;
66
67 if ((Math::min(line->x1, line->x2) <= x1 && x1 <= Math::max(line->x1, line->x2) &&
68 Math::min(line->y1, line->y2) <= y1 && y1 <= Math::max(line->y1, line->y2)) ||
69 (Math::min(line->x1, line->x2) <= x2 && x2 <= Math::max(line->x1, line->x2) &&
70 Math::min(line->y1, line->y2) <= y2 && y2 <= Math::max(line->y1, line->y2))
71 ) {
72 if (flip)
73 return {true, Vector(circle->x - (x1 + x2) / 2, circle->y - (y1 + y2) / 2).getNormal(), circle, line};
74 else
75 return {true, Vector(circle->x - (x1 + x2) / 2, circle->y - (y1 + y2) / 2).getNormal(), line, circle};
76 }
77 }
78
79 return {};
80 }
81};
82
83#endif //PHYZIX_BOUNDARYCALCULATIONS_H
A class that contains static methods for common calculating intersections between boundaries.
Definition BoundaryCalculations.h:12
static BoundaryIntersectionResult intersects(LineSegmentBoundary *line, CircleBoundary *circle, bool flip=false)
Check if a LineSegmentBoundary and a CircleBoundary intersect or not.
Definition BoundaryCalculations.h:22
Definition CircleBoundary.h:12
Definition LineSegmentBoundary.h:11
static float max(float a, float b)
get maximum float value
Definition Math.h:62
static float fabs(float n)
get the floating-point absolute value
Definition Math.h:81
static float sqrt(float n)
get the square root of the number
Definition Math.h:99
static float min(float a, float b)
get minimum float value
Definition Math.h:42
Definition Vector.h:12
A struct to hold the result of a possible intersection between two boundaries.
Definition Boundary.h:30