C Drawing Shapes
Drawing Circles
circle() draws a wireframe circle outline given a center coordinate (X, Y) and a radius in pixels, tracing just the boundary rather than filling the interior — for a solid disc you'd reach for fillellipse() with equal horizontal and vertical radii instead.
Example: Drawing Circles
#include <graphics.h>
int main() {
initgraph(0, 0, "");
circle(150, 150, 50);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Drawing Rectangles
rectangle() draws an outlined box from four coordinates: the top-left corner's X and Y, followed by the bottom-right corner's X and Y, so the function itself computes the width and height from the distance between those two corner points rather than taking them directly.
Example: Drawing Rectangles
#include <graphics.h>
int main() {
initgraph(0, 0, "");
rectangle(50, 50, 200, 150);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Drawing Ellipses
ellipse() draws an elliptical arc and takes eight parameters in total: the center coordinates, a start and end angle in degrees (letting you draw a partial arc rather than a full ellipse), and separate horizontal and vertical radii, which is what lets it draw true ellipses rather than only perfect circles.
Example: Drawing Ellipses
#include <graphics.h>
int main() {
initgraph(0, 0, "");
ellipse(150, 150, 0, 360, 80, 40);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Filled Shapes
fillellipse() and bar() draw solid, filled shapes instead of outlines — fillellipse() fills an elliptical region, while bar() fills a rectangular one, and bar3d() extends bar() with an extra depth parameter to render a pseudo-3D block with visible side faces.
Example: Filled Shapes
#include <graphics.h>
int main() {
initgraph(0, 0, "");
fillellipse(150, 150, 60, 40);
bar(50, 50, 150, 100);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Drawing Polygons
drawpoly() and fillpoly() both draw arbitrary multi-sided shapes from an array of (X, Y) coordinate pairs you supply as vertices, with drawpoly() tracing just the polygon's outline and fillpoly() additionally filling its interior with the current fill style.
Example: Drawing Polygons
#include <graphics.h>
int main() {
initgraph(0, 0, "");
int points[6] = {100, 50, 150, 150, 50, 150};
drawpoly(3, points);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: