Sanjay CG Pactical File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Page |1

A practical file of
Computer Graphics
Subject Code: BCA-405

Bachelors of Computer Application (BCA)

Harlal Institute of Management & Technology, Greater Noida


(Affiliated to CCSU, Meerut)
Knowledge park-I, Institution Area, Greater Noida 201306

SUBMITTED TO: SUBMITTED BY:

Mr. Akhilesh Nagar Name: Sanjay Kumar


(Professor, IT) Roll no: 210916106062
Semester: IV
Page |2

INDEX

S.no. Topic Page no.


1. Write a program to draw a line. 3

2. Write a program to draw a rectangle and bar. 4

3. Write a program to draw a circle 5

4. Write a program to draw ellipse. 6

5. Write a program to draw a bar graph. 7-8

6. Write a program to draw a 3-D bar. 9-10

7. Write a program to draw sin wave. 11-12

8. Write a program to draw stars in night sky. 13-14

9. Write a program to draw pie chart. 15-16

10. Write a program to make a digital clock. 17-18

11. Write a program to make a bouncing ball animation. 19-20

12. Write a program to draw a hut. 21-22

13. Write a program to make animation of a moving car 23-24

14. Write a program to implement DDA line drawing algorithm 25-26

Write a program to implement Bresenham’s line drawing 27-29


15.
algorithm
Write a program to draw circle using mid-point circle drawing 29-31
16.
algorithm.
Page |3

Q.1 Write a program to draw a line.


Program:
#include <graphics.h>

#include<stdio.h>

#include<conio.h>

void main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

int x1 = 100, y1 = 100;

int x2 = 500, y2 = 400;

// Draw a line from (x1, y1) to (x2, y2)

line(x1, y1, x2, y2);

getch();

closegraph();

Output:
Page |4

Q.2 Write a program to draw a rectangle and bar.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

// Draw a rectangle with given coordinates

int rectLeft = 100, rectTop = 100;

int rectRight = 300, rectBottom = 200;

rectangle(rectLeft, rectTop, rectRight, rectBottom);

// Draw a horizontal rectangle as a bar

int barLeft = 100, barTop = 250;

int barRight = 300, barBottom = 270;

bar(barLeft, barTop, barRight, barBottom);

getch(); // Wait for a key press before closing the window

closegraph();

Output:

Q.3 Write a program to draw a circle.


Page |5

Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int centerX = 200;

int centerY = 200;

int radius = 100;

circle(centerX, centerY, radius);

getch(); //

closegraph();

Output:

Q.4 Write a program to draw a ellipse.


Page |6

Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int centerX = 200; // X-coordinate of the center of the ellipse

int centerY = 200; // Y-coordinate of the center of the ellipse

int rx = 100; // Horizontal radius of the ellipse

int ry = 50; // Vertical radius of the ellipse

// Draw an ellipse with the given parameters

ellipse(centerX, centerY, 0, 360, rx, ry);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:

Q.5 Write a program to draw a bar graph.


Page |7

Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

// Function to draw a bar with a specified color

void drawBar(int startX, int endX, int barHeight, int color)

setfillstyle(SOLID_FILL, color); // Set the fill color for the bar

bar(startX, getmaxy(), endX, getmaxy() - barHeight);

// Draw a line at the top of the bar

setcolor(color);

line(startX, getmaxy() - barHeight, endX, getmaxy() - barHeight);

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

// Draw the bars of the bar graph

drawBar(100, 150, 100, RED); // Bar 1

drawBar(170, 220, 200, BLUE); // Bar 2

drawBar(240, 290, 150, GREEN); // Bar 3

drawBar(310, 360, 300, YELLOW); // Bar 4

drawBar(380, 430, 250, MAGENTA); // Bar 5

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
Page |8

Q.6 Write a program to draw a 3D bar.


Page |9

Program:
#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <graphics.h>

int main()

int gd, gm,i,j;

int xs,xe, ys, ye, x, y;

gd= DETECT;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

setfillstyle(SOLID_FILL,LIGHTRED);

x = 180;

y = 25;

outtextxy(x , y , "75000");

outtextxy(x + 50 , y + 50, "55000");

outtextxy(x + 100 , y + 100 , "45000");

outtextxy(x + 170, y + 170 , "30000");

outtextxy(x + 230, y + 230 , "20000");

j = 55;

xs = 100;xe = 150, ys = 10, ye = 320;

for(i = 0; i<5;i++)

xs = xs + j;

ys = ys + j;

xe = xe + j;

bar3d(xs, ys, xe, ye, (xe - xs)/4, 1);

}
P a g e | 10

getch();

closegraph();

return 0;

Output:
P a g e | 11

Q.7 Write a program to draw a sin wave.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int maxX = getmaxx(); // Maximum X-coordinate of the window

