如何计算距离路径的最短距离?
答
-
数学:
如wikipedia 的线和点之间的最短距离所解释的,可以被计算为 如下:
-
爪哇implimentaion
class Point { double x,y; Point(double pX, double pY){ this.x= pX; this.y= pY; } public double distance(Point A1, Point A2){ double numerator = Math.abs((A2.y - A1.y)*this.x + (A2.x - A1.x)*this.y + A2.x*A1.y - A2.y*A1.x); double denominator = Math.sqrt(Math.pow(A2.y - A1.y,2) + Math.pow(A2.x - A1.x,2)); return numerator/denominator; } }
为了计算由Points A1
和A2
定义Point B
和线之间的距离,可以使用方法distance
这样的:
public static void main (String[] args) throws java.lang.Exception
{
Point A1 = new Point(0,3);
Point A2 = new Point(2,0);
Point B = new Point(0,0);
System.out.println(
B.distance(A1,A2)
);
}
但恳求去降压的基本知识,选择了一些很好的和有趣的编码书或啧啧,并给它一去,快乐编码:)
这是数学问题不是Java? –
咨询谷歌或一本好的数学书籍 – UnholySheep
我对java相当新(并且数学不好)。不知道我应该使用哪个功能。 –