Javascript 为什么围绕原点旋转的点会失去与原点的距离?
我试图在JavaScript中计算点的旋转,并且发现这些值在大约1%的范围内(并且始终处于正确的角度,但距离旋转点近1%)。这比浮点不精确带来的误差要大得多,而且在同一方向上也太可靠了 下面是演示代码和问题的HTML/JavaScript。输出行36显示将点(100100)旋转10度36次的结果。所以,应该在(100100)的0.000000001范围内。但它更接近(57,57) 绘制点时,它正在制作蜗牛壳图案。即衰变轨道。有人能看出我做错了什么吗Javascript 为什么围绕原点旋转的点会失去与原点的距离?,javascript,geometry,rotation,Javascript,Geometry,Rotation,我试图在JavaScript中计算点的旋转,并且发现这些值在大约1%的范围内(并且始终处于正确的角度,但距离旋转点近1%)。这比浮点不精确带来的误差要大得多,而且在同一方向上也太可靠了 下面是演示代码和问题的HTML/JavaScript。输出行36显示将点(100100)旋转10度36次的结果。所以,应该在(100100)的0.000000001范围内。但它更接近(57,57) 绘制点时,它正在制作蜗牛壳图案。即衰变轨道。有人能看出我做错了什么吗 <!DOCTYPE html> &
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Why is the rotation losing distance to origin?</title>
</head>
<body>
<script>
let x = 100; let y = 100;
let Cx = 0; let Cy = 0;
let rotation = 10 * Math.PI / 180; // 10 degrees as radians
for(let i = 0; i <= 36; i++) {
if (i > 33 || i < 3) { document.write(i + ": {" + x + ", " + y + "} <br />"); }
else if (i === 33){ document.write(".<br />"); }
else { document.write("."); }
// Rotate 10 degrees from the last position.
x = Math.cos(rotation) * (x - Cx) - Math.sin(rotation) * (y - Cy) + Cx;
y = Math.sin(rotation) * (x - Cx) + Math.cos(rotation) * (y - Cy) + Cy;
}
</script>
</body>
</html>
为什么旋转会失去到原点的距离?
设x=100;设y=100;
设Cx=0;设Cy=0;
设旋转=10*Math.PI/180;//10度为弧度
对于(设i=0;i33 | | i<3){document.write(i+“:{“+x+”,“+y+”}
;}
else如果(i==33){document.write(“.
”);}
else{document.write(“.”;}
//从最后一个位置旋转10度。
x=数学cos(旋转)*(x-Cx)-数学sin(旋转)*(y-Cy)+Cx;
y=数学sin(旋转)*(x-Cx)+数学cos(旋转)*(y-Cy)+Cy;
}
提前感谢您的帮助。问题在于以下两种说法:
x = Math.cos(rotation) * (x - Cx) - Math.sin(rotation) * (y - Cy) + Cx;
y = Math.sin(rotation) * (x - Cx) + Math.cos(rotation) * (y - Cy) + Cy;
第一条语句用旋转的值覆盖x
的值,然后使用旋转的值计算旋转的y
坐标。请尝试以下方法:
let x1 = Math.cos(rotation) * (x - Cx) - Math.sin(rotation) * (y - Cy) + Cx;
y = Math.sin(rotation) * (x - Cx) + Math.cos(rotation) * (y - Cy) + Cy;
x = x1;
问题在于这两种说法:
x = Math.cos(rotation) * (x - Cx) - Math.sin(rotation) * (y - Cy) + Cx;
y = Math.sin(rotation) * (x - Cx) + Math.cos(rotation) * (y - Cy) + Cy;
第一条语句用旋转的值覆盖x
的值,然后使用旋转的值计算旋转的y
坐标。请尝试以下方法:
let x1 = Math.cos(rotation) * (x - Cx) - Math.sin(rotation) * (y - Cy) + Cx;
y = Math.sin(rotation) * (x - Cx) + Math.cos(rotation) * (y - Cy) + Cy;
x = x1;