0% found this document useful (0 votes)
22 views41 pages

Practical FILE For GRAPHICS

The document provides various C graphics programming examples using the graphics.h library, including drawing lines, circles, triangles, rectangles, squares, and polygons. It also includes examples of creating colored shapes and animations, such as a moving car and a walking figure. Each program demonstrates the initialization of the graphics system, drawing shapes, and closing the graphics mode.

Uploaded by

a7k004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views41 pages

Practical FILE For GRAPHICS

The document provides various C graphics programming examples using the graphics.h library, including drawing lines, circles, triangles, rectangles, squares, and polygons. It also includes examples of creating colored shapes and animations, such as a moving car and a walking figure. Each program demonstrates the initialization of the graphics system, drawing shapes, and closing the graphics mode.

Uploaded by

a7k004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Graphics program to Draw a line using line()

and lineto()
Let’s consider the following example:

/*C graphics program to draw a line.*/

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

main()
{
int gd = DETECT, gm;

//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init
graphics:
initgraph(&gd, &gm, "C:/TC/BGI");
*/

//draw a line

/*
line() function description
parameter left to right
x1: 100
y1: 100
x2: 200
y2: 100
*/
line(100,100,200,100); //will draw a horizontal
line

line(10,10,200,10); //will draw another horizonatl


line

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

In this example we will learn how to draw circles on output


screen in c programming language by using c graphics library
functions.

In this program we are using following library functions


of graphics.h header file.

1. circle()
2. getmaxx()
3. getmaxy()

circle()- This library function is declared in graphics.h and


used to draw a circle; it takes centre point coordinates and
radius.

circle(int x,int y, int radius);

getmaxx()- This function returns maximum value of x


coordinate.
int getmaxx();

getmaxy()- This function returns maximum value of y


coordinate.

int getmaxy();

C Code Snippet - Graphics program to draw


Circles
Let’s consider the following example:

/*C graphics program to draw circles.*/

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

main()
{
int gd = DETECT, gm;

//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init
graphics:
initgraph(&gd, &gm, "C:/TC/BGI");
*/

//first circle from point(100,100)


circle(100,100,50);

//second circle from point(200,200)


circle(200,200,50);

//third circle from point(center,center)


circle(getmaxx()/2,getmaxy()/2,50);
getch();
closegraph();
return 0;
}
Output

Explanation of functions
circle(100,100,50) – will draw a circle of 50 from point(100,100).

circle(200,200,50) – will draw a circle of 50 from point(200,200).

circle(getmaxx()/2,getmaxy()/2,50) – will draw a circle of 50 from


centre of the screen.
Program for Triangle:

#include <graphics.h>

#include <conio.h>

// Driver code

int main()

// gm is Graphics mode which

// is a computer display

// mode that generates

// image using pixels.

// DETECT is a macro

// defined in "graphics.h"

// header file

int gd = DETECT, gm;

// initgraph initializes

// the graphics system

// by loading a graphics

// driver from disk

initgraph(&gd, &gm, "");


// Triangle

// line for x1, y1, x2, y2

line(150, 150, 450, 150);

// line for x1, y1, x2, y2

line(150, 150, 300, 300);

// line for x1, y1, x2, y2

line(450, 150, 300, 300);

// closegraph function closes

// the graphics mode and

// deallocates all memory

// allocated by graphics system

getch();

// Close the initialized gdriver

closegraph();

}
}

Output:
Program for rectangle:
// C program to draw a rectangle

#include <graphics.h>

// Driver code

int main()

// gm is Graphics mode which is a computer display

// mode that generates image using pixels.

// DETECT is a macro defined in "graphics.h" header file

int gd = DETECT, gm;

// location of left, top, right, bottom

int left = 150, top = 150;

int right = 450, bottom = 450;

// initgraph initialises the graphics system

// by loading a graphics driver from the disk

initgraph(&gd, &gm, "");

// rectangle function
rectangle(left, top, right, bottom);

