Distance between two shapes / areas in Java?

If I have two java.awt.geom.Area made from a union of various simple shapes (polygons and ellipses), is there a way to find the distance (i.e. the closest distance) between the two areas?

To clarify: suppose I have two arbitrary areas, each of which is created from a union of any form:

 //Define the first area Area a = new Area(new Ellipse2D.Double(50, 50, 100, 100)); a.add(new Area(new Rectangle2D.Double(100, 100, 100, 100))); //Define the second area Area b = new Area(new Ellipse2D.Double(200, 300, 100, 100)); b.add(new Area(new Ellipse2D.Double(250, 250, 100, 100))); 

What I want is the getDistance(Area a, Area b) method, which gives me a double view representing the shortest distance between any point in area a and any point in area b. Here's an image of the two above areas with a blue line indicating the distance I'm interested in:

Areas a and b, and the distance between them

Is there any way to do this? If not, how can I implement one?

+6
source share
2 answers

There does not seem to be a method that does this for sure; however, using PathIterator s, you should be able to compare points with a point along the outline of the shapes and find the distance manually.

http://docs.oracle.com/javase/6/docs/api/java/awt/geom/PathIterator.html

This Wikipedia article describes how you could effectively implement this in order to avoid a quadratic obvious implementation.

+3
source

Use Hausdorf Distance

Check out this very clear explanation of how to use hausdorff Distance: http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html

+3
source

Source: https://habr.com/ru/post/947382/


All Articles