Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotating a rectangle
#18
A pygame program that tumbles a cube. Rotations about x, y and z.
import pygame, math
from pygame.locals import *
     
class Point():
    '''A point in space'''
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def projection(self):
        '''Return projection on xy plane.  Not doing perspective'''
        return self.x, self.y

    def move(self, x=0, y=0, z=0):
        '''Move self in x, y and z'''
        self.x += x
        self.y += y
        self.z += z

    def rotate_x(self, degrees, center=(0, 0, 0)):
        '''Rotate self about x axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation
        '''
        cx, cy, cz = center
        y = self.y - cy
        z = self.z - cz
        d = math.hypot(y, z)
        theta  = math.atan2(y, z) + degrees * math.pi/180
        self.z = cz + d * math.cos(theta)
        self.y = cy + d * math.sin(theta)

    def rotate_y(self, degrees, center=(0, 0, 0)):
        '''Rotate self about y axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation
        '''
        cx, cy, cz = center
        x = self.x - cx
        z = self.z - cz
        d = math.hypot(x, z)
        theta  = math.atan2(x, z) + degrees * math.pi/180
        self.z = cz + d * math.cos(theta)
        self.x = cx + d * math.sin(theta)

    def rotate_z(self, degrees, center=(0, 0, 0)):
        '''Rotate self about z axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation
        '''
        cx, cy, cz = center
        x = self.x - cx
        y = self.y - cy
        d = math.hypot(y, x)
        theta  = math.atan2(y, x) + degrees * math.pi/180
        self.x = cx + d * math.cos(theta)
        self.y = cy + d * math.sin(theta)

    def __str__(self):
        return f'({self.x}, {self.y}, {self.z})'
 
class Cube():
    '''A 3D cube'''
    def __init__(self, size, center=(0, 0, 0)):
        x, y, z = center
        a = size/2
        self.points = [
            Point(x-a, y-a, z+a),
            Point(x+a, y-a, z+a),
            Point(x+a, y+a, z+a),
            Point(x-a, y+a, z+a),
            Point(x-a, y-a, z-a),
            Point(x+a, y-a, z-a),
            Point(x+a, y+a, z-a),
            Point(x-a, y+a, z-a),
        ]

    def center(self):
        '''Calculate center of cube'''
        x = y = z = 0
        for point in self.points:
            x += point.x
            y += point.y
            z += point.z
        return x/8, y/8, z/8

    def move(self, x=0, y=0, z=0):
        '''Move cube in x, y and z'''
        for point in self.points:
            point.move(x, y, z)

    def rotate_x(self, degrees, center=None):
        '''Rotate self about x axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation.  If center not
        provided rotate about self.center.
        '''
        if center is None:
            center = self.center()
        for point in self.points:
            point.rotate_x(degrees, center)

    def rotate_y(self, degrees, center=None):
        '''Rotate self about y axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation.  If center not
        provided rotate about self.center.
        '''
        if center is None:
            center = self.center()
        for point in self.points:
            point.rotate_y(degrees, center)

    def rotate_z(self, degrees, center=None):
        '''Rotate self about z axis
        degrees is angle of rotation measured in degrees
        center is the center of rotation.  If center not
        provided rotate about self.center.
        '''
        if center is None:
            center = self.center()
        for point in self.points:
            point.rotate_z(degrees, center)

    def draw(self, surface, color=(255, 255, 255)):
        '''Draw self on surface'''
        for a, b in zip([0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3], [1, 2, 3, 0, 5, 6, 7, 4, 4, 5, 6, 7]):
            pygame.draw.line(surface, color, self.points[a].projection(), self.points[b].projection())

    def __str__(self):
        return f"{{{', '.join([str(point) for point in self.points])}}}"

FPS = 30
pygame.init()
DISPLAYSURF = pygame.display.set_mode((200, 200))
pygame.display.set_caption('Cube')
fpsClock = pygame.time.Clock()
cube = Cube(60, (160, 100, 0))
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            break
       
    DISPLAYSURF.fill((0, 0, 0))
    cube.rotate_x(1)
    cube.rotate_y(1)
    cube.rotate_z(1, (100, 100, 0))
    cube.draw(DISPLAYSURF)
    pygame.display.flip()
    fpsClock.tick(FPS)
  
pygame.quit()
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,547 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  Make rectangle snap to other rectangle corners Pedroski55 2 4,447 Aug-13-2021, 09:59 PM
Last Post: Pedroski55
  [PyGame] Rotating image issue Evoluxman 8 6,324 Oct-12-2019, 12:54 PM
Last Post: Evoluxman
  [PyGame] Rectangle keeps teleporting? keyfive 1 3,274 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