0% found this document useful (0 votes)
5 views

LAB FILE(CG)

The document contains multiple Python programs that utilize the turtle graphics library and matplotlib for drawing various shapes and implementing line drawing algorithms. It includes functions to draw rectangles, circles, triangles, arcs, ellipses, and simple objects, as well as user-interactive programs for drawing rectangles and lines using DDA and Bresenham's algorithms. Each program is structured with a main function and includes example outputs.

Uploaded by

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

LAB FILE(CG)

The document contains multiple Python programs that utilize the turtle graphics library and matplotlib for drawing various shapes and implementing line drawing algorithms. It includes functions to draw rectangles, circles, triangles, arcs, ellipses, and simple objects, as well as user-interactive programs for drawing rectangles and lines using DDA and Bresenham's algorithms. Each program is structured with a main function and includes example outputs.

Uploaded by

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

1

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

import turtle
def draw_rectangle(x, y, width, height, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()

def draw_circle(x, y, radius, color):


turtle.penup()
turtle.goto(x, y - radius) # Adjust to draw from center
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()

def draw_triangle(x, y, side_length, color):


turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for _ in range(3):
turtle.forward(side_length)
turtle.left(120)
turtle.end_fill()

def main():
turtle.speed(3)
draw_rectangle(-100, 100, 200, 100, "red")
draw_circle(0, -50, 50, "blue")
draw_triangle(-50, -150, 100, "green")
turtle.hideturtle()
turtle.done()

if __name__ == "__main__":
main()

OUTPUT:-
2

2. Write a program to divide screen into four region and draw


circle, rectangle, arc and ellipse.

import turtle
import math

def draw_rectangle(x, y, width, height, color):


turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()

def draw_circle(x, y, radius, color):


turtle.penup()
turtle.goto(x, y - radius)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
3

turtle.circle(radius)
turtle.end_fill()

def draw_arc(x, y, radius, extent, color):


turtle.penup()
turtle.goto(x, y - radius)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(radius, extent)
turtle.end_fill()

def draw_ellipse(x, y, a, b, color):


turtle.penup()
turtle.goto(x + a, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for angle in range(0, 360, 5):
rad = math.radians(angle)
x_pos = x + a * math.cos(rad)
y_pos = y + b * math.sin(rad)
turtle.goto(x_pos, y_pos)
turtle.end_fill()

def draw_regions():
turtle.speed(3)
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()
turtle.forward(400)
turtle.penup()
turtle.goto(0, 200)
turtle.right(90)
turtle.pendown()
turtle.forward(400)

def main():
turtle.speed(3)
draw_regions()
draw_circle(-100, 100, 50, "blue")
draw_rectangle(50, 150, 100, 50, "red")
draw_arc(-100, -100, 50, 180, "green")
draw_ellipse(100, -100, 50, 30, "purple")
turtle.hideturtle()
turtle.done()

if __name__ == "__main__":
main()
4

OUTPUT:-

3. Write a program for drawing a simple object.

import turtle

screen = turtle.Screen()
screen.bgcolor("lightblue")

smiley = turtle.Turtle()
smiley.pensize(3)
smiley.speed(3)

smiley.penup()
smiley.goto(0, -100)
smiley.pendown()
smiley.color("yellow")
smiley.begin_fill()
smiley.circle(100)
smiley.end_fill()

smiley.penup()
smiley.goto(-35, 35)
smiley.pendown()
smiley.color("black")
smiley.begin_fill()
5

smiley.circle(10)
smiley.end_fill()

smiley.penup()
smiley.goto(35, 35)
smiley.pendown()
smiley.begin_fill()
smiley.circle(10)
smiley.end_fill()

smiley.penup()
smiley.goto(-40, -20)
smiley.setheading(-60)
smiley.pendown()
smiley.circle(50, 120)

smiley.hideturtle()

turtle.done()

OUTPUT:-

4. Write a program to draw a rectangle. The four vertices of it


should be entered by the end user.

import matplotlib.pyplot as plt


def get_point(n):
x = int(input(f"Enter x-coordinate of Point {n}: "))
y = int(input(f"Enter y-coordinate of Point {n}: "))
return (x, y)

print("Enter 4 points (vertices of the rectangle) in order:")


points = [get_point(i) for i in range(1, 5)]
6

points.append(points[0])

x_coords, y_coords = zip(*points)


plt.figure(figsize=(6,6))
plt.plot(x_coords, y_coords, marker='o', linestyle='-', color='blue')
plt.title("Rectangle from User Input")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.axis('equal')
plt.show()

OUTPUT:-

5. Write a program for drawing a line using DDA Line Drawing


Algorithm.
import matplotlib.pyplot as plt

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


points = []

dx = x2 - x1
dy = y2 - y1

steps = int(max(abs(dx), abs(dy)))


7

x_inc = dx / steps
y_inc = dy / steps

x = x1
y = y1

for _ in range(steps + 1):


points.append((round(x), round(y)))
x += x_inc
y += y_inc

return points

x1 = int(input("Enter x1: "))


y1 = int(input("Enter y1: "))
x2 = int(input("Enter x2: "))
y2 = int(input("Enter y2: "))

line_points = DDA(x1, y1, x2, y2)

x_vals, y_vals = zip(*line_points)

plt.figure(figsize=(6, 6))
plt.plot(x_vals, y_vals, color='blue', linestyle='-', markersize=4)
plt.title("DDA Line Drawing Algorithm")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.axis("equal")
plt.show()

OUTPUT:-
8

6. Write a program for drawing a line using Bresahnams Line


Drawing Algorithm.

import matplotlib.pyplot as plt

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


points = []

dx = abs(x2 - x1)
dy = abs(y2 - y1)

x, y = x1, y1

sx = 1 if x2 > x1 else -1
sy = 1 if y2 > y1 else -1

if dy <= dx:
err = 2 * dy - dx
for _ in range(dx + 1):
points.append((x, y))
if err > 0:
y += sy
err -= 2 * dx
x += sx
err += 2 * dy
else:
err = 2 * dx - dy
for _ in range(dy + 1):
points.append((x, y))
if err > 0:
x += sx
err -= 2 * dy
y += sy
err += 2 * dx

return points

# User Input
x1 = int(input("Enter x1: "))
y1 = int(input("Enter y1: "))
x2 = int(input("Enter x2: "))
y2 = int(input("Enter y2: "))

# Generate points
line_points = bresenham(x1, y1, x2, y2)

# Separate x and y
x_vals, y_vals = zip(*line_points)

# Plotting
9

plt.figure(figsize=(6, 6))
plt.plot(x_vals, y_vals, color='red', marker='s', linestyle='-', markersize=6)
plt.title("Bresenham's Line Drawing Algorithm")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.axis("equal")
plt.show()

OUTPUT:-

You might also like