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

CG_Lab_Programs

Uploaded by

vinums11122
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)
9 views

CG_Lab_Programs

Uploaded by

vinums11122
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/ 6

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:

turtle.goto(x, y)

err -= dy

if err < 0:

y += sy

err += dx

x += sx

else:

err = dy / 2.0

while y != y2:

turtle.goto(x, y)

err -= dx
if err < 0:

x += sx

err += dy

y += sy

turtle.goto(x, y)

turtle.speed(0)

bresenham_line(100, 100, 400, 300)

turtle.done()

2. Basic Geometric Operations on 2D Object

import turtle

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

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

turtle.color(color)

for _ in range(2):

turtle.forward(width)

turtle.left(90)

turtle.forward(height)

turtle.left(90)

def translate(x, y, dx, dy):


return x + dx, y + dy

def rotate(x, y, angle):

rad = math.radians(angle)

x_new = x * math.cos(rad) - y * math.sin(rad)

y_new = x * math.sin(rad) + y * math.cos(rad)

return x_new, y_new

def scale(x, y, sx, sy):

return x * sx, y * sy

turtle.speed(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")

turtle.done()

3. Basic Geometric Operations on 3D Object

from vpython import canvas, box, cylinder, vector, color, rate


scene = canvas(width=800, height=600, background=color.white)

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):

obj.pos += vector(dx, dy, dz)

def rotate(obj, angle, axis):

obj.rotate(angle=angle, axis=vector(*axis))

def scale(obj, factor):

obj.length *= factor

obj.width *= factor

obj.height *= factor

cuboid = draw_cuboid((-2, 0, 0), 2, 2, 2, color.blue)

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, color.red)

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 = np.ones((canvas_height, canvas_width, 3), dtype=np.uint8) * 255

obj_points = np.array([[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 = np.array([np.dot(translation_matrix, [x, y, 1])[:2] for x, y in obj_points], dtype=np.int32)

rotated_obj = np.array([np.dot(rotation_matrix, [x, y, 1])[:2] for x, y in translated_obj], dtype=np.int32)

scaled_obj = np.array([np.dot(scaling_matrix, [x, y, 1])[:2] for x, y in rotated_obj], dtype=np.int32)

cv2.polylines(canvas, [obj_points], True, (0, 0, 0), 2)

cv2.polylines(canvas, [translated_obj], True, (0, 255, 0), 2)

cv2.polylines(canvas, [rotated_obj], True, (255, 0, 0), 2)


cv2.polylines(canvas, [scaled_obj], True, (0, 0, 255), 2)

cv2.imshow("2D Transformations", canvas)

cv2.waitKey(0)

cv2.destroyAllWindows()

You might also like