简体   繁体   English

如何围绕它的中心旋转三角形?

[英]How to rotate a triangle about it's center?

    document.onkeydown = function (event) {
        switch (event.keyCode) {
            case 74:
                //player presses "j"
                px = player.calculateCentreX();
                py = player.calculateCentreY();
                x1 = rotateX(player.x1,player.y1,35,px,py);
                x2 = rotateX(player.x2,player.y2,35,px,py);
                x3 = rotateX(player.x3,player.y3,35,px,py);
                y1 = rotateY(player.x1,player.y1,35,px,py);
                y2 = rotateY(player.x2,player.y2,35,px,py);
                y3 = rotateY(player.x3,player.y3,35,px,py);
                
                player.setPoints(x1,y1,x2,y2,x3,y3);
                break;
                
            default:
                break;
        }
    };
    
    function rotateX(cx,cy,angle,px,py) {
        x = Math.cos(angle)*(px-cx) - Math.sin(angle)*(py-cy) + cx
        return x
        }
        
    function rotateY(cx,cy,angle,px,py) {
        y = Math.sin(angle)*(px-cx) + Math.cos(angle)*(py-cy) + cy
        return y
        }

I am using the above to try to rotate a triangle (the player) about it's centre whenever the user presses "J".每当用户按下“J”时,我正在使用上面的方法尝试围绕它的中心旋转一个三角形(播放器)。 The setPoints method simply sets the triangles x1,y1,x2,y2,x3,y3 values to the updated points. setPoints 方法只是将三角形 x1,y1,x2,y2,x3,y3 的值设置为更新的点。 Whenever the user presses J, the triangle does rotate but grows in size - can someone point out what is wrong here?每当用户按下 J 时,三角形确实会旋转但会变大 - 有人可以指出这里出了什么问题吗?

Seems you are using wrong argument order: function is defined with center, angle, point sequence似乎您使用了错误的参数顺序:函数是用center, angle, point序列定义的

cx,cy, angle, px,py
  ^             ^
center        point

but you call it with point, angle, center sequence但你用point, angle, center序列来称呼它

px = player.calculateCentreX();
py = player.calculateCentreY();
x1 = rotateX(player.x1,player.y1,  35,  px,py); !!!!!
                     ^                   ^           
                   point                center calculated above

As a result, former center is rotated around vertices结果,前中心围绕顶点旋转

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM