0% found this document useful (0 votes)
5 views

Graphics

The document provides a guide on using color codes and drawing shapes using the graphics.h library in C++. It includes examples of functions to draw various smiley faces, a house, a computer, and other objects, along with their respective graphical representations. The document also outlines the standard color codes available in the library.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Graphics

The document provides a guide on using color codes and drawing shapes using the graphics.h library in C++. It includes examples of functions to draw various smiley faces, a house, a computer, and other objects, along with their respective graphical representations. The document also outlines the standard color codes available in the library.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Graphics

rakibhasancse20
March 2025

Color Codes in graphics.h

gray!20 Color Code Color Name Example


0 BLACK setcolor(BLACK);
1 BLUE setcolor(BLUE);
2 GREEN setcolor(GREEN);
3 CYAN setcolor(CYAN);
4 RED setcolor(RED);
5 MAGENTA setcolor(MAGENTA);
6 BROWN setcolor(BROWN);
7 LIGHTGRAY setcolor(LIGHTGRAY);
8 DARKGRAY setcolor(DARKGRAY);
9 LIGHTBLUE setcolor(LIGHTBLUE);
10 LIGHTGREEN setcolor(LIGHTGREEN);
11 LIGHTCYAN setcolor(LIGHTCYAN);
12 LIGHTRED setcolor(LIGHTRED);
13 LIGHTMAGENTA setcolor(LIGHTMAGENTA);
14 YELLOW setcolor(YELLOW);
15 WHITE setcolor(WHITE);

Table 1: Standard Colors in graphics.h

1
1 Shape

Figure 1: 19i mageo ne.cpp

# include < graphics .h >


# include < conio .h >

void d r a w _ w i n k _ s m i l e y ( int x , int y ) {


circle (x , y , 50) ; // Face
// line ( x - 25 , y - 10 , x - 10 , y - 10) ; // Wink eye
arc (x -20 , y -10 , 50 , 140 , 10) ;
circle ( x + 20 , y - 10 , 5) ; // Right eye
floodfill ( x +20 , y -10 , WHITE ) ;
arc (x , y + 10 , 200 , 340 , 25) ; // Smile
}

void d r a w _ g l a s s e s _ s m i l e y ( int x , int y ) {


circle (x , y , 50) ; // Face
circle ( x - 35 , y - 15 , 20) ; // Left glass frame
circle ( x + 35 , y - 15 , 20) ; // Right glass frame
// line ( x - 10 , y - 10 , x + 10 , y - 10) ; // Glasses bridge
line ( x - 35 , y - 10 , x - 25 , y - 10) ; // Left eye line
line ( x + 25 , y - 10 , x + 35 , y - 10) ; // Right eye line
arc (x , y + 10 , 200 , 340 , 25) ; // Smile
arc (x , y , 50 ,130 , 25) ; // Smile
}

void draw_smiley ( int x , int y ) {


circle (x , y , 50) ; // Face
circle ( x - 20 , y - 10 , 5) ; // Left eye
circle ( x + 20 , y - 10 , 5) ; // Right eye

floodfill ( x - 20 , y - 10 , WHITE ) ; // Left eye


floodfill ( x + 20 , y - 10 , WHITE ) ;

arc (x , y + 10 , 200 , 340 , 25) ; // Smile


line (x -23 , y +17 , x +23 , y +17) ;
}

// void d r a w _ a n g r y _ s m i l e y ( int x , int y ) {

2
// circle (x , y , 50) ; // Face
// line ( x - 20 , y - 25 , x - 8 , y - 10) ; // Left eyebrow
// line ( x + 22 , y - 25 , x + 10 , y - 10) ; // Right eyebrow
// circle ( x - 20 , y - 10 , 10) ; // Left eye ( big circle )
// circle ( x + 25 , y - 10 , 10) ; // Right eye ( big circle )
// circle ( x - 15 , y - 10 , 3) ; // Left eye dot
// circle ( x + 20 , y - 10 , 3) ; // Right eye dot
// arc (x , y + 30 , 20 , 160 , 20) ; // Frown
// arc (x , y +20 , 250 , 290 , 15) ; // Frown
// }

