Java中两个形状/区域之间的距离? [英] distance between two Shapes/Areas in Java?
问题描述
如果我用各种简单形状(多边形和椭圆形)的结合制成了两个java.awt.geom.Area
,是否有一种方法可以找到两个区域之间的距离(即最接近的距离)?
If I have two java.awt.geom.Area
's made out of the union of various simple Shapes (Polygons and Ellipses), is there a method out there to find the distance (i.e. closest distance) between the two Areas?
为了阐明这一点:假设我有两个任意的Area,每个Area是由任意形状的并集创建的:
To clarify: suppose I have two arbitrary Areas, each of which is created from the union of shapes of any sort:
//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)));
我想要的是方法getDistance(Area a, Area b)
,该方法给我一个双精度值,表示区域a中的任何点与区域b中的任何点之间的最短距离.这是上面两个区域的图像,蓝色线条表示我感兴趣的距离:
What I want is a method getDistance(Area a, Area b)
that gives me a double representing the shortest distance between any point in Area a and any point in Area b. Here's an image of the above two Areas with a line in blue indicating the distance I'm interested in:
有没有一种方法可以做到这一点?如果没有,我将如何实施?
Is there a method out there to do this? If not, how might I implement one?
推荐答案
似乎没有一种方法可以准确地做到这一点.但是,使用PathIterator
s,您应该能够沿形状轮廓进行点对点比较并手动找到距离.
There doesn't seem to be a method that does that exactly; however, using PathIterator
s, you should be able to compare point to 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
这篇Wikipedia文章介绍了如何有效实施这样可以避免二次明显的实现.
This Wikipedia article describes how you could efficiently implement this to avoid the quadratic obvious implementation.
这篇关于Java中两个形状/区域之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!