Home » C programs » graphics.h header file functions

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

	//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




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


Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.