计算给定角度和长度的向量 [英] Calculate vector with given angle and length

查看:45
本文介绍了计算给定角度和长度的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在javascript中调用一个带有x和y坐标以及方向(以度为单位的角度)的函数,它会返回一组已移动"的新坐标' 沿原始坐标给出的方向增加 10 像素?我环顾四周,但我只能找到获得两个给定坐标的角度的方法.

Is there any way in which, in javascript, I can call a function with an x and y co-ord and a direction(angle in degrees) and it will return a set of new co-ords that has been 'moved' by 10px in the direction given from the original co-ords? I looked around but all I can find is ways to get the angle of two given co-ords.

抱歉,如果措辞不好.

推荐答案

此函数返回新坐标的数组[xCoord, yCoord]:

This function returns an array [xCoord, yCoord] of the 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]
}

这篇关于计算给定角度和长度的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