← Back to C Course | Chapter 13: Graphics | Lesson 3 of 10

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

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	circle(150, 150, 50);
	getch();
	closegraph();
	return 0;
}

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

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	rectangle(50, 50, 200, 150);
	getch();
	closegraph();
	return 0;
}

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

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	ellipse(150, 150, 0, 360, 80, 40);
	getch();
	closegraph();
	return 0;
}

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

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	fillellipse(150, 150, 60, 40);
	bar(50, 50, 150, 100);
	getch();
	closegraph();
	return 0;
}

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

c
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.