getch();

// closegraph function closes the graphics

// mode and deallocates all memory allocated

// by the graphics system

closegraph();

return 0;

Output:
Program for Square:
#include <graphics.h>

#include <conio.h> // For getch()

int main() {

int gd = DETECT, gm;

// Initialize graphics mode

initgraph(&gd, &gm, NULL);

// Coordinates for the top-left corner and size of the square

int x1 = 200, y1 = 200; // Top-left corner

int side = 150; // Length of the side of the square

// Draw a square using the rectangle function (same as a rectangle)

rectangle(x1, y1, x1 + side, y1 + side);

// Wait for user input before closing the window

getch();

// Close the graphics mode


closegraph();

return 0;

Output:

Graphics Example Using


Colours of C Language
Graphics
A Coloured Circle and a Rectangle:
#include <graphics.h>

#include <conio.h>
int main() {

int gd = DETECT, gm;

// Initialize graphics mode

initgraph(&gd, &gm, NULL);

// Set color and draw a circle

setcolor(RED);

circle(200, 200, 100);

// Set color and draw a rectangle

setcolor(BLUE);

rectangle(150, 150, 250, 250);

// Wait for user input

getch();

// Close graphics mode

closegraph();
return 0;

Output:
1. Include the graphics.h library, which provides functions for graphics
programming.
2. In the main() function, declare variables gd and gm to store the graphics
driver and graphics mode.
3. Call the initgraph() function to initialize the graphics system.
4. Set the coordinates of the square by defining variables x and y for the top-left
corner and size for the side length.
5. Use the rectangle() function to draw the square.
The rectangle() function takes four arguments: the x and y coordinates of
the top-left corner and the x and y coordinates of the bottom-right corner.
6. Call getch() to wait for a key press before closing the graphics window.
7. Call closegraph() to close the graphics system.
8. Finally, return 0 to indicate successful execution of the program.
Make sure to link the graphics.h library in Turbo C++ before running the
program.
DRAW PENTAGON

/ C Implementation for drawpoly()

#include <graphics.h>

// driver code

int main()

// gm is Graphics mode which is

// a computer display mode that

// generates image using pixels.

// DETECT is a macro defined in

// "graphics.h" header file

int gd = DETECT, gm;

// coordinates of polygon

int arr[] = {320, 150, 400, 250,

250, 350, 320, 150};


// initgraph initializes the

// graphics system by loading a

// graphics driver from disk

initgraph(&gd, &gm, "");

// drawpoly function

drawpoly(4, arr);

getch();

// closegraph function closes the

// graphics mode and deallocates

// all memory allocated by

// graphics system .

closegraph();

return 0;

Output :
C program to draw a hut and color it using graphics
#include<graphics.h>

int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);

line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);

/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
closegraph();
return 0;
}

Output
Draw a moving car using computer
graphics programming in C
Last Updated : 08 Dec, 2022


In computer graphics, use graphics.h which provide direct


functions to draw different coordinate shapes (like circle,
rectangle etc). By using these functions we can draw
different objects like car, hut, trees, etc. In this program, we
will draw a moving car using line and circles.
Functions used in program:
 delay(n): This function is used for holding the program
output for a small period of time since processing is very
fast so use it to see the result.
 setcolor(n): A function from graphics.h header file which
set the color of the pointer (cursor). There are some
predefined colors in computer graphics. Here n is color
number.
 line(x1, y1, x2, y2): A function from graphics.h header
file which draw a line with (x1, y1) as first coordinate of
line and (x2, y2) as second coordinate of the line.
 circle(x, y, r): A function from graphics.h header file
which draw a circle with center (x, y) and radius r.
Example 1: This example create a moving car

C++

// C program to draw a moving car. This


// program run in gcc compiler having

// graphics.h library installed

#include <graphics.h>

#include <stdio.h>

// Function to draw moving car

