Java-多边形和直线的交点

12

有没有函数可以给我一个Polygon和一个Line2D的交点?

我有一个多边形和一条线段,我知道它们相交,我想要得到实际交点的值,而不仅仅是一个布尔值。


1
我应该补充一下,我知道它在正好一个地方相交,因为我知道一个点在多边形的外部,另一个点在内部。但是我不知道它与多边形的哪个线段相交。 - bryan
5个回答

10

这就是你需要的内容。有趣的方法是getIntersections和getIntersection。前者遍历所有多边形线段并检查交点,后者执行实际计算。请记住,该计算可以被严重优化并且不会检查除以0的情况。这仅适用于多边形,如果引入了三次和二次曲线的计算,它也可以被改编成适用于其他形状。假设使用Line2D.Double而不是Line2D.Float。使用Set来避免重复的点(可能出现在多边形角落交点处)。

请不要在没有经过广泛测试的情况下使用此代码,因为我只是很快地粗略拼凑而成,无法确定它完全可靠。

package quickpolygontest;

import java.awt.Polygon;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws Exception {

        final Polygon poly = new Polygon(new int[]{1,2,2,1}, new int[]{1,1,2,2}, 4);
        final Line2D.Double line = new Line2D.Double(2.5, 1.3, 1.3, 2.5);
        final Set<Point2D> intersections = getIntersections(poly, line);
        for(Iterator<Point2D> it = intersections.iterator(); it.hasNext();) {
            final Point2D point = it.next();
            System.out.println("Intersection: " + point.toString());
        }

    }

    public static Set<Point2D> getIntersections(final Polygon poly, final Line2D.Double line) throws Exception {

        final PathIterator polyIt = poly.getPathIterator(null); //Getting an iterator along the polygon path
        final double[] coords = new double[6]; //Double array with length 6 needed by iterator
        final double[] firstCoords = new double[2]; //First point (needed for closing polygon path)
        final double[] lastCoords = new double[2]; //Previously visited point
        final Set<Point2D> intersections = new HashSet<Point2D>(); //List to hold found intersections
        polyIt.currentSegment(firstCoords); //Getting the first coordinate pair
        lastCoords[0] = firstCoords[0]; //Priming the previous coordinate pair
        lastCoords[1] = firstCoords[1];
        polyIt.next();
        while(!polyIt.isDone()) {
            final int type = polyIt.currentSegment(coords);
            switch(type) {
                case PathIterator.SEG_LINETO : {
                    final Line2D.Double currentLine = new Line2D.Double(lastCoords[0], lastCoords[1], coords[0], coords[1]);
                    if(currentLine.intersectsLine(line))
                        intersections.add(getIntersection(currentLine, line));
                    lastCoords[0] = coords[0];
                    lastCoords[1] = coords[1];
                    break;
                }
                case PathIterator.SEG_CLOSE : {
                    final Line2D.Double currentLine = new Line2D.Double(coords[0], coords[1], firstCoords[0], firstCoords[1]);
                    if(currentLine.intersectsLine(line))
                        intersections.add(getIntersection(currentLine, line));
                    break;
                }
                default : {
                    throw new Exception("Unsupported PathIterator segment type.");
                }
            }
            polyIt.next();
        }
        return intersections;

    }

    public static Point2D getIntersection(final Line2D.Double line1, final Line2D.Double line2) {

        final double x1,y1, x2,y2, x3,y3, x4,y4;
        x1 = line1.x1; y1 = line1.y1; x2 = line1.x2; y2 = line1.y2;
        x3 = line2.x1; y3 = line2.y1; x4 = line2.x2; y4 = line2.y2;
        final double x = (
                (x2 - x1)*(x3*y4 - x4*y3) - (x4 - x3)*(x1*y2 - x2*y1)
                ) /
                (
                (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)
                );
        final double y = (
                (y3 - y4)*(x1*y2 - x2*y1) - (y1 - y2)*(x3*y4 - x4*y3)
                ) /
                (
                (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)
                );

        return new Point2D.Double(x, y);

    }

}

8

使用构造函数Area(Shape),并将您的多边形和Line2D作为要相交的区域传递给java.awt.geom.Area.intersect(Area),将会得到相交的区域。


2

请记住,它可能在多个位置相交。

我们将多边形的线段称为P,实线段称为L。

我们找到每条线的斜率(斜率为m)

ml = (ly1-ly2) / (lx1-lx2);
mp = (ply-pl2) / (px1-px2);

找到每条直线的y截距

// y = mx+b where b is y-intercept
bl = ly1 - (ml*lx1);
bp = py1 - (pl*px1);

您可以通过以下方式解决x的值:
x = (bp - bl) / (ml - mp)

然后将X代入其中一个方程式中,以求得Y。
y = ml * x + bl

这是算法的已实现版本。
class pointtest {

    static float[] intersect(float lx1, float ly1, float lx2, float ly2,
                           float px1, float py1, float px2, float py2) {
        // calc slope
        float ml = (ly1-ly2) / (lx1-lx2);
        float mp = (py1-py2) / (px1-px2);       

        // calc intercept        
        float bl = ly1 - (ml*lx1);
        float bp = py1 - (mp*px1);  

        float x = (bp - bl) / (ml - mp);
        float y = ml * x + bl;

        return new float[]{x,y};
    }

    public static void main(String[] args) {

        float[] coords = intersect(1,1,5,5,1,4,5,3);
        System.out.println(coords[0] + " " + coords[1]);

    }
}

和结果:

3.4 3.4

2
我应该指出这里有一些未涵盖的条件。当线条相同时会发生什么?当线条不相交(斜率相等)时会发生什么?如果交点在线段外部怎么办?如果其中一个是零斜率呢?还有一些特殊情况需要覆盖,留给读者自己练习。 - corsiKa

2

我采用了以下方法,取得了巨大的成功:

Area a = new Area(shape1);
Area b = new Area(shape2);
b.intersect(a);
if (!b.isEmpty()) {
  //Shapes have non-empty intersection, so do you actions.
  //In case of need, actual intersection is in Area b. (its destructive operation)
}

0

如果您不受限于使用PolygonLine2D对象,我建议使用JTS

简单的代码示例:

// create ring: P1(0,0) - P2(0,10) - P3(10,10) - P4(0,10)
LinearRing lr = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,10), new Coordinate(10,10), new Coordinate(10,0), new Coordinate(0,0)});
// create line: P5(5, -1) - P6(5, 11) -> crossing the ring vertically in the middle
LineString ls = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(5,-1), new Coordinate(5,11)});
// calculate intersection points
Geometry intersectionPoints = lr.intersection(ls);
// simple output of points
for(Coordinate c : intersectionPoints.getCoordinates()){
    System.out.println(c.toString());
}

结果是:

(5.0, 0.0, NaN)
(5.0, 10.0, NaN)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接