0% found this document useful (0 votes)
13 views3 pages

Import Numpy As NP

The document contains Python code for 3D transformations of a cube using NumPy and Matplotlib. It defines functions for translation, scaling, rotation, and shearing, and includes an animation sequence demonstrating each transformation. The cube's vertices and faces are generated, and the transformations are visualized in a 3D plot.
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)
13 views3 pages

Import Numpy As NP

The document contains Python code for 3D transformations of a cube using NumPy and Matplotlib. It defines functions for translation, scaling, rotation, and shearing, and includes an animation sequence demonstrating each transformation. The cube's vertices and faces are generated, and the transformations are visualized in a 3D plot.
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/ 3

import numpy as np

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import time

# ===============================
# FUNCIONES DE TRANSFORMACIÓN
# ===============================

def traslacion(vertices, tx, ty, tz):


T = np.array([
[1, 0, 0, tx],
[0, 1, 0, ty],
[0, 0, 1, tz],
[0, 0, 0, 1]
])
return aplicar_transformacion(vertices, T)

def escalamiento(vertices, sx, sy, sz):


S = np.array([
[sx, 0, 0, 0],
[0, sy, 0, 0],
[0, 0, sz, 0],
[0, 0, 0, 1]
])
return aplicar_transformacion(vertices, S)

def rotacion_z(vertices, angulo):


rad = np.radians(angulo)
R = np.array([
[np.cos(rad), -np.sin(rad), 0, 0],
[np.sin(rad), np.cos(rad), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
return aplicar_transformacion(vertices, R)

def sesgado(vertices, shx, shy):


Sh = np.array([
[1, shx, 0, 0],
[shy, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
return aplicar_transformacion(vertices, Sh)

def aplicar_transformacion(vertices, M):


homog = np.c_[vertices, np.ones(len(vertices))]
transformados = homog.dot(M.T)
return transformados[:, :3]

# ===============================
# FUNCIONES DEL CUBO
# ===============================
def cubo_vertices():
r = [-1, 1]
return np.array([[x, y, z] for x in r for y in r for z in r])

def cubo_caras(vertices):
return [
[vertices[j] for j in [0, 1, 3, 2]],
[vertices[j] for j in [4, 5, 7, 6]],
[vertices[j] for j in [0, 1, 5, 4]],
[vertices[j] for j in [2, 3, 7, 6]],
[vertices[j] for j in [0, 2, 6, 4]],
[vertices[j] for j in [1, 3, 7, 5]]
]

def dibujar_cubo(ax, vertices, titulo):


ax.cla()
caras = cubo_caras(vertices)
ax.add_collection3d(Poly3DCollection(caras, facecolors='cyan',
linewidths=1, edgecolors='k',
alpha=0.6))
ax.set_title(titulo)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.set_xlim([-4, 4])
ax.set_ylim([-4, 4])
ax.set_zlim([-4, 4])

# ==============================================================
# ANIMACIÓN
# ==============================================================

fig = plt.figure(figsize=(6, 6))


ax = fig.add_subplot(111, projection='3d')

vertices = cubo_vertices()

# 1. Traslación
print("Animación: Traslación")
for t in np.linspace(0, 2, 30):
v = traslacion(vertices, t, t, 0)
dibujar_cubo(ax, v, f"Traslación Tx=Ty={t:.2f}")
plt.pause(0.05)

# 2. Escalamiento
print("Animación: Escalamiento")
for s in np.linspace(1, 2, 30):
v = escalamiento(vertices, s, 0.5*s, 1)
dibujar_cubo(ax, v, f"Escalamiento Sx={s:.2f}, Sy={0.5*s:.2f}")
plt.pause(0.05)

# 3. Rotación
print("Animación: Rotación")
for ang in range(0, 181, 5):
v = rotacion_z(vertices, ang)
dibujar_cubo(ax, v, f"Rotación Z={ang}°")
plt.pause(0.05)

# 4. Sesgado
print("Animación: Sesgado")
for sh in np.linspace(0, 1, 30):
v = sesgado(vertices, sh, sh/2)
dibujar_cubo(ax, v, f"Sesgado Shx={sh:.2f}, Shy={sh/2:.2f}")
plt.pause(0.05)

plt.show()

You might also like