Lab Assignment CG Ts
Lab Assignment CG Ts
Output:
Fig 1
Question 2: Develop a physics-based particle system to simulate explosions or fire effects.
Solution:
Aim: To create dynamic and interactive particle effects using physics principles.
Objective: Learn to apply forces, gravity, and randomness to particles for realistic
animations.
Code:
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX_PARTICLES 100
struct Particle {
int x, y, dy, color;
};
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
srand(time(0));
Particle particles[MAX_PARTICLES];
// Initialize particles
for (int i = 0; i < MAX_PARTICLES; i++) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
particles[i].x = 320;
particles[i].y = 400;
particles[i].dy = rand() % 5 + 2;
particles[i].color = rand() % 15 + 1;
}
while (!kbhit()) {
cleardevice();
for (int i = 0; i < MAX_PARTICLES; i++) {
setcolor(particles[i].color);
line(particles[i].x, particles[i].y, particles[i].x, particles[i].y - 2);
particles[i].y -= particles[i].dy;
if (particles[i].y < 0) {
particles[i].y = 400;
particles[i].color = rand() % 15 + 1;
}
}
delay(50);
}
closegraph();
return 0;
}
Output:
Fig 2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Question 3: Create a 3D maze generator and solver, visualized using perspective transformations
and real-time rendering.
Solution:
Aim: To generate and navigate complex 3D environments
Objective: Implement maze generation algorithms and explore pathfinding techniques in 3D
space.
Code:
#include <graphics.h>
#include <conio.h>
#include <bits/stdc++.h>
#define ROWS 10
#define COLS 10
#define CELL_SIZE 40
using namespace std;
struct Cell {
bool visited;
bool walls[4]; // top, right, bottom, left
Cell() {
visited = false;
for (int i = 0; i < 4; i++) walls[i] = true;
}
};
Cell maze[ROWS][COLS];
int dx[4] = {0, 1, 0, -1}; // Directions: top, right, bottom, left
int dy[4] = {-1, 0, 1, 0};
bool isValid(int x, int y) {
return (x >= 0 && x < COLS && y >= 0 && y < ROWS && !maze[y][x].visited);
}
void generateMaze(int x, int y) {
maze[y][x].visited = true;
vector<int> dirs;
dirs.push_back(0);
dirs.push_back(1);
dirs.push_back(2);
dirs.push_back(3);
random_shuffle(dirs.begin(), dirs.end());
for (int i = 0; i < dirs.size(); i++) {
int dir = dirs[i];
int nx = x + dx[dir];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int ny = y + dy[dir];
if (isValid(nx, ny)) {
maze[y][x].walls[dir] = false;
maze[ny][nx].walls[(dir + 2) % 4] = false;
generateMaze(nx, ny);
}
}
}
void drawMaze() {
setcolor(WHITE);
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLS; x++) {
int x0 = x * CELL_SIZE;
int y0 = y * CELL_SIZE;
int x1 = x0 + CELL_SIZE;
int y1 = y0 + CELL_SIZE;
if (maze[y][x].walls[0]) line(x0, y0, x1, y0); // Top
if (maze[y][x].walls[1]) line(x1, y0, x1, y1); // Right
if (maze[y][x].walls[2]) line(x1, y1, x0, y1); // Bottom
if (maze[y][x].walls[3]) line(x0, y1, x0, y0); // Left
setfillstyle(SOLID_FILL, DARKGRAY);
floodfill(x0 + 1, y0 + 1, WHITE);
}
}
int goalX = (COLS - 1) * CELL_SIZE + CELL_SIZE / 2;
int goalY = (ROWS - 1) * CELL_SIZE + CELL_SIZE / 2;
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(goalX, goalY, 6, 6);
}
void drawPlayer(int x, int y) {
int cx = x * CELL_SIZE + CELL_SIZE / 2;
int cy = y * CELL_SIZE + CELL_SIZE / 2;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
fillellipse(cx, cy, 6, 6);
}
void showWinMessage() {
settextstyle(10, 0, 3);
setcolor(YELLOW);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Fig 3
Fig 4
Question 4: Build a dynamic water simulation system that uses particle or grid-based methods to
depict fluid motion..
Solution:
Aim: To simulate realistic water motion using computational techniques.
Objective: Understand fluid dynamics principles and their implementation in real-time
simulations.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
#include <graphics.h>
#include <math.h>
#include <dos.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
float t = 0;
int x, y;
while (!kbhit()) {
cleardevice();
setcolor(CYAN);
for (x = 0; x < getmaxx(); x++) {
y = 200 + 20 * sin((x + t) * 0.05); // Sine wave to simulate water surface
line(x, y, x, getmaxy());
}
t += 5;
delay(100);
}
getch();
closegraph();
return 0;
}
Output:
Fig 5
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Question 5: Write a program to model and render a 3D fractal object such as the Mandelbrot set
or a 3D Koch snowflake..
Solution:
Aim: To explore fractal geometry and its applications in graphics.
Objective: Learn iterative fractal generation techniques and visualize self-similarity
properties in 3D.
Code:
#include <graphics.h>
#include <math.h>
void koch(int x1, int y1, int x2, int y2, int iter) {
if (iter == 0) {
line(x1, y1, x2, y2);
} else {
int x3 = (2 * x1 + x2) / 3;
int y3 = (2 * y1 + y2) / 3;
int x4 = (x1 + 2 * x2) / 3;
int y4 = (y1 + 2 * y2) / 3;
int x = x4 - x3;
int y = y4 - y3;
int x5 = (x3 + x4) / 2 - (int)(sqrt(3) * (y4 - y3) / 2);
int y5 = (y3 + y4) / 2 + (int)(sqrt(3) * (x4 - x3) / 2);
koch(x1, y1, x3, y3, iter - 1);
koch(x3, y3, x5, y5, iter - 1);
koch(x5, y5, x4, y4, iter - 1);
koch(x4, y4, x2, y2, iter - 1);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Triangle base coordinates
koch(100, 300, 400, 300, 3);
koch(400, 300, 250, 100, 3);
koch(250, 100, 100, 300, 3);
getch();
closegraph();
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Fig 6