I017 CG Lab5-1
I017 CG Lab5-1
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
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]
Output:
2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
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
Ouptut:
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
a3 = mx + (x3 - mx) * Sx
b3 = my + (y3 - my) * Sy
# 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())
Output:
5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
if __name__ == '__main__':
# Define the vertices of the triangle
vertices = [[100, 100], [150, 200], [200, 150]]
plt.show()
Output:
7
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Output:
II. Scaling
Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
8
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]])
plt.show()
Output:
9
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
III. Rotation
Code:
import numpy as np
import matplotlib.pyplot as plt
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
Output:
11
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
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