int maxY = getmaxy(); // Maximum Y-coordinate of the window

// Draw the X and Y axes

line(0, maxY / 2, maxX, maxY / 2); // X-axis

line(maxX / 2, 0, maxX / 2, maxY); // Y-axis

int amplitude = 100; // Amplitude of the sine wave

int frequency = 1; // Frequency of the sine wave (number of cycles)

float phaseShift = 0; // Phase shift of the sine wave (in radians)

for (int x = 0; x < maxX; x++)

// Calculate the corresponding y-coordinate (sin value) for the given x-coordinate

float y = amplitude * sin(2 * M_PI * frequency * (x - maxX / 2) / maxX + phaseShift);

// Plot the point on the sine wave

putpixel(x, maxY / 2 - y, RED);

getch(); // Wait for a key press before closing the window

closegraph();
P a g e | 12

return 0;

Output:
P a g e | 13

Q.8 Write a program to draw stars in night sky.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <time.h>

// Function to draw a star (randomly changing intensity)

void drawStar(int x, int y)

int intensity = rand() % 256; // Generate a random intensity (0 to 255)

// Set the color to white with the specified intensity

setcolor(COLOR(intensity, intensity, intensity));

// Draw a point (star) at the specified location

putpixel(x, y, WHITE);

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

srand(time(NULL)); // Seed the random number generator

int maxX = getmaxx(); // Maximum X-coordinate of the window

int maxY = getmaxy(); // Maximum Y-coordinate of the window

// Draw a black background (night sky)

setbkcolor(BLACK);

cleardevice();

int numStars = 100; // Number of stars to draw

// Draw stars randomly on the screen

for (int i = 0; i < numStars; i++)


P a g e | 14

int x = rand() % maxX; // Random X-coordinate

int y = rand() % maxY; // Random Y-coordinate

drawStar(x, y);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
P a g e | 15

Q.9 Write a program to draw a pie chart.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

// Function to draw a pie chart segment

void drawPieSegment(int centerX, int centerY, int radius, int startAngle, int endAngle, int color)

setfillstyle(SOLID_FILL, color);

pieslice(centerX, centerY, startAngle, endAngle, radius);

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int centerX = 300; // X-coordinate of the center of the pie chart

int centerY = 200; // Y-coordinate of the center of the pie chart

int radius = 100; // Radius of the pie chart

int totalValue = 360; // Total value (sum of all segment values)

// Data for the pie chart (values in degrees)

int data[] = {60, 90, 120, 90}; // Four segments

// Colors for each segment

int colors[] = {RED, BLUE, GREEN, YELLOW};

int startAngle = 0;

int endAngle = 0;

for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++)

endAngle = startAngle + data[i];


P a g e | 16

drawPieSegment(centerX, centerY, radius, startAngle, endAngle, colors[i]);

startAngle = endAngle;

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
P a g e | 17

Q.10 Write a program to make a digital clock.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <time.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int maxX = getmaxx(); // Maximum X-coordinate of the window

int maxY = getmaxy(); // Maximum Y-coordinate of the window

while (!kbhit()) // Keep updating the clock until a key is pressed

// Clear the screen

cleardevice();

// Get the current system time

time_t currentTime;

struct tm* timeInfo;

time(&currentTime);

timeInfo = localtime(&currentTime);

// Get the time components (hours, minutes, seconds)

int hours = timeInfo->tm_hour;

int minutes = timeInfo->tm_min;

int seconds = timeInfo->tm_sec;

// Convert the time components to strings

char timeString[9]; // HH:MM:SS + '\0'

sprintf(timeString, "%02d:%02d:%02d", hours, minutes, seconds);


P a g e | 18

// Display the time on the screen

settextstyle(DEFAULT_FONT, HORIZ_DIR, 4); // Set font size

outtextxy(maxX / 2 - 120, maxY / 2 - 30, timeString);

delay(1000); // Wait for 1 second before updating the time again

getch(); // Wait for a key press before closing the window

closegraph();

Output:
P a g e | 19

Q.11 Write a program to make animation of bouncing ball.


Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int maxX = getmaxx(); // Maximum X-coordinate of the window

int maxY = getmaxy(); // Maximum Y-coordinate of the window

int ballRadius = 20; // Radius of the ball

int ballX = maxX / 2; // Initial X-coordinate of the ball (centered)

int ballY = ballRadius; // Initial Y-coordinate of the ball

int xSpeed = 5; // Horizontal speed of the ball

int ySpeed = 5; // Vertical speed of the ball

while (!kbhit()) // Keep the animation running until a key is pressed

// Clear the screen

cleardevice();

// Draw the ball

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

fillellipse(ballX, ballY, ballRadius, ballRadius);

// Update the position of the ball

ballX += xSpeed;

ballY += ySpeed;

// Check for collisions with the window boundaries


P a g e | 20

