我想将一个点转换为特定距离,沿着一个角度以度为单位的线。
var initialPoint = [0,0]; //Starting Point
var distance = 100; //Distance in pixels to be translated
var degree = 45; //Direction of move
var translatedPoint = moveByDegree(initialPoint, degree, distance);
function moveByDegree(initialPoint, degree, distance)
{
//formula to generate translatedPoint by degree & distance
// . . .
return translatedPoint;
}
给我简单的算法或JavaScript代码。
答案 0 :(得分:4)
你必须给出初始点,角度和运动单位。
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};
function move(point, angle, unit) {
var x = point[0];
var y = point[1];
var rad = Math.radians(angle % 360);
x += unit*Math.sin(rad);
y += unit*Math.cos(rad);
return [x, y];
}
move([0,0], 180, 100); // [0, 100]