void d r a w _ a n g r y _ s m i l e y ( int x , int y ) {


setcolor ( WHITE ) ;
circle (x , y , 50) ; // Face
line ( x - 20 , y - 25 , x - 8 , y - 10) ; // Left eyebrow
line ( x + 22 , y - 25 , x + 10 , y - 10) ; // Right eyebrow
circle ( x - 20 , y - 10 , 10) ; // Left eye ( big circle )
circle ( x + 25 , y - 10 , 10) ; // Right eye ( big circle )

circle ( x - 15 , y - 10 , 3) ; // Left eye dot


circle ( x + 20 , y - 10 , 3) ; // Right eye dot
// setfillstyle ( SOLID_FILL , WHITE ) ;
floodfill ( x - 15 , y - 10 , WHITE ) ;
floodfill ( x + 20 , y - 10 , WHITE ) ;

arc (x , y + 30 , 20 , 160 , 20) ; // Frown


arc (x , y + 20 , 250 , 290 , 15) ; // Frown
}

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

d r a w _ w i n k _ s m i le y (100 , 100) ;
d r a w _ g l a s s e s _ s m i l e y (250 , 100) ;
draw_smiley (400 , 100) ;
d r a w _ a n g r y _ s m i l e y (550 , 100) ;

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

3
Figure 2: 19i maget wo.cpp

# include < graphics .h >


# include < conio .h >
void draw_house ( int x , int y ) {
// Base of the house
rectangle (x , y , x + 100 , y + 100) ;
setcolor ( BLACK ) ;
line (x ,y , x +100 , y ) ;
setcolor ( WHITE ) ;
// Roof
line (x , y , x + 50 , y - 50) ;
line ( x + 50 , y - 50 , x + 100 , y ) ;

line (x -2 ,y -5 , x +50 , y -55) ;


line ( x +50 , y -55 , x +100 , y -5) ;
// Door
rectangle ( x + 40 , y + 60 , x + 60 , y + 100) ;
circle ( x + 45 , y + 80 , 2) ; // Door knob
// windows middle
rectangle ( x + 40 , y + 10 , x + 60 , y + 30) ;
line ( x + 40 , y + 20 , x + 60 , y + 20) ; // Horizontal line
line ( x + 50 , y + 10 , x +50 , y + 30) ;
// Windows ( Left )
rectangle ( x + 10 , y + 55 , x + 30 , y + 75) ;
line ( x + 10 , y + 65 , x + 30 , y + 65) ; // Horizontal line
line ( x + 20 , y + 55 , x + 20 , y + 75) ; // Vertical line
// Windows ( Right )
rectangle ( x + 70 , y + 55 , x + 90 , y + 75) ;
line ( x + 70 , y + 65 , x + 90 , y + 65) ; // Horizontal line
line ( x + 80 , y + 55 , x + 80 , y + 75) ; // Vertical line
// Chimney
rectangle ( x + 70 , y - 50 , x + 85 , y - 20) ;
setcolor ( BLACK ) ;
line ( x +70 , y -35 , x +70 , y -20) ;
line ( x +70 , y -20 , x +85 , y -20) ;
setcolor ( WHITE ) ;
}

void draw_computer ( int x , int y ) {


// Monitor outer frame
rectangle (x , y , x + 120 , y + 80) ;
// Monitor inner screen

4
rectangle ( x + 15 , y + 10 , x + 105 , y + 65) ;
// Stand
rectangle ( x + 50 , y + 80 , x + 70 , y + 90) ;
rectangle ( x + 20 , y + 90 , x + 100 , y + 100) ;
// Power button
circle ( x + 60 , y + 73 , 5) ;
floodfill ( x +60 , y +73 , WHITE ) ;
}

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

// Drawing the figures


draw_house (100 , 200) ;
draw_computer (300 , 200) ;

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

Figure 3: 18i mageo ne.cpp

# include < graphics .h >


# include < conio .h >

void draw_smiley ( int x , int y ) {


circle (x , y , 50) ; // Face outline
circle ( x - 20 , y - 15 , 5) ; // Left eye
circle ( x + 20 , y - 15 , 5) ; // Right eye
arc (x , y + 10 , 180 , 360 , 25) ; //
line (x -23 , y +10 , x +23 , y +10) ;
}