void draw_moving_car(void) {

int i, j = 0, gd = DETECT, gm;

// Passed three arguments to initgraph

// function to initialize graphics mode

initgraph(&gd, &gm, "");

for (i = 0; i <= 420; i = i + 10) {

// Set color of car as red


setcolor(RED);

// These lines for bonnet and

// body of car

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

// For left wheel of car

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);

// For right wheel of car

circle(145 + i, 330, 15);


circle(145 + i, 330, 2);

// Line left of left wheel

line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel

line(80 + i, 330, 130 + i, 330);

// Line right of right wheel

line(210 + i, 330, 160 + i, 330);

delay(100);

// To erase previous drawn car, draw

// the whole car at same position

// but color using black

setcolor(BLACK);
// Lines for bonnet and body of car

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

// For left wheel of car

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);

// For right wheel of car

circle(145 + i, 330, 15);

circle(145 + i, 330, 2);


// Line left of left wheel

line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel

line(80 + i, 330, 130 + i, 330);

// Line right of right wheel

line(210 + i, 330, 160 + i, 330);

getch();

closegraph();

// Driver code

int main()
{

draw_moving_car();

return 0;

Output:
DRAW CAR - GRAPHICS PROGRAMMING IN GCC - C++
PROGRAM
#include<graphics.h>
int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm,NULL);

line(0,300,640,300);

setcolor(4);

circle(100,285,15);
circle(200,285,15);
circle(100,285,5);
circle(200,285,5);

line(65,285,85,285);
line(115,285,185,285);
line(215,285,235,285);
line(65,285,65,260);
line(235,285,235,260);

line(65,260,100,255);
line(235,260,200,255);

line(100,255,115,235);
line(200,255,185,235);
line(115,235,185,235);

line(106,255,118,238);
line(118,238,118,255);
line(106,255,118,255);

line(194,255,182,238);
line(182,238,182,255);
line(194,255,182,255);

line(121,238,121,255);
line(121,238,148,238);
line(121,255,148,255);
line(148,255,148,238);

line(179,238,179,255);
line(179,238,152,238);
line(179,255,152,255);
line(152,255,152,238);
setcolor(4);
//floodfill(150,200,4);

getch();
closegraph();
}
ource code for creating a man walking or moving using C or C++
graphics programing language.

#include <graphics.h>

#include <conio.h>

#include <dos.h>

int main(void)

int x, y, i = 0, j = 0;

int graphic_driver = DETECT, graphic_mode;

initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");

x = getmaxx() / 2;

y = getmaxy() / 2;

setbkcolor(4);

setcolor(15);

for (i = 0; i <= 2; i++)


