Cg Practical File
Cg Practical File
PRACTICAL
FILE
NAME: SAKSHI KUMAR
Science
INDEX
CONTENT PAGE NUMEBRS
1. Write a program to implement Bresenham’s line drawing algorithm. 3
2. Write a program to implement a midpoint circle drawing algorithm. 5
3. Write a program to clip a line using Cohen and Sutherland line 7
clipping algorithm.
4. Write a program to clip a polygon using Sutherland Hodgemann 10
algorithm.
5. Write a program to fill a polygon using the Scan line fill algorithm. 13
6. Write a program to apply various 2D transformations on a 2D object 15
(use homogeneous Coordinates).
7. Write a program to apply various 3D transformations on a 3D object 17
and then apply parallel and perspective projection on it.
8. Write a program to draw Hermite /Bezier curve. 20
3|Pa ge
while(x<x1):
if(d<=0):
d+=incrE
x+=1
else:
d+=incrNE
x+=1
y+=1
points.append((x, y))
return points
line_points = mid_point_line(a0, b0, a1, b1)
plt.plot(*zip(*line_points), marker='o', color='b')
plt.grid(True)
plt.show()
4|Pa ge
OUTPUT
5|Pa ge
def Midpoint_circle(radius):
x=0
y=radius
d=5.0/4.0-radius
circle_points(x,y)
while(y>x):
if(d<0):
d+=2.0*x+3.0
else:
d+=2.0*(x-y)+5.0
y-=1
x+=1
circle_points(x,y)
Midpoint_circle(20)
plt.grid(True)
plt.show()
6|Pa ge
OUTPUT
7|Pa ge
3. Write a program to clip a line using Cohen and Sutherland line clipping
algorithm.
INPUT
import matplotlib.pyplot as plt
INSIDE = 0
LEFT = 1
RIGHT = 2
BOTTOM = 4
TOP = 8
while True:
if oc1 == 0 and oc2 == 0:
accept = True
break
elif oc1 & oc2 != 0:
break
else:
x, y = 0, 0
oco = oc1 if oc1 != 0 else oc2
y = y_min
elif oco & RIGHT:
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1)
x = x_max
elif oco & LEFT:
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1)
x = x_min
if oco == oc1:
x1, y1 = x, y
oc1 = compute_out_code(x1, y1)
else:
x2, y2 = x, y
oc2 = compute_out_code(x2, y2)
if accept:
plt.plot([x1, x2], [y1, y2], 'g')
else:
plt.plot([x1, x2], [y1, y2], 'r')
def drawing_the_clipping_window():
plt.plot([x_min, x_max], [y_min, y_min], 'b')
plt.plot([x_max, x_max], [y_min, y_max], 'b')
plt.plot([x_max, x_min], [y_max, y_max], 'b')
plt.plot([x_min, x_min], [y_max, y_min], 'b')
x1, y1 = 60, 60
x2, y2 = 250, 250
drawing_the_clipping_window()
cohen_sutherland_clip(x1, y1, x2, y2)
plt.xlim(0, 300)
plt.ylim(0, 300)
plt.title('Cohen-Sutherland Line Clipping')
plt.show()
9|Pa ge
OUTPUT
10 | P a g e
def draw_polygon(polygon):
polygon.append(polygon[0])
xs, ys = zip(*polygon)
plt.plot(xs, ys)
# Example
polygon = [(10, 10), (100, 30), (90, 90), (20, 70)]
clip_edges = [[(20, 20), (80, 20)], [(80, 20), (80, 80)], [(80, 80), (20, 80)], [(20, 80), (20, 20)]]
plt.figure()
draw_polygon(polygon)
plt.title("Original Polygon")
plt.figure()
draw_polygon(clipped_polygon)
plt.title("Clipped Polygon")
plt.show()
OUTPUT
12 | P a g e
13 | P a g e
5. Write a program to fill a polygon using the Scan line fill algorithm.
INPUT
import matplotlib.pyplot as plt
import numpy as np
def edge_table(vertices):
edges = []
for i in range(len(vertices)):
x1, y1 = vertices[i]
x2, y2 = vertices[(i + 1) % len(vertices)]
if y1 != y2:
if y1 > y2:
x1, y1, x2, y2 = x2, y2, x1, y1
edges.append((x1, y1, x2, y2))
return edges
def scanline_fill(vertices):
edges = edge_table(vertices)
y_min = min([y for _, y, _, _ in edges])
y_max = max([y for _, y, _, _ in edges])
def draw_polygon(vertices):
vertices = np.array(vertices)
plt.fill(vertices[:, 0], vertices[:, 1], edgecolor='black', fill=False)
scanline_fill(vertices)
plt.show()
vertices = [(10, 10), (100, 50), (90, 100), (50, 70), (20, 90)]
draw_polygon(vertices)
14 | P a g e
OUTPUT
15 | P a g e
# Translation matrix
def translation(tx, ty):
return np.array([
[1, 0, tx],
[0, 1, ty],
[0, 0, 1]
])
# Scaling matrix
def scaling(sx, sy):
return np.array([
[sx, 0, 0],
[ 0, sy, 0],
[ 0, 0, 1]
])
# Rotation matrix
def rotation(angle):
rad = np.deg2rad(angle)
return np.array([
[ np.cos(rad), -np.sin(rad), 0],
[ np.sin(rad), np.cos(rad), 0],
[ 0, 0, 1]
])
# Apply transformation
def apply_transformation(points, transformation_matrix):
return np.dot(points, transformation_matrix.T)
# Example of transformations
translated_points = apply_transformation(points, translation(1, 2))
scaled_points = apply_transformation(points, scaling(2, 2))
rotated_points = apply_transformation(points, rotation(45))
16 | P a g e
OUTPUT
17 | P a g e
points = np.array([
[1, 1, 1, 1],
[2, 1, 1, 1],
[2, 2, 1, 1],
[1, 2, 1, 1],
[1, 1, 2, 1],
[2, 1, 2, 1],
[2, 2, 2, 1],
[1, 2, 2, 1]
])
def rotation_x(angle):
rad = np.deg2rad(angle)
return np.array([
[1, 0, 0, 0],
[0, np.cos(rad), -np.sin(rad), 0],
[0, np.sin(rad), np.cos(rad), 0],
[0, 0, 0, 1]
])
def rotation_y(angle):
rad = np.deg2rad(angle)
return np.array([
[np.cos(rad), 0, np.sin(rad), 0],
[0, 1, 0, 0],
18 | P a g e
def rotation_z(angle):
rad = np.deg2rad(angle)
return np.array([
[np.cos(rad), -np.sin(rad), 0, 0],
[np.sin(rad), np.cos(rad), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
def parallel_projection():
return np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]
])
def perspective_projection(d):
return np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1/d, 0]
])
OUTPUT
20 | P a g e
for i in range(len(points)):
curve += np.outer((1 - t)**(len(points) - 1 - i) * t**i, points[i]) * comb(len(points) - 1, i)
return curve
OUTPUT