Eric
Eric
in
Computer Graphics
R1UC408B
Submitted By
ERIC PRAJAPATI
23SCSE1012417
Submitted To
Dr. Monika
ERIC PRAJAPATI
23SCSE1012417
INDEX
PROGRAM 1
Objective : Write a program for drawing graphics primitives and color it.
Line :
Code :
Output :
Circle :
Code :
import numpy as np
import cv2
import matplotlib.pyplot as plt
canvas=np.zeros((300,300,1),dtype="uint8")
cv2.circle(canvas,(150,150),50,(255,0,255),-1)
plt.imshow(canvas)
plt.axis("off")
plt.show()
Output :
PROGRAM :- 2
Objective : Write a program to divide screen into four region and draw circle,
Graphics rectangle, arc and ellipse.
Code :
Output :
PROGRAM :- 3
Objective : Write a program for drawing a simple object.
Code :
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis("on")
plt.title("Triangle with Circle Inside")
plt.show()
Output :
PROGRAM :- 4
Objective : Write a program to draw a rectangle. The four vertices of it should be entered by the
end user.
Code :
x_coords.append(x1)
y_coords.append(y1)
plt.plot(x_coords, y_coords)
plt.title("Rectangle")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Output :
PROGRAM :- 5
Objevtive : Write a program for drawing a line using DDA Line Drawing Algorithm.
Code 1 :
plt.show()
Output :
Code 2 :
y = y + yinc
plt.plot(x_coorinates, y_coorinates, marker="o", markersize=1, markerfacecolor="green")
plt.grid("true")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("DDA Line Drawing Algorithm")
plt.show()
Output :
PROGRAM :- 6
Objective : Write a program for drawing a line using Bresahnams Line Drawing Algorithm.
Code :
points = []
dx = abs(x2 - x1)
dy = abs(y2 - y1)
x, y = x1, y1
p = 2 * dy - dx
while x != x2:
points.append((x, y))
x += incx
if p < 0:
p += 2 * dy
else:
p += 2 * dy - 2 * dx
y += incy
points.append((x, y))
return points
x1, y1 = 10, 20
x2, y2 = 80, 100
line_points = bresenham_line_decision_parameter(x1, y1, x2, y2)
Output :
PROGRAM :- 7
Objective : Write a program to draw circle using mid-point circle drawing algorithm.
Code :
while x <= y:
plot_circle_points(x_center, y_center, x, y, points)
x += 1
if p < 0:
p += 2 * x + 1
else:
y -= 1
p += 2 * (x - y) + 1
return points
# Example usage
r = 10
circle_points = midpoint_circle(r)
# Plotting
x_coords, y_coords = zip(*circle_points)
plt.figure(figsize=(6, 6))
plt.plot(x_coords, y_coords, 'ro') # red dots
JEET KUMAR PAL
23SCSE1012128
Computer Graphics
R1UC408B
plt.gca().set_aspect('equal', adjustable='box')
plt.title(f"Midpoint Circle Drawing (r={r})")
plt.grid(True)
plt.show()
Output :
PROGRAM :- 8
Objective : Write a program to draw two concentric circles using mid-point circle drawing
algorithm.
Code :
while x <= y:
points.extend([(xc + x, yc + y), (xc + y, yc + x),
(xc - x, yc + y), (xc - y, yc + x),
(xc + x, yc - y), (xc + y, yc - x),
(xc - x, yc - y), (xc - y, yc - x)])
x += 1
if p < 0:
p += 2 * x + 1
else:
y -= 1
p += 2 * (x - y) + 1
return points
xc, yc = 0, 0
r1 = 4
r2 = 8
x1, y1 = zip(*points1)
x2, y2 = zip(*points2)
plt.scatter(x1, y1, marker='.', color='blue')
plt.scatter(x2, y2, marker='.', color='red')
plt.gca().set_aspect('equal', adjustable='box')
plt.grid()
Output :
PROGRAM 9
Objective - Write a program to draw a house like figure and perform the following operations.
for _ in range(4):
pen.forward(200)
pen.left(90)
for _ in range(2):
pen.forward(60)
pen.left(90)
JEET KUMAR PAL
23SCSE1012128
Computer Graphics
R1UC408B
pen.forward(100)
pen.left(90)
pen.hideturtle()
# Exit on click
screen.exitonclick()
import turtle
# Set up the screen
screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Turtle Graphics: House")
for _ in range(4):
pen.forward(200)
pen.left(90)
pen.end_fill()
pen.goto(-100, 100)
pen.pendown()
pen.color("red")
pen.begin_fill()
pen.goto(0, 200)
pen.goto(100, 100)
pen.goto(-100, 100)
pen.end_fill()
for _ in range(2):
pen.forward(60)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.end_fill()
pen.hideturtle()
# Exit on click
screen.exitonclick()