如果您转到以下链接,您会看到一个非常酷的Javascript模拟,该模拟是根据鼠标位置旋转的立方体。有人可以解释这个旋转多维数据集脚本如何工作背后的数学吗?
模拟:here.
如果您查看立方体旋转脚本的来源,你会看到:
<script type="text/javascript">
/* I wrote this script in a few minutes just for fun. It's not made to win any
competition. */
var dimension = 1, a = 0, b = 0, i = 27;
while (i--) document.write('<b id="l' + i + '">+</b>');
function f()
{
i = 0;
for (x = -dimension; x <= dimension; x += dimension)
for (y = -dimension; y <= dimension; y += dimension)
for (z = -dimension; z <= dimension; z += dimension)
{
u = x;
v = y;
w = z;
u2 = u * Math.cos(a) - v * Math.sin(a);
v2 = u * Math.sin(a) + v * Math.cos(a);
w2 = w;
u = u2; v = v2; w = w2;
u2 = u;
v2 = v * Math.cos(b) - w * Math.sin(b);
w2 = v * Math.sin(b) + w * Math.cos(b);
u = u2; v = v2; w = w2;
var c = Math.round((w + 2) * 70);
if (c < 0) c = 0;
if (c > 255) c = 255;
s = document.getElementById('l' + i).style;
s.left = 300 + u * (w + 2) * 50;
s.top = 300 + v * (w + 2) * 50;
s.color = 'rgb(' + c + ', ' + c + ', 0)';
s.fontSize = (w + 2) * 16 + 'px';
/* The Digg users missed depth sort, so here it is. */
s.zIndex = Math.round((w + 2) * 10);
i++;
}
}
/* Using a timer instead of the onmousemove handler wastes CPU time but makes
the animation much smoother. */
setInterval('f()', 17);
</script>
我看过了好几遍了,我还是不明白如何计算立方体的点数。这是使用“欧拉旋转”?我遇到的一个重大问题是使用单个字母的变量名,这对我来说毫无意义。
是否有必要的数学知识的人有助于解释在模拟中旋转立方体后的数学运算方式?我想制作类似的东西,但是在计算积分位置时,我有点失落。
难道你只是讨厌人们使用这样的变量名而没有评论。我讨厌混淆代码!这使反编译变得更加困难! ;-) – Kredns
@Lucas Aardvark,说实话,我认为他不认为他是在试图混淆他的代码。我认为他是如何编码的,或者是在试图缩小他的脚本的大小。他与Digg用户对源头进行了大量的讨论,并调整了某些东西。 – KingNestor
我认为它是真正可读的,但是你忘记了onmousemove()事件,所以唯一的问题是:如何设置a和b –