看一下这里的例子:http://www.brianhare.com/physicals/so.html http://www.brianhare.com/physics/so.html
看一下 console.log,我在其中使用了这两个主要函数:
function distanceBetween2pts(x1, y1, x2, y2) {
console.log("Particle: ("+x1+","+y1+") Mouse: ("+x2+","+y2+")");
// Pythagoras Theorem
// PQ = sqrt( (x2-x1)^2 + (y2-y1)^2 )
var x = (x2-x1);
var y = (y2-y1);
this.radius = Math.sqrt(x*x + y*y);
this.x = x;
this.y = y;
}
function polar2cartesian(R, theta) {
this.x = R * Math.cos(theta);
this.y= R * Math.sin(theta);
}
Where when the mouse is above and to the right of the particle (center circle) such as :
控制台日志显示:
Particle: (300,250) Mouse: (326,223)
artan(-27 / 26) = angle: -46.08092418666069 - theta -0.8042638494191191
其中应该是 arctan(27/26) = angle : 46 : theta = 0.8。因为即使鼠标位于中心“上方”,它也会将 y2-y1 读取为 -27,因为坐标系统基于 0,0 位于左上角。
那么问题是,当 X 和 Y 均为负值时,θ 为正值,此时它应该指向相反的方向(从中心点向外)。我知道我可以在这里做一个 180 度的把戏,但我想明白我做错了什么。