void draw_wifi ( int x , int y ) {


setlinestyle ( SOLID_LINE , 0 , 3) ; // Thickness set to 3

// fillellipse (x , y + 50 , 6 , 6) ; // Bottom dot


circle (x , y +50 ,6) ;

5
floodfill (x , y +50 , WHITE ) ;

arc (x , y + 30 , 40 , 140 , 18) ; // First arc ( thicker )


arc (x , y + 15 , 30 , 150 , 32) ; // Second arc ( thicker )
arc (x , y , 20 , 160 , 46) ; // Third arc ( thicker )

// setlinestyle ( SOLID_LINE , 0 , 1) ; // Reset thickness back to


normal
}

void draw_computer ( int x , int y ) {


// Outer Frame ( Border )
rectangle (x , y , x + 120 , y + 80) ;

// Inner Frame ( Screen )


rectangle ( x + 10 , y + 10 , x + 110 , y + 70) ;

// Fill between Outer & Inner rectangle with white


setfillstyle ( SOLID_FILL , WHITE ) ;
bar (x , y , x + 120 , y + 80) ;

// Fill inner screen with black


setfillstyle ( SOLID_FILL , BLACK ) ;
bar ( x + 10 , y + 10 , x + 110 , y + 70) ;

// Stand ( Rounded Base )


ellipse ( x + 60 , y + 95 , 0 , 360 , 25 , 5) ; // Round Base
// floodfill ( x + 60 , y + 95 , WHITE ) ;
setfillstyle ( SOLID_FILL , WHITE ) ;
fillellipse ( x + 60 , y + 95 , 25 , 5) ; // Round Base
rectangle ( x + 50 , y + 80 , x + 70 , y + 90) ; // Stand Neck
}

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

setbkcolor ( BLACK ) ;
cleardevice () ;

draw_smiley (100 , 200) ;


draw_wifi (250 , 190) ;
draw_computer (400 , 200) ;

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

6
Figure 4: 18i maget wo.cpp

# include < graphics .h >


# include < conio .h >

// Function to draw a radio ( boombox )


void drawRadio ( int x , int y ) {
// Main body ( Black )
setcolor ( WHITE ) ;
rectangle (x , y , x + 120 , y + 60) ;
rectangle ( x + 45 , y - 10 , x + 75 , y ) ; // Handle

// Speakers
circle ( x + 25 , y + 30 , 15) ;
circle ( x + 95 , y + 30 , 15) ;
circle ( x + 25 , y + 30 , 7) ; // Inner speaker
circle ( x + 95 , y + 30 , 7) ;

// Buttons
rectangle ( x + 50 , y + 10 , x + 70 , y + 20) ;
circle ( x + 60 , y + 40 , 5) ;

// Fill the radio body black


setfillstyle ( SOLID_FILL , BLACK ) ;
floodfill ( x + 2 , y + 2 , WHITE ) ;
}

// Function to draw an ID card ( White card on black background )


void drawIDCard ( int x , int y ) {
setlinestyle ( SOLID_LINE , 0 , 3) ;
// Outer rectangle ( White ID Card )
setcolor ( WHITE ) ;
rectangle (x , y , x + 120 , y + 70) ;

// Fill the inside white


setfillstyle ( SOLID_FILL , WHITE ) ;
// floodfill ( x + 2 , y + 2 , WHITE ) ;

// Profile icon
setcolor (15) ; // Black elements inside
circle ( x + 30 , y + 25 , 12) ; // Head
arc ( x + 30 , y + 50 , 0 , 180 , 15) ; // Shoulders

7
line ( x +15 , y +50 , x +45 , y +50) ;

// Lines for text


line ( x + 55 , y + 20 , x + 105 , y + 20) ;
line ( x + 55 , y + 30 , x + 105 , y + 30) ;
line ( x + 55 , y + 40 , x + 105 , y + 40) ;
line ( x + 55 , y + 50 , x + 75 , y + 50) ;
}

