CG Lab Manual
CG Lab Manual
BELGAVI -590018
LAB MANUAL
PALLAVI R
Asst.Professor, GMIT
(AIML Branch)
PROGRAM LIST
PART-A Programs
1. Bresenham’s Algorithm
2. 2D object geometric operations
3. 3D object geometric operations
4. 2D transformation basic objects
5. 3D transformation basic objects
6. Animation effects on simple objects
7. display image into 4 quadrants
8. rotation, scaling, and translation on an image.
9. Filtering techniques
10. blur and smoothing an image.
11. contour an image.
12. detect a face/s in an image.
CG LAB MANUAL
import turtle
dx = abs(x2 - x1)
dy = abs(y2 - y1)
error = 2 * dy - dx
line_points = []
x, y = x1, y1
line_points.append((x, y))
21CSL66
CG LAB MANUAL
y += y_step
error -= 2 * dx
error += 2 * dy
x += x_step
return line_points
# Example usage
turtle.setup(500, 500)
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
for x, y in line_points:
turtle.goto(x, y)
turtle.exitonclick()
21CSL66
CG LAB MANUAL
output
21CSL66
CG LAB MANUAL
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("white")
t = turtle.Turtle()
t.penup()
t.goto(x, y)
t.pendown()
t.color(color)
for _ in range(2):
t.forward(width)
t.left(90)
21CSL66
CG LAB MANUAL
t.forward(height)
t.left(90)
t.penup()
t.goto(x, y - radius)
t.pendown()
t.color(color)
t.circle(radius)
t.penup()
t.pendown()
t.penup()
t.goto(x, y)
t.setheading(angle)
t.pendown()
21CSL66
CG LAB MANUAL
t.penup()
t.pendown()
# Draw a rectangle
translate(-200, 0, 200, 0)
rotate(0, 0, 45)
scale(0, 0, 2, 2)
# Draw a circle
21CSL66
CG LAB MANUAL
scale(300, 100, 2, 2)
turtle.done()
output
21CSL66
CG LAB MANUAL
# Create a 3D canvas
return cuboid
obj.rotate(angle=angle, axis=vector(*axis))
21CSL66
CG LAB MANUAL
# Draw a cuboid
translate(cuboid, 4, 0, 0)
# Draw a cylinder
translate(cylinder, 0, -2, 0)
21CSL66
CG LAB MANUAL
output
21CSL66
CG LAB MANUAL
import cv2
import numpy as np
canvas_width = 500
canvas_height = 500
# Apply transformations
21CSL66
CG LAB MANUAL
cv2.waitKey(0)
cv2.destroyAllWindows()
output
21CSL66
CG LAB MANUAL
import numpy as np
# Initialize Pygame
pygame.init()
display_width = 800
display_height = 600
pygame.display.set_caption("3D Transformations")
# Set up OpenGL
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glMatrixMode(GL_MODELVIEW)
21CSL66
CG LAB MANUAL
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)
scaling_matrix[0, 0] = 1.5
scaling_matrix[1, 1] = 1.5
scaling_matrix[2, 2] = 1.5
# Main loop
running = True
angle = 0
while running:
21CSL66
CG LAB MANUAL
if event.type == pygame.QUIT:
running = False
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Apply transformations
glLoadIdentity()
glMultMatrixf(translation_matrix)
glRotatef(angle, 1, 1, 0)
glMultMatrixf(rotation_matrix)
glMultMatrixf(scaling_matrix)
glBegin(GL_LINES)
glVertex3fv(vertices[vertex])
glEnd()
angle += 1
21CSL66
CG LAB MANUAL
pygame.display.flip()
# Quit Pygame
pygame.quit()
output
21CSL66
CG LAB MANUAL
import random
# Initialize Pygame
pygame.init()
screen_width = 800
screen_height = 600
# Define colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
num_objects = 10
objects = []
for _ in range(num_objects):
21CSL66
CG LAB MANUAL
speed_x = random.randint(-5, 5)
speed_y = random.randint(-5, 5)
# Main loop
running = True
clock = pygame.time.Clock()
while running:
# Handle events
if event.type == pygame.QUIT:
running = False
screen.fill(WHITE)
21CSL66
CG LAB MANUAL
obj["x"] += obj["speed_x"]
obj["y"] += obj["speed_y"]
obj["speed_x"] = -obj["speed_x"]
obj["speed_y"] = -obj["speed_y"]
pygame.display.flip()
# Quit Pygame
pygame.quit()
output
21CSL66
CG LAB MANUAL
21CSL66
CG LAB MANUAL
import numpy as np
img = cv2.imread(image_path)
21CSL66
CG LAB MANUAL
cv2.waitKey(0)
cv2.destroyAllWindows()
output
21CSL66
CG LAB MANUAL
import cv2
import numpy as np
img = cv2.imread(image_path)
# Apply transformations
21CSL66
CG LAB MANUAL
cv2.waitKey(0)
cv2.destroyAllWindows()
output
21CSL66
CG LAB MANUAL
21CSL66
CG LAB MANUAL
21CSL66
CG LAB MANUAL
import cv2
import numpy as np
img = cv2.imread(image_path)
# Edge detection
# Texture extraction
texture = cv2.filter2D(gray, -1, kernel) # Apply the averaging filter for texture
extraction
cv2.imshow("Edges", edges)
cv2.imshow("Texture", texture)
cv2.waitKey(0)
21CSL66
CG LAB MANUAL
cv2.destroyAllWindows()
output
21CSL66
CG LAB MANUAL
21CSL66
CG LAB MANUAL
import cv2
image = cv2.imread('image/atc.jpg')
# Gaussian Blur
# Median Blur
median_blur = cv2.medianBlur(image, 5)
# Bilateral Filter
cv2.waitKey(0)
cv2.destroyAllWindows()
21CSL66
CG LAB MANUAL
output
21CSL66
CG LAB MANUAL
import cv2
import numpy as np
image = cv2.imread('image/atc.jpg')
# Find contours
contour_image = image.copy()
cv2.imshow('Contours', contour_image)
21CSL66
CG LAB MANUAL
cv2.waitKey(0)
cv2.destroyAllWindows()
output
21CSL66
CG LAB MANUAL
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
image = cv2.imread('image/face.jpeg')
cv2.waitKey(0)
cv2.destroyAllWindows()
21CSL66
CG LAB MANUAL
output
21CSL66