[removed] figure out point Y by angle and distance

前端 未结 2 1130
猫巷女王i
猫巷女王i 2021-02-19 01:37

In my project, I would like to draw a line from point X to point Y.

While I know the position of point X, I only know the angle and the distance of point Y.

So m

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 02:11

    Here is a code snippet that wraps @IgnacioVazquez-Abrams's answer into a function with an example of how to use it:

    function findNewPoint(x, y, angle, distance) {
        var result = {};
    
        result.x = Math.round(Math.cos(angle * Math.PI / 180) * distance + x);
        result.y = Math.round(Math.sin(angle * Math.PI / 180) * distance + y);
    
        return result;
    }
    
    var newPoint = findNewPoint(10, 20, 10, 200);
    console.log('newPoint:', newPoint);
    

提交回复
热议问题