MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / LineIntersectsRect

Method LineIntersectsRect

Source/Engine/Core/Math/CollisionsHelper.cpp:1302–1390  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1300}
1301
1302bool CollisionsHelper::LineIntersectsRect(const Float2& p1, const Float2& p2, const Rectangle& rect)
1303{
1304 /*Float2 c = rect.Location; // Box center-point
1305 Float2 e= rect.Size * 0.5f; // Box halflength extents
1306 Float2 m=(p2+p1)*0.5f; // Segment midpoint
1307 Float2 d=p1-m; // Segment halflength vector
1308 m = m - c; // Translate box and segment to origin
1309
1310 // Try world coordinate axes as separating axes
1311 float adx = Math::Abs(d.X);
1312 if (Math::Abs(m.X) > e.X + adx) return false;
1313 float ady = Math::Abs(d.Y);
1314 if (Math::Abs(m.Y) > e.Y + ady) return false;
1315
1316 // Add in an epsilon term to counteract arithmetic errors when segment is
1317 // (near) parallel to a coordinate axis (see text for detail)
1318 adx += ZeroTolerance; ady += ZeroTolerance;
1319
1320 // Try cross products of segment direction vector with coordinate axes
1321 if (Math::Abs(m.Y * d.X - m.X * d.Y) > e.X * ady + e.Y * adx) return false;
1322 if (Math::Abs(m.X * d.Y - m.Y * d.X) > e.X * ady + e.Y * adx) return false;
1323
1324 // No separating axis found; segment must be overlapping AABB
1325 return true;*/
1326
1327 // TODO: optimize it
1328 const Float2 pA(rect.GetRight(), rect.GetY());
1329 const Float2 pB(rect.GetRight(), rect.GetBottom());
1330 const Float2 pC(rect.GetX(), rect.GetBottom());
1331 return LineIntersectsLine(p1, p2, rect.Location, pA) ||
1332 LineIntersectsLine(p1, p2, pA, pB) ||
1333 LineIntersectsLine(p1, p2, pB, pC) ||
1334 LineIntersectsLine(p1, p2, pC, rect.Location) ||
1335 (rect.Contains(p1) && rect.Contains(p2));
1336
1337 /*float minX = Math::Min(p1.X, p2.X);
1338 float maxX = Math::Max(p1.X, p2.X);
1339 float minY = Math::Min(p1.Y, p2.Y);
1340 float maxY = Math::Max(p1.Y, p2.Y);*/
1341
1342 /*float l = rect.GetLeft();
1343 float r = rect.GetRight();
1344 float t = rect.GetTop();
1345 float b = rect.GetBottom();
1346
1347 // Calculate m and c for the equation for the line (y = mx+c)
1348 float m = (p2.Y - p1.Y) / (p2.X - p1.X);
1349 float c = p1.Y - (m * p1.X);
1350
1351 // if the line is going up from right to left then the top intersect point is on the left
1352 float top_intersection, bottom_intersection;
1353 if (m > 0)
1354 {
1355 top_intersection = (m*l + c);
1356 bottom_intersection = (m*r + c);
1357 }
1358 // otherwise it's on the right
1359 else

Callers

nothing calls this directly

Calls 4

GetRightMethod · 0.80
GetYMethod · 0.80
GetXMethod · 0.80
ContainsMethod · 0.45

Tested by

no test coverage detected