int main () {
int gd = DETECT , gm ;
initgraph (& gd , & gm , NULL ) ;

// Set the background to black


setbkcolor ( BLACK ) ;
cleardevice () ;

// Draw the radio


drawRadio (100 , 200) ;

// Draw the ID card with a white body


drawIDCard (250 , 190) ;

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

2 Check if a rectangle is inside a circle

\\ reccir . cpp
# include < stdio .h >
# include < math .h >
# include < graphics .h >

// Function to calculate distance between two points


double distance ( double x1 , double y1 , double x2 , double y2 ) {
return sqrt ( pow ( x2 - x1 , 2) + pow ( y2 - y1 , 2) ) ;
}

// Function to check if the rectangle is inside the circle


int i s R e c t a n g l e I n s i d e C i r c l e ( int cx , int cy , int r ,
int x1 , int y1 , int x2 , int y2 ) {
// Rectangle corners
int corners [4][2] = {
{ x1 , y1 } , // Bottom - left
{ x1 , y2 } , // Top - left
{ x2 , y1 } , // Bottom - right
{ x2 , y2 } // Top - right
};

// Draw Circle

8
circle ( cx , cy , r ) ;

// Draw Rectangle
rectangle ( x1 , y1 , x2 , y2 ) ;

// Check if all corners are within the circle


for ( int i = 0; i < 4; i ++) {
double d = distance ( cx , cy , corners [ i ][0] , corners [ i ][1]) ;
if ( d > r ) {
return 0; // Outside the circle
}
}
return 1; // Fully inside the circle
}

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

int cx , cy , r ; // Circle center and radius


int x1 , y1 , x2 , y2 ; // Rectangle coordinates

// Input Circle
printf ( " Enter ␣ circle ␣ center ␣ ( cx , ␣ cy ) ␣ and ␣ radius ␣ ( r ) : ␣ " ) ;
scanf ( " % d ␣ % d ␣ % d " , & cx , & cy , & r ) ;

// Input Rectangle
printf ( " Enter ␣ bottom - left ␣ ( x1 , ␣ y1 ) ␣ and ␣ top - right ␣ ( x2 , ␣ y2 ) ␣ of ␣
rectangle : ␣ " ) ;
scanf ( " % d ␣ % d ␣ % d ␣ % d " , & x1 , & y1 , & x2 , & y2 ) ;

// Convert coordinates for graphics mode ( flip y - axis )


// int screen_height = getmaxy () ;
// cy = screen_height - cy ;
// y1 = screen_height - y1 ;
// y2 = screen_height - y2 ;

// Check condition
if ( i s R e c t a n g l e I n s i d e C i r c l e ( cx , cy , r , x1 , y1 , x2 , y2 ) ) {
printf ( " The ␣ rectangle ␣ is ␣ inside ␣ the ␣ circle .\ n " ) ;
} else {
printf ( " The ␣ rectangle ␣ is ␣ NOT ␣ inside ␣ the ␣ circle .\ n " ) ;
}

getch () ; // Wait for user input


closegraph () ; // Close graphics mode
return 0;
}

9
3 check if a line touch the circle

Figure 5: This is image 2

// linecircle . cpp

# include < stdio .h >


# include < math .h >
# include < graphics .h >

// Function to calculate perpendicular distance of circle center


from the line
double p e r p e n d i c u l a r D i s t a n c e ( double cx , double cy , double x1 ,
double y1 , double x2 , double y2 ) {
double numerator = fabs (( y2 - y1 ) * cx - ( x2 - x1 ) * cy + x2 *
y1 - y2 * x1 ) ;
double denominator = sqrt ( pow ( y2 - y1 , 2) + pow ( x2 - x1 , 2) ) ;
return numerator / denominator ;
}

// Function to check if line touches the circle


int d o e s L i n e T o u c h C i r c l e ( double cx , double cy , double r , double x1 ,
double y1 , double x2 , double y2 ) {
double d = p e r p e n d i c u l a r D i s t a n c e ( cx , cy , x1 , y1 , x2 , y2 ) ;
if ( d > r ) {
printf ( " The ␣ line ␣ is ␣ completely ␣ OUTSIDE ␣ the ␣ circle .\ n " ) ;
} else if ( d == r ) {
printf ( " The ␣ line ␣ is ␣ a ␣ TANGENT ␣ to ␣ the ␣ circle .\ n " ) ;
} else {
printf ( " The ␣ line ␣ INTERSECTS ␣ the ␣ circle .\ n " ) ;
}
}

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

double cx , cy , r ; // Circle center and radius


double x1 , y1 , x2 , y2 ; // Line endpoints

// Input Circle
printf ( " Enter ␣ circle ␣ center ␣ ( cx , ␣ cy ) ␣ and ␣ radius ␣ ( r ) : ␣ " ) ;
scanf ( " % lf ␣ % lf ␣ % lf " , & cx , & cy , & r ) ;

// Input Line
printf ( " Enter ␣ line ␣ endpoints ␣ ( x1 , ␣ y1 ) ␣ and ␣ ( x2 , ␣ y2 ) : ␣ " ) ;

10
scanf ( " % lf ␣ % lf ␣ % lf ␣ % lf " , & x1 , & y1 , & x2 , & y2 ) ;

// Draw Circle
circle ( cx , cy , r ) ;

// Draw Line
line ( x1 , y1 , x2 , y2 ) ;

// Check if the line touches the circle


d o e s L i n e T o u c h C i r c l e ( cx , cy , r , x1 , y1 , x2 , y2 ) ;

getch () ; // Wait for user input


closegraph () ; // Close graphics mode
return 0;
}

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

void house ()
{
setcolor ( WHITE ) ;
setlinestyle ( SOLID_LINE , 0 , 3) ; // Thicker lines to ensure
visibility

// Roof
line (280 , 150 , 400 , 50) ;
line (285 , 155 , 400 , 60) ;
line (400 , 60 , 500 , 159) ;
line (400 , 50 , 510 , 155) ;
line (280 , 150 , 285 , 155) ;
line (500 , 159 , 510 , 155) ;

// House Body
rectangle (294 , 151 , 494 , 280) ;
setcolor (0) ;

// delay (10) ; // Small delay for proper rendering


line (294 , 151 , 494 , 151) ;

setcolor (15) ;
// Door
rectangle (364 , 280 , 426 , 189) ;
circle (370 , 230 , 4) ;

// Windows
rectangle (364 , 120 , 426 , 151) ;
line (395 , 120 , 395 , 151) ;
line (364 , 136 , 426 , 136) ;

11
rectangle (310 , 250 , 350 , 200) ;
line (310 , 225 , 350 , 225) ;
line (330 , 200 , 330 , 250) ;

rectangle (436 , 250 , 476 , 200) ;


line (456 , 200 , 456 , 250) ;
line (436 , 225 , 476 , 225) ;

// Chimney
line (440 , 88 , 440 , 50) ;
line (450 , 99 , 450 , 50) ;
rectangle (438 , 47 , 452 , 52) ;
}

int main ()
{
int gd = DETECT , gm ;
initgraph (& gd , & gm , NULL ) ;

setbkcolor ( BLACK ) ;
cleardevice () ;

house () ;

// refresh () ; // Redraw everything

getch () ;
closegraph () ;
}

12
5 White house

Figure 6: whiteh ouse

# include < stdio .h >


# include < conio .h >
# include < graphics .h >
void draw_image ()
{
setlinestyle ( SOLID_LINE , 0 , 3) ;
line (100 ,200 ,250 ,100) ;
line (100 ,200 ,400 ,200) ;
line (250 ,100 ,400 ,200) ;
floodfill (200 ,150 , WHITE ) ;

bar (100 ,220 ,400 ,240) ;


bar (120 ,260 ,180 ,380) ;

13
bar (220 ,260 ,280 ,380) ;
bar (320 ,260 ,380 ,380) ;

bar (100 ,400 ,400 ,420) ;


bar (90 ,440 ,410 ,460) ;
}

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

setbkcolor ( BLACK ) ;
cleardevice () ;

draw_image () ;
printf ( " % d ␣ % d ␣ " , getmaxx () , getmaxy () ) ;

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

6 Write a program to draw a circle and when


press ‘i’ on the keyboard the radius of the
circle increases by 15 pixel and when press ‘d’
decreases by 15 pixe

\\19 _setA_b . cpp

# include < graphics .h >


# include < conio .h >

// Function to draw the circle


void drawCircle ( int x , int y , int radius ) {
cleardevice () ; // Clear previous frame
circle (x , y , radius ) ;
}

// Function to update the circle based on user input


void updateCircle ( int * radius , char input ) {
if ( input == ’i ’) {
* radius += 15; // Increase radius
} else if ( input == ’d ’ && * radius > 15) {
* radius -= 15; // Decrease radius ( avoid negative values )
}
}

14
int main () {
int gd = DETECT , gm ;
initgraph (& gd , & gm , " C :\\ Turboc3 \\ BGI " ) ;

int x = getmaxx () / 2; // Center X


int y = getmaxy () / 2; // Center Y
int radius = 50; // Initial radius
char ch ;

while (1) {
drawCircle (x , y , radius ) ; // Draw the circle

ch = getch () ; // Get user input

if ( ch == 27) // ESC key to exit


break ;

updateCircle (& radius , ch ) ; // Update radius


}

closegraph () ;
return 0;
}

