[go: up one dir, main page]

0% found this document useful (0 votes)
36 views7 pages

Lab Assignment 2

Uploaded by

2022btcse014
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)
36 views7 pages

Lab Assignment 2

Uploaded by

2022btcse014
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/ 7

Lab Assignment:

1. WAP to implement mid-point circle generating algorithm


import matplotlib.pyplot as plt

def draw_circle(x_center, y_center, radius):


x = radius
y = 0

p = 1 - radius

points = []

def plot_points(x_center, y_center, x, y):


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),
])

plot_points(x_center, y_center, x, y)

while x > y:
y += 1
if p <= 0:
p = p + 2 * y + 1
else:
x -= 1
p = p + 2 * y - 2 * x + 1
plot_points(x_center, y_center, x, y)

x_vals, y_vals = zip(*points)


plt.scatter(x_vals, y_vals)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()

draw_circle(x_center=50, y_center=50, radius=30)


Output:
2. WAP to implement 2D transformations
import numpy as np
import matplotlib.pyplot as plt

def translate(points, tx, ty):


translation_matrix = np.array([tx, ty])
return points + translation_matrix

def scale(points, sx, sy):


scaling_matrix = np.array([[sx, 0], [0, sy]])
return np.dot(points, scaling_matrix.T)

def rotate(points, angle_degrees):


angle_radians = np.radians(angle_degrees)
rotation_matrix = np.array([
[np.cos(angle_radians), -np.sin(angle_radians)],
[np.sin(angle_radians), np.cos(angle_radians)]
])
return np.dot(points, rotation_matrix.T)

points = np.array([
[1, 1],
[1, 4],
[4, 4],
[4, 1],
[1, 1]
])

center = np.mean(points, axis=0)

scale_x, scale_y = 2, 1.5


rotation_angle = 45

translated_points = translate(points, -center[0], -center[1])

scaled_points = translate(scale(translated_points, scale_x, scale_y),


center[0] + 6, center[1])
rotated_points = translate(rotate(translated_points, rotation_angle),
center[0] + 12, center[1])

plt.figure(figsize=(10, 8))
plt.plot(points[:, 0], points[:, 1], 'bo-', label='Original Shape')
plt.plot(scaled_points[:, 0], scaled_points[:, 1], 'ro-', label='Scaled
Shape')
plt.plot(rotated_points[:, 0], rotated_points[:, 1], 'go-', label='Rotated
Shape')

plt.xlim(-5, 20)
plt.ylim(-5, 10)

plt.xlabel('X')
plt.ylabel('Y')
plt.title('2D Transformations: Aligned along Y-Axis without Overlap')
plt.legend()
plt.gca().set_aspect('equal', adjustable='box')
plt.grid(True)
plt.show()
Output:
3. WAP to implement 2D line clipping
import matplotlib.pyplot as plt

# region codes
INSIDE = 0 # 0000
LEFT = 1 # 0001
RIGHT = 2 # 0010
BOTTOM = 4 # 0100
TOP = 8 # 1000

xmin, ymin, xmax, ymax = 50, 50, 200, 200

def compute_outcode(x, y):


code = INSIDE
if x < xmin:
code |= LEFT
elif x > xmax:
code |= RIGHT
if y < ymin:
code |= BOTTOM
elif y > ymax:
code |= TOP
return code

def intersect(p1, p2, code_out, xmin, ymin, xmax, ymax):


x1, y1 = p1
x2, y2 = p2

if code_out & TOP:


x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)
y = ymax
elif code_out & BOTTOM:
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
y = ymin
elif code_out & RIGHT:
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
x = xmax
elif code_out & LEFT:
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
x = xmin
return x, y

def cohen_sutherland_clip(x1, y1, x2, y2, xmin, ymin, xmax, ymax):


outcode1 = compute_outcode(x1, y1)
outcode2 = compute_outcode(x2, y2)

while True:
# Case 1: Both endpoints are inside the window
if outcode1 == INSIDE and outcode2 == INSIDE:
return [(x1, y1), (x2, y2)]

# Case 2: Both endpoints are outside the window (trivial


rejection)
elif (outcode1 & outcode2) != 0:
return None

# Case 3: Some part of the line is inside the window


else:
if outcode1 != INSIDE:
outcode_out = outcode1
x, y = x1, y1
else:
outcode_out = outcode2
x, y = x2, y2

x, y = intersect((x1, y1), (x2, y2), outcode_out, xmin, ymin,


xmax, ymax)

if outcode_out == outcode1:
x1, y1 = x, y
outcode1 = compute_outcode(x1, y1)
else:
x2, y2 = x, y
outcode2 = compute_outcode(x2, y2)

def plot_lines(x1, y1, x2, y2, xmin, ymin, xmax, ymax, clipped_line):
plt.figure(figsize=(8, 8))
plt.xlim(0, 250)
plt.ylim(0, 250)

plt.plot([xmin, xmax, xmax, xmin, xmin], [ymin, ymin, ymax, ymax,


ymin], 'r-', label="Clipping Window")

plt.plot([x1, x2], [y1, y2], 'b-', label="Original Line")

if clipped_line:
plt.plot([clipped_line[0][0], clipped_line[1][0]],
[clipped_line[0][1], clipped_line[1][1]], 'g-',
label="Clipped Line")

plt.xlabel("X")
plt.ylabel("Y")
plt.title("2D Line Clipping - Cohen-Sutherland Algorithm")
plt.legend()
plt.grid(True)
plt.show()

x1, y1 = 30, 30
x2, y2 = 250, 250

clipped_line = cohen_sutherland_clip(x1, y1, x2, y2, xmin, ymin, xmax,


ymax)
plot_lines(x1, y1, x2, y2, xmin, ymin, xmax, ymax, clipped_line)
Output:

You might also like