Professional Documents
Culture Documents
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)
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-
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")
xl = [x1, x2]
yl = [y1, y2]
PI = math.pi
sa = 0 * (PI / 180)
ea = 360 * (PI / 180)
xl = []
yl = []
xl.append(xc+x)
yl.append(yc+y)
theta += 0.01
3
plt.scatter(xl , yl, color = c, s = 3, label = l)
PI = math.pi
sa = 0 * (PI / 180)
ea = 360 * (PI / 180)
xl = []
yl = []
xl.append(xc+x)
yl.append(yc+y)
theta += 0.01
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 : "))
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