Rotate a square by an angle in degree
Question: I have a square with center x0, y0. I wish to rotate the vertex of this square
for a given angle (theta) expressed in degree and return the new rotated
vertex in clockwise direction. I am using this
[approach](http://stackoverflow.com/questions/786472/rotate-a-point-by-an-
angle) to rotate a single point applied for each vertex

rotate point (px, py) around point (x0, y0) by angle theta you'll get:

    
    
    p'x = cos(theta) * (px-x0) - sin(theta) * (py-y0) + x0
    p'y = sin(theta) * (px-x0) + cos(theta) * (py-y0) + y0
    
    where: 
    px, py = coordinate of the point
    y0, x0, = centre of rotation
    theta = angle of rotation
    

I wrote a function in Python where the parameters are: x, y (=centre of the
square), side of the square, and theta_degree (angle of rotation in degree)
but the returns is in anticlockwise direction

    
    
    from math import cos, sin
    
    def get_square_plot(x, y, side, theta_degree=0):
        theta = theta_degree * pi/180
        xa = x-side/2
        ya = y+side/2
        xb = x+side/2
        yb = y+side/2
        xc = x+side/2
        yc = y-side/2
        xd = x-side/2
        yd = y-side/2
        xa_new = cos(theta) * (xa - x) - sin(theta) * (ya - y) + x
        ya_new = sin(theta) * (xa - x) - cos(theta) * (ya - y) + y
        xb_new = cos(theta) * (xb - x) - sin(theta) * (yb - y) + x
        yb_new = sin(theta) * (xb - x) - cos(theta) * (yb - y) + y
        xc_new = cos(theta) * (xc - x) - sin(theta) * (yc - y) + x
        yc_new = sin(theta) * (xc - x) - cos(theta) * (yc - y) + y
        xd_new = cos(theta) * (xd - x) - sin(theta) * (yd - y) + x
        yd_new = sin(theta) * (xd - x) - cos(theta) * (yd - y) + y
        return [(xa_new, ya_new),(xb_new, yb_new),(xc_new, yc_new),(xd_new, yd_new)]
    
    get_square_plot(0, 0, 10, 0)
    [(-5.0, -5.0), (5.0, -5.0), (5.0, 5.0), (-5.0, 5.0)]
    

instead of

    
    
    [(-5.0, 5.0), (5.0, 5.0), (5.0, -5.0), (-5.0, -5.0)]
    


Answer: It's such a simple thing -- you've got the formula wrong for all of your
y-values.

It should be:

    
    
    ya_new = sin(theta) * (xa - x) + cos(theta) * (ya - y) + y
    

addition instead of subtraction.