if (ballX + ballRadius >= maxX || ballX - ballRadius <= 0)

xSpeed = -xSpeed; // Change horizontal direction

if (ballY + ballRadius >= maxY || ballY - ballRadius <= 0)

ySpeed = -ySpeed; // Change vertical direction

delay(50); // Adjust the delay for animation speed

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
P a g e | 21

Q.12 Write a program to draw a hut.


Program
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

// Draw the base of the hut

rectangle(150, 300, 450, 450);

// Draw the roof of the hut

line(150, 300, 300, 200);

line(450, 300, 300, 200);

// Draw the door of the hut

rectangle(250, 400, 350, 450);

// Draw the windows of the hut

rectangle(200, 320, 250, 370);

rectangle(350, 320, 400, 370);

// Draw the chimney of the hut

rectangle(375, 180, 425, 280);

line(375, 180, 400, 150);

line(425, 180, 400, 150);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

}
P a g e | 22

Output:
P a g e | 23

Q.13 Write a program to make animation of moving car.


Program:
#include <graphics.h>

#include <dos.h>

#include <conio.h>

main()

int i, j = 0, gd = DETECT, gm;

initgraph(&gd,&gm," C:\\TURBOC3\\BGI ");

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(25,240,"Press any key to view the moving car");

getch();

setviewport(0,0,639,440,1);

for (i = 0; i <= 420; i = i + 10, j++)

rectangle(50+i,275,150+i,400);

rectangle(150+i,350,200+i,400);

circle(75+i,410,10);

circle(175+i,410,10);

setcolor(j);

delay(100);

if (i == 420)

break;

clearviewport();

getch();

closegraph();
P a g e | 24

return 0;

Output:
P a g e | 25

Q.14 Write a program to draw a line using DDA line drawing algorithm.
Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void drawLineDDA(int x1, int y1, int x2, int y2)

int dx = x2 - x1;

int dy = y2 - y1;

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

float xIncrement = (float)dx / steps;

float yIncrement = (float)dy / steps;

float x = x1;

float y = y1;

for (int i = 0; i <= steps; i++)

putpixel(round(x), round(y), WHITE);

x += xIncrement;

y += yIncrement;

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int x1 = 100; // X-coordinate of the starting point

int y1 = 100; // Y-coordinate of the starting point

int x2 = 300; // X-coordinate of the ending point


P a g e | 26

int y2 = 200; // Y-coordinate of the ending point

drawLineDDA(x1, y1, x2, y2);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
P a g e | 27

Q.15 Write a program to draw a line using bresenham’s line drawing


algorithm.
Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void drawLineBresenham(int x1, int y1, int x2, int y2)

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int x, y;

int twody = 2 * dy;

int twodyMinusdx = 2 * (dy - dx);

int p;

if (x1 > x2)

x = x2;

y = y2;

x2 = x1;

else

x = x1;

y = y1;

putpixel(x, y, WHITE);

if (dy <= dx)


P a g e | 28

p = 2 * dy - dx;

while (x < x2)

x++;

if (p < 0)

p += twody;

else

y++;

p += twodyMinusdx;

putpixel(x, y, WHITE);

else

p = 2 * dx - dy;

while (y < y2)

y++;

if (p < 0)

p += 2 * dx;

else

x++;

p += 2 * (dx - dy);

putpixel(x, y, WHITE);
P a g e | 29

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int x1 = 100; // X-coordinate of the starting point

int y1 = 100; // Y-coordinate of the starting point

int x2 = 300; // X-coordinate of the ending point

int y2 =200; // Y-coordinate of the ending point

drawLineBresenham(x1, y1, x2, y2);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:
P a g e | 30

Q.16 Write a program to draw circle using mid-point circle drawing


algorithm.
Program:
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void drawCircleMidpoint(int centerX, int centerY, int radius)

int x = 0;

int y = radius;

int p = 1 - radius;

while (x <= y)

putpixel(centerX + x, centerY + y, WHITE);

putpixel(centerX - x, centerY + y, WHITE);

putpixel(centerX + x, centerY - y, WHITE);

putpixel(centerX - x, centerY - y, WHITE);

putpixel(centerX + y, centerY + x, WHITE);

putpixel(centerX - y, centerY + x, WHITE);

putpixel(centerX + y, centerY - x, WHITE);

putpixel(centerX - y, centerY - x, WHITE);

if (p < 0)

p += 2 * x + 3;

else

p += 2 * (x - y) + 5;

y--;

}
P a g e | 31

x++;

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int centerX = 300; // X-coordinate of the center of the circle

int centerY = 200; // Y-coordinate of the center of the circle

int radius = 100; // Radius of the circle

drawCircleMidpoint(centerX, centerY, radius);

getch(); // Wait for a key press before closing the window

closegraph();

return 0;

Output:

You might also like