Simple Traffic Control System using C/C++

The following code demonstrates the graphical representation of a simple traffic control system using the BGI graphics library of

Turbo C/C++ 3.0 :
1:


2 :

3 :


4 :


While running this program , make sure using correct path for the BGI graphics .For example : if

Turbo C/C++ is installed in the follwing location "C:\TC" , then the BGI graphics path should be "C:\\TC\\BGI" :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#define M 1000
#define N 3
int data;
int r1=0,g1=0,y1=0,r2=0,g2=0,y2=0,r3=0,g3=0,y3=0,r4=0,g4=0,y4=0;
void initialize()
{
 int gd=DETECT,gm=0;
 initgraph(&gd,&gm,"C:\\TC\\BGI");         //path for the BGI graphics library
}
void drawlights()
{
//Lights
if(g1){setfillstyle(1,0);bar(240,180,246,186);setfillstyle(1,2);bar(240,180,246,186);}
else{setfillstyle(1,0);bar(240,180,246,186);setcolor(2);rectangle(240,180,246,186);}
if(r1){setfillstyle(1,0);bar(240,170,246,176);setfillstyle(1,4);bar(240,170,246,176);}
else{setfillstyle(1,0);bar(240,170,246,176);setcolor(4);rectangle(240,170,246,176);}
if(y1){setfillstyle(1,0);bar(240,160,246,166);setfillstyle(1,14);bar(240,160,246,166);}
else{setfillstyle(1,0);bar(240,160,246,166);setcolor(14);rectangle(240,160,246,166);}

if(g2){setfillstyle(1,0);bar(240,294,246,301);setfillstyle(1,2);bar(240,294,246,301);}
else{setfillstyle(1,0);bar(240,294,246,301);setcolor(2);rectangle(240,294,246,301);}
if(r2){setfillstyle(1,0);bar(240-10,294,246-10,301);setfillstyle(1,4);bar(240-10,294,246-10,301);}
else{setfillstyle(1,0);bar(240-10,294,246-10,301);setcolor(4);rectangle(240-10,294,246-10,301);}
if(y2){setfillstyle(1,0);bar(240-20,294,246-20,301);setfillstyle(1,14);bar(240-20,294,246-20,301);}
else{setfillstyle(1,0);bar(240-20,294,246-20,301);setcolor(14);rectangle(240-20,294,246-20,301);}


if(g3){setfillstyle(1,0);bar(240+100+15,160+100+35,246+100+15,166+100+35);setfillstyle(1,2);bar(240+100+15,160+100+35,246+100+15,166+100+35);}
else{setfillstyle(1,0);bar(240+100+15,160+100+35,246+100+15,166+100+35);setcolor(2);rectangle(240+100+15,160+100+35,246+100+15,166+100+35);}
if(r3){setfillstyle(1,0);bar(240+100+15,170+100+35,246+100+15,176+100+35);setfillstyle(1,4);bar(240+100+15,170+100+35,246+100+15,176+100+35);}
else{setfillstyle(1,0);bar(240+100+15,170+100+35,246+100+15,176+100+35);setcolor(4);rectangle(240+100+15,170+100+35,246+100+15,176+100+35);}
if(y3){setfillstyle(1,0);bar(340+15,280+35,346+15,286+35);setfillstyle(1,14);bar(340+15,280+35,346+15,286+35);}
else{setfillstyle(1,0);bar(340+15,280+35,346+15,286+35);setcolor(14);rectangle(340+15,280+35,346+15,286+35);}


if(g4){setfillstyle(1,0);bar(240+115,294-115,246+115,301-115);setfillstyle(1,2);bar(240+115,294-115,246+115,301-115);}
else{setfillstyle(1,0);bar(240+115,294-115,246+115,301-115);setcolor(2);rectangle(240+115,294-115,246+115,301-115);}
if(r4){setfillstyle(1,0);bar(240-10+115+20,294-115,246-10+115+20,301-115);setfillstyle(1,4);bar(240-10+115+20,294-115,246-10+115+20,301-115);}
else{setfillstyle(1,0);bar(240-10+115+20,294-115,246-10+115+20,301-115);setcolor(4);rectangle(240-10+115+20,294-115,246-10+115+20,301-115);}
if(y4){setfillstyle(1,0);bar(240-20+115+40,294-115,246-20+115+40,301-115);setfillstyle(1,14);bar(240-20+115+40,294-115,246-20+115+40,301-115);}
else{setfillstyle(1,0);bar(240-20+115+40,294-115,246-20+115+40,301-115);setcolor(14);rectangle(240-20+115+40,294-115,246-20+115+40,301-115);}
}
void close2()
{
setcolor(7);
setfillstyle(6,7);
bar(251,290,300,297);
rectangle(251,290,300,297);
}
void close3()
{
setcolor(7);
setfillstyle(6,7);
bar(349,289,354,239);
rectangle(349,289,354,239);
}
void close4()
{
setcolor(7);
setfillstyle(6,7);
bar(301,190,349,184);
rectangle(301,190,349,184);
}
void wait()
{
for(int i=N;i>0;i--)
{
char s[80];
setcolor(7);
sprintf(s,"Waiting for %d seconds :",i);
outtextxy(0,140,s);
delay(M);
setcolor(0);
outtextxy(0,140,s);
if(kbhit()) exit(0);
}

}
void close1()
{
setcolor(7);
setfillstyle(6,7);
bar(250,190,254,240);
rectangle(250,190,254,240);
}
void arrow1(){bar(275,170,275,135);line(270,135,275,130);line(280,135,275,130);}
void arrow2(){bar(275+50,170,275+50,135);line(270+50,135+35,275+50,130+45);line(280+50,135+35,275+50,130+45);}
void arrow3(){bar(190,215,225,215);line(230,215,225,210);line(230,215,225,220);}
void arrow4(){bar(190,265,225,265);line(230-45,265,225-35,260);line(230-45,265,225-35,270);}
void arrow5(){bar(275,170+180,275,135+180);line(270,135+180,275,130+180);line(280,135+180,275,130+180);}
void arrow6(){bar(275+50,170+180,275+50,135+180);line(270+50,135+35+180,275+50,130+45+180);line(280+50,135+35+180,275+50,130+45+180);}
void arrow7(){bar(190+180,215,225+180,215);line(230+180,215,225+180,210);line(230+180,215,225+180,220);}
void arrow8(){bar(190+180,265,225+180,265);line(230-45+180,265,225-35+180,260);line(230-45+180,265,225-35+180,270);}
void arrows()
{
setcolor(0);setfillstyle(1,0);
arrow1();arrow2();arrow3();arrow4();arrow5();arrow6();arrow7();arrow8();
setcolor(7);setfillstyle(4,7);
if(g1){arrow1();arrow3();arrow7();arrow6();}
else if(g2){arrow1();arrow4();arrow5();arrow7();}
else if(g3){arrow1();arrow4();arrow6();arrow8();}
else if(g4){arrow2();arrow7();arrow6();arrow4();}
}
void open1()
{
setcolor(7);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"1124");
data=1124;
outportb(0x378,data);
r1=0;g1=1;y1=0;r2=0;g2=0;y2=1;r3=1;g3=0;y3=0;r4=1;g4=0;y4=0;
drawlights();arrows();
close2();close3();close4();
setfillstyle(0,0);
bar(250,190,254,240);
setfillstyle(6,14);
wait();
setcolor(0);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"1124");
}
void open3()
{
setcolor(7);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"2321");
data=2321;
outportb(0x378,data);
r1=1;g1=0;y1=0;r2=1;g2=0;y2=0;r3=0;g3=1;y3=0;r4=0;g4=0;y4=1;
drawlights();arrows();
close1();close2();close4();
setfillstyle(0,0);
bar(349,289,354,239);
setfillstyle(6,14);
wait();
setcolor(0);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"2321");

}

void open2()
{
setcolor(7);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"2188");
data=2188;
outportb(0x378,data);

r1=1;g1=0;y1=0;r2=0;g2=1;y2=0;r3=0;g3=0;y3=1;r4=1;g4=0;y4=0;
drawlights();arrows();
close1();close3();close4();
setfillstyle(0,0);
bar(251,290,300,297);
setfillstyle(6,14);
wait();
setcolor(0);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"2188");

}
void open4()
{
setcolor(7);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"802");
data=802;
outportb(0x378,data);
r1=0;g1=0;y1=1;r2=1;g2=0;y2=0;r3=1;g3=0;y3=0;r4=0;g4=1;y4=0;
drawlights();arrows();
close2();close3();close1();
setfillstyle(0,0);
bar(301,190,349,184);
setfillstyle(6,14);
wait();
setcolor(0);
outtextxy(0,0,"Sending data to port 0x378 :");
outtextxy(10,15,"802");

}
void showinformation()
{
settextstyle(2, HORIZ_DIR, 0);
setcolor(7);
outtextxy(225,157,"Y1");
outtextxy(225,167,"R1");
outtextxy(225,177,"G1");
outtextxy(214,302,"Y2");
outtextxy(226,302,"R2");
outtextxy(238,302,"G2");
outtextxy(225+140,157+135,"G3");
outtextxy(225+140,167+135,"R3");
outtextxy(225+140,177+135,"Y3");
outtextxy(214+140,302-135,"G4");
outtextxy(226+140,302-135,"R4");
outtextxy(238+140,302-135,"Y4");
outtextxy(0,50,"R1");
outtextxy(15,50,"G1");
outtextxy(30,50,"Y1");
outtextxy(45,50,"R2");
outtextxy(60,50,"G2");
outtextxy(75,50,"Y2");
outtextxy(90,50,"R3");
outtextxy(105,50,"G3");
outtextxy(120,50,"Y3");
outtextxy(135,50,"R4");
outtextxy(150,50,"G4");
outtextxy(165,50,"Y4");
//binary values
outtextxy(0,75,"0");
outtextxy(15,75,"1");
outtextxy(30,75,"0");
outtextxy(45,75,"0");
outtextxy(60,75,"0");
outtextxy(75,75,"1");
outtextxy(90,75,"1");
outtextxy(105,75,"0");
outtextxy(120,75,"0");
outtextxy(135,75,"1");
outtextxy(150,75,"0");
outtextxy(165,75,"0");
outtextxy(175,75,"  (1124)");
outtextxy(0,85,"1");
outtextxy(15,85,"0");
outtextxy(30,85,"0");
outtextxy(45,85,"0");
outtextxy(60,85,"1");
outtextxy(75,85,"0");
outtextxy(90,85,"0");
outtextxy(105,85,"0");
outtextxy(120,85,"1");
outtextxy(135,85,"1");
outtextxy(150,85,"0");
outtextxy(165,85,"0");
outtextxy(175,85,"  (2188)");
outtextxy(0,95,"1");
outtextxy(15,95,"0");
outtextxy(30,95,"0");
outtextxy(45,95,"1");
outtextxy(60,95,"0");
outtextxy(75,95,"0");
outtextxy(90,95,"0");
outtextxy(105,95,"1");
outtextxy(120,95,"0");
outtextxy(135,95,"0");
outtextxy(150,95,"0");
outtextxy(165,95,"1");
outtextxy(175,95,"  (2321)");
outtextxy(0,105,"0");
outtextxy(15,105,"0");
outtextxy(30,105,"1");
outtextxy(45,105,"1");
outtextxy(60,105,"0");
outtextxy(75,105,"0");
outtextxy(90,105,"1");
outtextxy(105,105,"0");
outtextxy(120,105,"0");
outtextxy(135,105,"0");
outtextxy(150,105,"1");
outtextxy(165,105,"0");
outtextxy(175,105,"  (802)");

setfillstyle(1,4);
bar(0,70,5,65);
setfillstyle(1,2);
bar(15,70,20,65);
setfillstyle(1,14);
bar(30,70,35,65);
setfillstyle(1,4);
bar(45,70,50,65);
setfillstyle(1,2);
bar(60,70,65,65);
setfillstyle(1,14);
bar(75,70,80,65);
setfillstyle(1,4);
bar(90,70,95,65);
setfillstyle(1,2);
bar(105,70,110,65);
setfillstyle(1,14);
bar(120,70,125,65);
setfillstyle(1,4);
bar(135,70,140,65);
setfillstyle(1,2);
bar(150,70,155,65);
setfillstyle(1,14);
bar(165,70,170,65);

}
void main()
{
initialize();
cleardevice();
setbkcolor(0);
setcolor(7);
setfillstyle(2,7);
line(250,0,250,190);
line(350,0,350,190);
bar(300,0,300,180);
line(250,getmaxy()-190,250,getmaxy());
line(350,getmaxy()-190,350,getmaxy());
bar(300,getmaxy()-180,300,getmaxy());
line(0,190,250,190);
line(0,290,250,290);
circle(getmaxx()/2-20,getmaxy()/2,5);
setfillstyle(4,7);
bar(0,240,240,240);
line(getmaxx()-290,190,getmaxx(),190);
line(getmaxx()-290,290,getmaxx(),290);
bar(getmaxx()-280,240,getmaxx(),240);
drawlights();
showinformation();
for(;;)
{
open1();
open2();
open3();
open4();
}
closegraph();
restorecrtmode();
}

No comments:

Post a Comment