Expand wireframe 3d shape made in pygame

Viewed 97

So I have this draw_rectangular_prism function which, using pygame, draws 9 lines. I have added 100 to the original points so they will not be in the top left. I want to make the rectangular prism larger so it's not all clumped together. Here's my function:

def draw_rectangular_prism(color=BLACK, display_surface=display_surface, width=3): #using lines    (5, 0) (15, 0) (0, 2) (10, 2) (0, 10) (10, 10) (15, 8)
    draw_line((105, 100), (115, 100)) # (5, 0) : (15, 0)
    draw_line((100, 102), (105, 100)) # (0, 2) : (5, 0)
    draw_line((100, 102), (100, 110)) # (0, 2) : (0, 10)
    draw_line((100, 110), (110, 110)) # (0, 10) : (10, 10) 
    draw_line((110, 110), (115, 108)) # (10, 10) : (15, 8)
    draw_line((115, 108), (115, 100)) # (15, 8) : (15, 0)
    draw_line((100, 102), (110, 102)) # (0, 2) : (10, 2)
    draw_line((110, 102), (110, 110)) # (10, 2) : (10, 10)
    draw_line((110, 102), (115, 100)) # (10, 2) : (15, 0)
1 Answers

Pygame has no feature to scale the coordinates. You have to transform the coordinates your self.

Add a scale and origin argument to the draw_rectangular_prism function:

def draw_rectangular_prism(origin, scale, color=BLACK, display_surface=display_surface, width=3):

    lines = [
         ((5, 0), (15, 0)),
         ((0, 2), (5, 0)),
         ((0, 2), (0, 10)),
         ((0, 10), (10, 10)), 
         ((10, 10), (15, 8)),
         ((15, 8), (15, 0)),
         ((0, 2), (10, 2)),
         ((10, 2), (10, 10)),
         ((10, 2), (15, 0))]

    for p1, p2 in lines: 
        tp1 = (round(origin[0] + p1[0] * scale[0]), round(origin[1] + p1[1] * scale[1]))
        tp2 = (round(origin[0] + p2[0] * scale[0]), round(origin[1] + p2[1] * scale[1]))
        draw_line(tp1, tp2)

Minimal example:

import pygame
from pygame.locals import *

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

def draw_rectangular_prism(origin, scale, color, surf, width=3):

    lines = [
         ((5, 0), (15, 0)),
         ((0, 2), (5, 0)),
         ((0, 2), (0, 10)),
         ((0, 10), (10, 10)), 
         ((10, 10), (15, 8)),
         ((15, 8), (15, 0)),
         ((0, 2), (10, 2)),
         ((10, 2), (10, 10)),
         ((10, 2), (15, 0))]

    for p1, p2 in lines: 
        tp1 = (round(origin[0] + p1[0] * scale[0]), round(origin[1] + p1[1] * scale[1]))
        tp2 = (round(origin[0] + p2[0] * scale[0]), round(origin[1] + p2[1] * scale[1]))
        pygame.draw.line(surf, color, tp1, tp2, width)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window.fill(0)
    draw_rectangular_prism((50, 100), (10, 20), (255, 0, 0), window)
    draw_rectangular_prism((240, 140), (15, 10), (255, 255, 0), window)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
Related