0% found this document useful (0 votes)
5 views21 pages

Eric

This document is a lab practical file for a Bachelor of Technology in Computer Science and Engineering, focusing on Computer Graphics. It includes a series of programming tasks related to graphics primitives, drawing shapes, and implementing algorithms such as DDA and Bresenham's line drawing. The file is submitted by Eric Prajapati and includes code examples and objectives for each practical exercise.

Uploaded by

savitapatel1724
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)
5 views21 pages

Eric

This document is a lab practical file for a Bachelor of Technology in Computer Science and Engineering, focusing on Computer Graphics. It includes a series of programming tasks related to graphics primitives, drawing shapes, and implementing algorithms such as DDA and Bresenham's line drawing. The file is submitted by Eric Prajapati and includes code examples and objectives for each practical exercise.

Uploaded by

savitapatel1724
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/ 21

Bachelor of Technology

in

Computer Science and Engineering

Lab Practical File

Computer Graphics
R1UC408B
Submitted By
ERIC PRAJAPATI
23SCSE1012417

Submitted To
Dr. Monika

Department of Computer Science and Engineering


School of Computer Science and Engineering
Galgotias University, Greater Noida, India
Session 2025-26

ERIC PRAJAPATI
23SCSE1012417
INDEX

S.No Practical Date Signature

1 Write a program for drawing graphics primitives and color it.


Write a program to divide screen into four region and draw
2
circle, Graphics rectangle, arc and ellipse.
3 Write a program for drawing a simple object.
Write a program to draw a rectangle. The four vertices of it
4
should be entered by the end user.
Write a program for drawing a line using DDA Line Drawing
5
Algorithm.
Write a program for drawing a line using Bresahnams Line
6
Drawing Algorithm.
Write a program to draw circle using mid-point circle drawing
7
algorithm.
Write a program to draw two concentric circles using mid-point
8
circle drawing algorithm.

Write a program to draw a house like figure and perform the


following operations.
9
A. Scaling about the origin followed by translation.
B. Scaling with reference to an arbitrary point.
Write a program to draw any 2-D object and perform the
10 transformations on it according to the input parameters from
the user, namely: Shearing and Reflection.
Write a program to perform clipping on a line against the clip
11
window using Cohen Sutherland line clipping algorithm.
12 Write a program to implement LiangBarsky 2D Line clipping.
Write a program to draw a concave polygon and fill it with
13
desired color using scan fill algorithm.
Write a program to draw any 3-D object and perform the
14 translation on it according to the input parameters from the
user.
Write a program to draw any 3-D object and perform the
15
rotation on it according to the input parameters from the user.

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM 1
Objective : Write a program for drawing graphics primitives and color it.

Line :

Code :

mport matplotlib.pyplot as plt


x=[50,200]
y=[100,250]
plt.plot(x,y)
plt.show

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

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 :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 2
Objective : Write a program to divide screen into four region and draw circle,
Graphics rectangle, arc and ellipse.

Code :

!pip install pillow

from PIL import Image, ImageDraw


from IPython.display import display

image = Image.new("RGB", (400, 300), "grey")


draw = ImageDraw.Draw(image)

draw.rectangle((0, 0, 399, 299), outline="black") # Border


draw.line((200, 0, 200, 300), fill="black") # Vertical center line
draw.line((0, 150, 400, 150), fill="black") # Horizontal center line

draw.ellipse((80, 30, 140, 90), outline="black") # , fill="lightblue")


draw.text((90, 95), "Circle", fill="blue")

draw.rectangle((250, 30, 310, 90), outline="red") # , fill="lightgreen")


draw.text((250, 95), "Rectangle", fill="black")

draw.arc((80, 180, 140, 240), start=0, end=120, fill="yellow")


draw.text((95, 245), "Arc", fill="black")

draw.ellipse((250, 180, 300, 230), outline="green") # , fill="pink")


draw.text((240, 235), "Ellipse", fill="black")

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 3
Objective : Write a program for drawing a simple object.
Code :

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros((500, 500, 3), dtype=np.uint8)

triangle = np.array([[150, 400], [350, 400], [250, 150],], np.int32)


cv2.polylines(img, [triangle], isClosed=True, color=(0, 255, 0), thickness=3)

cv2.circle(img, (250, 300), 50, (0, 0, 255), 3)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis("on")
plt.title("Triangle with Circle Inside")
plt.show()

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 4

Objective : Write a program to draw a rectangle. The four vertices of it should be entered by the
end user.

Code :

import matplotlib.pyplot as plt

x1 = float(input("Enter x-coordinate of vertex 1: "))


y1 = float(input("Enter y-coordinate of vertex 1: "))
x2 = float(input("Enter x-coordinate of vertex 2: "))
y2 = float(input("Enter y-coordinate of vertex 2: "))
x3 = float(input("Enter x-coordinate of vertex 3: "))
y3 = float(input("Enter y-coordinate of vertex 3: "))
x4 = float(input("Enter x-coordinate of vertex 4: "))
y4 = float(input("Enter y-coordinate of vertex 4: "))

vertices = [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]

x_coords = [x[0] for x in vertices]


y_coords = [x[1] for x in vertices]

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 :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 5

