的計算角度我想計算攝氏鼠標移動的角度。雖然我知道你不必將鼠標移動到一條直線上,但我只是試圖根據起點和終點來計算它以創建一個很好的直角。度鼠標移動
log("ANGLE: " + getAngle(x1, y1, x2, y2));
是給怪異的結果:
ANGLE: 0.24035975832980774
mouse has stopped
ANGLE: 1.334887709726425
mouse has stopped
ANGLE: 0.2722859857950508
mouse has stopped
ANGLE: 0.3715485780567732
mouse has stopped
代碼:
$("canvas").mousemove(function(e) {
getDirection(e);
if (!set) {
x1 = e.pageX, //set starting mouse x
y1 = e.pageY, //set starting mouse y
set = true;
}
clearTimeout(thread);
thread = setTimeout(callback.bind(this, e), 100);
});
function getAngle (x1, y1, x2, y2) {
var distY = Math.abs(y2-y1); //opposite
var distX = Math.abs(x2-x1); //adjacent
var dist = Math.sqrt((distY*distY)+(distX*distX)); //hypotenuse,
//don't know if there is a built in JS function to do the square of a number
var val = distY/dist;
var aSine = Math.asin(val);
return aSine; //return angle in degrees
}
function callback(e) {
x2 = e.pageX; //new X
y2 = e.pageY; //new Y
log("ANGLE: " + getAngle(x1, y1, x2, y2));
log("mouse has stopped");
set = false;
}
啊哎呀。你能告訴我爲什麼乘以180/PI的弧度給出度數? – Growler
這就是以弧度表示度數的定義。 http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees – gcochard
LOL我應該看這件事。最後一件事,我注意到當我在圈子中移動時,它總是以「90」來表示度數。例如:從原點移動到右上象限可以給出0到90度的範圍。從原點移動到右下象限,我期望270-360度的範圍。我該如何解決? – Growler