0% found this document useful (0 votes)
68 views32 pages

Figur As

Uploaded by

Maria Rivera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views32 pages

Figur As

Uploaded by

Maria Rivera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Figuras

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

import math

def generate_torus_vertices(num_segments=50, num_sides=30, major_radius=0.5,


minor_radius=0.2):

vertices = []

for i in range(num_segments):

theta = i * 2 * math.pi / num_segments

cos_theta = math.cos(theta)

sin_theta = math.sin(theta)

for j in range(num_sides):

phi = j * 2 * math.pi / num_sides

cos_phi = math.cos(phi)

sin_phi = math.sin(phi)

x = (major_radius + minor_radius * cos_phi) * cos_theta

y = (major_radius + minor_radius * cos_phi) * sin_theta

z = minor_radius * sin_phi

vertices.append((x, y, z))

return vertices

vertices = generate_torus_vertices(num_segments=50, num_sides=30, major_radius=0.5,


minor_radius=0.2)

def color_by_position(vertex):

x, y, z = vertex

r = abs(x)

g = abs(y)

b = abs(z)
return (r, g, b)

def Torus():

glBegin(GL_TRIANGLES)

for i in range(len(vertices)):

glColor3fv(color_by_position(vertices[i]))

glVertex3fv(vertices[i])

glColor3fv(color_by_position(vertices[(i + 1) % len(vertices)]))

glVertex3fv(vertices[(i + 1) % len(vertices)])

glColor3fv(color_by_position((0, 0, 0))) # Color para el centro de la dona

glVertex3fv((0, 0, 0)) # Vértice central

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -2) # Ajustar la distancia de la dona en z

glRotatef(90, 1, 0, 0) # Rotar la dona para que se vea mejor el relleno en el medio

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()
elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Torus()

pygame.display.flip()

pygame.time.wait(10)

main()
Esfera

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

import math

vertices = []

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1)] # Mismos colores que el
cubo

rotation_speed = 1 # Velocidad de rotación de la esfera

def Sphere(radius, num_vertices):

