tags:

views:

51

answers:

1

My code doesn't work right:

function rotate(Points, Angle) {
    for (var i=0; i<Points.length;i++) {
        Points[i] = [Math.cos(Angle) * Points[i][0] - Math.sin(Angle) * Points[i][1], Math.sin(Angle) * Points[i][0] + Math.cos(Angle) * Points[i][1]];
    }
    return Points;
}


rotate([[0, 0], [50, 0], [25, 25]], 5);

I used the following: http://www.vb-helper.com/howto_rotate_polygon_points.html

+2  A: 

cos and sin in most programming languages are in radians. Are you sure you want to rotate by 5 radians (= 286 degrees)?

KennyTM
Oh, that could be the problem.I want to rotate by 5 degrees.How could I rotate by degrees?
Poru
1 degree = 1 radian * 180/π.Therefore, `rotate(..., 5 * Math.PI/180);`
KennyTM
Math.sin() expects radians you cannot switch to degrees, btw it would be a goog idea to calculate sin and cos only once and assign their values to variables to resuse them
stacker
Thanks, now it works!
Poru