Practical FILE For GRAPHICS
Practical FILE For GRAPHICS
and lineto()
Let’s consider the following example:
#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
getch();
closegraph();
return 0;
}
Output
1. circle()
2. getmaxx()
3. getmaxy()
int getmaxy();
#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");
*/
Explanation of functions
circle(100,100,50) – will draw a circle of 50 from point(100,100).
#include <graphics.h>
#include <conio.h>
// Driver code
int main()
// is a computer display
// DETECT is a macro
// defined in "graphics.h"
// header file
// initgraph initializes
// by loading a graphics
getch();
closegraph();
}
}
Output:
Program for rectangle:
// C program to draw a rectangle
#include <graphics.h>
// Driver code
int main()
// rectangle function
rectangle(left, top, right, bottom);
getch();
closegraph();
return 0;
Output:
Program for Square:
#include <graphics.h>
int main() {
getch();
return 0;
Output:
#include <conio.h>
int main() {
setcolor(RED);
setcolor(BLUE);
getch();
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
#include <graphics.h>
// driver code
int main()
// coordinates of polygon
// drawpoly function
drawpoly(4, arr);
getch();
// 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
C++
#include <graphics.h>
#include <stdio.h>
void draw_moving_car(void) {
// body of car
delay(100);
setcolor(BLACK);
// Lines for bonnet and body of car
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;
x = getmaxx() / 2;
y = getmaxy() / 2;
setbkcolor(4);
setcolor(15);
circle(x, y, 20);
if (j < 41)
else
delay(25);
if (j < 41)
delay(25);
else
delay(25);
if (j < 41)
{
line(x + 21, y + 25, x + 36 - j, y + 70 + j / 8);
delay(25);
else
delay(10);
if (j < 41)
delay(25);
else
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 legs
line(290,200,285,280);
line(320,225,305,280);
line(322,225,335,280);
line(350,200,355,280);
//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
C
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
// DETECT macros
setfillstyle(SOLID_FILL, WHITE);
setfillstyle(SOLID_FILL, LIGHTRED);
setfillstyle(SOLID_FILL, BLUE);
// Create and fill the left
// Create a Circle
setfillstyle(SOLID_FILL, WHITE);
// strip
setfillstyle(SOLID_FILL, GREEN);
closegraph();
}
Output: