2011-06-28 39 views

回答

4

Java没有一个指数操作符。请尝试使用Math.pow(x,2)x * x

+2

谢谢我明白了! d = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); – user779848

0

根据http://processing.org/reference/这应该工作:

d = sqrt (pow (x2 - x1, 2) + pow (y2 - y1, 2)); 

虽然我不是,如果你在此处理或Java中需要完全清楚。

0

只需使用内置的加工类和方法:

PVector x = new PVector(random(width), random(height)); 
PVector y = new PVector(random(width), random(height)); 
System.out.println(getEuclidianDistance(x, y)) 
3

处理已经自带了一个函数来计算在2D和3D两个点之间的距离。只实现dist()如通过移交这两点你xy参数在reference提到:

dist (x1, y1, x2, y2); 
1

你有几件事情有点不对劲。它应该是:

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)); 

distance

想象一下,你是距离作为直角三角形的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); 
相关问题