Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotating a rectangle
#2
I think the pygame rotations are rotations about the Z axis (axis pointing out of the screen). Sounds like you want to rotate about the Y axis instead. This would make appear like the rectangle is coming out of the screen. Like this?
import math
import tkinter as tk

class Point():
    '''A point in space'''
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def rotate_y(self, angle, center=(0, 0, 0)):
        '''Rotate self around center.y.  Angle is in radians'''
        cx, cy, cz = center
        x = self.x - cx
        z = self.z - cz
        d = math.hypot(x, z)
        theta  = math.atan2(x, z) + angle
        self.z = cz + d * math.cos(theta)
        self.x = cx + d * math.sin(theta)

    def __str__(self):
        return f'({self.x}, {self.y}, {self.z})'

class Rectangle():
    def __init__(self, width, height):
        self.points = [
            Point(-width/2, height/2, 0),
            Point(width/2, height/2, 0),
            Point(width/2, -height/2, 0),
            Point(-width/2, -height/2, 0),
        ]

    def rotate_y(self, angle, center=(0, 0, 0)):
        '''Rotate self around center.y.  Angle is in radians'''
        for point in self.points:
            point.rotate_y(angle, center)

    def draw(self, canvas, center=(0, 0)):
        '''Draw self on canvas'''
        cx, cy = center
        for i in range(4):
            a = rectangle.points[i]
            b = rectangle.points[(i+1)%4]
            canvas.create_line(a.x+cx, a.y+cy, b.x+cx, b.y+cy)

    def __str__(self):
        return f'{self.points[0]}, {self.points[1]}, {self.points[2]}, {self.points[3]}'

rectangle = []

def rotate_y(rectangle, angle, center=(0, 0, 0)):
    canvas.delete("all")
    rectangle.rotate_y(angle, center)
    rectangle.draw(canvas, (140, 100))
    canvas.after(10, lambda: rotate_y(rectangle, angle, center))

root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()

rectangle = Rectangle(80, 80)
rotate_y(rectangle, 0.02, (-40, 0, 0))
root.mainloop()
For a more realistic view the rectangle should be drawn in perspective instead of drawing the projection.
Reply


Messages In This Thread
Rotating a rectangle - by CompleteNewb - Aug-23-2021, 09:20 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 09:54 AM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 04:15 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 05:49 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 06:19 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 08:22 PM
RE: Rotating a rectangle - by BashBedlam - Aug-24-2021, 09:46 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 11:30 PM
RE: Rotating a rectangle - by Windspar - Aug-25-2021, 04:15 AM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 03:47 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 03:55 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 04:34 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 05:27 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 06:44 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 07:13 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 07:32 PM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 09:39 AM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 11:42 AM
RE: Rotating a rectangle - by Windspar - Aug-27-2021, 05:18 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-28-2021, 03:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,629 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  Make rectangle snap to other rectangle corners Pedroski55 2 4,486 Aug-13-2021, 09:59 PM
Last Post: Pedroski55
  [PyGame] Rotating image issue Evoluxman 8 6,392 Oct-12-2019, 12:54 PM
Last Post: Evoluxman
  [PyGame] Rectangle keeps teleporting? keyfive 1 3,296 Jun-27-2018, 11:49 PM
Last Post: sonnekrieger7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020