用角度计算坐标

Calculating cordanates with angles

所以基本上我有起始向量和角度,但我正在使用的代码只更新了我的角度,我正试图到达另一个向量。

var start_x = 0;
var start_y = 0;
var speed = 200;
var current_x; //This needs to be calculated 
var current_y; //This needs to be calculated 
var current_angle = 53;

如何使用速度和起始位置计算当前的 X 向量和 Y 向量?我浏览了这个网站和其他网站,但似乎找不到答案。

好的你

首先你有速度,所以你也需要一个时间范围,所以让我们假设我们需要 1 秒后的坐标。测量速度的公式是 V = S/T 其中 V 是速度(某个方向的速度),S 是距离,T 是时间。 因此 S = VxT 按照你的速度200,1秒走的距离是200M 现在我们也有你给它的角度 53 度。 因此我们可以画一个假想的三角形来找到 (x,y) 未知的新坐标。 要知道 x,y 的公式是

y= sin(theta) x Distance
x = cos(theta) x Distance

其中 theta 等于 53deg,距离为 200 因此 (x,y) = ()

为了更具描述性,在我们想象的三角形中,y 是相反的,x 是相邻的,并且 x,y 只不过是距 0,0 的距离。三角学中有一个公式,它指出

Sin(theta) = opposite/Hypotenuse
 hence 53 = unknown/200
similarly
Cos(theta) = Adjacent/Hypotenuse
 hence 53 = unknown/200
 So after calculating we get the result (120.36,159.72)

所以在 java 脚本中你可以使用

// since Math.cos takes input in radians you  have to convert it into degrees.

    var speed = 200;
    var time = 1;
    var angle = 53;
    x = (Math.cos(angle*(Math.PI/100))* (speed*time);
    y = (Math.sin(angle*(Math.PI/100))* (speed*time);

我们使用弧度而不是度数进行计算,因此您可能需要将其转换为度数,但这并不困难,只需将 (x,y) 转换为 (y,x),这将是度数的结果.

矢量数学很难,所以我会写一点 class 来完成所有繁重的工作:

function LameVector(x,y) {
  this.startx = this.x = x;
  this.starty = this.y = y;
  this.angle = false;
  this.mag = 0;
  
  this.moveXY = function (x, y) {
    this.x += x;
    this.y += y;
    this.angle = Math.atan2(this.y - this.starty, this.x - this.startx) * 180 / Math.PI;
    this.mag = Math.sqrt(Math.pow(this.y - this.starty, 2) + Math.pow(this.x - this.startx, 2));
  }
  
  this.move = function (speed, angle) {
    var ang = angle / 180 * Math.PI;
    this.moveXY(speed * Math.cos(ang), speed * Math.sin(ang));
  }
}
var o = document.getElementById("out");
var vec1 = new LameVector(0, 0); // starting position 0,0
o.innerHTML += "start x " + vec1.x + ", start y " + vec1.y + "<br>";

vec1.move(200, 53); // move 200 units at angle 53 deg
o.innerHTML += "move1 x " + vec1.x + ", move1 y " + vec1.y + "<br>";

vec1.move(200, 27); // move 200 more units at angle 27 deg
o.innerHTML += "move2 x " + vec1.x + ", move2 y " + vec1.y + "<br>";

// can also get the angle and length
o.innerHTML += "final angle " + vec1.angle + ", magnitude " + vec1.mag + "<br>";
<div id="out"></div>

我以前遇到过这个问题 () 并使用了以下方法。

您将使用 Math.cos()Math.sin() 方法,它们将以弧度为单位的角度作为参数。所以

var start_x = 0;
var start_y = 0;
var speed = 200;
var current_x; //This needs to be calculated 
var current_y; //This needs to be calculated 
var current_angle = 53;

// converting degrees to radian
var angleInRad = current_angle * (Math.PI / 180);

var time = 1; // second
var distance = speed * time;

// calculate the x and y values
current_x = Math.cos(angleInRad) * distance;
current_y = Math.sin(angleInRad) * distance;

console.log(current_x, current_y); // 120.36, 159.72