0% found this document useful (0 votes)
24 views26 pages

Index: S.N Program Name

The document contains a list of C programming examples for various graphics applications, including drawing shapes, animations, and charts. Each program is accompanied by its source code, demonstrating the use of graphics libraries in C. The examples range from simple shapes like rectangles and circles to more complex animations like bouncing balls and moving cars.

Uploaded by

kingkhanadil5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

Index: S.N Program Name

The document contains a list of C programming examples for various graphics applications, including drawing shapes, animations, and charts. Each program is accompanied by its source code, demonstrating the use of graphics libraries in C. The examples range from simple shapes like rectangles and circles to more complex animations like bouncing balls and moving cars.

Uploaded by

kingkhanadil5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

INDEX

S.N PROGRAM NAME


1 C program to draw rectangle and bar
2 C program to draw an eclipse
3 C program to draw concentric circles
4 C program to draw circle
5 C Program to Draw Bar Graph
6 C program to draw 3D bar graph
7 C program to draw sine wave
8 C program to draw cosine wave
9 C program to draw stars in night sky
10 C program to draw pie chart
11 C program to make a digital clock
12 C program for bouncing ball graphics animation
13 C program to draw a hut and colour it
14 C program for moving car graphics animation
15 Draw a line using DDA

16 Bresenham’s Line Drawing Algorithm

17 circle using Midpoint Circle Algorithm


18 implement flood Fill algorithm in C
1. C program to draw rectangle and bar using graphics.
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
rectangle(150, 50, 400, 150);
bar(150, 200, 400, 350);
getch();
return 0;
}
2. C program to draw an eclipse using graphics .
#include<stdio.h>

#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "X:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");
ellipse(x, y, 0, 360, 120, 60);

getch();
return 0;
}
3. C program to draw concentric circles using graphics
#include<stdio.h>
#include<graphics.h>
#include<conio.h>

int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
/* Initialize center of circle with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;

outtextxy(240, 50, "Concentric Circles");


/* Draw circles on screen */
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);
getch();
return 0;
}

4. C program to draw circle using c graphics.


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

int main()
{
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "C:\\TC\\BGI")
x = getmaxx()/2;
y = getmaxy()/2;

outtextxy(x-100, 50, "CIRCLE Using Graphics in C");


circle(x, y, radius);

getch();
return 0;
}
5. C Program to Draw Bar Graph Using C Graphics.
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);
outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");
setfillstyle(XHATCH_FILL, RED);
bar(150,80,200,410);
bar(225,100,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);
getch();
return 0;
}

6. C program to draw 3D bar graph using graphics.


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

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);

outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");

setfillstyle(XHATCH_FILL, RED);
bar3d(150,80,200,410, 15, 1);
bar3d(225,100,275,410, 15, 1);
bar3d(300,120,350,410, 15, 1);
bar3d(375,170,425,410, 15, 1);
bar3d(450,135,500,410, 15, 1);

getch();
return 0;
}
7. C program to draw sine wave using graphics.
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>

int main()
{
int gd = DETECT, gm;
int angle = 0;
double x, y;

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

line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);


for(x = 0; x < getmaxx(); x+=3) {

y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(100);

angle+=5;
}

getch();

return 0;
}

8. C program to draw cosine wave using graphics.


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

int main() {
int gd = DETECT, gm;
int angle = 0;
double x, y;

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

line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);


/* generate a sine wave */
for(x = 0; x < getmaxx(); x+=3) {

y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(100);
angle+=5;
}
getch();
return 0;
}

9. C program to draw stars in night sky using graphics.

#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
int main() {
int gd = DETECT, gm;
int i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
while (!kbhit()) {
for(i=0; i<=500; i++) {
x=rand()%getmaxx();
y=rand()%getmaxy();
putpixel(x,y,15);
}
delay(500);
cleardevice();
}
getch();
closegraph();
return 0;
}

10. C program to draw pie chart using graphics.

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

int main() {
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE CHART");
x = getmaxx()/2;
y = getmaxy()/2;

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
setfillstyle(SOLID_FILL, RED);
pieslice(x, y, 0, 60, 120);
outtextxy(x + 140, y - 70, "FOOD");
setfillstyle(SOLID_FILL, YELLOW);
pieslice(x, y, 60, 160, 120);
outtextxy(x - 30, y - 170, "RENT");
setfillstyle(SOLID_FILL, GREEN);
pieslice(x, y, 160, 220, 120);
outtextxy(x - 250, y, "ELECTRICITY");

setfillstyle(SOLID_FILL, BROWN);
pieslice(x, y, 220, 360, 120);
outtextxy(x, y + 150, "SAVINGS");
getch();
return 0;
}

