0% found this document useful (0 votes)
3 views9 pages

COMputerDA 3

Computer 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)
3 views9 pages

COMputerDA 3

Computer 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/ 9

CSI3011 – COMPUTER GRAPHICS AND MULTIMEDIA

LAB ASSESSMENT – 3

Name: DevaDharshini.P

Reg.No: 20MIC0162

Implement the following Algorithms in Python/ C and get the one-


dimensional primitives as output.
1. Draw a 3D object like a cube, rectangular box, triangular pyramid,
square pyramid, etc. (2)
CODE:
import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d.art3d import Poly3DCollection

def draw_pyramid(vertices, ax, color='r', alpha=0.5, label=''):

edges = [[vertices[j] for j in [0, 1, 2]],

[vertices[j] for j in [0, 1, 3]],

[vertices[j] for j in [1, 2, 3]],

[vertices[j] for j in [2, 0, 3]]]

poly3d = Poly3DCollection(edges, alpha=alpha, linewidths=1, edgecolors=color)

poly3d.set_facecolor([0.5, 0.5, 1, alpha])

ax.add_collection3d(poly3d)

ax.scatter3D(vertices[:, 0], vertices[:, 1], vertices[:, 2], color=color, label=label)

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')
# De ine the vertices of the triangular pyramid

vertices = np.array([[0, 0, 0], # Vertex 0

[1, 0, 0], # Vertex 1

[0.5, np.sqrt(3)/2, 0], # Vertex 2

[0.5, np.sqrt(3)/6, np.sqrt(6)/3]]) # Vertex 3 (apex)

# Plotting the original triangular pyramid

ig = plt. igure()

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

ax.set_title('Original Triangular Pyramid')

draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

ax.legend()

plt.show()

OUTPUT:
f
f
f
f
2. Perform 3D transformation on the drawn object (3)
CODE:
def translate(vertices, tx, ty, tz):

T = np.array([[1, 0, 0, tx],

[0, 1, 0, ty],

[0, 0, 1, tz],

[0, 0, 0, 1]])

vertices_homogeneous = np.c_[vertices, np.ones(len(vertices))]

translated_vertices = vertices_homogeneous.dot(T.T)

return translated_vertices[:, :3]

# Plotting the original and translated triangular pyramid

ig = plt. igure()

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

ax.set_title('Original and Translated Triangular Pyramid')

draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

translated_vertices = translate(vertices, 1, 1, 1)

draw_pyramid(translated_vertices, ax, color='g', alpha=0.1, label='Translated')

ax.legend()

plt.show()
f
f
f
OUTPUT:
3. Perform 3D scaling with respect to pivot point of the drawn object
(3)
CODE:
def scale(vertices, sx, sy, sz, pivot):

S = np.array([[sx, 0, 0, 0],

[0, sy, 0, 0],

[0, 0, sz, 0],

[0, 0, 0, 1]])

pivot_homogeneous = np.append(pivot, 1)
vertices_homogeneous = np.c_[vertices, np.ones(len(vertices))]

scaled_vertices = (vertices_homogeneous - pivot_homogeneous).dot(S.T) +

pivot_homogeneous

return scaled_vertices[:, :3]

pivot_point = np.mean(vertices, axis=0) # Calculate the centroid of the triangular


pyramid

# Plotting the original and scaled triangular pyramid

ig = plt. igure()

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

ax.set_title('Original and Scaled Triangular Pyramid')

draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

scaled_vertices = scale(vertices, 2, 2, 2, pivot_point)

draw_pyramid(scaled_vertices, ax, color='b', alpha=0.1, label='Scaled')

ax.legend()

plt.show()
f
f
f
OUTPUT:
4. Perform the 3D rotation concerning the x-axis, y-axis, and z-axis on
the drawn 3D object. (2)
CODE:
def rotate_x(vertices, angle, pivot):

R = np.array([[1, 0, 0, 0],

[0, np.cos(angle), -np.sin(angle), 0],

[0, np.sin(angle), np.cos(angle), 0],

[0, 0, 0, 1]])

pivot_homogeneous = np.append(pivot, 1)
vertices_homogeneous = np.c_[vertices, np.ones(len(vertices))]

rotated_vertices = (vertices_homogeneous - pivot_homogeneous).dot(R.T) +

pivot_homogeneous

return rotated_vertices[:, :3]

# Plotting the original and rotated triangular pyramid around X axis

ig = plt. igure()

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

ax.set_title('Original and Rotated Triangular Pyramid around X')

draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

rotated_vertices_x = rotate_x(vertices, np.pi/4, pivot_point)

draw_pyramid(rotated_vertices_x, ax, color='g', alpha=0.1, label='Rotated X')

ax.legend()

plt.show()

def rotate_y(vertices, angle, pivot):

R = np.array([[np.cos(angle), 0, np.sin(angle), 0],

[0, 1, 0, 0],

[-np.sin(angle), 0, np.cos(angle), 0],

[0, 0, 0, 1]])

pivot_homogeneous = np.append(pivot, 1)
f
f
f
vertices_homogeneous = np.c_[vertices, np.ones(len(vertices))]

rotated_vertices = (vertices_homogeneous - pivot_homogeneous).dot(R.T) +


pivot_homogeneous

return rotated_vertices[:, :3]

# Plotting the original and rotated triangular pyramid around Y axis

ig = plt. igure()

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

ax.set_title('Original and Rotated Triangular Pyramid around Y')

draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

rotated_vertices_y = rotate_y(vertices, np.pi/4, pivot_point)

draw_pyramid(rotated_vertices_y, ax, color='b', alpha=0.1, label='Rotated Y')

ax.legend()

plt.show()

def rotate_z(vertices, angle, pivot):

R = np.array([[np.cos(angle), -np.sin(angle), 0, 0],

[np.sin(angle), np.cos(angle), 0, 0],

[0, 0, 1, 0],

[0, 0, 0, 1]])

pivot_homogeneous = np.append(pivot, 1)

vertices_homogeneous = np.c_[vertices, np.ones(len(vertices))]

rotated_vertices = (vertices_homogeneous - pivot_homogeneous).dot(R.T) +


pivot_homogeneous

return rotated_vertices[:, :3]

# Plotting the original and rotated triangular pyramid around Z axis

ig = plt. igure()

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

ax.set_title('Original and Rotated Triangular Pyramid around Z')


f
f
f
f
f
f
draw_pyramid(vertices, ax, color='r', alpha=0.1, label='Original')

rotated_vertices_z = rotate_z(vertices, np.pi/4, pivot_point)

draw_pyramid(rotated_vertices_z, ax, color='m', alpha=0.1, label='Rotated Z')

ax.legend()

plt.show()

OUTPUT:

You might also like