CG Lab Programs
1. Bresenham's Line Drawing Technique
import turtle
def bresenham_line(x1, y1, x2, y2):
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 dx > dy:
err = dx / 2.0
while x != x2:
[Link](x, y)
err -= dy
if err < 0:
y += sy
err += dx
x += sx
else:
err = dy / 2.0
while y != y2:
[Link](x, y)
err -= dx
if err < 0:
x += sx
err += dy
y += sy
[Link](x, y)
[Link](0)
bresenham_line(100, 100, 400, 300)
[Link]()
2. Basic Geometric Operations on 2D Object
import turtle
def draw_rectangle(x, y, width, height, color):
[Link]()
[Link](x, y)
[Link]()
[Link](color)
for _ in range(2):
[Link](width)
[Link](90)
[Link](height)
[Link](90)
def translate(x, y, dx, dy):
return x + dx, y + dy
def rotate(x, y, angle):
rad = [Link](angle)
x_new = x * [Link](rad) - y * [Link](rad)
y_new = x * [Link](rad) + y * [Link](rad)
return x_new, y_new
def scale(x, y, sx, sy):
return x * sx, y * sy
[Link](1)
draw_rectangle(-200, 0, 100, 50, "blue")
new_x, new_y = translate(-200, 0, 200, 0)
draw_rectangle(new_x, new_y, 100, 50, "green")
new_x, new_y = rotate(new_x, new_y, 45)
draw_rectangle(new_x, new_y, 100, 50, "red")
new_x, new_y = scale(new_x, new_y, 2, 2)
draw_rectangle(new_x, new_y, 100, 50, "yellow")
[Link]()
3. Basic Geometric Operations on 3D Object
from vpython import canvas, box, cylinder, vector, color, rate
scene = canvas(width=800, height=600, background=[Link])
def draw_cuboid(pos, length, width, height, color):
return box(pos=vector(*pos), length=length, width=width, height=height, color=color)
def draw_cylinder(pos, radius, height, color):
return cylinder(pos=vector(*pos), radius=radius, height=height, color=color)
def translate(obj, dx, dy, dz):
[Link] += vector(dx, dy, dz)
def rotate(obj, angle, axis):
[Link](angle=angle, axis=vector(*axis))
def scale(obj, factor):
[Link] *= factor
[Link] *= factor
[Link] *= factor
cuboid = draw_cuboid((-2, 0, 0), 2, 2, 2, [Link])
translate(cuboid, 4, 0, 0)
rotate(cuboid, angle=45, axis=(0, 1, 0))
scale(cuboid, 1.5)
cylinder_obj = draw_cylinder((2, 2, 0), 1, 10, [Link])
translate(cylinder_obj, 0, -2, 0)
rotate(cylinder_obj, angle=30, axis=(1, 0, 0))
scale(cylinder_obj, 1.5)
while True:
rate(30)
4. 2D Transformation on Basic Objects
import cv2
import numpy as np
canvas_width, canvas_height = 500, 500
canvas = [Link]((canvas_height, canvas_width, 3), dtype=np.uint8) * 255
obj_points = [Link]([[100, 100], [200, 100], [200, 200], [100, 200]], dtype=np.int32)
translation_matrix = np.float32([[1, 0, 100], [0, 1, 50]])
rotation_matrix = cv2.getRotationMatrix2D((150, 150), 45, 1)
scaling_matrix = np.float32([[1.5, 0, 0], [0, 1.5, 0]])
translated_obj = [Link]([[Link](translation_matrix, [x, y, 1])[:2] for x, y in obj_points], dtype=np.int32)
rotated_obj = [Link]([[Link](rotation_matrix, [x, y, 1])[:2] for x, y in translated_obj], dtype=np.int32)
scaled_obj = [Link]([[Link](scaling_matrix, [x, y, 1])[:2] for x, y in rotated_obj], dtype=np.int32)
[Link](canvas, [obj_points], True, (0, 0, 0), 2)
[Link](canvas, [translated_obj], True, (0, 255, 0), 2)
[Link](canvas, [rotated_obj], True, (255, 0, 0), 2)
[Link](canvas, [scaled_obj], True, (0, 0, 255), 2)
[Link]("2D Transformations", canvas)
[Link](0)
[Link]()