I'm trying to rotate a svg polygon using javascript with Math.sin
and Math.cos
. I have created a codepen here that illustrates what I am trying to do and the problem. I am using Math.sin
and Math.cos
rather than transform
because I need to access the coordinates of the rotated points. Essentially my code:
var r = (Math.PI / 180.0) * a,
cos = Math.cos(r),
sin = Math.sin(r),
cx=350, cy=250;
for(var p of rectangle){
p[0]=cos*(p[0]-cx)-sin*(p[1]-cy)+cx;
p[1]=cos*(p[1]-cy)+sin*(p[0]-cx)+cy;
}
does not work correctly, with the rotated points not quite right. They should be rotated anti-clockwise in 2D about cx=350, cy=250
. Instead the rectangle is distorted, seemingly doing a figure-8 in 3D.