0
votes

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 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)]
2

2 Answers

2
votes

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.

1
votes

Don't forget about the geometry module, either. It can deal with a variety of basic shapes and handle translation, rotation, etc...

A square can be constructed with RegularPolygon. It does so by locating vertices a given radius from the center; to get a square with a given side length, divide by sqrt(2). Here is a function to rotate the diamond-orientation so sides are parallel to the axes and then rotate the desired angle, a:

>>> Square = lambda c, r, a: RegularPolygon(c, r/sqrt(2), 4, -rad(a) - pi/4)
>>> Square((0,0),10,0).vertices
[Point(5, -5), Point(5, 5), Point(-5, 5), Point(-5, -5)]
>>> [w.n(2) for w in Square((0,0),10,1).vertices]
[Point(4.9, -5.1), Point(5.1, 4.9), Point(-4.9, 5.1), Point(-5.1, -4.9)]

Note that the slight CW rotation of 1 degree (-rad(1)) puts the first vertex a little closer to the y-axis and a little lower as we expect. You can also enter a symbol for the angle:

>>> from sympy.utilities.misc import filldedent
>>> print filldedent(Square((0,0),10,a).vertices)

[Point(5*sqrt(2)*cos(pi*a/180 + pi/4), -5*sqrt(2)*sin(pi*a/180 +
pi/4)), Point(5*sqrt(2)*sin(pi*a/180 + pi/4), 5*sqrt(2)*cos(pi*a/180 +
pi/4)), Point(-5*sqrt(2)*cos(pi*a/180 + pi/4), 5*sqrt(2)*sin(pi*a/180
+ pi/4)), Point(-5*sqrt(2)*sin(pi*a/180 + pi/4),
-5*sqrt(2)*cos(pi*a/180 + pi/4))]

You can also check your point rotation formula by rotating a point -theta (for CW):

>>> var('px py theta x0 y0')
(px, py, theta, x0, y0)
>>> R = Point(px,py).rotate(-theta, Point(x0,y0))
>>> R.x
x0 + (px - x0)*cos(theta) + (py - y0)*sin(theta)
>>> R.y
y0 + (-px + x0)*sin(theta) + (py - y0)*cos(theta)