diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java index f51c392f..0355fb24 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java @@ -142,12 +142,11 @@ public List getIntersectingRegions(List region intersectingRegions.add(region); continue; } else if (region instanceof ProtectedPolygonalRegion) { - // If either region contains the points of the other, they intersect - if (containsAny(region.getPoints())) { - intersectingRegions.add(region); - continue; - } - if (region.containsAny(getPoints())) { + // If either region contains the points of the other, + // or if any edges intersect, the regions intersect + if (containsAny(region.getPoints()) + || region.containsAny(getPoints()) + || intersectsEdges(region)) { intersectingRegions.add(region); continue; } diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java index d4c3672b..d2db8a07 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java @@ -130,12 +130,11 @@ public List getIntersectingRegions(List region if (!intersectsBoundingBox(region)) continue; if (region instanceof ProtectedPolygonalRegion || region instanceof ProtectedCuboidRegion) { - // If either region contains the points of the other, they intersect - if (containsAny(region.getPoints())) { - intersectingRegions.add(region); - continue; - } - if (region.containsAny(getPoints())) { + // If either region contains the points of the other, + // or if any edges intersect, the regions intersect + if (containsAny(region.getPoints()) + || region.containsAny(getPoints()) + || intersectsEdges(region)) { intersectingRegions.add(region); continue; } diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java index 698bbb5b..33097200 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java @@ -19,6 +19,7 @@ package com.sk89q.worldguard.protection.regions; +import java.awt.geom.Line2D; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -472,6 +473,40 @@ protected boolean intersectsBoundingBox(ProtectedRegion region) { return true; } + /** + * Compares all edges of two regions to see if any of them intersect + * + * @param region + * @return + */ + protected boolean intersectsEdges(ProtectedRegion region) { + List pts1 = getPoints(); + List pts2 = region.getPoints(); + BlockVector2D lastPt1 = pts1.get(pts1.size() - 1); + BlockVector2D lastPt2 = pts2.get(pts2.size() - 1); + for (int i = 0; i < pts1.size(); i++ ) { + for (int j = 0; j < pts2.size(); j++) { + + Line2D line1 = new Line2D.Double( + lastPt1.getBlockX(), + lastPt1.getBlockZ(), + pts1.get(i).getBlockX(), + pts1.get(i).getBlockZ()); + + if (line1.intersectsLine( + lastPt2.getBlockX(), + lastPt2.getBlockZ(), + pts2.get(j).getBlockX(), + pts2.get(j).getBlockZ())) { + return true; + } + lastPt2 = pts2.get(j); + } + lastPt1 = pts1.get(i); + } + return false; + } + /** * Checks to see if the given ID is accurate. * diff --git a/src/test/java/com/sk89q/worldguard/protection/RegionIntersectTest.java b/src/test/java/com/sk89q/worldguard/protection/RegionIntersectTest.java index e4552e49..7de0e303 100644 --- a/src/test/java/com/sk89q/worldguard/protection/RegionIntersectTest.java +++ b/src/test/java/com/sk89q/worldguard/protection/RegionIntersectTest.java @@ -5,7 +5,6 @@ import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; @@ -15,7 +14,6 @@ public class RegionIntersectTest { @Test - @Ignore public void testCuboidGetIntersectingRegions() { ProtectedRegion region = new ProtectedCuboidRegion("square", new BlockVector(100, 40, 0), new BlockVector(140, 128, 40));