Graphics in C
Introduction
Graphics in C refers to the use of visual representations like lines, shapes, and colors in a C program, typically for drawing, game development, and simulations.
C doesn’t have built-in graphics support, but it can be done using external libraries like:
graphics.h(mainly used in Turbo C/C++)Modern alternatives: SDL, OpenGL (for advanced applications)
Initialization
int gd = DETECT, gm;
initgraph(&gd, &gm, “C:\\Turboc3\\BGI”);
DETECTautomatically detects the graphics driver.initgraph()initializes the graphics system.
Graphics functions
line() , lineto(), and linerel() functions
line():
Draws a straight line from point (x1, y1) to (x2, y2).
Syntax:
- void line(int x1, int y1, int x2, int y2);
lineto():
Draws a line from the current position (set by
moveto()) to the specified (x, y) coordinates.Updates the current position to the new endpoint after drawing the line.
Syntax:
- void lineto(int x, int y);
linerel():
Draws a line from the current position to a point that is dx units horizontally and dy units vertically away from the current position.
Uses relative coordinates, meaning the end point is calculated based on the current drawing position.
Updates the current position to the new endpoint after drawing.
Syntax:
- void linerel(int dx, int dy);
getmaxx() and getmaxy() funcions
getmaxx()
Returns the maximum x-coordinate (horizontal width in pixels) of the screen for the current graphics mode.
Helps in determining the rightmost point where you can draw on the screen.
- Syntax:
- int getmaxx();
getmaxy()
Returns the maximum y-coordinate (vertical height in pixels) of the screen for the current graphics mode.
Helps in determining the bottom-most point where you can draw on the screen.
- Syntax:
- int getmaxy();
circle(), arc(), and ellipse() functions
- circle()
- Draws a circle with center (x, y) and specified radius.
- Syntax:
- void circle(int x, int y, int radius);
- arc()
- Draws an arc of a circle from the start_angle to end_angle.
- Syntax:
- void arc(int x, int y, int start_angle, int end_angle, int radius);
- ellipse()
- Draws an ellipse starting from start_angle to end_angle.
- Syntax:
- void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius);
rectangle(), bar(), bar3d() functions
- Rectangle
- Draws a rectangle with the given coordinates.
- Syntax:
- void rectangle(int left, int top, int right, int bottom);
- Bar
- Draws a solid rectangle (filled rectangle).
- Syntax:
- void bar(int left, int top, int right, int bottom);
- Bar3d
- Draws a 3D rectangular bar.
- Syntax:
- void bar3d(int left, int top, int right, int bottom, int depth, int topflag);
- Drawpoly
- Draws a polygon connecting the points in the given array.
- Syntax:
- void drawpoly(int num_points, int *points);
fillpoly(), fillellipse(), and pieslice()
- Fillpoly
- Draws a filled polygon.
- Syntax:
- void fillpoly(int num_points, int *points);
- Fillellipse
- Draws a filled ellipse.
- Syntax
- void fillellipse(int x, int y, int x_radius, int y_radius);
- Pieslice
- Draws a pie-shaped slice of a circle.
- Syntax:
- void pieslice(int x, int y, int start_angle, int end_angle, int radius);