11. C program to make a digital clock using graphics.


#include <conio.h>
#include <graphics.h>
#include <time.h>
#include <dos.h>
#include <string.h>

int main() {
int gd = DETECT, gm;
int midx, midy;
long current_time;
char timeStr[256];
initgraph(&gd, &gm, "C:\\TC\\BGI");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
while (!kbhit()) {
cleardevice();
setcolor(WHITE);
setfillstyle(SOLID_FILL, WHITE);
rectangle(midx - 250, midy - 40, midx + 250, midy + 40);
floodfill(midx, midy, WHITE);
current_time = time(NULL);

strcpy(timeStr, ctime(&current_time));
setcolor(RED);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);
moveto(midx, midy);
outtext(timeStr);
delay(1000);
} getch();
return 0;
}

12. C program for bouncing ball graphics animation.


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

int main() {
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = 30;
while (!kbhit()) {
if(y >= getmaxy()-30 || y <= 30)
flag = !flag;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);
delay(50);
cleardevice();

if(flag)
{
y = y + 5;
}
else
{
y = y - 5;
}
}
getch();
return 0;
}
13. C program to draw a hut and color it using graphics.
#include<graphics.h>
#include<conio.h>

int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);

line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);

/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
getch();
return 0;
}

14. C program for moving car graphics animation.


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

int main() {
int gd = DETECT, gm;
int i, maxx, midy;

/* initialize graphic mode */


initgraph(&gd, &gm, "X:\\TC\\BGI");
/* maximum pixel in horizontal axis */
maxx = getmaxx();
/* mid pixel in vertical axis */
midy = getmaxy()/2;

for (i=0; i < maxx-150; i=i+5) {


/* clears screen */
cleardevice();

/* draw a white road */


setcolor(WHITE);
line(0, midy + 37, maxx, midy + 37);

/* Draw Car */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);

line(i, midy + 23, i, midy);


line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20);
line(80 + i, midy - 20, 100 + i, midy);
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12);
line(42 + i, midy + 23, 78 + i, midy + 23);

arc(90 + i, midy + 23, 0, 180, 12);


line(102 + i, midy + 23, 120 + i, midy + 23);
line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy - 15);
line(57 + i, midy - 15, 57 + i, midy);
line(57 + i, midy, 28 + i, midy);
line(62 + i, midy - 15, 77 + i, midy - 15);
line(77 + i, midy - 15, 92 + i, midy);
line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
/* Draw Wheels */
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
/* Add delay of 0.1 milli seconds */
delay(100);
}

getch();
return 0;
}

15. Draw a line using DDA.

#include<stdio.h>
#include<graphics.h>
int abs (int n)
{
return ( (n>0) ? n : ( n * (-1)));
}
void DDA(int X0, int Y0, int X1, int Y1)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel (X,Y,RED); // put pixel at (X,Y)
X += Xinc;
Y += Yinc;
delay(100);
}}
int main()
{int gd = DETECT, gm;
initgraph (&gd, &gm, "");
int X0 = 2, Y0 = 2, X1 = 14, Y1 = 16;
DDA(2, 2, 14, 16);
return 0;
}

16. Program for Bresenham’s Line Drawing Algorithm in C.

#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
} x=x+1;
} }
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
17. C program for drawing a circle using Midpoint Circle
Algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pixel(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
printf("*** Mid-Point Subdivision algorithm of circle ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of Yc \t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
Pk=1-r;
pixel(xc,yc,x,y);
while(x<y)
{
if(Pk<0)
{
x=x+1;
Pk=Pk+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+(2*x)-(2*y)+1;
}
pixel(xc,yc,x,y);
}
getch();
closegraph();
}
void pixel(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7);

putpixel(xc-y,yc+x,7);
putpixel(xc-x,yc+y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc-x,7);
putpixel(xc+x,yc-y,7);
}
18. Program to implement flood Fill algorithm in C.

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,n,i,x,y,a[10][10];
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Flood Fill Algorithm ***\n");
printf("Enter the no. of edges of polygon\t");
scanf("%d",&n);
printf("Enter the cordinates of polygon\n");
for(i=0;i<n;i++)
{
printf("X%d Y%d ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];

printf("Enter the seed Point (X,Y)\t");


scanf("%d%d",&x,&y);
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(int x,int y)

{
if(getpixel(x,y) == 0)

{
putpixel(x,y,RED);
fill_left(--x,y);
x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy