Write a program in C to draw an eclipse on screen using graphics.h header file
In this program, we will draw an eclipse on screen having centre at mid of the screen. We will use ellipse functions of graphics.h header file to draw eclipse on screen. Below is the detailed descriptions of ellipse function.
void ellipse(int xCenter, int yCenter, int startAngle, int endAngle, int xRadius, int yRadius);
Function Argument | Description |
---|---|
xCenter | X coordinate of center of eclipse. |
yCenter | Y coordinate of center of eclipse. |
startAngle | Start angle of the eclipse arc. |
endAngle | End angle of the eclipse arc. It will draw eclipse starting form startAngle till endAngle. |
xRadius | Horizontal radius of the eclipse. |
yRadius | Vertical radius of the eclipse. |
To draw a complete eclipse, we should pass start and end angle as 0 and 360 respectively.
C program to draw an eclipse using graphics
In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). First of all we will calculate the center co-ordinates of eclipse which is the center of screen bu calling getmaxx and getmaxy function. Then we draw full eclipse by calling ellipse function.
#include<stdio.h> #include<graphics.h> #include<conio.h> int main(){ int gd = DETECT,gm; int x ,y; initgraph(&gd, &gm, "X:\\TC\\BGI"); /* Initialize center of ellipse with center of screen */ x = getmaxx()/2; y = getmaxy()/2; outtextxy(x-100, 50, "ELLIPSE Using Graphics in C"); /* Draw ellipse on screen */ ellipse(x, y, 0, 360, 120, 60); getch(); closegraph(); return 0; }
Program Output