Polygons python test

I need to work on the 12 sider

Showing 20 sider

import pygame
import math

# — Configuration —
WIDTH, HEIGHT = 800, 600
BACKGROUND_COLOR = (10, 10, 20)      # Dark Blue-Gray
LINE_COLOR = (255, 255, 255)         # White
ROTATION_SPEED = 0.01

# — Menu Configuration —
pygame.font.init()
try:
    MENU_FONT = pygame.font.SysFont(“sans”, 28)
except:
    MENU_FONT = pygame.font.Font(None, 36)

MENU_TEXT_COLOR = (200, 200, 200) # Light Gray
MENU_HIGHLIGHT_COLOR = (255, 215, 0) # Gold
MENU_BACKGROUND_COLOR = (30, 30, 40)
MENU_WIDTH = 220

# — Platonic Solid Data —
def get_platonic_solid_data(name):
    “””Provides the correctly scaled vertices and faces for a given Platonic solid.”””

    if name == “Tetrahedron”:
        scale = 150
        v_base = [(1,1,1), (-1,-1,1), (-1,1,-1), (1,-1,-1)]
        f = [(0,1,2), (0,2,3), (0,3,1), (1,3,2)]
        v = [(p[0]*scale, p[1]*scale, p[2]*scale) for p in v_base]
        return v, f

    if name == “Cube”:
        scale = 130
        v_base = [(-1,-1,1), (1,-1,1), (1,1,1), (-1,1,1),
                  (-1,-1,-1), (1,-1,-1), (1,1,-1), (-1,1,-1)]
        f = [(0,1,2,3), (4,5,1,0), (5,6,2,1), (6,7,3,2), (7,4,0,3), (7,6,5,4)]
        v = [(p[0]*scale, p[1]*scale, p[2]*scale) for p in v_base]
        return v, f

    if name == “Octahedron”:
        scale = 160
        v_base = [(0,0,1), (0,0,-1), (1,0,0), (-1,0,0), (0,1,0), (0,-1,0)]
        f = [(0,2,4), (0,4,3), (0,3,5), (0,5,2), (1,2,5), (1,5,3), (1,3,4), (1,4,2)]
        v = [(p[0]*scale, p[1]*scale, p[2]*scale) for p in v_base]
        return v, f

    if name == “Dodecahedron”:
        # — CORRECTED AND VERIFIED DATA —
        scale = 100
        phi = (1 + math.sqrt(5)) / 2
        v_base = [
            (-1, -1, -1), ( 1, -1, -1), ( 1,  1, -1), (-1,  1, -1),
            (-1, -1,  1), ( 1, -1,  1), ( 1,  1,  1), (-1,  1,  1),
            (0, -1/phi, -phi), (0, -1/phi,  phi), (0,  1/phi, -phi), (0,  1/phi,  phi),
            (-1/phi, -phi, 0), ( 1/phi, -phi, 0), (-1/phi,  phi, 0), ( 1/phi,  phi, 0),
            (-phi, 0, -1/phi), ( phi, 0, -1/phi), ( phi, 0,  1/phi), (-phi, 0,  1/phi)
        ]
        f = [
            (3, 11,  7, 18, 19), (3, 19, 16,  2, 14), (3, 14, 15,  1, 11),
            (7, 11,  1,  5, 18), (1,  5, 13,  0, 10), (5, 13,  4, 17, 18),
            (4, 13,  0,  8, 12), (0,  8, 16, 19, 18), (2, 14, 15,  9, 10),
            (6, 17,  8, 12, 16), (6, 12,  4, 13, 5), (6, 17,  9, 15, 14)
        ]
        v = [(p[0]*scale, p[1]*scale, p[2]*scale) for p in v_base]
        return v, f

    if name == “Icosahedron”:
        scale = 130
        phi = (1 + math.sqrt(5)) / 2
        v_base = [
            (-1, phi, 0), ( 1, phi, 0), (-1, -phi, 0), ( 1, -phi, 0),
            (0, -1, phi), (0, 1, phi), (0, -1, -phi), (0, 1, -phi),
            (phi, 0, -1), (phi, 0, 1), (-phi, 0, -1), (-phi, 0, 1)
        ]
        f = [
            (0, 11, 5), (0, 5, 1), (0, 1, 7), (0, 7, 10), (0, 10, 11), (1, 5, 9),
            (5, 11, 4), (11, 10, 2), (10, 7, 6), (7, 1, 8), (3, 9, 4), (3, 4, 2),
            (3, 2, 6), (3, 6, 8), (3, 8, 9), (4, 9, 5), (2, 4, 11), (6, 2, 10),
            (8, 6, 7), (9, 8, 1)
        ]
        v = [(p[0]*scale, p[1]*scale, p[2]*scale) for p in v_base]
        return v, f

