2D中一点绕原点旋转收敛到0
Rotation of a point around origin in 2D converge to 0
编写了以下代码以在 2D 中围绕原点旋转一个点。然而,它并没有绕圈,而是不断向中心汇聚。 (最初 运行 在 Three.js 上)这段代码绝对正确。 (我在其他平台上做过这个,甚至 运行 在 excel sheet 中也做过数值模拟)。
为什么会这样?
<body>
<div id="vals"></div>
<script>
theta = Math.PI / 90.0;
var x = 1;
var y = 1;
var element = document.getElementById("vals");
var i;
for (i = 0; i < 10000; i++){
var r = Math.sqrt( x * x + y * y );
var para = document.createElement("pre");
var node = document.createTextNode("x="+x+" y="+y+" r="+r);
para.appendChild(node);
element.appendChild(para);
x = (x * Math.cos(theta)) - (y * Math.sin(theta));
y = (y * Math.cos(theta)) + (x * Math.sin(theta));
}
</script>
</body>
p.s。
我知道如果我将值归一化并乘以幅度,我会得到正确的答案。但是我想知道为什么这个确切的东西不起作用。
首先,您应用 changed x 值来获得 y。这是错误的。而是记住旧值并使用它。
var tempx = x;
x = (x * Math.cos(theta)) - (y * Math.sin(theta));
y = (y * Math.cos(theta)) + (tempx * Math.sin(theta));
其次:重复使用相同的值会导致误差累积,因此半径可能会收敛 - 正如您所见。所以更精确的方法是存储初始值并使用当前角度(而不是角度差)旋转它们
var x0 = 1;
var y0 = 1;
in the loop:
x = (x0 * Math.cos(theta * i)) - (y0 * Math.sin(theta * i));
y = (y0 * Math.cos(theta * i)) + (x0 * Math.sin(theta * i));
编写了以下代码以在 2D 中围绕原点旋转一个点。然而,它并没有绕圈,而是不断向中心汇聚。 (最初 运行 在 Three.js 上)这段代码绝对正确。 (我在其他平台上做过这个,甚至 运行 在 excel sheet 中也做过数值模拟)。
为什么会这样?
<body>
<div id="vals"></div>
<script>
theta = Math.PI / 90.0;
var x = 1;
var y = 1;
var element = document.getElementById("vals");
var i;
for (i = 0; i < 10000; i++){
var r = Math.sqrt( x * x + y * y );
var para = document.createElement("pre");
var node = document.createTextNode("x="+x+" y="+y+" r="+r);
para.appendChild(node);
element.appendChild(para);
x = (x * Math.cos(theta)) - (y * Math.sin(theta));
y = (y * Math.cos(theta)) + (x * Math.sin(theta));
}
</script>
</body>
p.s。 我知道如果我将值归一化并乘以幅度,我会得到正确的答案。但是我想知道为什么这个确切的东西不起作用。
首先,您应用 changed x 值来获得 y。这是错误的。而是记住旧值并使用它。
var tempx = x;
x = (x * Math.cos(theta)) - (y * Math.sin(theta));
y = (y * Math.cos(theta)) + (tempx * Math.sin(theta));
其次:重复使用相同的值会导致误差累积,因此半径可能会收敛 - 正如您所见。所以更精确的方法是存储初始值并使用当前角度(而不是角度差)旋转它们
var x0 = 1;
var y0 = 1;
in the loop:
x = (x0 * Math.cos(theta * i)) - (y0 * Math.sin(theta * i));
y = (y0 * Math.cos(theta * i)) + (x0 * Math.sin(theta * i));