putpixel() and line() functions of graphics.h in C

In this article, we are going to learn about the putpixel() and line() functions of graphics.h header file in C programming language and then create a point and line using them.
Submitted by Manu Jemini, on March 18, 2018

Creating a line and putting a pixel in C with graphics.h is fairly simple. What it takes is to initialize a graph with two parameters and a path to the bgi folder in your system.

After that, we will call the function called line() with three numbers as the x and y co-ordinates of the line. The Third parameter decides the color of the line. With these parameters, we will have a line on our screen.

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.

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.

graphics.h - putpixel() and line() functions Example in C

#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

void main() {
	//initilizing graphic mode and 
	//graphic driver variable
	int graphicdriver=DETECT,graphicmode;

	//calling initgraph function with graphic 
	//mode and graphic driver
	initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

	//Printing message for user
	outtextxy(10, 10 + 10, "Program to draw point(with diffrent colors) and line in C graphics");

	//calling putpixel to create a pixel 
	//on screen with color white
	putpixel(300, 100, WHITE);

	//calling putpixel to create a pixel on 
	//screen with color yellow
	putpixel(350, 100, YELLOW);

	// calling ellipse function
	line(100,100,200,100);

	getch();

}

Output

graphics.h - putpixel() and line() functions example in C



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.