Design traffic signal using graphics.h functions in C

In this article, we are going to learn about the setcolor(), settextstyle(), rectangle() and circle() function of graphics.h header file in C programming language, and use them design a traffic signal design.
Submitted by Manu Jemini, on March 24, 2018

To create a rectangle and circle in our graph all we need to do is use rectangle() and circle () functions.

For a rectangle, we will call the function called rectangle with four numbers as the four coordinates of the rectangle. With these parameters, we will have a rectangle on our screen.

For circle, we will call the function called circle() with three numbers as the co-ordinates of the center and radius. So this function will create a circle with a center with the given radius.

setcolor() function is used to set the color of the shape which we will draw after that statement.

Now, the first thing is we need to create a rectangle which will include all three circles with red, yellow and green. Then, make the circles in a vertical direction.

graphics.h - Design traffic signal in C

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

int main() 
{
	//initilizing graphic driver and 
	//graphic mode variable
	int graphicdriver=DETECT,graphicmode;

	//calling initgraph with parameters
	initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

	//Printing message for user
	outtextxy(50, 50 + 50, "Program to create traffic signal in C graphics");

	//initilizing variables
	int middlex, middley;

	//getting middle x and y
	middlex = getmaxx()/2;
	middley = getmaxy()/2;

	//setting color as white for the outline
	setcolor(WHITE);
	settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
	rectangle(middlex-30,middley-80,middlex+30,middley+80);
	circle(middlex, middley-50, 22);

	//filling red color to signify stop sign
	setfillstyle(SOLID_FILL,RED);
	floodfill(middlex, middley-50,WHITE);
	setcolor(BLUE);
	outtextxy(middlex-15,middley-50,"STOP");

	//setting color as white for the outline
	setcolor(WHITE);
	rectangle(middlex-30,middley-80,middlex+30,middley+80);
	circle(middlex, middley, 20);

	//filling yellow color to signify ready sign
	setfillstyle(SOLID_FILL,YELLOW);
	floodfill(middlex, middley,WHITE);
	setcolor(BLUE);
	outtextxy(middlex-18,middley-3,"READY");

	//setting white color for outline
	setcolor(WHITE);
	rectangle(middlex-30,middley-80,middlex+30,middley+80);
	circle(middlex, middley+50, 22);

	//filling green color to signify go sign
	setfillstyle(SOLID_FILL,GREEN);
	floodfill(middlex, middley+50,WHITE);
	setcolor(BLUE);
	outtextxy(middlex-7,middley+48,"GO");
	setcolor(RED);
	settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);


	getch();
	return 0;
}

Output

graphics.h - Design traffic signal using graphics.h functions in C




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.