Graphics function in C

Graphics function in C

Published by: Nuru

Published date: 21 Jun 2021

Graphics function in C Photo

Graphics function in C

There are numerous graphics functions available in c. Let us see some graphics functions used in C to understand how and where a pixel is placed in each when each of the graphics functions gets invoked.

Plotting and getting points

Function: putpixel(x, y, color)

Purpose:
The functionality of this function is it put a pixel or in other words a dot at position x, y given in inputted argument. Here one must understand that the whole screen is imagined as a graph. In other words the pixel at the top left-hand corner of the screen represents the value (0, 0).
Here the color is the integer value associated with colors and when specified the picture element or the dot is placed with the appropriate color associated with that integer value.

Function: getpixel(x, y)

Purpose:
This function when invoked gets the color of the pixel specified. The color got will be the integer value associated with that color and hence the function gets an integer value as a return value. So the smallest element on the graphics display screen is a pixel or a dot and the pixels are used in this way to place images in graphics screen in C language.

Drawing lines

Function:Line(x1, y1, x2, y2)

Purpose: The functionality of this function is to draw a line from (x1,y1) to (x2,y2).Here also the coordinates are passed taking the pixel (0,0) at the top left-hand corner of the screen as the origin. And also one must note that the line formed is by using the number of pixels placed near each other.

Some Examples of C graphics:

// Drawing a Circle
#include
void main()
{
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(200,100,10);
setcolor(WHITE);
getch();
closegraph();
}

//Drawing Lines
#include
void main()
{
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(90,70,60,100);
line(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}

// Drawing rectangle
#include
void main()
{
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}

//Constructing triangle

#include
void main()
{
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(200,100,10,20);line(10,20,50,60);line(50,60,200,100);
setcolor(WHITE);
getch();
closegraph();

}