Let's begin with beginning..Shortest path program in c Graphics
Helloow friends.. We all think, graphics in c is quite complicated.. but only in the beginning .Once you have kept your hands on it..practice more and more and graphics will be your cup of tea..
Here's a program for finding shortest route among cities using graphics:
I keep posting such interesting new programs.. Follow our blog for more.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void dennix();
void denmia();
void denaus();
void miaatl();
void ausmia();
void ausatl();
void auston();
void auschi();
void phoaus();
void bosatl();
void bostle();
void boschi();
void phochi();
void chitle();
void atlsea();
void citynode(int x,int y)
{
setcolor(WHITE);
circle(x,y,5);
setfillstyle(SOLID_FILL,GREEN);
floodfill(x,y,WHITE);
}
void map()
{
settextstyle(0,HORIZ_DIR,1);
citynode(160,120);
setcolor(14);
outtextxy(145,110,"Denver");
citynode(400,100);
setcolor(14);
outtextxy(385,90,"Phoenix");
citynode(320,240);
setcolor(14);
outtextxy(305,230,"Austin");
citynode(500,200);
setcolor(14);
outtextxy(485,190,"Chicago");
citynode(100,300);
setcolor(14);
outtextxy(85,290,"Miami");
citynode(260,420);
setcolor(14);
outtextxy(245,410,"Atlanta");
citynode(400,300);
setcolor(14);
outtextxy(385,290,"Boston");
citynode(500,400);
setcolor(14);
outtextxy(485,390,"Seattle");
setcolor(WHITE);
dennix();
denmia();
denaus();
miaatl();
ausmia();
ausatl();
auston();
auschi();
phoaus();
bosatl();
bostle();
boschi();
phochi();
chitle();
atlsea();
}
void dennix()
{
line(160,120,400,100);
}
void denmia()
{
line(160,120,100,300);
}
void denaus()
{
line(160,120,320,240);
}
void ausmia()
{
line(100,300,320,240);
}
void miaatl()
{
line(100,300,260,420);
}
void ausatl()
{
line(320,240,260,420);
}
void auston()
{
line(320,240,400,300);
}
void auschi()
{
line(320,240,500,200);
}
void phoaus()
{
line(320,240,400,100);
}
void phochi()
{
line(400,100,500,200);
}
void chitle()
{
line(500,200,500,400);
}
void atlsea()
{
line(260,420,500,400);
}
void bosatl()
{
line(400,300,260,420);
}
void boschi()
{
line(400,300,500,200);
}
void bostle()
{
line(400,300,500,400);
}
int main()
{
int gd,gm,ch;
char start[20],dest[20];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
do
{
setcolor(CYAN);
settextstyle(0,HORIZ_DIR,2);
outtextxy(180,50,"SHORTEST ROUTE FINDER");
setlinestyle(0,0,1);
map();
printf("Start: ");
scanf("%s",start);
printf("Dest: ");
scanf("%s",dest);
printf("Shortest route is as highlighted in map!");
setlinestyle(SOLID_LINE,0xFFFF,THICK_WIDTH);
if(!(strcmp(start,"Denver")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
dennix();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
denmia();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
denaus();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
dennix();
phochi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
denaus();
ausatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
denaus();
auston();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
denaus();
auston();
bostle();
}
}
else if(!(strcmp(start,"Phoenix")))
{
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
dennix();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
phoaus();
ausmia();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
phoaus();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
phochi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
phoaus();
ausatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
phoaus();
auston();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
phochi();
chitle();
}
}
else if(!(strcmp(start,"Chicago")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
phochi();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
auschi();
ausmia();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
auschi();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
dennix();
phochi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
boschi();
bosatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
boschi();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
chitle();
}
}
else if(!(strcmp(start,"Seattle")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
chitle();
phochi();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
atlsea();
miaatl();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
auston();
bostle();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
chitle();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
atlsea();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
bostle();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
denaus();
auston();
bostle();
}
}
else if(!(strcmp(start,"Atlanta")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
ausatl();
phoaus();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
miaatl();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
ausatl();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
bosatl();
boschi();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
denaus();
ausatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
bosatl();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
atlsea();
}
}
else if(!(strcmp(start,"Miami")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
denmia();
dennix();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
denmia();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
ausmia();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
ausmia();
auschi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
miaatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
ausmia();
auston();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
miaatl();
atlsea();
}
}
else if(!(strcmp(start,"Austin")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
phoaus();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
ausmia();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
denaus();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
auschi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
ausatl();
}
if(!(strcmp(dest,"Boston")))
{
setcolor(RED);
auston();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
auston();
bostle();
}
}
else if(!(strcmp(start,"Boston")))
{
if(!(strcmp(dest,"Phoenix")))
{
setcolor(RED);
auston();
phoaus();
}
if(!(strcmp(dest,"Miami")))
{
setcolor(RED);
ausmia();
auston();
}
if(!(strcmp(dest,"Austin")))
{
setcolor(RED);
auston();
}
if(!(strcmp(dest,"Chicago")))
{
setcolor(RED);
boschi();
}
if(!(strcmp(dest,"Atlanta")))
{
setcolor(RED);
bosatl();
}
if(!(strcmp(dest,"Denver")))
{
setcolor(RED);
denaus();
auston();
}
if(!(strcmp(dest,"Seattle")))
{
setcolor(RED);
bostle();
}
}
printf("\n1-Repeat\n2-Exit: ");
scanf("%d",&ch);
if(ch==1)
{
cleardevice();
gotoxy(1,1);
continue;
}
else
{
cleardevice();
break;
}
}
while(1);
getch();
closegraph();
return 0;
}
fig a: Output of above program. |
Comments
Post a Comment