diff options
| author | Victor Gallet <[email protected]> | 2020-12-21 21:04:02 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-12-21 21:04:02 +0100 |
| commit | 668b3e4cfb8a11d46f6492e263463fca899faec2 (patch) | |
| tree | 6aecb37585676d3ec441d0953cd4a33c41ebff8d /src | |
| parent | 51e75be9d1ffeaf6d47d3670017cca36f4785a17 (diff) | |
| download | raylib-668b3e4cfb8a11d46f6492e263463fca899faec2.tar.gz raylib-668b3e4cfb8a11d46f6492e263463fca899faec2.zip | |
[Math Feature]: Add two functions for Vector2 to know if two lines intersect and two segments intersect (#1466)
* Add a function to know if two lines intersect each other and if yes, get the intersection point
* Remove indents
* Rework the declaration of the 'Vector2LineIntersect' function, and add the 'Vector2SegmentIntersect' function
* Remove bad indents
* Fix compilation issues
* Fix compilation error
* Fix compilation error
* Replace keyword '_Bool' by 'bool'
Diffstat (limited to 'src')
| -rw-r--r-- | src/raymath.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/raymath.h b/src/raymath.h index 7f05ea4e..4ab5468a 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -336,6 +336,45 @@ RMDEF Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance) return result; } +// Get the intersection point of two lines A and B defined by A(p1, p2) and B(p3, p4), return true if it exists, else false +RMDEF bool Vector2LineIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection) +{ + const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y); + + if (div == 0.f) return false; + + const float coeff = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / div; + + if (pointIntersection) + { + pointIntersection->x = p1.x + (p2.x - p1.x) * coeff; + pointIntersection->y = p1.y + (p2.y - p1.y) * coeff; + } + return true; +} + +// Get the intersection point of two segments A and B defined by A(p1, p2) and B(P3, p4), return true if it exists, else false +RMDEF bool Vector2SegmentIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection) +{ + const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y); + + if (div == 0.f) return false; + + const float xi = ((p3.x - p3.x)*(p1.x * p2.y - p1.y * p2.x) - (p1.x - p2.x)*(p3.x * p4.y - p3.y * p4.x)) / div; + const float yi = ((p3.y - p4.y)*(p1.x * p2.y - p1.y * p2.x) - (p1.y - p2.y)*(p3.x * p4.y - p3.y * p4.x)) / div; + + if (xi < fminf(p1.x, p2.x) || xi > fmaxf(p1.x, p2.x)) return false; + if (xi < fminf(p3.x, p4.x) || xi > fmaxf(p3.x, p4.x)) return false; + if (yi < fminf(p1.y, p2.y) || yi > fmaxf(p1.y, p2.y)) return false; + if (yi < fminf(p3.y, p4.y) || yi > fmaxf(p3.y, p4.y)) return false; + if (pointIntersection) + { + pointIntersection->x = xi; + pointIntersection->y = yi; + } + return true; +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Vector3 math //---------------------------------------------------------------------------------- |
