Search code examples
javaswinggraphicsawtgraphics2d

Polygon crossed by line


I have polygon and a line.

g.drawLine(this.getLine().getP1().getX(), this.getLine().getP1().getY(),
                this.getLine().getP2().getX(), this.getLine().getP2().getY());

g.drawPolygon(this.polygon);

and I have to find the cross-points between the polygon and the line(if any), and what part of the line is in the polygon. Any ideas how to do that .


Solution

  • In Java, the problem can be reduced to finding the intersection of two lines. Each pair of lines consists of the original line and a line obtained from the PathIterator of the Polygon. There's a related example here.