Write ‘C’ program to draw line, circle, ellipse, and bar.
Answers
Draw circle in C graphics
The header file graphics.h contains circle() function which draws a circle with center at (x, y) and given radius.
Syntax :
circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle.
Examples :
Input : x = 250, y = 200, radius = 50 Output : Input : x = 300, y = 150, radius = 90 Output :
Below is the implementation to draw circle in C:
// C Implementation for drawing circle
#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;
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");
// circle fuction
circle(250, 200, 50);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
Draw ellipse in C graphics
Program to draw ellipse in C using graphics.h header file.
graphics.h library is used to include and facilitate graphical operations in program. C graphics using graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them.
Examples:
Input : x=250, y=200, start_angle = 0, end_angle = 360, x_rad = 100, y_rad = 50 Output :  Input : x=250, y=200, start_angle = 0, end_angle = 180, x_rad = 80, y_rad = 150 Output : 
Recommended: Please try your approach on {IDE}first, before moving on to the solution.
Explanation :The header file graphics.h contains ellipse() function which is described below :
void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius)
In this function x, y is the location of the ellipse. x_radius and y_radius decide the radius of form x and y.
start_angle is the starting point of angle and end_angle is the ending point of angle. The value of angle can vary from 0 to 360 degree.
// C Implementation for drawing ellipse
#include <graphics.h>
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 ellipse
int x = 250, y = 200;
// here is the starting angle
// and end angle
int start_angle = 0;
int end_angle = 360;
// radius from x axis and y axis
int x_rad = 100;
int y_rad = 50;
// initgraph initializes the graphics system
// by loading a graphics driver from disk
initgraph(&gd, &gm, "");
// ellipse fuction
ellipse(x, y, start_angle,
end_angle, x_rad, y_rad);
getch();
// closegraph function closes the graphics
// mode and deallocates all memory allocated
// by graphics system .
closegraph();
return 0;
}
Explanation:
#include <graphics.h>
#include <dos.h>
int main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
for( i = 0 ; i <= 420 ; i = i + 10, j++ )
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);
if( i == 420 )
break;
if ( j == 15 )
j = 2;
cleardevice(); // clear screen
}
getch();
closegraph();
return 0;
}