Graphics Assm
Graphics Assm
Code:
import matplotlib.pyplot as plt
x1=int(input("Enter the value of X1:"))
y1=int(input("Enter the value of Y1:"))
x2=int(input("Enter the value of X2:"))
y2=int(input("Enter the value of Y2:"))
dy=y2-y1
dx=x2-x1
m=dy/dx
if dy>dx:
steps=dy
else:
steps=dx
xcor=[]
ycor=[]
i=0
while i<steps:
i=i+1
if m<1:
x1=x1+1
y1=y1+m
elif m>1:
x1=x1+1/m
y1=y1+1
else:
x1=x1+1
y1=y1+1
xcor.append(x1)
ycor.append(y1)
print("X1:",x1,"Y1:",y1)
plt.plot(xcor,ycor,marker="o",color="red")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("DDA Algorithm")
plt.show()
1|Page
Output:
Enter the value of X1:32
Enter the value of Y1:35
Enter the value of X2:41
Enter the value of Y2:41
X1: 33 Y1: 35.666666666666664
X1: 34 Y1: 36.33333333333333
X1: 35 Y1: 36.99999999999999
X1: 36 Y1: 37.66666666666666
X1: 37 Y1: 38.33333333333332
X1: 38 Y1: 38.999999999999986
X1: 39 Y1: 39.66666666666665
X1: 40 Y1: 40.333333333333314
X1: 41 Y1: 40.99999999999998
2|Page
LAB 3: Bresenham Line Drawing algorithm
Code:
import matplotlib.pyplot as plt
print("Enter the value of x1")
x1=int(input())
print("Enter the value of x2")
x2=int(input())
print("Enter the value of y1")
y1=int(input())
print("Enter the value of y2")
y2=int(input())
dx= x2-x1
dy=y2-y1
pk=2*dy-dx
if abs(dx) > abs(dy):
steps = abs(dx)
else:
steps = abs(dy)
xcoordinate = []
ycoordinate = []
i=0
while i<steps:
i+=1
if pk>=0:
x1=x1+1
y1=y1+1
pk=pk+2*dy-2*dx
elif pk<1:
x1=x1+1
y1=y1
pk=pk+2*dy
print("x1: ",x1, "y1:", y1)
xcoordinate.append(x1)
ycoordinate.append(y1)
plt.plot(xcoordinate,ycoordinate, color='red', marker='o')
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Bresenham Line Drawing Algorithm")
plt.show()
3|Page
Output:
Enter the value of x1
32
Enter the value of x2
41
Enter the value of y1
35
Enter the value of y2
41
x1: 33 y1: 36
x1: 34 y1: 36
x1: 35 y1: 37
x1: 36 y1: 38
x1: 37 y1: 38
x1: 38 y1: 39
x1: 39 y1: 40
x1: 40 y1: 40
x1: 41 y1: 41
4|Page
LAB 4: Mid-point Line Drawing algorithm
Code:
import matplotlib.pyplot as plt
print("Enter the value of x1")
x1=int(input())
print("Enter the value of x2")
x2=int(input())
print("Enter the value of y1")
y1=int(input())
print("Enter the value of y2")
y2=int(input())
dx= x2-x1
dy=y2-y1
Dk=2*dy-dx
D=2*(dy-dx)
if abs(dx) > abs(dy):
steps = abs(dx)
else:
steps = abs(dy)
xcoordinate = []
ycoordinate = []
i=0
while i<steps:
i+=1
if Dk>=0:
x1=x1+1
y1=y1+1
Dk=Dk+D
elif Dk<1:
x1=x1+1
y1=y1
Dk=Dk+2*dy
print("x1: ",x1, "y1:", y1)
xcoordinate.append(x1)
ycoordinate.append(y1)
plt.plot(xcoordinate,ycoordinate, color='red', marker='o')
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Mid-point Line Drawing Algorithm")
plt.show()
5|Page
Output:
Enter the value of x1
20
Enter the value of x2
30
Enter the value of y1
10
Enter the value of y2
18
x1: 21 y1: 11
x1: 22 y1: 12
x1: 23 y1: 12
x1: 24 y1: 13
x1: 25 y1: 14
x1: 26 y1: 15
x1: 27 y1: 16
x1: 28 y1: 16
x1: 29 y1: 17
x1: 30 y1: 18
6|Page
Lab 5: Mid point Circle Algo
Code:
import matplotlib.pyplot as plt
print("Enter the x-coordinate of the center:")
x_center = int(input())
x_coords = []
y_coords = []
while x <= y:
plot_circle_points(x, y)
if p < 0:
p=p+2*x+3
else:
p = p + 2 * (x - y) + 5
y -= 1
x += 1
7|Page
Output:
Enter the x-coordinate of the center:
0
Enter the y-coordinate of the center:
0
Enter the radius of the circle:
10
8|Page
Lab 6: Bresenham Circle Algo
Code:
import matplotlib.pyplot as plt
print("Enter the x-coordinate of the center:")
x_center = int(input())
x_coords = []
y_coords = []
while y >= x:
x += 1
if d >= 0: #changed to >= to work with 1-r
y -= 1
d = d + 2 * (x - y) + 1
else:
d=d+2*x+3
plot_circle_points(x, y)
9|Page
plt.show()
Output:
Enter the x-coordinate of the center:
0
Enter the y-coordinate of the center:
0
Enter the radius of the circle:
8
10 | P a g e