# — State Variables —
solid_names = [“Tetrahedron”, “Cube”, “Octahedron”, “Icosahedron”, “Dodecahedron”]
current_solid_name = solid_names[0]
vertices, faces = [], []
angle_x, angle_y, angle_z = 0, 0, 0

# — Function to load a solid —
def load_solid(name):
    global current_solid_name, vertices, faces, angle_x, angle_y, angle_z
    current_solid_name = name
    vertices, faces = get_platonic_solid_data(name)
    angle_x, angle_y, angle_z = 0, 0, 0

# — Build Menu Items —
menu_rects = {}
y_offset = 20
for name in solid_names:
    text_surf = MENU_FONT.render(name, True, MENU_TEXT_COLOR)
    rect = text_surf.get_rect(topleft=(20, y_offset))
    menu_rects[name] = rect
    y_offset += 40

# — Pygame Setup —
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“Select and Spin a Platonic Solid”)
clock = pygame.time.Clock()
load_solid(current_solid_name)

# — Main Loop —
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_AC_BACK: running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                for name, rect in menu_rects.items():
                    if rect.collidepoint(event.pos) and name != current_solid_name:
                        load_solid(name)
                        break

    angle_x += ROTATION_SPEED
    angle_y += ROTATION_SPEED
    angle_z += ROTATION_SPEED

    rotated_vertices = []
    for x, y, z in vertices:
        y_rot = y * math.cos(angle_x) – z * math.sin(angle_x)
        z_rot = y * math.sin(angle_x) + z * math.cos(angle_x)
        x_rot = x * math.cos(angle_y) – z_rot * math.sin(angle_y)
        z_final = x * math.sin(angle_y) + z_rot * math.cos(angle_y)
        x_final = x_rot * math.cos(angle_z) – y_rot * math.sin(angle_z)
        y_final = x_rot * math.sin(angle_z) + y_rot * math.cos(angle_z)
        rotated_vertices.append((x_final, y_final, z_final))

    projected_vertices = []
    for x, y, z in rotated_vertices:
        distance = 5
        scale = distance / (distance – z / 400)
        proj_x = int(x * scale + (WIDTH + MENU_WIDTH) / 2)
        proj_y = int(y * scale + HEIGHT / 2)
        projected_vertices.append((proj_x, proj_y))

    screen.fill(BACKGROUND_COLOR)

    if projected_vertices:
        # — MODIFIED DRAWING LOGIC FOR WIREFRAME —
        for face in faces:
            points = [projected_vertices[i] for i in face]
            # Draw only the outline of the polygon
            pygame.draw.polygon(screen, LINE_COLOR, points, 1)

    pygame.draw.rect(screen, MENU_BACKGROUND_COLOR, (0, 0, MENU_WIDTH, HEIGHT))
    for name, rect in menu_rects.items():
        color = MENU_HIGHLIGHT_COLOR if name == current_solid_name else MENU_TEXT_COLOR
        text_surf = MENU_FONT.render(name, True, color)
        screen.blit(text_surf, rect)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Leave a Reply