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

CG LAB FILE (3)

The document is a practical lab file for the Computer Graphics course at Madhav Institute of Technology & Science, Gwalior. It includes multiple experiments demonstrating various graphics algorithms in C++, such as DDA, Bresenham's, and Midpoint Circle algorithms, along with examples of drawing shapes, text, bar charts, and a working analog clock. Each experiment contains code snippets and descriptions of the graphics functions used.

Uploaded by

yadav1258ji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CG LAB FILE (3)

The document is a practical lab file for the Computer Graphics course at Madhav Institute of Technology & Science, Gwalior. It includes multiple experiments demonstrating various graphics algorithms in C++, such as DDA, Bresenham's, and Midpoint Circle algorithms, along with examples of drawing shapes, text, bar charts, and a working analog clock. Each experiment contains code snippets and descriptions of the graphics functions used.

Uploaded by

yadav1258ji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR

(Deemed to be University)
NAAC Accredited with A++ Grade

Department of CSE
Practical Lab File
On
Computer Graphics (3150224)
Session January – June 2024

Submitted to: Submitted By:


Dr. Devesh Kumar Lal Aryan Yadav
(0901CS231025)
Experiment - 1

 Drawing a line using Digital Differential Analyzer Algorithm


#include <graphics.h>
#include <conio.h>
#include <cmath>

// Function to implement DDA algorithm


void drawLineDDA(int x1, int y1, int x2, int y2) {
// Calculate dx and dy
int dx = x2 - x1;
int dy = y2 - y1;

// Calculate steps required for generating pixels


int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

// Calculate increment in x & y for each step


float xIncrement = dx / (float)steps;
float yIncrement = dy / (float)steps;

// Initialize starting point


float x = x1;
float y = y1;

// Set the pixel for each step


for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE); // Set pixel at (x, y)
x += xIncrement; // Increment x
y += yIncrement; // Increment y
}
}
int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Coordinates of the line


int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
// Draw the line using DDA
drawLineDDA(x1, y1, x2, y2);
// Wait for user to press a key
getch();
// Close the graphics mode
closegraph();
return 0;
}
OUTPUT
Experiment - 2

 Drawing a Circle using Digital Differential analyser algorithm


#include <graphics.h>
#include <conio.h>
#include <cmath>

void drawCircleDDA(int xc, int yc, int radius) {


// Set the step size (angle increment in radians)
float step = 1.0 / radius;

// Loop to calculate the points on the circle


for (float angle = 0; angle <= 2 * M_PI; angle += step) {
// Calculate x and y using polar coordinates
int x = xc + radius * cos(angle);
int y = yc + radius * sin(angle);

// Plot the point


putpixel(x, y, WHITE);
}
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Center and radius of the circle


int xc = 200, yc = 200, radius = 100;

// Draw the circle using DDA-like approach


drawCircleDDA(xc, yc, radius);

// Wait for user to press a key


getch();

// Close the graphics mode


closegraph();
return 0;
}
OUTPUT
Experiment – 3

 Drawing a line using Bresenham’s Line Drawing Algorithm


#include <graphics.h>
#include <conio.h>

// Function to implement Bresenham's line drawing algorithm


void drawLineBresenham(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
while (true) {
putpixel(x1, y1, WHITE); // Set pixel at (x1, y1)
if (x1 == x2 && y1 == y2) break;
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}
}
int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Coordinates of the line


int x1 = 100, y1 = 100, x2 = 200, y2 = 200;

// Draw the line using Bresenham's algorithm


drawLineBresenham(x1, y1, x2, y2);

// Wait for user to press a key


getch();

// Close the graphics mode


closegraph();
return 0;
}
OUTPUT
Experiment – 4

 Drawing a Circle using Midpoint Circle algorithm.


#include <graphics.h>
#include <conio.h>

// Function to plot the points for a circle


void plotCirclePoints(int xc, int yc, int x, int y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}

// Function to implement Midpoint Circle Drawing Algorithm


void drawCircleMidpoint(int xc, int yc, int radius) {
int x = 0;
int y = radius;
int p = 1 - radius; // Initial decision parameter

plotCirclePoints(xc, yc, x, y);

while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
plotCirclePoints(xc, yc, x, y);
}
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Center and radius of the circle


