Index: S.N Program Name
Index: S.N Program Name
#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;
int main()
{
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "C:\\TC\\BGI")
x = getmaxx()/2;
y = getmaxy()/2;
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;
}
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;
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(100);
angle+=5;
}
getch();
return 0;
}
int main() {
int gd = DETECT, gm;
int angle = 0;
double x, y;
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(100);
angle+=5;
}
getch();
return 0;
}
#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;
}
#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;
}
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(¤t_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;
}
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;
}
int main() {
int gd = DETECT, gm;
int i, maxx, midy;
/* Draw Car */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);
getch();
return 0;
}
#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;
}
#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];
{
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);
}