CG Lab - Manual
CG Lab - Manual
import turtle
# Example usage
turtle.setup(500, 500)
turtle.speed(0) # Fastest drawing speed
x1, y1 = 50, 50
x2, y2 = 100, 200
line_points = bresenham_line(x1, y1, x2, y2)
# Draw the line
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
for x, y in line_points:
turtle.goto(x, y)
turtle.exitonclick()
2. Develop a program to demonstrate basic geometric operations on the 2D object
import turtle
import math
# Draw a rectangle
draw_rectangle(-200, 0, 100, 50, "blue")
# Draw a circle
draw_circle(100, 100, 50, "red")
# Apply transformations
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)
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
# Initialize Pygame
pygame.init()
# Set up OpenGL
glClearColor(0.0, 0.0, 0.0, 1.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display_width / display_height), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
edges = np.array([
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
], dtype=np.uint32)
# Main loop
running = True
angle = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Apply transformations
glLoadIdentity()
glMultMatrixf(translation_matrix)
glRotatef(angle, 1, 1, 0)
glMultMatrixf(rotation_matrix)
glMultMatrixf(scaling_matrix)
# Quit Pygame
pygame.quit()
6. Develop a program to demonstrate Animation effects on simple objects.
import pygame
import random
# Initialize Pygame
pygame.init()
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
for _ in range(num_objects):
x = random.randint(50, screen_width - 50)
y = random.randint(50, screen_height - 50)
radius = random.randint(10, 30)
color = random.choice([RED, GREEN, BLUE])
speed_x = random.randint(-5, 5)
speed_y = random.randint(-5, 5)
objects.append({"x": x, "y": y, "radius": radius, "color": color, "speed_x":
speed_x, "speed_y": speed_y})
# Main loop
running = True
clock = pygame.time.Clock()
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill(WHITE)
# Quit Pygame
pygame.quit()
7. Write a Program to read a digital image. Split and display image into 4 quadrants, up, down, right and
left
import cv2
import numpy as np
import cv2
import numpy as np
# Apply transformations
rotated_img = cv2.warpAffine(img, rotation_matrix, (width, height))
scaled_img = cv2.warpAffine(img, scaling_matrix, (int(width*1.5),
int(height*1.5)))
translated_img = cv2.warpAffine(img, translation_matrix, (width, height))
import cv2
import numpy as np
try:
img = cv2.imread(image_path)
if img is None:
raise FileNotFoundError("Image not found or cannot be read. Please check
the file path.")
except FileNotFoundError as e:
print(e)
exit()
# Edge detection
edges = cv2.Canny(gray, 100, 200) # Use Canny edge detector
# Texture extraction
kernel = np.ones((5, 5), np.float32) / 25 # Define a 5x5 averaging kernel
texture = cv2.filter2D(gray, -1, kernel) # Apply the averaging filter for texture
extraction
import cv2
import cv2
import numpy as np
# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
import cv2