2

我有一个 Line2D 和一个 Area 对象,我想要相交的 Line2D。结果也可以是 GeneralPath。我怎样才能做到这一点?

4

3 回答 3

1

您可以在类 Area中使用方法instersects 。不过 Line2D 可以用 Rectangle2D 代替。

于 2012-04-05T12:22:04.713 回答
0

Second chance :

  • build your line2D, it is a shape.
  • Build an area around it (using new Area( line2d ) );
  • take the first area and call intersect with the second area obtained from the line.
  • your first area is now the intersection of the first.
  • take your leftmost, topmost, bottomost, rightmost coordinates
  • turn them into a line2d

and here you are.

于 2012-04-05T12:59:06.227 回答
0

选项 1,用三角形近似 Line2D:

    Polygon polygon = new Polygon(new int[]{x1, x2, x2}, new int[]{y1, y2+e, y2-e}, 3);
    Area triangle = new Area(polygon);
    triangle.intersect(area); // intersection of ray and area
    return !triangle.isEmpty(); // returns true if intersects

选项 2,改用 Geometry 类

  private final GeometryFactory geometryFactory = new GeometryFactory();
  private ShapeReader shapeReader = new ShapeReader(geometryFactory);

  Path2D.Double thePath = new Path2D.Double(area);
  Geometry geometry  = shapeReader.read(thePath.getPathIterator(null));

  Coordinate[] coordinate = new Coordinate[] {new Coordinate(x1, y1), new Coordinate(x2, y2)};
  LineString centerRay = geometryFactory.createLineString(coordinate);

  return geometry.intersects(centerRay); // returns true if intersects

于 2020-03-27T10:58:10.520 回答