C++ Programming Code Examples
Learn C++ Language
putpixel() Function in C++ Programming Language
putpixel() Function in C++
The header file graphics.h contains putpixel() function which plots a pixel at location (x, y) of specified color. Where, (x, y) is the location at which pixel is to be put, and color specifies the color of the pixel.
To put a pixel on the screen at a particular position, calling the pixel() function is a good way. This function takes three parameters as the position of the pixel and also the color of the pixel.
Syntax for putpixel() Function in C++
#include <graphics.h>
void putpixel(int x, int y, int color);
x
X coordinate of the point
y
Y coordinate of the point
color
specifies the color of the pixel
To use these function in your program, we would need to include graphics.h file in your program. You should also use getch() function to make the screen freeze.
/* putpixel() function writes a pixel to the specified position in the bitmap, using the current drawing mode and the bitmap's clipping rectangle. */
/* plot a point in the color defined by color at (x,y) by putpixel() function code example. */
#include <graphics.h>
#include <stdio.h>
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm, color;
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");
// putpixel function
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}