Graphics DA 3
Graphics DA 3
Digital Assignment - 3
Cube:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
points = np.array([
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]
])
Z=[
[points[j] for j in [0, 1, 2, 3]], # Bottom face
[points[j] for j in [4, 5, 6, 7]], # Top face
[points[j] for j in [0, 3, 7, 4]], # Front face
[points[j] for j in [1, 2, 6, 5]], # Back face
[points[j] for j in [0, 1, 5, 4]], # Left face
[points[j] for j in [2, 3, 7, 6]] # Right face
]
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, 2])
ax.set_ylim([0, 2])
ax.set_zlim([0, 2])
plt.show()
Output:
Rectangular Box:
# Rectangular box
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
points = np.array([[0, 0, 0],
[2, 0, 0], # Length extended to 2
[2, 1, 0], # Width is 1
[0, 1, 0],
[0, 0, 1], # Height is 1
[2, 0, 1],
[2, 1, 1],
[0, 1, 1]])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, 3])
ax.set_ylim([0, 3])
ax.set_zlim([0, 2])
plt.show()
Output:
Triangular Pyramid
# Triangular pyramid
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
points = np.array([
[0, 0, 0], # Vertex 0
[1, 0, 0], # Vertex 1
[0.5, 1, 0], # Vertex 2 (Middle of the base)
[0.5, 0.5, 1.5] # Vertex 3 (Top vertex, above center of the base, made taller)
])
=Z = [
[points[j] for j in [0, 1, 2]], # Base face
[points[j] for j in [0, 1, 3]], # Side face 1
[points[j] for j in [1, 2, 3]], # Side face 2
[points[j] for j in [2, 0, 3]] # Side face 3
]
ax.view_init(elev=20, azim=30)
Output:
Rectangular Pyramid:
#Recangular Pyramid
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
base_vertices = np.array([
[1, 1, 0], # Bottom right of the square base
[-1, 1, 0], # Bottom left of the square base
[-1, -1, 0], # Top left of the square base
[1, -1, 0] # Top right of the square base
])
sides = []
for i in range(len(base_vertices)):
sides.append([base_vertices[i], base_vertices[(i+1) % len(base_vertices)], apex])
Output:
Q2. Perform 3D transformation on the drawn object.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
original_points = np.array([
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]
])
# Translation vector
translation_vector = np.array([2, 2, 0])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, 4])
ax.set_ylim([0, 4])
ax.set_zlim([0, 2])
plt.show()
Output:
original_points = np.array([
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]
])
faces = [
[0, 1, 2, 3], # Bottom face
[4, 5, 6, 7], # Top face
[0, 3, 7, 4], # Front face
[1, 2, 6, 5], # Back face
[0, 1, 5, 4], # Left face
[2, 3, 7, 6] # Right face
]
Output:
Q4. Perform 3D scaling with respect to the pivot point of the drawn object
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
original_points = np.array([
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]
])
faces = [
[0, 1, 2, 3], # Bottom face
[4, 5, 6, 7], # Top face
[0, 3, 7, 4], # Front face
[1, 2, 6, 5], # Back face
[0, 1, 5, 4], # Left face
[2, 3, 7, 6] # Right face
]
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([-0.5, 2])
ax.set_ylim([-0.5, 2])
ax.set_zlim([-0.5, 2])
legend_elements = [
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor='blue', markersize=10,
label='Original Cube'),
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor='red', markersize=10,
label='Scaled Cube')
]
ax.legend(handles=legend_elements, loc='upper right')
plt.show()
Output:
Q5. Perform the 3D rotation concerning the x-axis, y-axis, and z-axis on the drawn
3D object.
x_inc = dx / steps
y_inc = dy / steps
z_inc = dz / steps
x, y, z = x1, y1, z1
line_points = []
for _ in range(steps + 1):
line_points.append([x, y, z])
x += x_inc
y += y_inc
z += z_inc
return np.array(line_points)
Output:
Original Pyramid
Pyramid Rotated Around the X-axis: