GRAPICS
graphicsDriver - DETECT
graphicsMode
driverDirectoryPath - BGI files
Colors
16 colors declared in C Graphics
COLOR MACRO |
INTEGER VALUE |
BLACK |
0 |
BLUE |
1 |
GREEN |
2 |
CYAN |
3 |
RED |
4 |
MAGENTA |
5 |
BROWN |
6 |
LIGHTGRAY |
7 |
DARKGRAY |
8 |
LIGHTBLUE |
9 |
LIGHTGREEN |
10 |
LIGHTCYAN |
11 |
LIGHTRED |
12 |
LIGHTMAGENTA |
13 |
YELLOW |
14 |
WHITE |
15 |
/* C graphics program to draw a line */ #include<stdio.h> #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; /* initialization of
graphic mode */ initgraph(&gd,
&gm, "C:\\Turboc3\\BGI "); line(100,100,200, 200); getch(); closegraph(); getch(); } Output |
RAINBOW PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver = DETECT,gmode;
int x,y,i;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
for(i=30;i<200;i++)
{
delay(100);
setcolor(i/10);
arc(x,y,0,180,i-10);
}
getch();
}
Output
CLOCK
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define WBC 5
//^watchbackcolor
#define X 200
#define Y 200
void dial(int
x, int y);
void sechand(int
timeminute);
void minhand(int
t)
{
int x1,y1;
setlinestyle(0,0,3);
x1= X+ (80 * cos(t*0.1047));
y1= Y+ (80 * sin(t*0.1047));
setcolor(BLACK);
line( X, Y, x1, y1);
setcolor(WBC+1);
line( X, Y, X+ 80 * cos((t-1)*0.1047),Y+ 80 *
sin((t-1)*0.1047));
circle(X,Y,4);
}
void sechand(int
t)
{
int x1,y1;
setlinestyle(0,0,3);
x1= X+(100 * cos(t*0.1047));
y1= Y+(100 * sin(t*0.1047));
setcolor(RED);
line(X, Y, x1, y1);
setcolor(WBC+1);
line(X, Y, X+ 100 * cos((t-1)*0.1047),Y+ 100 *
sin((t-1)*0.1047));
circle(X,Y,4);
}
void dial(int
x,int y)
{
int const
size=200;
setfillstyle(1,WBC);
fillellipse(x,y,size,size);
setfillstyle(1,WBC+1);
fillellipse(x,y,size-20,size-20);
outtextxy(x,y-(size-40),"12");
outtextxy(x,y+(size-40),"6");
outtextxy(x+(size-40),y,"3");
outtextxy(x-(size-40),y,"9");
outtextxy(x+size/3,y-2*size/3,"1");
outtextxy(x+2*size/3,y-size/3,"2");
outtextxy(x+2*size/3,y+size/3,"4");
outtextxy(x+size/3,y+2*size/3,"5");
outtextxy(x-size/3,y+2*size/3,"7");
outtextxy(x-2*size/3,y+size/3,"8");
outtextxy(x-size/3,y-2*size/3,"11");
outtextxy(x-2*size/3,y-size/3,"10");
circle(x,y,4);
}
void main()
{
int gd=DETECT, gm,i,j, flag=1;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
dial(200,200);
do
{
minhand(i);
for(j=0;j<60;j++)
{
sechand(j);
delay(1000
);
if(kbhit()) {
flag =0;
break;
}
}
i++;
}while(flag);
closegraph();
}
Output
C Graphics Programs
CIRCLE
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main(){
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "
C:\\Turboc3\\BGI ");
/* Initialize center of circle with center of
screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "CIRCLE Using Graphics
in C");
/* Draw circle on screen */
circle(x, y, radius);
closegraph();
getch();
}
Output
RECTANGLE & SOLID BAR
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
/* Draw rectangle on screen */
rectangle(150, 50, 400, 150);
/* Draw Bar on screen */
bar(150, 200, 400, 350);
closegraph();
getch();
}
ECLIPSE
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm,
"C:\\Turboc3\\BGI");
/* Initialize center of ellipse with center of
screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "ELLIPSE Using
Graphics in C");
/* Draw ellipse on screen */
ellipse(x, y, 0, 360, 120, 60);
closegraph();
getch();
}
CONCENTRIC CIRCLES
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm,
"C:\\Turboc3\\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);
closegraph();
getch();
}
BAR GRAPH
#include <stdio.h>
#include<conio.h>
#include <graphics.h>
main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"BAR GRAPH");
setlinestyle(SOLID_LINE,0,2);
/* Draw X and Y Axis */
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");
/* Draw bars on screen */
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);
closegraph();
getch();
}
3D BAR GRAPH
#include <stdio.h>
#include<conio.h>
#include <graphics.h>
main() {
int gd = DETECT, gm;
initgraph(&gd, &gm,
"C:\\Turboc3\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");
setlinestyle(SOLID_LINE,0,2);
/* Print X and Y Axis */
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");
/* Print 3D bars */
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);
closegraph();
getch();
}
SINE WAVE
#include <stdio.h>
#include<conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
main() {
int gd = DETECT, gm;
int angle = 0;
double x, y;
initgraph(&gd,
&gm, "C:\\Turboc3\\BGI");
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
/* generate a sine wave */
for(x = 0; x < getmaxx(); x+=3) {
/* calculate y value
given x */
y =
50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
/* color a pixel at
the given position */
putpixel(x, y, 15);
delay(100);
/* increment angle */
angle+=5;
}
closegraph();
getch();
}
PIE CHART
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
main() {
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm,
"C:\\Turboc3\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE
CHART");
/* Setting cordinate of center of
circle */
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");
closegraph();
getch();
}
DIGITAL CLOCK
#include <stdio.h>
#include<conio.h>
#include <graphics.h>
#include <time.h>
#include <dos.h>
#include <string.h>
main() {
int gd = DETECT, gm;
int midx, midy;
long current_time;
char timeStr[256];
initgraph(&gd, &gm,
"C:\\Turboc3\\BGI");
/* mid pixel in horizontal
and vertical axis */
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);
/*
Get Current epoch time in seconds */
current_time
= time(NULL);
/*
store the date and time in string */
strcpy(timeStr,
ctime(¤t_time));
setcolor(RED);
settextjustify(CENTER_TEXT,
CENTER_TEXT);
settextstyle(SANS_SERIF_FONT,
HORIZ_DIR, 4);
moveto(midx,
midy);
/*
print current time */
outtext(timeStr);
/*
Add delay of 1000 milliseconds(1 second) */
delay(1000);
}
closegraph();
getch();
}
BOUNCING BALL ANIMATION
#include <stdio.h>
#include<conio.h>
#include <graphics.h>
#include <dos.h>
main() {
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm,
"C:\\Turboc3\\BGI");
/* get mid positions in x and y-axis */
x = getmaxx()/2;
y = 30;
while (!kbhit()) {
if(y >= getmaxy()-30 || y <= 30)
flag = !flag;
/* draws the gray
board */
setcolor(RED);
setfillstyle(SOLID_FILL,
RED);
circle(x, y, 30);
floodfill(x, y, RED);
/* delay for 50 milli seconds */
delay(50);
/* clears screen */
cleardevice();
if(flag){
y = y + 5;
} else {
y = y - 5;
}
}
closegraph();
getch();
}
MOVING
CAR ANIMATION
#include <stdio.h>
#include<conio.h>
#include <graphics.h>
#include <dos.h>
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);
}
closegraph();
getch();
}
Output
Snake game in C
0 comments:
Post a Comment