Implemented edge intersection checking

This commit is contained in:
Eric Puidokas 2011-10-18 15:52:25 -07:00 committed by TomyLobo
parent 27251dda2e
commit 35e6ef5cd8
4 changed files with 45 additions and 14 deletions

View File

@ -142,12 +142,11 @@ public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> 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;
}

View File

@ -130,12 +130,11 @@ public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> 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;
}

View File

@ -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<BlockVector2D> pts1 = getPoints();
List<BlockVector2D> 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.
*

View File

@ -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));