Calculate a vector with a given angle and length

Is there a way in which in javascript I can call a function with x and y coordinates and direction (angle in degrees) and it will return a set of new coordinates that were “moved” by 10px in the direction indicated in the original coordinates? I looked around, but all I can find are ways to get the angle of two given coordinates.

Sorry if this is poorly written.

+4
source share
2 answers

This function returns an array [xCoord, yCoord] of new coordinates:

function myFunction(xCoord, yCoord, angle, length) {
    length = typeof length !== 'undefined' ? length : 10;
    angle = angle * Math.PI / 180; // if you're using degrees instead of radians
    return [length * Math.cos(angle) + xCoord, length * Math.sin(angle) + yCoord]
}
+6
source

x L*cos(a), y L*sin(a), a - ( " " ), L - 10 px .

0

All Articles