I want to translate a point to specific distance, along a line whose angle is given in degrees.
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;
}
Give me simple algorithm or JavaScript code.
You have to give the initial point, the angle and the unit of movement.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With