我尝试在http://processing.org/得到两点之间的距离是像Java,但不工作:处理应用程序中两点之间的距离?
d = sqrt ((x2 - x1)**2 + (y2 - y1)**2);
的距离公式为: http://www.purplemath.com/modules/xyplane/dist07b.gif
我尝试在http://processing.org/得到两点之间的距离是像Java,但不工作:处理应用程序中两点之间的距离?
d = sqrt ((x2 - x1)**2 + (y2 - y1)**2);
的距离公式为: http://www.purplemath.com/modules/xyplane/dist07b.gif
Java没有一个指数操作符。请尝试使用Math.pow(x,2)或x * x。
谢谢我明白了! d = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); – user779848
根据http://processing.org/reference/这应该工作:
d = sqrt (pow (x2 - x1, 2) + pow (y2 - y1, 2));
虽然我不是,如果你在此处理或Java中需要完全清楚。
只需使用内置的加工类和方法:
PVector x = new PVector(random(width), random(height));
PVector y = new PVector(random(width), random(height));
System.out.println(getEuclidianDistance(x, y))
处理已经自带了一个函数来计算在2D和3D两个点之间的距离。只实现dist()
如通过移交这两点你x
和y
参数在reference提到:
dist (x1, y1, x2, y2);
你有几件事情有点不对劲。它应该是:
d = sqrt ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
其他选项:
d = dist(x1,y1,x2,y2);
或
d = PVector.dist(new PVector(x1,y1),new PVector(x2,y2));
想象一下,你是距离作为直角三角形的hypothenuse。 一侧由X轴(长度为x2-x1)定义,另一侧由Y轴(其长度为y2-y1)定义为 。由于距离是hypothenuse, 你知道双方,你简单地套用勾股定理:
BC squared = AB squared + AC squared
BC = square root (AB squared + AC squared)
AC = (x2-x1) = dx
AB = (y2-y1) = dy
or
d = sqrt(dx*dx + dy*dy);
Math.pow()约为10倍比使用X * X更加昂贵。 –