0% found this document useful (0 votes)
9 views10 pages

Graphics Assm

The document outlines multiple line and circle drawing algorithms implemented in Python using matplotlib. It includes the DDA, Bresenham, and Mid-point algorithms for lines, as well as Mid-point and Bresenham algorithms for circles, providing code snippets and example outputs for each. Each algorithm is designed to take user input for coordinates or radius and visually plot the resulting shapes.

Uploaded by

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

Graphics Assm

The document outlines multiple line and circle drawing algorithms implemented in Python using matplotlib. It includes the DDA, Bresenham, and Mid-point algorithms for lines, as well as Mid-point and Bresenham algorithms for circles, providing code snippets and example outputs for each. Each algorithm is designed to take user input for coordinates or radius and visually plot the resulting shapes.

Uploaded by

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

LAB 2: DDA Line Drawing Algorithm

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())

print("Enter the y-coordinate of the center:")


y_center = int(input())

print("Enter the radius of the circle:")


radius = int(input())

def midpoint_circle(x_c, y_c, r):


x=0
y=r
p=1-r

x_coords = []
y_coords = []

def plot_circle_points(x, y):


x_coords.extend([
x + x_c, y + x_c, y + x_c, x + x_c,
-x + x_c, -y + x_c, -y + x_c, -x + x_c
])
y_coords.extend([
y + y_c, x + y_c, -x + y_c, -y + y_c,
-y + y_c, -x + y_c, x + y_c, y + y_c
])

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

plt.plot(x_coords, y_coords, 'ro') # 'ro' means red circles


plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Mid-point Circle Drawing Algorithm")
plt.axis('equal') # Ensures the circle looks like a circle
plt.grid(True)
plt.show()

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())

print("Enter the y-coordinate of the center:")


y_center = int(input())

print("Enter the radius of the circle:")


radius = int(input())

def bresenham_circle_modified(x_center, y_center, radius):


x=0
y = radius
d = 1 - radius

x_coords = []
y_coords = []

def plot_circle_points(x, y):


x_coords.extend([
x + x_center, y + x_center, y + x_center, x + x_center,
-x + x_center, -y + y_center, -y + x_center, -x + y_center
])
y_coords.extend([
y + y_center, x + y_center, -x + y_center, -y + y_center,
-y + y_center, -x + y_center, x + y_center, y + y_center
])

plot_circle_points(x, y) # Plot initial points

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)

plt.plot(x_coords, y_coords, 'ro') # 'ro' means red circles


plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Modified Bresenham Circle Drawing Algorithm")
plt.axis('equal') # Ensures the circle looks like a circle
plt.grid(True)

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

You might also like