Programmatically rotate shapes using coordinates

后端 未结 3 1211
时光说笑
时光说笑 2021-01-23 07:47

If I have some shapes defined using arrays of coordinates e.g.

[[-30, -30], [-30, 30], [30, 30], [30, -30]]

and edges defined using:

         


        
3条回答
  •  死守一世寂寞
    2021-01-23 08:01

    You could use a formular for rotating the point around the origin.

    function rotate(array, angle) {
        return array.map(function (p) {
            function r2d(a) { return a * Math.PI / 180; }
            return [
                Math.cos(r2d(angle)) * p[0] - Math.sin(r2d(angle)) * p[1],
                Math.sin(r2d(angle)) * p[0] - Math.cos(r2d(angle)) * p[1],
            ];
        });
    }
    console.log(rotate([[-30, -30], [-30, 30], [30, 30], [30, -30]], 45));
    console.log(rotate([[-30, -30], [-30, 30], [30, 30], [30, -30]], 90));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

提交回复
热议问题