I am creating a tool where I have a map in JavaFX. I have to draw an existing polygon on this map in order to create areas for the location service for it. Then I want to click somewhere on the map to add a new corner to this polycon. Now adding the corner to the polygon is not so difficult. When I click somewhere on the map with the right mouse button, I want to create a new corner there. But I want to add this corner to the “right” position, that is, before or after this existing corner closest to the new one, and not at the end of the polygon. In addition, the new polygon must not be cut out through the polygon (see Fig. At the end of this message).
I used the Pythagorean theorem to find the nearest point, but now my problem is that I do not want to add this angle BEFORE or AFTER the nearest corner.
Polygon poly = new Polygon(...);
private void insertPoint(double x, double y)
{
int positionInPolygon = 0;
double minDistance = Double.MAX_VALUE;
for ( int i = 0; i <= poly.getPoints().size()-1; i += 2 )
{
double cornerA_x = poly.getPoints().get(i);
double cornerA_y = poly.getPoints().get(i+1);
double tmpDistance = distance(x, y, cornerA_x, cornerA_y);
if(minDistance > tmpDistance)
{
minDistance = tmpDistance;
positionInPolygon = i;
}
}
...
}
private double distance(double x1, double y1, double x2, double y2)
{
double result = 0;
result = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
return result;
}
This should be the result, to be honest, I did not find how the polygon that I want as a result is called correctly.

source
share