This folder contains five computer graphics experiments implemented in Python using OpenGL. Each experiment demonstrates a fundamental graphics algorithm and produces a visual result.
File: experiments/Experiment_1/exp1.py
This experiment implements two classic line drawing algorithms:
- DDA (Digital Differential Analgorithm) - A simple line drawing algorithm that uses floating-point arithmetic
- Bresenham's Line Algorithm - An efficient line drawing algorithm using integer arithmetic
- Step calculation:
steps = max(abs(dx), abs(dy))wheredx = x2 - x1anddy = y2 - y1 - Increment values:
x_inc = dx / stepsy_inc = dy / steps
- Line generation: Iterates from 0 to
steps, plotting points at each step by incrementing x and y by their respective increments - Pixel plotting:
plot(x, y)rounds coordinates to nearest integer:int(round(x)), int(round(y))
- Error term initialization:
err = dx - dywheredx = abs(x2 - x1)anddy = abs(y2 - y1) - Decision parameters:
- If
e2 > -dy: adjust error by-dyand increment x bysx - If
e2 < dx: adjust error bydxand increment y bysy
- If
- Step direction:
sx = 1 if x1 < x2 else -1,sy = 1 if y1 < y2 else -1 - Plot points at
(x1, y1)iteratively until reaching(x2, y2)
The image shows a red line drawn using DDA algorithm and a green line drawn using Bresenham algorithm between the points (50, 100) to (500, 450) and (50, 500) to (500, 150) respectively.
File: experiments/Experiment_2/exp2.py
This experiment implements the Midpoint Circle Algorithm, an efficient method for drawing circles using only integer arithmetic and symmetry.
- Initial decision parameter:
p = 1 - rwhereris the circle radius - Starting point:
(x, y) = (0, r)at the top of the circle - Iteration loop continues while
x < y:- Increment x:
x += 1 - Decision logic:
- If
p < 0:p += 2 * x + 1(move east only) - If
p >= 0:y -= 1andp += 2 * (x - y) + 1(move southeast)
- If
- Increment x:
- Symmetry plotting: For each calculated point
(x, y), all 8 symmetric points are plotted:(xc ± x, yc ± y)and(xc ± y, yc ± x)
The algorithm exploits the 8-way symmetry of circles, so only one octant needs to be calculated, and the rest are derived by symmetry.
The image shows a green circle centered at (300, 300) with radius 150, drawn using the midpoint circle algorithm.
File: experiments/Experiment_3/exp3.py
This experiment demonstrates 2D geometric transformations using homogeneous coordinates. It applies multiple transformation matrices to a triangle and displays the original and transformed results.
- Points represented as
[x, y, 1]for transformation compatibility - All transformations use 3×3 matrices operating on homogeneous coordinate vectors
1. Translation
T(tx, ty) = [[1, 0, tx],
[0, 1, ty],
[0, 0, 1]]
- Moves point by
txin x-direction andtyin y-direction
2. Rotation
R(angle) = [[cos(θ), -sin(θ), 0],
[sin(θ), cos(θ), 0],
[0, 0, 1]]
- Rotates point by
angledegrees counterclockwise - Uses
θ = math.radians(angle)to convert to radians
3. Scaling
S(sx, sy) = [[sx, 0, 0],
[0, sy, 0],
[0, 0, 1]]
- Scales by
sxin x-direction andsyin y-direction
4. Reflection
Reflection X: [[1, 0, 0], [0, -1, 0], [0, 0, 1]]
Reflection Y: [[-1, 0, 0], [0, 1, 0], [0, 0, 1]]
5. Shearing
Sh(shx, shy) = [[1, shx, 0],
[shy, 1, 0],
[0, 0, 1]]
- Original triangle vertices:
(0, 0),(100, 0),(50, 100) - Each transformation is applied via
transform_triangle(matrix)which multiplies the matrix by each point - Original triangle displayed in gray, transformed triangles in various colors
The image displays the original triangle (gray) and its transformed versions under translation, rotation (45°), scaling (0.5x), reflection about X-axis, and shearing (0.5), each shown in separate panels with their respective parameter labels.
File: experiments/Experiment_4/exp4.py
This experiment demonstrates composite 2D geometric transformations using matrix representation with homogeneous coordinates. It applies a sequence of scaling, rotation, and translation transformations to a 2D triangle and displays the individual and composite matrices alongside the visual output.
- Scaling Matrix S:
sx = 1.5, sy = 1.5 - Rotation Matrix R:
θ = 45°(math.radians(45)) - Translation Matrix T:
tx = 250, ty = 120
- Pipeline:
Original -> Scaling -> Rotation -> Translation -> Final - Composite Matrix:
M = T x R x S - Transformed Point:
P' = M x P - Non-Commutativity: Note that matrix multiplication is non-commutative (
T x R x S != S x R x T), so the transformations are evaluated right-to-left.
The image displays the original triangle (gray), the composite transformed result (red), and the individual transformation matrices alongside the computed 3x3 composite matrix.
File: experiments/Experiment_5/exp5.py
This experiment implements the Cohen-Sutherland Line Clipping Algorithm, an efficient 2D line clipping technique that uses 4-bit region codes (outcodes) to clip line segments against a rectangular viewing window.
The 2D space is divided into 9 regions defined by 4-bit region codes [TOP, BOTTOM, RIGHT, LEFT]:
- TOP (
1000= 8):y > Y_MAX - BOTTOM (
0100= 4):y < Y_MIN - RIGHT (
0010= 2):x > X_MAX - LEFT (
0001= 1):x < X_MIN - INSIDE (
0000= 0)
- Trivial Accept:
code1 == 0andcode2 == 0 - Trivial Reject:
(code1 & code2) != 0 - Intersection Formulas:
- Top (
y = Y_MAX):x = x1 + (x2 - x1) * (Y_MAX - y1) / (y2 - y1) - Bottom (
y = Y_MIN):x = x1 + (x2 - x1) * (Y_MIN - y1) / (y2 - y1) - Right (
x = X_MAX):y = y1 + (y2 - y1) * (X_MAX - x1) / (x2 - x1) - Left (
x = X_MIN):y = y1 + (y2 - y1) * (X_MIN - x1) / (x2 - x1)
- Top (
X_MIN = 100,X_MAX = 500,Y_MIN = 100,Y_MAX = 400
The image shows a side-by-side comparison: the left panel displays the original test lines inside/outside the blue clipping window, the middle panel shows only the accepted and clipped line segments in green, and the right panel details the 4-bit region codes and step-by-step clipping algorithm logic.
Each experiment can be run independently using Python with OpenGL support:
# Experiment 1
python experiments/Experiment_1/exp1.py
# Experiment 2
python experiments/Experiment_2/exp2.py
# Experiment 3
python experiments/Experiment_3/exp3.py
# Experiment 4
python experiments/Experiment_4/exp4.py
# Experiment 5
python experiments/Experiment_5/exp5.pyRequirements:
- Python 3.x
- PyOpenGL
- PyGLUT
These experiments are for educational purposes, demonstrating fundamental computer graphics algorithms.
This folder contains various computer graphics and Python projects completed during the semester.
File: projects/spider-man.py
A Python script using OpenGL to render a Spider-Man character animation. The project demonstrates character rendering, animation loops, and interactive graphics using OpenGL.
- Spider-Man character design using OpenGL primitives
- Animation loop for continuous rendering
- 2D graphics rendering pipeline
- Keyboard/mouse interaction support
The image shows the Spider-Man character rendered with OpenGL, demonstrating character animation and rendering techniques.
python projects/spider-man.pyRequirements:
- Python 3.x
- PyOpenGL
- PyGLUT





