# Geotools JTS, find most distant points in polyline or polygon?

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

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. • Thanks @Ian! I had a hope I don't have to do it. – Sergey Oct 20 '18 at 15:30

You can use the MinimumBoundingCircle algorithm to do this.