7 Write a program to draw a circle on the center


of the screen. When press ‘l’ it moves on the
left of the screen, when press ‘r’ moves on the
right, when press ’u’ moves on the upward and
when press ‘d’ moves on the downward.

\\19 _setB_b . cpp


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

// Function to draw the circle at a given position


void drawCircle ( int x , int y , int radius ) {
cleardevice () ; // Clear previous drawings
setcolor ( WHITE ) ;
circle (x , y , radius ) ; // Draw new circle
}

// Function to handle movement


void moveCircle ( int x , int y , int radius ) {
char ch ;
drawCircle (x , y , radius ) ; // Initial circle display

while (1) {
ch = getch () ; // Get user input

15
if ( ch == ’l ’) x -= 10; // Move left
else if ( ch == ’r ’) x += 10; // Move right
else if ( ch == ’u ’) y -= 10; // Move up
else if ( ch == ’d ’) y += 10; // Move down
else if ( ch == ’q ’) break ; // Exit on ’q ’

drawCircle (x , y , radius ) ; // Update position


}
}

int main () {
int gd = DETECT , gm ;
initgraph (& gd , & gm , " C :\\ Turboc3 \\ BGI " ) ; // Initialize graphics
mode

int x = getmaxx () / 2; // Center X


int y = getmaxy () / 2; // Center Y
int radius = 30; // Circle Radius

moveCircle (x , y , radius ) ; // Start moving the circle

closegraph () ;
return 0;
}

8 Plane

Figure 7: Plane

# include < graphics .h >


# include < conio .h >

void draw_plane ()
{
setlinestyle ( SOLID_LINE , 0 , THICK_WIDTH ) ;

16
line (210 , 150 , 420 , 150) ;
line (210 , 250 , 480 , 250) ;
arc (480 , 240 , 280 , 360 , 10) ;
line (490 , 240 , 450 , 170) ;
line (450 , 170 , 420 , 150) ;
arc (210 , 200 , 205 , 270 , 50) ;
arc (214 , 178 , 190 , 220 , 65) ;
line (149 , 188 , 140 , 120) ;
line (140 , 120 , 210 , 150) ;

line (300 , 150 , 280 , 110) ;


line (360 , 150 , 310 , 110) ;

line (310 , 110 , 280 , 110) ;

line (300 , 240 , 240 , 290) ;


line (360 , 240 , 290 , 290) ;

line (290 , 290 , 240 , 290) ;

line (300 , 240 , 360 , 240) ;

setcolor ( BLACK ) ;
line (290 , 250 , 345 , 250) ;

setcolor ( WHITE ) ;

for ( int x = 220; x <= 410; x += 35)


{
rectangle ( x - 10 , 190 , x + 10 , 210) ;
}

rectangle (445 , 190 , 474 , 210) ;

setcolor (0) ;
line (460 , 190 , 474 , 190) ;
line (474 , 190 , 474 , 210) ;

setcolor ( WHITE ) ;
floodfill (458 , 192 , 0) ;
}

