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)

int gd = DETECT, gm;
initgraph(&gd, &gm, “C:\\Turboc3\\BGI”);

  • DETECT automatically detects the graphics driver.

  • initgraph() initializes the graphics system.

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()

  • 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()
    • 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
    • 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
    • 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);
Chess Board
#include <graphics.h>
#include <conio.h>
void main()
{
    int gd=DETECT, gm;
    int i,j,x,y,black=0;
    initgraph(&gd, &gm, “c:\\turboc3\\bgi”);
    cleardevice();
    for(j=0,y=20; j<8; j++,y+=70)
   {
        for(i=0,x=20; i<8; i++,x+=70)
       {
              if(black==1)
             {
                    setfillstyle(1,BLACK);
                    rectangle(x,y,x+70,y+70);
                    floodfill(x+1,y+1,BLACK);
             }
             else
             {
                    setfillstyle(1,WHITE);
                    rectangle(x,y,x+70,y+70);
                    floodfill(x+1,y+1,WHITE);
             }
             if(black==0)
                   black=1;
             else
                   black=0;
     }
      if(black==1)
            black=0;
      else
            black=1;
     }
getch();
closegraph();
}
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm;
int r,i,a,b,x,y;
 
detectgraph(&gd,&gm);
initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);
 
//draw the top rectangle and color it
setcolor(RED);
rectangle(100,100,450,150);
setfillstyle(SOLID_FILL,RED);
floodfill(101,101,RED);
 
//draw the middle rectangle and color it
setcolor(WHITE);
rectangle(100,150,450,200);
setfillstyle(SOLID_FILL,WHITE);
floodfill(101,151,WHITE);
 
//draw the bottom rectangle and color it
setcolor(GREEN);
rectangle(100,200,450,250);
setfillstyle(SOLID_FILL,GREEN);
floodfill(101,201,GREEN);
 
//draw the circle
a=275;//center
b=175;//center
r=25;//radius
setcolor(BLUE);
circle(a,b,r);
 
//spokes
for(i=0;i<=360;i=i+15)
{
x=r*cos(i*M_PI/180);
y=r*sin(i*M_PI/180);
line(a,b,a+x,b-y);
}
 
getch();
closegraph();
}