Putpixel in C++ Graphics by R4R Team

What is pixel ?
Pixel is the smallest entity in the computer screen.

How we display pixel ?
Answer: by putpixel() function

Syntax-
putpixel(x,y,color)
Where:
x and y is the co-ordinate
'color' is the pixel color


program-

#include< iostream>
#include< conio.h>
#include< graphics.h>
void main()
{
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"");
putpixel(200,200,GREEN);
getch();
}


output:



-In this program, we simply display the pixel at (200,200) with green color.
-It is difficult to seen the only one pixel because of it's small size.

Now print pixels randomly with random colors:

program:

:#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
#include< graphics.h>
void main()
{
int gd=DETECT,gm=0,x,y,i;
initgraph(&gd,&gm,"");
for(i=0;i< 1000;i++)
{
x=rand()%500;
y=rand()%500;
putpixel(x,y,i);
}
getch();
}


output:



-In this program, we include < stdlib.h> because of rand() function which are used to generate the random number.
-We run loop 1000 times and put pixel randomly anywhere in the screen.




Leave a Comment: