3

is there any convenient API in JTS that would help to

  • find two most distant points in line
  • find two most distant points in polygon

2 Answers 2

4

I think all of the JTS methods relating to distance are looking for the nearest or shortest. But providing you don't have too many vertices then you could do a brute force O(N*N) search:

GeometryFactory gf = new GeometryFactory();
double maxDist = 0;
Point start = null, end = null;
for (Coordinate c : geom.getCoordinates()) {
  Point p = gf.createPoint(c);
  for (Coordinate c2 : geom.getCoordinates()) {
    Point p2 = gf.createPoint(c2);
    double d = DistanceOp.distance(p, p2);
    if (d > maxDist) {
      maxDist = d;
      start = p;
      end = p2;
    }
  }
} 

And if you need to consider points along the lines then add

geom = Densifier.densify(geom, .01);

before you start, though obviously this pushes N up.

enter image description here

1
  • Thanks @Ian! I had a hope I don't have to do it.
    – Capacytron
    Commented Oct 20, 2018 at 15:30
2

You can use the MinimumBoundingCircle algorithm to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.