You are on page 1of 9

COMPUTER GRAPHICS

Shivank Gupta
2K19/CO/364

Aim
To implement Translation an Object

Theory
Consider a point object O has to be moved from one position to another in a 2D plane.

Let-
• Initial coordinates of the object O = (Xold, Yold)
• New coordinates of the object O after translation = (Xnew, Ynew)
• Translation vector or Shift vector = (Tx, Ty)

Given a Translation vector (Tx, Ty)-


• Tx defines the distance the Xold coordinate has to be moved.
• Ty defines the distance the Yold coordinate has to be moved.

This translation is achieved by adding the translation coordinates to the old coordinates of the object as-
• Xnew = Xold + Tx (This denotes translation towards X axis)
• Ynew = Yold + Ty (This denotes translation towards Y axis)
In Matrix form, the above translation equations may be represented as-

• The homogeneous coordinates representation of (X, Y) is (X, Y, 1).


• Through this representation, all the transformations can be performed using matrix /
vectormultiplications.

The above translation matrix may be represented as a 3 x 3 matrix as-

2
Code
import matplotlib.pyplot as plt
import math

plt.style.use('seaborn')
plt.figure(figsize=(7,7))
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.xlim(-50,150)
plt.ylim(-50,150)
plt.title("Translation")

def line(x1, y1, x2, y2, c, l) :

xl = [x1, x2]
yl = [y1, y2]

plt.plot(xl, yl, color = c, label = l)

def polygon(x1, y1, x2, y2, x3, y3, x4, y4, c, l) :

xl = [x1, x2, x3, x4, x1]


yl = [y1, y2, y3, y4, y1]

plt.plot(xl,yl, color = c, label = l)

def circle(xc, yc, r, c, l) :

PI = math.pi
sa = 0 * (PI / 180)
ea = 360 * (PI / 180)

xl = []
yl = []

# loop for all theta


theta = sa
while theta <= ea :
x = (r * math.cos(theta))
y = (r * math.sin(theta))

xl.append(xc+x)
yl.append(yc+y)

theta += 0.01

3
plt.scatter(xl , yl, color = c, s = 3, label = l)

def ellipse(xc, yc, rx, ry, c, l) :

PI = math.pi
sa = 0 * (PI / 180)
ea = 360 * (PI / 180)

xl = []
yl = []

# loop for all theta


theta = sa
while theta <= ea :
x = (rx * math.cos(theta))
y = (ry * math.sin(theta))

xl.append(xc+x)
yl.append(yc+y)

theta += 0.01

plt.scatter(xl , yl, color = c, s = 3, label = l)

print("------ TRANSLATION ----- ")


print("1. Line")
print("2. Polygon")
print("3. Circle")
print("4. Ellipse")

choice = int(input("Choice : "))

if choice == 1 :

x1 = int(input("x1 : "))
y1 = int(input("y1 : "))
x2 = int(input("x2 : "))
y2 = int(input("y2 : "))
tx = int(input("tx : "))
ty = int(input("ty : "))

line(x1,y1,x2,y2,"red","Original Line")

x1_ = x1 + tx
y1_ = y1 + ty
x2_ = x2 + tx
y2_ = y2 + ty

4
line(x1_,y1_,x2_,y2_,"green", "Translated Line")

elif choice == 2 :

x1 = int(input("x1 : "))
y1 = int(input("y1 : "))
x2 = int(input("x2 : "))
y2 = int(input("y2 : "))
x3 = int(input("x3 : "))
y3 = int(input("y3 : "))
x4 = int(input("x4 : "))
y4 = int(input("y4 : "))
tx = int(input("tx : "))
ty = int(input("ty : "))

polygon(x1,y1,x2,y2,x3,y3,x4,y4,"red","Original Polygon")

x1_ = x1 + tx
y1_ = y1 + ty
x2_ = x2 + tx
y2_ = y2 + ty
x3_ = x3 + tx
y3_ = y3 + ty
x4_ = x4 + tx
y4_ = y4 + ty

polygon(x1_,y1_,x2_,y2_,x3_,y3_,x4_,y4_,"green","Translated Polygon")

elif choice == 3 :

x = int(input("x : "))
y = int(input("y : "))
r = int(input("r : "))
tx = int(input("tx : "))
ty = int(input("ty : "))

circle(x,y,r,"red", "Original Circle")

x_ = x + tx
y_ = y + ty

circle(x_,y_,r,"green","Translated Circle")

elif choice == 4 :

x = int(input("x : "))

5
y = int(input("y : "))
rx = int(input("rx : "))
ry = int(input("ry : "))
tx = int(input("tx : "))
ty = int(input("ty : "))

ellipse(x,y,rx,ry,"red","Original Ellipse")

x_ = x + tx
y_ = y + ty

ellipse(x_,y_,rx,ry,"green","Translated Ellipse")

plt.legend()
plt.show()

6
Output

7
8
Conclusion

Translation an Object was implemented and verified using the matplotlib.pyplot header file of Python. The
algorithm was simple to design and avoid using multiple operations of big-time complexity

You might also like