Car movement during Traffic Signal

On October 7, 2008, in Animation, Drawing, by , Views : 16233


The signal is Green initially.
Press ‘r’ to turn it to red, ‘y’ for yellow and ‘g’ for green.
Press ‘Enter’ to exit the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<conio.h>
#include<dos.h>
#include<graphics.h>
 
void car(int x, int c)
{
    setcolor(c);
    line(x + 150, 100, x + 242, 100);
    ellipse(x + 242, 105, 0, 90, 10, 5);
    line(x + 150, 100, x + 120, 150);
    line(x + 252, 105, x + 280, 150);
    line(x + 100, 150, x + 320, 150);
    line(x + 100, 150, x + 100, 200);
    line(x + 320, 150, x + 320, 200);
    line(x + 100, 200, x + 110, 200);
    line(x + 320, 200, x + 310, 200);
    arc(x + 130, 200, 0, 180, 20);
    arc(x + 290, 200, 0, 180, 20);
    line(x + 270, 200, x + 150, 200);
    circle(x + 130, 200, 17);
    circle(x + 290, 200, 17);
 
}
 
 
void main()
{
    int gd = DETECT, gm = DETECT, i = -200;
    int ch = 'g';
    initgraph(&gd, &gm, "");
 
    line(0, 70, 300, 70);
    line(450, 70, 630, 70);
    line(300, 70, 300, 0);
    line(450, 70, 450, 0);
    line(0, 230, 300, 230);
    line(450, 230, 630, 230);
    line(300, 230, 300, 500);
    line(450, 230, 450, 500);
    circle(280, 50, 10);
    setfillstyle(SOLID_FILL, GREEN);
    floodfill(280, 50, 15);
    car(i, 15);
    while (1) {
	while (!kbhit()) {
	    //Press 'r' to turn it to red
	    if (ch == 'r') {
		setfillstyle(SOLID_FILL, RED);
		floodfill(280, 50, 15);
		do {
		    while (!kbhit()) {
			if (i < -20 || i > -10) {
			    car(i++, 0);
			    car(i, 15);
			    delay(50);
			}
		    }
		    ch = getch();
		    //Press 'y' to turn it to yellow
		    if (ch == 'y') {
			setfillstyle(SOLID_FILL, YELLOW);
			floodfill(280, 50, 15);
		    }
		    //Press 'g' to turn it to red
		    if (ch == 'g') {
			setfillstyle(SOLID_FILL, GREEN);
			floodfill(280, 50, 15);
		    }
		} while (ch != 'g' && ch != 13);
	    } else {
		car(i++, 0);
		car(i, 15);
		delay(50);
	    }
	}
	ch = getch();
	if (ch == 13)
	    break;
    }
 
 
    getch();
}

Hope, it helps! 🙂



10 Responses to Car movement during Traffic Signal

  1. Springfairy says:

    Wow! Dipin! You blog has helped me a lot!Keep up the good work! Could you please upload a simple C++ program demonstrating 2d transformations: scaling, translation and rotation.? Thanks!

  2. poornima says:

    we need explanation for programs

  3. nazia says:

    it's a nice program please do more programs

  4. Sona says:

    please uplod a c program to show the rocket populsion and also upload a c program to show ripples in a pond

  5. benku says:

    plz upload a c pgm to show a car moving over a hump…..

  6. jibin says:

    Sir,
    Could you please upload a program to animate the scene of a car turning a curve along a road ,while approaching a four road connecting point junction the traffic signal at the junction show green light while car is approaching the junction.When the car reaches the junction light should turn red and car should stop.

  7. nitin says:

    hello sir,
    after compiling no error but no execution occuriing wats the error!!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
line
Bresenham’s Line Drawing Algorithm

chess
Chess Board using DDA line drawing algorithm

line
DDA Line Drawing Algorithm in C

MinMaxClose