Home > OS >  how i can rotate an object by a specified angle in python or jypiter notebook?
how i can rotate an object by a specified angle in python or jypiter notebook?

Time:11-16

enter image description here

I need to rotate object in python with this formula (enter image description here)

rotate an object by a specified angle.

CodePudding user response:

If you want to rotate an object, you are just changing the coordinates of the vertices.

You can do this using matrix multiplication:

import numpy as np

theta = np.radians(int(input("How many radians? ")))
c,s = np.cos(theta), np.sin(theta) #get sin and cosine of the angle

rotate = np.array(((c, -s), (s,c))) #put them into the rotation matrix


shape = np.array(((0,0))) #put the coordinates that you would like to rotate
print(f'new coords = {np.matmul(rotate,shape)}') #output

This takes the radians as an input and then rotates the coordinate. If you wanted to do the repeatedly on an object with multiple vertices (and therefore coordinates) you could iterate the calculation and append the new coordinates into an array:

import numpy as np

theta = np.radians(int(input("How many radians? ")))

number_of_coords = int(input("How many coordinates? "))

result = []

for i in range(number_of_coords):
    c,s = np.cos(theta), np.sin(theta) #get sin and cosin of the angle
    rotate = np.array(((c, -s), (s,c))) #put them into the rotation matrix

    for l in range(2):
        x = int(input("what is the first number? "))
        y = int(input("what is the second input? "))
        coord = (x,y)
        shape = np.array((coord)) 
        result.append(np.matmul(rotate,shape))


print(f"new shape = {result}")

This outputs the arrays that then contain the new coordinates

I hope this helps.

CodePudding user response:

Task variations:

  • move an object by a specified amount on the x and y axis,
  • scaling – grow or shrink the object by a specified amount on the x and y axis
  • rotate an object by a specified angle.

Move - x: 300, y: 100

Scale - x: 3, y: 2

Rotate - 120

Formulas for rotating - (

  • Related