Objevtive : Write a program for drawing a line using DDA Line Drawing Algorithm.

Code 1 :

from matplotlib import pyplot as plt

def DDA(x0, y0, x1, y1):


dx = abs(x0 - x1)
dy = abs(y0 - y1)
steps = max(dx, dy)
xinc = dx / steps
yinc = dy / steps
x = float(x0)
y = float(y0)
x_coorinates = []
y_coorinates = []
for i in range(steps):
x_coorinates.append(x)
y_coorinates.append(y)
x = x + xinc
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()

if name == " main ":


x0, y0 = 2, 3
x1, y1 = 10, 8
DDA(x0, y0, x1, y1)

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

Code 2 :

from matplotlib import pyplot as plt

def DDA(x0, y0, x1, y1):


dx = abs(x0 - x1)
dy = abs(y0 - y1)
steps = max(dx, dy)
xinc = dx / steps
yinc = dy / steps
x = float(x0)
y = float(y0)
x_coorinates = []
y_coorinates = []
for i in range(steps):
x_coorinates.append(round(x))
y_coorinates.append(round(y))
x = x + xinc
JEET KUMAR PAL
23SCSE1012128
Computer Graphics
R1UC408B

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

if name == " main ":


x0, y0 = 2, 3
x1, y1 = 10, 8
DDA(x0, y0, x1, y1)

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 6

Objective : Write a program for drawing a line using Bresahnams Line Drawing Algorithm.

Code :

import matplotlib.pyplot as plt

def bresenham_line_decision_parameter(x1, y1, x2, y2):

points = []
dx = abs(x2 - x1)
dy = abs(y2 - y1)
x, y = x1, y1

p = 2 * dy - dx

incx = 1 if x2 > x1 else -1


incy = 1 if y2 > y1 else -1

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)

x_coords, y_coords = zip(*line_points)

plt.plot(x_coords, y_coords, marker='^', linestyle='--', color='g', lw='1')


plt.title("Bresenham's Line Algorithm")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.show()

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 7

Objective : Write a program to draw circle using mid-point circle drawing algorithm.

Code :

import matplotlib.pyplot as plt

def plot_circle_points(x_center, y_center, x, y, points):


# 8-way symmetry
points.extend([
(x_center + x, y_center + y),
(x_center - x, y_center + y),
(x_center + x, y_center - y),
(x_center - x, y_center - y),
(x_center + y, y_center + x),
(x_center - y, y_center + x),
(x_center + y, y_center - x),
(x_center - y, y_center - x),
])

def midpoint_circle(r, x_center=0, y_center=0):


x=0
y=r
p=1-r
points = []

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 :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM :- 8

Objective : Write a program to draw two concentric circles using mid-point circle drawing
algorithm.

Code :

import matplotlib.pyplot as plt

def midpoint_circle(xc, yc, r):


x=0
y=r
# Initial decision parameter:
p=1-r
points = []

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

points1 = midpoint_circle(xc, yc, r1)


points2 = midpoint_circle(xc, yc, r2)

x1, y1 = zip(*points1)
x2, y2 = zip(*points2)
plt.scatter(x1, y1, marker='.', color='blue')
plt.scatter(x2, y2, marker='.', color='red')

plt.title('Concentric Circles using Midpoint Circle Algorithm')


plt.xlabel('X-axis')
plt.ylabel('Y-axis')
JEET KUMAR PAL
23SCSE1012128
Computer Graphics
R1UC408B

plt.gca().set_aspect('equal', adjustable='box')
plt.grid()

Output :

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

PROGRAM 9

Objective - Write a program to draw a house like figure and perform the following operations.

.(a)Write a program for drawing a simple object (without color).


import turtle

# Set up the screen


screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Turtle Graphics: House")

# Create the turtle


pen = turtle.Turtle()
pen.shape("turtle")
pen.speed(3)

# Draw the square base of the house


pen.penup()
pen.goto(-100, -100)
pen.pendown()
pen.color("blue")

for _ in range(4):
pen.forward(200)
pen.left(90)

# Draw the roof


pen.penup()
pen.goto(-100, 100)
pen.pendown()
pen.color("red")
pen.goto(0, 200)
pen.goto(100, 100)
pen.goto(-100, 100)

# Draw the door


pen.penup()
pen.goto(-30, -100)
pen.pendown()
pen.color("brown")

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

.(b)Write a program for drawing a simple object (with color).

import turtle
# Set up the screen
screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Turtle Graphics: House")

# Create the turtle


pen = turtle.Turtle()
pen.shape("turtle")
pen.speed(3)

# Draw the square base of the house


pen.penup()
pen.goto(-100, -100)
pen.pendown()
pen.color("blue")
pen.begin_fill()

for _ in range(4):
pen.forward(200)
pen.left(90)

pen.end_fill()

# Draw the roof


pen.penup()

JEET KUMAR PAL


23SCSE1012128
Computer Graphics
R1UC408B

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

# Draw the door


pen.penup()
pen.goto(-30, -100)
pen.pendown()
pen.color("brown")
pen.begin_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()

JEET KUMAR PAL


23SCSE1012128

You might also like