for i in range(num_vertices // 2):

theta1 = math.pi * i / (num_vertices // 2)

theta2 = math.pi * (i + 1) / (num_vertices // 2)

for j in range(num_vertices):

phi = 2 * math.pi * j / num_vertices

x1 = radius * math.sin(theta1) * math.cos(phi)

y1 = radius * math.sin(theta1) * math.sin(phi)

z1 = radius * math.cos(theta1)

x2 = radius * math.sin(theta2) * math.cos(phi)

y2 = radius * math.sin(theta2) * math.sin(phi)

z2 = radius * math.cos(theta2)

vertices.append((x1, y1, z1))

vertices.append((x2, y2, z2))


def DrawSphere():

glBegin(GL_QUAD_STRIP)

for i, vertex in enumerate(vertices):

glColor3fv(colors[i % len(colors)]) # Aplicar colores del cubo a la esfera

glVertex3fv(vertex)

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT]:

glRotatef(rotation_speed, 0, 1, 0)

if keys[pygame.K_RIGHT]:

glRotatef(-rotation_speed, 0, 1, 0)

if keys[pygame.K_UP]:

glRotatef(rotation_speed, 1, 0, 0)

if keys[pygame.K_DOWN]:

glRotatef(-rotation_speed, 1, 0, 0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

vertices.clear() # Limpiar los vértices para que no se acumulen en cada iteración

Sphere(1, 50) # Radio = 1, 50 * 2 = 100 vertices para una esfera suave

DrawSphere()

pygame.display.flip()

pygame.time.wait(10)

main()

Estrella

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

import math

def generate_star_vertices():

num_points = 10

outer_radius = 1.0
inner_radius = 0.5

depth = 0.5 # Profundidad de la estrella

angle = 0

angle_increment = math.pi * 2 / num_points

vertices = []

for i in range(num_points * 2):

radius = outer_radius if i % 2 == 0 else inner_radius

x = radius * math.cos(angle)

y = radius * math.sin(angle)

vertices.append((x, y, -depth)) # Aplicar profundidad

vertices.append((x, y, depth)) # Aplicar profundidad

angle += angle_increment

return vertices

vertices = generate_star_vertices()

# Definir los bordes para la estrella

edges = [(i, (i + 2) % len(vertices)) for i in range(0, len(vertices), 2)]

colors = (

(1, 0, 0),

(0, 1, 0),

(0, 0, 1),

(1, 1, 0),

(1, 0, 1),

(0, 1, 1),

def Star():

glBegin(GL_QUAD_STRIP)

for i, vertex in enumerate(vertices):


glColor3fv(colors[i % len(colors)])

glVertex3fv(vertex)

glEnd()

glBegin(GL_LINES)

for i, edge in enumerate(edges):

glColor3fv((1, 1, 1)) # Color de los bordes en blanco

for vertex in edge:

glVertex3fv(vertices[vertex])

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -7) # Ajustar la distancia de la estrella en z

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1
elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

# Eliminar la rotación constante

# glRotatef(1, 1, 1, 1)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Star()

pygame.display.flip()

pygame.time.wait(10)

main()
cubo

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

vertices = (

(1,-1,-1),

(1,1,-1),

(-1,1,-1),

(-1,-1,-1),

(1,-1,1),

(1,1,1),

(-1,-1,1),

(-1,1,1)

bordes = (

(0,1),

(0,3),

(0,4),

(2,1),

(2,3),

(2,7),

(6,3),

(6,4),

(6,7),

(5,1),

(5,4),

(5,7)

)
colors = (

(1,0,0),

(0,1,0),

(0,0,1),

(0,1,0),

(1,1,1)

superficies = (

(0,1,2,3),

(3,1,7,6),

(6,7,5,4),

(4,5,1,0),

(1,5,7,2),

(4,0,3,6)

def cubo():

glBegin(GL_QUADS)

for i, superficie in enumerate(superficies):

for j, vertice in enumerate(superficie):

if j < len(colors): # Verifica que el índice del color sea válido

glColor3fv(colors[j])

glVertex3fv(vertices[vertice])

glEnd()

glBegin(GL_LINES)

for borde in bordes:

for vertice in borde:

glVertex3fv(vertices[vertice])

glEnd()
def main():

pygame.init()

display = (1280, 720)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

glRotatef(25, 2, 1, 0) # Rotación inicial

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

glRotatef(1, 0, -1, 0)

if event.key == pygame.K_RIGHT:

glRotatef(1, 0, 1, 0)

if event.key == pygame.K_UP:

glRotatef(1, -1, 0, 0)

if event.key == pygame.K_DOWN:

glRotatef(1, 1, 0, 0)

if event.type == pygame.MOUSEBUTTONDOWN:

if event.button == 4:

glTranslatef(0, 0, .1)

if event.button == 5:

glTranslatef(0, 0, -.1)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
cubo()

pygame.display.flip()

pygame.time.wait(10)

main()

Heptágono

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

vertices = (

(0, 1, 0),

(0.623, 0.782, 0),

(0.951, 0.309, 0),

(0.951, -0.309, 0),

(0.623, -0.782, 0),

(0, -1, 0),

(-0.623, -0.782, 0),


(-0.951, -0.309, 0),

(-0.951, 0.309, 0),

(-0.623, 0.782, 0),

(0, 0, 1)

edges = (

(0, 1),

(1, 2),

(2, 3),

(3, 4),

(4, 5),

(5, 6),

(6, 7),

(7, 8),

(8, 9),

(9, 0),

(0, 10),

(1, 10),

(2, 10),

(3, 10),

(4, 10),

(5, 10),

(6, 10),

(7, 10),

(8, 10),

(9, 10)

colors = (

(1, 0, 0), # Color del vértice 0


(0, 1, 0), # Color del vértice 1

(0, 0, 1), # Color del vértice 2

(1, 1, 0), # Color del vértice 3

(1, 0, 1), # Color del vértice 4

(0, 1, 1), # Color del vértice 5

(0.5, 0.5, 0.5), # Color del vértice 6

(0, 0.5, 0.5), # Color del vértice 7

(0.5, 0, 0.5), # Color del vértice 8

(0.5, 0.5, 0), # Color del vértice 9

(1, 1, 1), # Color del vértice 10 (centro)

def Hectagon():

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

glBegin(GL_POLYGON)

for i, vertex in enumerate(vertices[:10]):

glColor3fv(colors[i])

glVertex3fv(vertex)

glEnd()

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Dibujar solo los bordes

glBegin(GL_LINES)

for i, edge in enumerate(edges):

glColor3fv(colors[i % 10]) # Color del borde igual al color del vértice


correspondiente

for vertex in edge:

glVertex3fv(vertices[vertex])

glEnd()

def main():

pygame.init()
display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Hectagon()

pygame.display.flip()

pygame.time.wait(10)

main()

Pentagono

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

vertices = (

(0, 1, 0),
(0.951, 0.309, 0),

(0.588, -0.809, 0),

(-0.588, -0.809, 0),

(-0.951, 0.309, 0),

(0, 0, 1)

edges = (

(0, 1),

(1, 2),

(2, 3),

(3, 4),

(4, 0),

(0, 5),

(1, 5),

(2, 5),

(3, 5),

(4, 5)

colors = (

(1, 0, 0), # Color del vértice 0

(0, 1, 0), # Color del vértice 1

(0, 0, 1), # Color del vértice 2

(1, 1, 0), # Color del vértice 3

(1, 0, 1), # Color del vértice 4

(0, 1, 1), # Color del vértice 5

def Pentagon():

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glBegin(GL_POLYGON)

for i, vertex in enumerate(vertices[:5]):

glColor3fv(colors[i])

glVertex3fv(vertex)

glEnd()

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Dibujar solo los bordes

glBegin(GL_LINES)

for i, edge in enumerate(edges):

glColor3fv(colors[i % 5]) # Color del borde igual al color del vértice correspondiente

for vertex in edge:

glVertex3fv(vertices[vertex])

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()
elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Pentagon()

pygame.display.flip()

pygame.time.wait(10)

main()
pirámide 4

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

vertices = (

(0, 1, 0),

(1, -1, -1),

(-1, -1, -1),

(-1, -1, 1),

(1, -1, 1)

)
faces = (

(0, 1, 2),

(0, 1, 3),

(0, 2, 4),

(0, 3, 4),

(1, 2, 3),

(1, 3, 4),

(1, 2, 4),

(0, 2, 3)

# Define los colores para los vértices

colors = (

(1, 0, 0), # Color del vértice 0

(0, 1, 0), # Color del vértice 1

(0, 0, 1), # Color del vértice 2

(1, 1, 0), # Color del vértice 3

(1, 0, 1) # Color del vértice 4

def Pyramid():

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

glBegin(GL_TRIANGLES)

for face in faces:

for vertex in face:

glColor3fv(colors[vertex])

glVertex3fv(vertices[vertex])

glEnd()

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

glBegin(GL_LINES)
for face in faces:

for vertex in face:

glColor3fv((0, 0, 0))

glVertex3fv(vertices[vertex])

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:


rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Pyramid()

pygame.display.flip()

pygame.time.wait(10)

main()

Piramide
import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *

verticies = (

(1, -1, -1),

(1, 1, -1),

(-1, 1, -1),

(-1, -1, -1),

(0, 0, 1)

edges = (

(4, 0),

(4, 1),

(4, 2),

(4, 3),

(0, 1),

(0, 3),

(2, 1),

(2, 3)

surfaces = (

(0, 1, 2, 3),

(0, 3, 2, 4),

(0, 4, 2, 1),

(0, 1, 2, 4)

)
colors = (

(1, 0, 0),

(0, 1, 0),

(0, 0, 1),

(0, 1, 0),

(1, 1, 1),

(0, 1, 1),

(1, 0, 0),

(0, 1, 0),

(0, 0, 1),

(1, 0, 0),

(1, 1, 1),

(0, 1, 1)

def Cube():

glBegin(GL_QUADS)

for surface in surfaces:

x=0

for vertex in surface:

x += 1

glColor3fv(colors[x])

glVertex3fv(verticies[vertex])

glEnd()

glBegin(GL_LINES)

for edge in edges:

for vertex in edge:

glVertex3fv(verticies[vertex])

glEnd()
def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)

gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)

Cube()

pygame.display.flip()

pygame.time.wait(10)

main()

Prisma

import pygame

from pygame.locals import *

from OpenGL.GL import *

from OpenGL.GLU import *


# Define los vértices del prisma rectangular (cubo) con las dimensiones
especificadas

vertices = (

(-4, -2.5, -2.5),

(4, -2.5, -2.5),

(4, 2.5, -2.5),

(-4, 2.5, -2.5),

(-4, -2.5, 2.5),

(4, -2.5, 2.5),

(4, 2.5, 2.5),

(-4, 2.5, 2.5)

# Define los bordes del prisma rectangular (cubo)

edges = (

(0, 1),

(1, 2),

(2, 3),

(3, 0),

(4, 5),

(5, 6),

(6, 7),

(7, 4),

(0, 4),

(1, 5),

(2, 6),

(3, 7)

# Define los colores para los vértices

colors = (
(1, 0, 0), # Color del vértice 0

(0, 1, 0), # Color del vértice 1

(0, 0, 1), # Color del vértice 2

(1, 1, 0), # Color del vértice 3

(1, 0, 1), # Color del vértice 4

(0, 1, 1), # Color del vértice 5

(0.5, 0.5, 0.5), # Color del vértice 6

(0, 0.5, 0.5) # Color del vértice 7

def RectangularPrism():

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

glBegin(GL_QUADS)

for i, vertex in enumerate(vertices):

glColor3fv(colors[i])

glVertex3fv(vertex)

glEnd()

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

glBegin(GL_LINES)

for edge in edges:

for vertex in edge:

glVertex3fv(vertices[vertex])

glEnd()

def main():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

pygame.mouse.set_visible(True)
gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

glTranslatef(0.0, 0.0, -15) # Aleja el prisma para que sea visible

rotate_x = 0

rotate_y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

rotate_y = 1

elif event.key == pygame.K_RIGHT:

rotate_y = -1

elif event.key == pygame.K_UP:

rotate_x = 1

elif event.key == pygame.K_DOWN:

rotate_x = -1

elif event.type == pygame.KEYUP:

if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:

rotate_y = 0

elif event.key in [pygame.K_UP, pygame.K_DOWN]:

rotate_x = 0

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glRotatef(rotate_x, 1, 0, 0)

glRotatef(rotate_y, 0, 1, 0)
RectangularPrism()

pygame.display.flip()

pygame.time.wait(10)

main()

You might also like