Draw lines from one co-ordinate to another using graphics.h in C

In this article, we are going to learn about the moveto() and lineto() function of graphics.h header file and use them to draw lines from one co-ordinate to another.
Submitted by Manu Jemini, on March 21, 2018

Every line is made of two endpoints. Making lines in c with graphics.h's function is very easy.

First thing is to initiate a graph. Then set the current position to a location by using the function moveto(). moveto() function takes up the x and y co-ordinate which define the location.

After setting up the current location, we can use the function lineto(). This lineto() function takes two parameters which define the end point of the line.

That’s all we have to do to make a line.

graphics.h - Draw lines from one co-ordinate to another in C

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

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

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

	//Printing message for user
	outtextxy(10, 10 + 10, "Program to draw lines from one co-ordinate to another in C graphics");

	//line 1 move to and line to co-ordinate

	moveto(60,60);
	lineto(500, 60);

	//line 2 move to and line to co-ordinate

	moveto(80,80);
	lineto(480,80);

	//line 3 move to and line to co-ordinate

	moveto(100,100);
	lineto(460,100);
	
	getch();

	return 0;
}

Output

graphics.h - Draw lines from one co-ordinate to another in C




Comments and Discussions!

Load comments ↻






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