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?
澄清:假设我有两个任意区域,每个区域都是由任何形状的联合创建的:
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
这篇维基百科文章描述了如何有效地实施这是为了避免二次明显的实现.
This Wikipedia article describes how you could efficiently implement this to avoid the quadratic obvious implementation.
这篇关于Java中两个形状/区域之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!