LAB FILE(CG)
LAB FILE(CG)
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 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
import turtle
import math
turtle.circle(radius)
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:-
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:-
points.append(points[0])
OUTPUT:-
dx = x2 - x1
dy = y2 - y1
x_inc = dx / steps
y_inc = dy / steps
x = x1
y = y1
return 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
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:-