0% found this document useful (0 votes)
8 views23 pages

Graphics DA 3

Graphics
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)
8 views23 pages

Graphics DA 3

Graphics
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/ 23

Name: S Aditya

Registration Number: 20MIC0037


Course Code: CSI3011- Computer Graphics and Multimedia
Slot: L7+L8+L37+L38

Digital Assignment - 3

Q. Draw a 3D object like a cube, rectangular box, triangular pyramid, Square


pyramid, etc

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
]

colors = ['blue', 'red', 'green', 'yellow', 'purple', 'orange']


for z, color in zip(Z, colors):
xs, ys, zs = zip(*z)
ax.add_collection3d(Poly3DCollection([list(zip(xs, ys, zs))], facecolors=color, alpha=0.5)) #
Added transparency

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

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],
[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]])

# List of sides' polygons of the box


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

colors = ['blue', 'red', 'green', 'yellow', 'purple', 'orange']


for z, color in zip(Z, colors):
xs, ys, zs = zip(*z)
ax.add_collection3d(Poly3DCollection([list(zip(xs, ys, zs))], facecolors=color, alpha=0.5))

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
]

colors = ['blue', 'green', 'red', 'yellow']


for z, color in zip(Z, colors):
xs, ys, zs = zip(*z)
ax.add_collection3d(Poly3DCollection([list(zip(xs, ys, zs)),], facecolors=color, alpha=0.5))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

ax.view_init(elev=20, azim=30)

# Setting the limits for the axes


ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 2])
plt.show()

Output:
Rectangular Pyramid:

#Recangular 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')

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
])

apex = np.array([0, 0, 2])

base = [base_vertices[i] for i in range(len(base_vertices))]

sides = []
for i in range(len(base_vertices)):
sides.append([base_vertices[i], base_vertices[(i+1) % len(base_vertices)], apex])

ax.add_collection3d(Poly3DCollection([base], facecolors='yellow', linewidths=1, edgecolors='r',


alpha=0.5))
side_colors = ['blue', 'green', 'red', 'purple']

for side, color in zip(sides, side_colors):


ax.add_collection3d(Poly3DCollection([side], facecolors=color, linewidths=1, edgecolors='r',
alpha=0.5))
ax.set_xlim([-2, 2])
ax.set_ylim([-2, 2])
ax.set_zlim([0, 3])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

Output:
Q2. Perform 3D transformation on the drawn object.

# Translation of a 3-D 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')

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])

# Translating the cube by adding the translation vector to each point


translated_points = original_points + translation_vector
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
]

def plot_cube(points, ax, color):


for face in faces:
xs, ys, zs = zip(*points[face])
ax.add_collection3d(Poly3DCollection([list(zip(xs, ys, zs))], facecolors=color,
edgecolors='k', linewidths=1, alpha=0.25))

# Plot the original cube


plot_cube(original_points, ax, 'blue')

# Plot the translated cube


plot_cube(translated_points, ax, 'red')

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:

Q3. Perform 3D scaling concerning the origin of the drawn object.

# Scaling concerning the origin


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')

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]
])

scaling_factor = np.array([2, 2, 2]) # Scale by a factor of 2 along each axis


scaled_points = original_points * scaling_factor

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
]

def plot_cube(points, ax, color):


for face in faces:
xs, ys, zs = zip(*points[face])
ax.add_collection3d(Poly3DCollection([list(zip(xs, ys, zs))], facecolors=color,
edgecolors='k', linewidths=1, alpha=0.25))

# Plot the original cube


plot_cube(original_points, ax, 'blue')
# Plot the scaled cube
plot_cube(scaled_points, ax, 'red')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, max(scaling_factor) * 2])
ax.set_ylim([0, max(scaling_factor) * 2])
ax.set_zlim([0, max(scaling_factor) * 2])
plt.show()

Output:
Q4. Perform 3D scaling with respect to the pivot point of the drawn object