{

for (j = 0; j <= 70; j++)

circle(x, y, 20);

circle(x + 10, y - 10, 5);

arc(x + 20, y, 90, 270, 5);

arc(x + 10, y + 15, 30, 120, 5);

arc(x + 10, y + 10, 210, 330, 5);

arc(x + 20, y + 10, 117, 180, 35);

arc(x - 15, y - 5, 280, 45l, 10);

line(x - 2, y + 20, x - 2, y + 30);

line(x + 2, y + 20, x + 2, y + 30);

line(x - 15, y + 36, x + 21, y + 25);

line(x - 15, y + 36, x - 15, y + 96);

line(x + 21, y + 25, x + 21, y + 85);

line(x - 15, y + 96, x + 21, y + 85);

if (j < 41)

line(x - 15, y + 36, x - 30 + j, y + 85 - j / 8);


delay(25);

else

line(x - 15, y + 36, x + 40 - j, y + 75 + j / 8);

delay(25);

if (j < 41)

line(x + 21, y + 85, x + 5 + j, y + 135 - j / 8);

delay(25);

else

line(x + 21, y + 85, x + 75 - j, y + 125 + j / 8);

delay(25);

if (j < 41)

{
line(x + 21, y + 25, x + 36 - j, y + 70 + j / 8);

delay(25);

else

line(x + 21, y + 25, x - 34 + j, y + 80 - j / 8);

delay(10);

if (j < 41)

line(x - 15, y + 96, x - j, y + 145 + j / 8);

delay(25);

else

line(x - 15, y + 96, x - 70 + j, y + 155 - j / 8);

delay(10);

cleardevice();
x++;

getch();

closegraph();

return 0;

Output :
C++ Program to print a man using
graphics
Graphics

#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver, &gmode, “c:\turboc3\bgi”);

//for head
ellipse(320,95,360,0,25,20);
line(298,85,341,85);
circle(310,90,2);
circle(330,90,2);
arc(320,100,200,-20,10);

//for neck
line(313,115,313,125);
line(328,115,328,125);

//For centre part


arc(320,225,72,107,100);
line(290,129,290,200);
line(350,129,350,200);
line(290,193,350,193);
line(290,200,350,200);

//for legs
line(290,200,285,280);
line(320,225,305,280);
line(322,225,335,280);
line(350,200,355,280);

//for right hand


line(290,129,255,165);
line(255,165,290,200);
line(290,149,275,165);
line(275,165,290,182);

//for left hand


line(350,129,385,165);
line(385,165,350,200);
line(350,149,365,165);
line(365,165,350,182);

//for shoes
line(285,280,275,287);
line(275,287,305,287);
line(305,280,305,287);

line(335,280,335,287);
line(335,287,365,287);
line(355,280,365,287);
//for name
settextstyle(2,HORIZ_DIR,4);
outtextxy(293,150,”The Crazy”);
outtextxy(292,160,”Programmer”);

getch();
closegraph();
}
C program to create Indian
National Flag using Graphics


In this article, we will discuss how to draw the Indian National


Flag using Graphics.
Approach:
 Draw a rectangle using the function rectangle() .
 Equally, divide the above rectangle into three parts by creating
the line using the function line().
 Draw double lines on each other i.e., draw 4 lines and among
them, 2 lines will act as a divider between Red (Light red as there
is no direct saffron color present in the graphics library) & White,
and the other 2 lines will divide White and Green color.
 The Ashoka Chakra will be made using the function circle().
 To finish, all the spaces will be filled using the
functions setfillstyle() and floodfill().
Below is the implementation of the above approach:

 C

// C program for the above approach

#include <conio.h>

#include <graphics.h>

#include <stdio.h>
// Driver Code

void main()

// Initialize of gdriver with

// DETECT macros

initgraph(&gd, &gm, "C:\\turboc3\\bgi");

// Creating the base rectangle

line(250, 100, 250, 600);

line(250, 100, 250, 600);

// Fill the White Color

setfillstyle(SOLID_FILL, WHITE);

// Create and fill the top strip

rectangle(225, 600, 275, 610);


rectangle(200, 610, 300, 620);

floodfill(227, 608, 15);

floodfill(202, 618, 15);

// Fill the Light Red Color

setfillstyle(SOLID_FILL, LIGHTRED);

// Create and fill the ashoka

// chakra with Blue

rectangle(250, 100, 650, 280);

line(250, 160, 650, 160);

floodfill(252, 158, 15);

// Fill the Blue Color

setfillstyle(SOLID_FILL, BLUE);
// Create and fill the left

// part of the middle strip

// Create a Circle

circle(450, 190, 30);

floodfill(452, 188, 15);

// Fill the White Color

setfillstyle(SOLID_FILL, WHITE);

// Create and fill the right

// part of the middle strip

line(250, 160, 480, 160);

line(250, 220, 480, 220);

floodfill(252, 162, 15);

// Fill the White Color


setfillstyle(SOLID_FILL, WHITE);

// Create and fill the bottom

// strip

line(480, 160, 650, 160);

line(480, 220, 650, 220);

floodfill(482, 162, 15);

// Fill the Green Color

setfillstyle(SOLID_FILL, GREEN);

line(250, 220, 650, 220);

floodfill(252, 278, 15);

// Close the initialized gdriver

closegraph();

}
Output:

You might also like