int xc = 200, yc = 200, radius = 100;
// Draw the circle using Midpoint Circle Algorithm
drawCircleMidpoint(xc, yc, radius);

// Wait for user to press a key


getch();

// Close the graphics mode


closegraph();
return 0;
}

OUTPUT
Experiment – 5

 Drawing a Multiple Different Shapes using Graphics in C++

#include <graphics.h>
#include <conio.h>

// Function to draw a line using the Bresenham's algorithm


void drawLineBresenham(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;

while (true) {
putpixel(x1, y1, WHITE);

if (x1 == x2 && y1 == y2) break;


int e2 = 2 * err;

if (e2 > -dy) {


err -= dy;
x1 += sx;
}

if (e2 < dx) {


err += dx;
y1 += sy;
}
}
}

// Function to draw a rectangle


void drawRectangle(int left, int top, int right, int bottom) {
rectangle(left, top, right, bottom);
}

// Function to draw a circle using the Midpoint Circle algorithm


void drawCircleMidpoint(int xc, int yc, int radius) {
int x = 0;
int y = radius;
int p = 1 - radius;
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);

if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
x++;
}
}

// Function to draw an ellipse


void drawEllipse(int xc, int yc, int xRadius, int yRadius) {
ellipse(xc, yc, 0, 360, xRadius, yRadius);
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Coordinates for the line


int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
drawLineBresenham(x1, y1, x2, y2);

// Coordinates for the rectangle


int left = 250, top = 100, right = 350, bottom = 200;
drawRectangle(left, top, right, bottom);

// Center and radius for the circle


int xc = 150, yc = 300, radius = 50;
drawCircleMidpoint(xc, yc, radius);

// Center and radii for the ellipse


int xe = 300, ye = 300, xRadius = 75, yRadius = 50;
drawEllipse(xe, ye, xRadius, yRadius);

// Wait for user to press a key


getch();
// Close the graphics mode
closegraph();
return 0;
}

OUTPUT
Experiment – 6

 Showing Text in Different colours using graphics in C++.


#include <graphics.h>
#include <conio.h>

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Set the background color


setbkcolor(BLACK);
cleardevice();

// Set text styles and display text in different colors


settextstyle(DEFAULT_FONT, HORIZ_DIR, 2); // Default font, horizontal direction, size 2
setcolor(RED);
outtextxy(100, 100, "This is Red Text");

setcolor(GREEN);
outtextxy(100, 150, "This is Green Text");

setcolor(BLUE);
outtextxy(100, 200, "This is Blue Text");

setcolor(YELLOW);
outtextxy(100, 250, "This is Yellow Text");

setcolor(CYAN);
outtextxy(100, 300, "This is Cyan Text");

setcolor(MAGENTA);
outtextxy(100, 350, "This is Magenta Text");

setcolor(WHITE);
outtextxy(100, 400, "This is White Text");

// Wait for user to press a key


getch();

// Close the graphics mode


closegraph();
return 0;
}
OUTPUT
Experiment – 7

 Drawing a Bar Chart using Graphics in C++


#include <graphics.h>
#include <conio.h>

// Function to draw a single bar in the bar chart


void drawBar(int left, int top, int right, int bottom, int color) {
setfillstyle(SOLID_FILL, color);
bar(left, top, right, bottom);
}

// Function to draw the bar chart


void drawBarChart(int data[], int n, int width, int max_height, int x_start, int y_start) {
int gap = 10; // Gap between bars
for (int i = 0; i < n; i++) {
int height = (data[i] * max_height) / 100; // Scale height according to max_height
int left = x_start + i * (width + gap);
int top = y_start - height;
int right = left + width;
int bottom = y_start;

drawBar(left, top, right, bottom, i + 1); // Different color for each bar
}
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Data for the bar chart


int data[] = {30, 70, 50, 90, 60};
int n = sizeof(data) / sizeof(data[0]);

// Parameters for the bar chart


int width = 50; // Width of each bar
int max_height = 300; // Maximum height of the bars
int x_start = 100; // X coordinate of the start of the bar chart
int y_start = 400; // Y coordinate of the base of the bar chart

// Draw the bar chart


drawBarChart(data, n, width, max_height, x_start, y_start);

// Wait for user to press a key


getch();

// Close the graphics mode


closegraph();
return 0;
}

OUTPUT
Experiment – 8

 Drawing a working analog clock using Graphics in C++.


#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <math.h>

#define PI 3.14159

// Function to draw the clock face


void drawClockFace(int xc, int yc, int radius) {
// Set color to white
setcolor(WHITE);

// Draw the outer circle


circle(xc, yc, radius);

// Draw the clock numbers


char numStr[3];
for (int num = 1; num <= 12; num++) {
float angle = num * 30 * PI / 180;
int x = xc + (radius - 20) * cos(angle - PI / 2);
int y = yc + (radius - 20) * sin(angle - PI / 2);
sprintf(numStr, "%d", num);
outtextxy(x - 5, y - 5, numStr); // Adjust the position to center the text
}
}

// Function to draw the hands of the clock


void drawHand(int xc, int yc, int length, float angle, int color) {
int x = xc + length * cos(angle - PI / 2);
int y = yc + length * sin(angle - PI / 2);
setcolor(color);
line(xc, yc, x, y);
}

// Function to get the current time and update the clock hands
void updateClockHands(int xc, int yc, int radius) {
time_t rawtime;
struct tm *timeinfo;

// Get the current time


time(&rawtime);
timeinfo = localtime(&rawtime);
// Calculate angles for the hands
float secondAngle = timeinfo->tm_sec * 6 * PI / 180;
float minuteAngle = (timeinfo->tm_min + timeinfo->tm_sec / 60.0) * 6 * PI / 180;
float hourAngle = (timeinfo->tm_hour % 12 + timeinfo->tm_min / 60.0) * 30 * PI / 180;

// Draw the clock hands


drawHand(xc, yc, radius - 10, hourAngle, WHITE); // Hour hand
drawHand(xc, yc, radius - 5, minuteAngle, WHITE); // Minute hand
drawHand(xc, yc, radius - 2, secondAngle, WHITE); // Second hand
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Center and radius of the clock


int xc = getmaxx() / 2;
int yc = getmaxy() / 2;
int radius = 150;

// Main loop to update the clock


while (!kbhit()) {
// Clear the screen
cleardevice();

// Draw the clock face


drawClockFace(xc, yc, radius);

// Update the clock hands


updateClockHands(xc, yc, radius);

// Delay to update every second


delay(1000);
}

// Close the graphics mode


closegraph();
return 0;
}
OUTPUT
Experiment – 9

 Drawing a bouncing ball using Graphics in C++.


#include <graphics.h>
#include <conio.h>
#include <dos.h> // For delay function

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Ball properties
int x = 100, y = 100; // Starting position of the ball
int radius = 20; // Radius of the ball
int dx = 5, dy = 5; // Change in position (velocity)

int screenWidth = getmaxx();


int screenHeight = getmaxy();

while (!kbhit()) {
// Clear previous frame
cleardevice();

// Draw the ball


setcolor(WHITE);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x, y, radius, radius);

// Update the ball's position


x += dx;
y += dy;

// Check for collision with the walls and reverse direction if necessary
if (x - radius < 0 || x + radius > screenWidth) {
dx = -dx;
}
if (y - radius < 0 || y + radius > screenHeight) {
dy = -dy;
}

// Delay to control the speed of the animation


delay(30);
}

// Close the graphics mode


closegraph();
return 0;
}

OUTPUT
Experiment – 10

 Drawing the face of a cat using Graphics in C++.

#include <graphics.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode

// Increase head size


circle(200, 150, 70); // Increase radius from 50 to 70

// Scale eyes proportionally


setfillstyle(1, WHITE);
fillellipse(180, 130, 7, 5); // Increase width and height
fillellipse(220, 130, 7, 5);

// Scale nose proportionally (adjust position if needed)


setfillstyle(1, BLACK);
fillellipse(200, 145, 3, 3); // Increase width and height

// Scale mouth proportionally (adjust curvature for expression)


setcolor(BLACK);
arc(200, 170, 0, 210, 20); // Increase radius and adjust center

// Scale whiskers proportionally (adjust length and angle)


line(170, 155, 140, 130); // Increase length
line(230, 155, 250, 130);

getch();
closegraph();
return 0;
}
OUTPUT

You might also like