Rotate point about another point in degrees python

203
April 16, 2022, at 9:00 PM

If you had a point (in 2d), how could you rotate that point by degrees around the other point (the origin) in python?

You might, for example, tilt the first point around the origin by 10 degrees.

Basically you have one point PointA and origin that it rotates around. The code could look something like this:

PointA=(200,300)
origin=(100,100)
NewPointA=rotate(origin,PointA,10) #The rotate function rotates it by 10 degrees
Answer 1

An option to rotate a point by some degrees about another point is to use numpy instead of math. This allows to easily generalize the function to take any number of points as input, which might e.g. be useful when rotating a polygon.

import numpy as np
def rotate(p, origin=(0, 0), degrees=0):
    angle = np.deg2rad(degrees)
    R = np.array([[np.cos(angle), -np.sin(angle)],
                  [np.sin(angle),  np.cos(angle)]])
    o = np.atleast_2d(origin)
    p = np.atleast_2d(p)
    return np.squeeze((R @ (p.T-o.T) + o.T).T)

points=[(200, 300), (100, 300)]
origin=(100,100)
new_points = rotate(points, origin=origin, degrees=10)
print(new_points)
Answer 2

The following rotate function performs a rotation of the point point by the angle angle (counterclockwise, in radians) around origin, in the Cartesian plane, with the usual axis conventions: x increasing from left to right, y increasing vertically upwards. All points are represented as length-2 tuples of the form (x_coord, y_coord).

import math
def rotate(origin, point, angle):
    """
    Rotate a point counterclockwise by a given angle around a given origin.
    The angle should be given in radians.
    """
    ox, oy = origin
    px, py = point
    qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
    qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
    return qx, qy

If your angle is specified in degrees, you can convert it to radians first using math.radians. For a clockwise rotation, negate the angle.

Example: rotating the point (3, 4) around an origin of (2, 2) counterclockwise by an angle of 10 degrees:

>>> point = (3, 4)
>>> origin = (2, 2)
>>> rotate(origin, point, math.radians(10))
(2.6375113976783475, 4.143263683691346)

Note that there's some obvious repeated calculation in the rotate function: math.cos(angle) and math.sin(angle) are each computed twice, as are px - ox and py - oy. I leave it to you to factor that out if necessary.

Answer 3

This is easy if you represent your points as complex numbers and use the exp function with an imaginary argument (which is equivalent to the cos/sin operations shown in the other answers, but is easier to write and remember). Here's a function that rotates any number of points about the chosen origin:

import numpy as np
def rotate(points, origin, angle):
    return (points - origin) * np.exp(complex(0, angle)) + origin

To rotate a single point (x1,y1) about the origin (x0,y0) with an angle in degrees, you could call the function with these arguments:

points = complex(x1,y1)
origin = complex(x0,y0)
angle = np.deg2rad(degrees)

To rotate multiple points (x1,y1), (x2,y2), ..., use:

points = np.array([complex(x1,y1), complex(x2,y2), ...])

An example with a single point (200,300) rotated 10 degrees about (100,100):

>>> new_point = rotate(complex(200,300), complex(100,100), np.deg2rad(10))
>>> new_point
(163.75113976783473+314.3263683691346j)
>>> (new_point.real, new_point.imag)
(163.75113976783473, 314.3263683691346)
Answer 4

After going through a lot of code and repositories. This function worked best for me. Also it is efficient as it calculates sine and cosine values only once.

import numpy as np
def rotate(point, origin, degrees):
    radians = np.deg2rad(degrees)
    x,y = point
    offset_x, offset_y = origin
    adjusted_x = (x - offset_x)
    adjusted_y = (y - offset_y)
    cos_rad = np.cos(radians)
    sin_rad = np.sin(radians)
    qx = offset_x + cos_rad * adjusted_x + sin_rad * adjusted_y
    qy = offset_y + -sin_rad * adjusted_x + cos_rad * adjusted_y
    return qx, qy
Answer 5
import math
def rotate(x,y,xo,yo,theta): #rotate x,y around xo,yo by theta (rad)
    xr=math.cos(theta)*(x-xo)-math.sin(theta)*(y-yo)   + xo
    yr=math.sin(theta)*(x-xo)+math.cos(theta)*(y-yo)  + yo
    return [xr,yr]
Rent Charter Buses Company
READ ALSO
Seaborn Lineplot- Displaying Values on top of the markers

Seaborn Lineplot- Displaying Values on top of the markers

We could display a values from dataframe to the graph using the brand new feature of the matplotlib in bar graph

86
Plotly: Scatter plot with dropdown menu and color by group

Plotly: Scatter plot with dropdown menu and color by group

I'm trying to make a scatter plot with 2 dropdown menus that select a data column (from a pandas data frame) to be plotted for x and y-axis, but I'm also wanting the points to be colored by a third categorical variable that is fixed (no dropdown needed...

131
How can I read output from multiple Linux commands as input streams in a Python script

How can I read output from multiple Linux commands as input streams in a Python script

I'm trying to write a Python program that takes 2 very large files as inputThey are both currently compressed on disk in

90