# Scaling around pivot point

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')

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]
])

# Define a pivot point (the center of the cube in this case)


pivot_point = np.array([0.5, 0.5, 0.5])
scaling_factor = np.array([1.3, 1.3, 1.3]) # Scale by a factor of 1.3 along each axis

# Translate points so pivot is at the origin, scale, then translate back


translated_points = original_points - pivot_point
scaled_translated_points = translated_points * scaling_factor
scaled_points = scaled_translated_points + pivot_point

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
]

def plot_cube(points, ax, color, label):


poly3d = [list(zip(points[face][:,0], points[face][:,1], points[face][:,2])) for face in faces]
collection = Poly3DCollection(poly3d, facecolors=color, edgecolors='k', linewidths=1,
alpha=0.25)
ax.add_collection3d(collection)
return collection

original_cube = plot_cube(original_points, ax, 'blue', 'Original Cube')

scaled_cube = plot_cube(scaled_points, ax, 'red', 'Scaled Cube')

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.

# Rotation along X, Y, Z direction


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def dda_line_3d(p1, p2):


x1, y1, z1 = p1
x2, y2, z2 = p2
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
steps = int(max(abs(dx), abs(dy), abs(dz)))

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)

def construct_pyramid(base_center, base_size, height):


half_size = base_size / 2
base_vertices = np.array([
[base_center[0] - half_size, base_center[1] - half_size, base_center[2]],
[base_center[0] - half_size, base_center[1] + half_size, base_center[2]],
[base_center[0] + half_size, base_center[1] + half_size, base_center[2]],
[base_center[0] + half_size, base_center[1] - half_size, base_center[2]],
])
apex = np.array([base_center[0], base_center[1], base_center[2] + height])
return np.vstack((base_vertices, apex))

def generate_edges_with_dda(vertices, edge_pairs):


all_line_points = []
for edge in edge_pairs:
p1, p2 = vertices[edge[0]], vertices[edge[1]]
line_points = dda_line_3d(p1, p2)
all_line_points.append(line_points)
return all_line_points

def plot_shape(vertices, all_line_points):


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

for line_points in all_line_points:


ax.plot(line_points[:, 0], line_points[:, 1], line_points[:, 2], 'b')
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2], color='r')
plt.show()

def rotate(vertices, angle, axis):


rad = np.deg2rad(angle)
if axis == 'x':
rotation_matrix = np.array([
[1, 0, 0],
[0, np.cos(rad), -np.sin(rad)],
[0, np.sin(rad), np.cos(rad)]
])
elif axis == 'y':
rotation_matrix = np.array([
[np.cos(rad), 0, np.sin(rad)],
[0, 1, 0],
[-np.sin(rad), 0, np.cos(rad)]
])
elif axis == 'z':
rotation_matrix = np.array([
[np.cos(rad), -np.sin(rad), 0],
[np.sin(rad), np.cos(rad), 0],
[0, 0, 1]
])
return vertices @ rotation_matrix.T

base_center = np.array([0, 0, 0])


base_size = 2
height = 3
vertices = construct_pyramid(base_center, base_size, height)
edges = [
[0, 1], [1, 2], [2, 3], [3, 0], # Base edges
[0, 4], [1, 4], [2, 4], [3, 4] # Side edges connecting to apex
]
all_line_points = generate_edges_with_dda(vertices, edges)
plot_shape(vertices, all_line_points)
angles = 45 # Rotate by 45 degrees around each axis
for axis in ['x', 'y', 'z']:
rotated_vertices = rotate(vertices, angles, axis)
rotated_line_points = generate_edges_with_dda(rotated_vertices, edges)
plot_shape(rotated_vertices, rotated_line_points)

Output:
Original Pyramid
Pyramid Rotated Around the X-axis:

Pyramid Rotated Around the Y-axis:


Pyramid Rotated Around the Z-axis:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy