Friday, 27 May 2016

C++ graphics program for traffic light (Source code)

/*Program for traffic Light*/

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

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

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   setcolor(CYAN);
   settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy-10, "Traffic Light Simulation");
   outtextxy(midx, midy+10, "Press any key to start");
   getch();
   cleardevice();
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(midx, midy-50,WHITE);
   setcolor(BLUE);
   outtextxy(midx,midy-50,"STOP");
   delay(2000);
   graphdefaults();
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(midx, midy,WHITE);
   setcolor(BLUE);
   outtextxy(midx-18,midy-3,"READY");

   delay(2000);
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(midx, midy+50,WHITE);
   setcolor(BLUE);
   outtextxy(midx-7,midy+48,"GO");
   setcolor(RED);
   settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
   outtextxy(midx-150, midy+100, "Brrrrrmmmmmm... :)");

   getch();
}


Output:


C++ Graphics program analog clock (Source Code)

  /*Program for analog CLock*/

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

 void minSecPos(int xrad, int midx, int midy, int x[60], int y[60]) 
 {
  int i, j=45;
  for (i=360; i>=0; i=i-6)
  {
   x[j] = midx-(xrad*cos((i*3.14)/180));
   y[j--] = midy-(xrad*sin((i*3.14)/180));
   j = (j==-1)?59:j;
  }
  return;
 }

  void calcPoints(int radius, int midx, int midy, int x[12], int y[12])
 {
  int x1, y1;
  x[0] = midx, y[0] = midy-radius;
  x[6] = midx, y[6] = midy+radius;
  x[3] = midx+radius, y[3] = midy;
  x[9] = midx-radius, y[9] = midy;

  x1 = (int) ((radius/2)*sqrt(3));
  y1 = (radius/2);
  x[2] = midx+x1, y[2] = midy-y1;
  x[4] = midx+x1, y[4] = midy+y1;
  x[8] = midx-x1, y[8] = midy+y1;
  x[10] = midx-x1, y[10] = midy-y1;

  x1 = radius/2;
  y1 = (int) ((radius/2)*sqrt(3));
  x[1] = midx+x1, y[1] = midy-y1;
  x[5] = midx+x1, y[5] = midy+y1;
  x[7] = midx-x1, y[7] = midy+y1;
  x[11] = midx-x1, y[11] = midy-y1;
  return;
 }

 int main() {
   int gd=DETECT, gm, err, tmp;
   initgraph(&gd, &gm, "C:\\tc\\bgi");

int i, j, midx, midy, radius, hr, min, sec;
int x[12], y[12], minx[60], miny[60];
int hrx[12], hry[12], secx[60], secy[60];
int secx1, secy1;
char str[256];
time_t t1;
struct tm*data;

err = graphresult();

if (err != grOk)
{
printf("Graphics Error: %s",
grapherrormsg(err));
return 0;
}

midx = getmaxx()/2;
midy = getmaxy()/2;

radius = 200;

calcPoints(radius-30, midx, midy, x, y);
calcPoints(radius-90, midx, midy, hrx, hry);

minSecPos(radius-50, midx, midy, minx, miny);
minSecPos(radius-70, midx, midy, secx, secy);

while (!kbhit())
{
setlinestyle(SOLID_LINE, 1, 3);
settextstyle(GOTHIC_FONT, 0, 3);

circle(midx, midy, radius);

for (j=0; j<12; j++)
{
 if (j==0)
  {
   sprintf(str, "%d", 12);
    } else {
  sprintf(str, "%d", j);
  }
settextjustify(CENTER_TEXT, CENTER_TEXT);
moveto(x[j], y[j]);
outtext(str);
}

t1 = time(NULL);
data = localtime(&t1);

sec = data->tm_sec % 60;
line(midx, midy, 
[sec], secy[sec]);

min = data->tm_min % 60;
line(midx, midy, minx[min], miny[min]);

hr = data->tm_hour % 12;
line(midx, midy, hrx[hr], hry[hr]);
delay(1000);
cleardevice();
}

     getch();
     closegraph();
     return 0;
  }

  Output:



C++ program for Digital Clock (Source Code)

/*DIGITAL CLOCK*/

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

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

 int midx, midy, err=graphresult(); ;
 long currtime;
  char date[256];

if (err != 0)
{
printf("Graphics Error: %s\n",
grapherrormsg(err));
return 0;
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

while (!kbhit())
{
cleardevice();
currtime = time(NULL);

strcpy(date, ctime(&currtime));

settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 5);
moveto(midx, midy);
outtext(date);

sleep(1);
}

getch();
closegraph();
return 0;
  }

Output:

Monday, 21 December 2015

C++ program Prime number or Not (Source Code)

/*Check whether number is Prime or Not*/

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
 int number;
  cout<<"Enter a number to find whether PRIME or NOT: ";
  cin>>number;
   for(int i=2; i<number; i++)
    {
     if((number%i) == 0)
   //  break;
    }

    if ((number ==1) || (i==number))
       cout<<"\n"<<number<<" is prime";
    else
       cout<<"\n"<<number<<" is not prime";

getch();
}

For OUTPUT check out the video! :

Click here




Tuesday, 10 November 2015

How to move a car in C++ (Computer Graphics)- (Source code)

/*Moving a car*/

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

void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");


for (int i=0;i<500;i++)
{
/***CAR BODY ******/
line(50+i,370,90+i,370);
arc(110+i,370,0,180,20);
line(130+i,370,220+i,370);
arc(240+i,370,0,180,20);
line(260+i,370,300+i,370);
line(300+i,370,300+i,350);
line(300+i,350,240+i,330);
line(240+i,330,200+i,300);
line(200+i,300,110+i,300);
line(110+i,300,80+i,330);
line(80+i,330,50+i,340);
line(50+i,340,50+i,370);

/***CAR Windows***/
line(165+i,305,165+i,330);
line(165+i,330,230+i,330);
line(230+i,330,195+i,305);
line(195+i,305,165+i,305);

line(160+i,305,160+i,330);
line(160+i,330,95+i,330);
line(95+i,330,120+i,305);
line(120+i,305,160+i,305);

/**Wheels**/
circle(110+i,370,17);
circle(240+i,370,17);

delay(10);
cleardevice();

line(0,390,639,390);  //ROAD
}
getch();
}


C++ Animation #2 Deal With It!! (Source Code)

//DEAL WITH IT ANIMATION C++

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

void main()
{
clrscr();
int gd=DETECT,gm,font=1;
initgraph(&gd,&gm,"C:\\tc\\bgi");


for(int i=0;i<200;i++)
{
settextstyle(0,HORIZ_DIR,3);
outtextxy(250+i-200,225,"C++");
outtextxy(430+i-200,225,"C++");
delay(10);
cleardevice();
}
line(80,210,100,200);
line(81,211,100,201);
line(82,212,100,202);

line(100,200,540,200);
line(100,201,540,201);
line(100,202,540,202);

line(180,200,240,260);
line(240,260,320,260);
line(320,260,360,220);
line(360,220,380,220);

line(380,220,420,260);
line(420,260,500,260);
line(500,260,540,200);

settextstyle(0,HORIZ_DIR,4);
outtextxy(150,300,"DEAL WITH IT!!");

settextstyle(0,HORIZ_DIR,1);
outtextxy(450,420,"-TPA Production()");

settextstyle(0,HORIZ_DIR,3);
outtextxy(250+i-200,225,"C++");
outtextxy(430+i-200,225,"C++");

getch();
}



Wednesday, 28 October 2015

Flood Fill Algorithm in C++(Source Code)

/*Flood Fill Algorithm*/

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

void floodFill(int x, int y, int old, int fill)
{
int current;
current=getpixel(x,y);
if(current==old)
{
putpixel(x,y,fill);
delay(1);
floodFill(x+1,y,old,fill);
floodFill(x-1,y,old,fill);
floodFill(x, y+1,old,fill);
floodFill(x,y-1,old,fill);
}
}

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

rectangle(100,100,150,150);    //RED
rectangle(200,200,250,250);    //MAGENTA
rectangle(300,100,350,150);    //BROWN
rectangle(400,200,450,250);    //LIGHTGRAY

floodFill(125,125,o,4);        //RED
floodFill(225,225,o,5);        //MAGENTA
floodFill(325,125,o,6);        //BROWN
floodFill(425,225,o,7);        //LIGHTGRAY

getch();
closegraph();
}

For OUTPUT check out the video! :