Skip to content

Instantly share code, notes, and snippets.

@uzl
Last active March 15, 2019 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzl/1c413683701271b21b257847bf5327bc to your computer and use it in GitHub Desktop.
Save uzl/1c413683701271b21b257847bf5327bc to your computer and use it in GitHub Desktop.
rotate 4 points(x,y) of a polygon with respect to a center coordinate
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@uzl
Copy link
Author

uzl commented Mar 15, 2019

from shapely.geometry import Point, LineString
import numpy as np
from PIL import Image
from shapely import affinity
import matplotlib.pyplot as plt
# from shapely.figures import SIZE, set_limits, plot_coords, plot_bounds, plot_line_issimple

import math


def rotate_box(angle, canvas_cord, landmark_cord):

    W, H = canvas_cord

    a = Point(landmark_cord)

    canvas_center_cord = Point(W/2, H/2)

    upper_left_cord = Point(0,0)
    lower_left_cord = Point(0, H)

    ve = 2
    if angle > 90:
        ve = 2 + angle // 90
        angle = angle % 90

    p = math.pow(-1, ve)
    angle = angle*p

    new_upper_left_cord = affinity.rotate(upper_left_cord, angle, origin=canvas_center_cord)
    new_lower_left_cord = affinity.rotate(lower_left_cord, angle, origin=canvas_center_cord)

    shift_x = new_lower_left_cord.x
    shift_y = new_upper_left_cord.y




    current_center_cord = Point(canvas_center_cord.x - (shift_x),
                                canvas_center_cord.y - (shift_y) )

    return current_center_cord

for i in range(0,365,5):
    c = rotate_box(i, (200, 100), (50, 50))
    print('{}\t{}\t{}\n'.format(i, c.x,c.y))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment