MCPcopy Create free account
hub / github.com/TheForceEngine/TheForceEngine / lineSegmentIntersect

Function lineSegmentIntersect

TheForceEngine/TFE_System/math.cpp:149–163  ·  view source on GitHub ↗

Returns true if the segment (a0, a1) intersects the line segment (b0, b1) intersection is: I = a0 + s*(a1-a0) = b0 + t*(b1 - b0) Returns false if the intersection occurs between the lines but not the segments.

Source from the content-addressed store, hash-verified

147 // intersection is: I = a0 + s*(a1-a0) = b0 + t*(b1 - b0)
148 // Returns false if the intersection occurs between the lines but not the segments.
149 bool lineSegmentIntersect(const Vec2f* a0, const Vec2f* a1, const Vec2f* b0, const Vec2f* b1, f32* s, f32* t)
150 {
151 const Vec2f u = { a1->x - a0->x, a1->z - a0->z };
152 const Vec2f v = { b1->x - b0->x, b1->z - b0->z };
153 const Vec2f w = { a0->x - b0->x, a0->z - b0->z };
154
155 f32 det = v.x*u.z - v.z*u.x;
156 if (fabsf(det) < FLT_EPSILON) { return false; }
157 det = 1.0f / det;
158
159 *s = (v.z*w.x - v.x*w.z) * det;
160 *t = -(u.x*w.z - u.z*w.x) * det;
161
162 return (*s) > -FLT_EPSILON && (*s) < 1.0f + FLT_EPSILON && (*t) > -FLT_EPSILON && (*t) < 1.0f + FLT_EPSILON;
163 }
164
165 // line: p0, p1; plane: planeHeight + planeDir (+/-Y)
166 // returns true if the intersection occurs within the segment

Callers 3

constraintSplitFunction · 0.85
insertConstraintFunction · 0.85
traceRayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected