0% found this document useful (0 votes)
20 views12 pages

I017 CG Lab5-1

Uploaded by

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

I017 CG Lab5-1

Uploaded by

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

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER V

COURSE: Computer Graphics Practical Experiment: 5

Part A (To be referred by students)

SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp1)

Topic covered: 2D and 3D Transformation in Computer graphics.


Learning Objective: Learner would be able to
1. To understand the world coordinates and view point coordinates.
2. To recognize the World coordinate – It is the Cartesian coordinate w.r.t which we define the diagram,
like Xwmin, Xwmax, Ywmin, Ywmax.
3. To recognize the Device Coordinate –It is the screen coordinate where the objects are to be
displayed, like Xvmin, Xvmax, Yvmin, Yvmax.
4. It may be possible that the size of the Viewport is much smaller or greater than the Window. In these
cases, we have to increase or decrease the size of the Window according to the Viewport and for this,
we need some mathematical calculations

Prerequisites:-
- python

Outcomes:-
- Student will explore the method to fill the 2D and 3D geometry transformation in Computer graphics.

1|P a g e
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

Student Name Student Sap ID:-


Student Roll Number Date of Conduction :/ / 2024
Class :- MBA.Tech IT/B.Tech IT VI sem

Aim:- To learn the method to apply 2D and 3D transformation.

Assignment 5
Objective:To apply 2D and 3d on line and polygon entity.
1. Write a 2D transformation program using the methods listed below.
I. Translation (Line clipping)
Code:
import matplotlib.pyplot as plt

def translate(P,t):
plt.plot([P[0][0], P[1][0]], [P[0][1], P[1][1]], color='green')

P[0][0] += T[0]
P[0][1] += T[1]
P[1][0] += T[0]
P[1][1] += T[1]

plt.plot([P[0][0], P[1][0]], [P[0][1], P[1][1]], color='blue')


plt.show()

P = [[5, 8], [12, 18]]


T = [2, 1]
translate(P, T)

Output:

2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

II. 2.scaling (Line and Triangle)


Code:
import matplotlib.pyplot as plt

# function to draw the triangle


def draw_line(x1, y1, x2, y2):
plt.plot([x1, x2], [y1, y2], color='green')
plt.show()

# function to scale the line


def scale_line(p, q, r, s, Sx, Sy):
p_scaled = p * Sx
q_scaled = q * Sy
r_scaled = r * Sx
s_scaled = s * Sy

plt.plot([p_scaled, r_scaled], [q_scaled, s_scaled], color='blue')


plt.show()

p, q = map(float, input("Enter the first coordinate of a line (format: x y): ").split())


r, s = map(float, input("Enter the second coordinate of a line (format: x y): ").split())
Sx, Sy = map(float, input("Enter the scaling factors (format: Sx Sy): ").split())

draw_line(p, q, r, s)
scale_line(p, q, r, s, Sx, Sy)

3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

Ouptut:

III. 3.Rotations (Triangle)


Code:
import matplotlib.pyplot as plt

# function to draw the triangle


def draw_triangle(x1, y1, x2, y2, x3, y3):
plt.plot([x1, x2], [y1, y2], color='green')
plt.plot([x2, x3], [y2, y3], color='green')
plt.plot([x3, x1], [y3, y1], color='green')
plt.show()

# function to scale the triangle


def scale_triangle(x1, y1, x2, y2, x3, y3, Sx, Sy):
mx = (x1 + x2 + x3) / 3
my = (y1 + y2 + y3) / 3

a1 = mx + (x1 - mx) * Sx
b1 = my + (y1 - my) * Sy
a2 = mx + (x2 - mx) * Sx
b2 = my + (y2 - my) * Sy
4
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

a3 = mx + (x3 - mx) * Sx
b3 = my + (y3 - my) * Sy

plt.plot([a1, a2], [b1, b2], color='blue')


plt.plot([a2, a3], [b2, b3], color='blue')
plt.plot([a3, a1], [b3, b1], color='blue')
plt.show()

# driver code
x1, y1 = map(int, input("Enter the 1st point for the triangle (format: x y): ").split())
x2, y2 = map(int, input("Enter the 2nd point for the triangle (format: x y): ").split())
x3, y3 = map(int, input("Enter the 3rd point for the triangle (format: x y): ").split())
Sx, Sy = map(int, input("Enter the scaling factors (format: Sx Sy): ").split())

draw_triangle(x1, y1, x2, y2, x3, y3)


scale_triangle(x1, y1, x2, y2, x3, y3, Sx, Sy)

Output:

5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

IV. 4.Composite Transformation(Triangle)


Code:
import math
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def rotate_point(x, y, x_center, y_center, angle):


x_shifted = x - x_center
y_shifted = y - y_center
x_new = x_center + (x_shifted * math.cos(math.radians(angle)) - y_shifted *
math.sin(math.radians(angle)))
y_new = y_center + (x_shifted * math.sin(math.radians(angle)) + y_shifted *
math.cos(math.radians(angle)))
6
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

return x_new, y_new

if __name__ == '__main__':
# Define the vertices of the triangle
vertices = [[100, 100], [150, 200], [200, 150]]

# Calculate the centroid of the triangle


x_center = sum([v[0] for v in vertices]) / 3
y_center = sum([v[1] for v in vertices]) / 3

# Create a figure and two subplots


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# Plot the original triangle


ax1.plot([v[0] for v in vertices + [vertices[0]]], [v[1] for v in vertices + [vertices[0]]], 'r-')
ax1.set_title('Original Triangle')

# Rotate and plot the triangle around its center


rotated_vertices = []
for vertex in vertices:
new_x, new_y = rotate_point(vertex[0], vertex[1], x_center, y_center, 40) # Rotate by
90 degrees
rotated_vertices.append([new_x, new_y])
ax2.plot([v[0] for v in rotated_vertices + [rotated_vertices[0]]], [v[1] for v in
rotated_vertices + [rotated_vertices[0]]], 'b-')
ax2.set_title('Rotated Triangle')

plt.show()

Output:

7
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

2. Write a program for 3D transformation with following techniques on Cuboid.


I. Translation
Code:
import matplotlib.pyplot as plt
import numpy as np

def trans(x, y):


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x3 = [50, 60, 60, 50, 50]
y3 = [-100, -100, -90, -90, -100]
z3 = [0, 0, 0, 0, 0]
ax.bar3d(x3, y3, z3, 10, 10, 1)
ax.bar3d([xi + x for xi in x3], [yi - y for yi in y3], z3, 10, 10, 1)
plt.show()

x, y = map(int, input("Enter translation factors (x y): ").split())


trans(x, y)

Output:

II. Scaling
Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the 3D rectangle vertices


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

8
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]])

# Define the edges of the rectangle


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

# Create a figure and 3D axes


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

# Plot the original rectangle


for edge in edges:
ax.plot3D(*zip(*vertices[edge]), color='b')

# Apply scaling transformation


scale_factor = 2
scaled_vertices = vertices * scale_factor

# Plot the scaled rectangle


for edge in edges:
ax.plot3D(*zip(*scaled_vertices[edge]), color='r')

# Set plot labels


ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

Output:

9
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

III. Rotation
Code:
import numpy as np
import matplotlib.pyplot as plt

# Define the vertices of the 3D rectangle


vertices = 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 the edges of the rectangle


edges = [[0, 1, 2, 3, 0],
[4, 5, 6, 7, 4],
[0, 4],
[1, 5],
[2, 6],
[3, 7]]

# Plot original rectangle


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

for edge in edges:


ax.plot(vertices[edge, 0], vertices[edge, 1], vertices[edge, 2], 'b-')

# Define rotation angle (in radians) around y-axis


theta = np.pi / 4 # Rotate by 45 degrees

# Define rotation matrix around y-axis


rotation_matrix = np.array([[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]])

# Apply rotation transformation


rotated_vertices = np.dot(vertices, rotation_matrix)

# Plot rotated rectangle


for edge in edges:
ax.plot(rotated_vertices[edge, 0], rotated_vertices[edge, 1], rotated_vertices[edge, 2],
'r-')

# Set plot labels


10
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

Output:

IV. Reflection (Only x-axis).


Code:
import numpy as np
import matplotlib.pyplot as plt

# Define the vertices of the 3D rectangle


vertices = 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 the edges of the rectangle


edges = [[0, 1, 2, 3, 0],
[4, 5, 6, 7, 4],
[0, 4],
[1, 5],
[2, 6],
[3, 7]]

# Plot original rectangle


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

11
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 5

for edge in edges:


ax.plot(vertices[edge, 0], vertices[edge, 1], vertices[edge, 2], 'b-')

# Define reflection matrix about xy-plane


reflection_matrix = np.array([[1, 0, 0],
[0, 1, 0],
[0, 0, -1]])

# Apply reflection transformation


reflected_vertices = np.dot(vertices, reflection_matrix)

# Plot reflected rectangle


for edge in edges:
ax.plot(reflected_vertices[edge, 0], reflected_vertices[edge, 1], reflected_vertices[edge,
2], 'r-')

# Set plot labels


ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

Output:

Conclusion: -
Shearing is a process of slanting an object in 3D space either in x, y, or in the z-direction. Shearing changes, the
shape of the object. Shearing in 3D space can be done in any of the three directions: x, y, and z. Shearing in the x-
direction changes the y and z coordinates while keeping the x coordinate unchanged. Similarly, shearing in the y-
direction changes the x and z coordinates while keeping the y coordinate unchanged, and shearing in the z-direction
changes the x and y coordinates while keeping the z coordinate unchanged. Shearing is done through the Shearing
Transformation matrix. The matrix is multiplied by the coordinates of the object to get the new coordinates after
shearing. Shearing is used to change the shape of an existing object in a three-dimensional plane

12

You might also like