int main ()
{
int gd = DETECT , gm ;
initgraph (& gd , & gm , NULL ) ;

draw_plane () ;

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

17
9 Shower

Figure 8: Shower

# include < graphics .h >


# include < conio .h >

void rectangular ( int x , int y )


{
rectangle (x -3 , y +8 , x +3 , y -8) ;
}

void shower ()
{
arc (200 , 200 , 0 , 180 , 40) ;
line (160 , 200 , 240 , 200) ;

line (190 , 160 , 190 , 140) ;


line (210 , 160 , 210 , 140) ;
arc (180 , 140 , 0 , 80 , 10) ;
arc (180 , 140 , 0 , 90 , 30) ;
line (180 , 130 , 100 , 130) ;
line (180 , 110 , 100 , 110) ;

18
line (100 , 110 , 100 , 130) ;
floodfill (200 , 190 , WHITE ) ;
floodfill (200 , 150 , WHITE ) ;

rectangular (170 ,220) ;


rectangular (190 ,220) ;
rectangular (210 ,220) ;
rectangular (230 ,220) ;

floodfill (170 ,220 , 15) ;


floodfill (190 ,220 , 15) ;
floodfill (210 ,220 , 15) ;
floodfill (230 ,220 , 15) ;

rectangular (170 ,250) ;


rectangular (190 ,250) ;
rectangular (210 ,250) ;
rectangular (230 ,250) ;

floodfill (170 ,250 , 15) ;


floodfill (190 ,250 , 15) ;
floodfill (210 ,250 , 15) ;
floodfill (230 ,250 , 15) ;
}

int main ()
{
int gd = DETECT , gm ;
initgraph (& gd , & gm , " ␣ " ) ;

shower () ;

getch () ;
closegraph () ;
}

19
10 Shower with animation

Figure 9: Shower

# include < graphics .h >


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

void draw_shower ()
{
// Shower
arc (200 , 200 , 0 , 180 , 40) ;
line (160 , 200 , 240 , 200) ;

line (190 , 160 , 190 , 140) ;


line (210 , 160 , 210 , 140) ;

arc (180 , 140 , 0 , 80 , 10) ;


arc (180 , 140 , 0 , 90 , 30) ;

line (180 , 130 , 100 , 130) ;


line (180 , 110 , 100 , 110) ;

line (100 , 110 , 100 , 130) ;

// Pipe

20
floodfill (200 , 150 , WHITE ) ;
// head
floodfill (200 , 190 , WHITE ) ;

void draw_droplets ( int y )


{
for ( int x = 170; x <= 230; x += 20)
{
rectangle ( x - 3 , y , x + 3 , y + 15) ;
floodfill (x , y + 5 , WHITE ) ;
}
for ( int x = 170; x <= 230; x += 20)
{
rectangle ( x - 3 , y + 30 , x + 3 , y + 45) ;
floodfill (x , y + 35 , WHITE ) ;
}
}

int main ()
{
int gd = DETECT , gm ;
initgraph (& gd , & gm , " " ) ;

draw_shower () ;
outtextxy (100 , 400 , " Press ␣ any ␣ key ␣ to ␣ start ␣ water " ) ;

getch () ; // Wait for key press to start animation

int y = 220;
while (! kbhit () ) // Run animation until another key is pressed
{
cleardevice () ;
draw_shower () ;
draw_droplets ( y ) ;
delay (100) ;
y += 10;
if ( y > getmaxy () ) y = 220; // Reset position
}

closegraph () ;
return 0;
}

21
11 DIRECT USE OF LINE EQUATION
// *************************************
// * DIRECT USE OF LINE EQUATION *
// *************************************

# include < graphics .h >


# include < stdlib .h >
# include < stdio .h >
# include < conio .h >

int main ( )
{

int x1 , y1 , x2 , y2 ;
float m ,c ,x , y ;
/* request auto detection */
int gdriver = DETECT , gmode , errorcode ;

/* initialize graphics and local variables */


initgraph (& gdriver , & gmode , " c :\\ tc \\ bgi " ) ;

x1 =200;
x2 =350;
y1 =250;
y2 =450;

m =( y2 - y1 ) /( x2 - x1 ) ;
c = y1 - m * x1 ;
printf ( " Direct ␣ Use ␣ of ␣ the ␣ Line ␣ Equation ␣ " ) ;

if (m <=1)
{
x = x1 ;
while (x <= x2 )
{
y=m*x+c;
putpixel (x ,y ,3) ;
x = x +1;
}
}

if (m >1)
{
y = y1 ;
while (y <= y2 )
{
y = y +1;
x =( y - c ) / m ;
putpixel (x ,y ,3) ;
}
}
getch () ;
closegraph () ;
return 0;
}

22
12 Cycle

Figure 10: Cycle

# include < graphics .h >


# include < conio .h >

void cycle ()
{
setlinestyle ( SOLID_LINE , 0 , THICK_WIDTH ) ;

circle (100 , 200 , 50) ;


circle (300 , 200 , 50) ;
circle (200 , 200 , 15) ;
line (100 , 200 , 185 , 200) ;

line (100 , 100 , 150 , 100) ; // seat

line (125 , 100 , 189 , 189) ;

line (100 , 200 , 145 , 130) ;

line (248 , 100 , 300 , 200) ;

line (248 , 100 , 280 , 94) ;

line (210 , 185 , 260 , 125) ;

outtextxy (150 , 350 , " Meraj ␣ Cycle ␣ Store " ) ;


}

int main ()
{
int gd = DETECT , gm ;
initgraph (& gd , & gm , " ␣ " ) ;

23
cycle () ;

getch () ;
closegraph () ;
}

24

You might also like