Skip to content

Commit 605bc87

Browse files
committed
fix: BoundingArea.Intersects to handle coplanar/flat area overlap
1 parent 2459598 commit 605bc87

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

FixedMathSharp.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FixedMathSharp.Tests", "tes
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{882324AE-67F3-4F5B-8485-6EF324A15CBF}"
1111
ProjectSection(SolutionItems) = preProject
12+
CONTRIBUTING.md = CONTRIBUTING.md
1213
LICENSE.md = LICENSE.md
1314
README.md = README.md
1415
EndProjectSection

src/FixedMathSharp/Bounds/BoundingArea.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,24 @@ public bool Intersects(IBound other)
183183
if (Contains(other.Min) && Contains(other.Max))
184184
return true; // Full containment
185185

186-
// General intersection logic (allowing for overlap)
187-
return !(Max.x <= other.Min.x || Min.x >= other.Max.x ||
188-
Max.y <= other.Min.y || Min.y >= other.Max.y ||
189-
Max.z <= other.Min.z || Min.z >= other.Max.z);
186+
// Determine which axis is "flat" (thickness zero)
187+
bool flatX = Min.x == Max.x && other.Min.x == other.Max.x;
188+
bool flatY = Min.y == Max.y && other.Min.y == other.Max.y;
189+
bool flatZ = Min.z == Max.z && other.Min.z == other.Max.z;
190+
191+
if (flatZ) // Rectangle in XY
192+
return !(Max.x < other.Min.x || Min.x > other.Max.x ||
193+
Max.y < other.Min.y || Min.y > other.Max.y);
194+
else if (flatY) // Rectangle in XZ
195+
return !(Max.x < other.Min.x || Min.x > other.Max.x ||
196+
Max.z < other.Min.z || Min.z > other.Max.z);
197+
else if (flatX) // Rectangle in YZ
198+
return !(Max.y < other.Min.y || Min.y > other.Max.y ||
199+
Max.z < other.Min.z || Min.z > other.Max.z);
200+
else // fallback to 3D volume logic
201+
return !(Max.x < other.Min.x || Min.x > other.Max.x ||
202+
Max.y < other.Min.y || Min.y > other.Max.y ||
203+
Max.z < other.Min.z || Min.z > other.Max.z);
190204
}
191205
case BoundingSphere sphere:
192206
// Find the closest point on the area to the sphere's center

tests/FixedMathSharp.Tests/Bounds/BoundingArea.Tests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public void Intersects_WithOverlappingArea_ReturnsTrue()
8888
var area2 = new BoundingArea(new Vector3d(3, 3, 3), new Vector3d(6, 6, 6));
8989

9090
Assert.True(area1.Intersects(area2));
91+
92+
var area3 = new BoundingArea(new Vector3d(-2, -2, 0), new Vector3d(2, 2, 0));
93+
var area4 = new BoundingArea(new Vector3d(-1, -1, 0), new Vector3d(3, 3, 0));
94+
Assert.True(area3.Intersects(area4));
9195
}
9296

9397
[Fact]

tests/FixedMathSharp.Tests/Bounds/BoundingBox.Tests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public void Intersects_WithOverlappingBox_ReturnsTrue()
7474
var box2 = new BoundingBox(new Vector3d(1, 1, 1), new Vector3d(4, 4, 4));
7575

7676
Assert.True(box1.Intersects(box2));
77+
78+
var area3 = new BoundingBox(new Vector3d(-2, -2, 0), new Vector3d(2, 2, 0));
79+
var area4 = new BoundingBox(new Vector3d(-1, -1, 0), new Vector3d(3, 3, 0));
80+
Assert.False(area3.Intersects(area4));
7781
}
7882

7983
[Fact]

0 commit comments

Comments
 (0)