Skip to content Skip to sidebar Skip to footer

Graphics to Draw Lines C++

Draw a line in C++ graphics

In this tutorial, we will learn to draw different types of lines in C++ using the graphics library as it is used quite frequently in C++ graphics coding. Because sometimes to get the desired output we may require to draw different types of lines. Here we will learn to draw horizontal lines, vertical lines, and slant lines along with various styles. So let's begin to learn about the types and syntax of line functions.

Types of line functions of the graphics library

The graphics library of C++ contains these three functions to draw lines –

  • line() – The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1,y1) coordinates to (x2,y2) coordinates on the graphics screen. The syntax of line() function is –

void line(int x1, int y1, int x2, int y2);

  • linerel() –This function draws a line from the current position to a point that is at a relative distance (x,y) from the current position on the graphics screen. The syntax of linerel() function is –

void linerel(int dx, int dy);

  • lineto() –This function draws a line from the current position to (x,y) position on the graphics screen. The syntax of lineto() function is –

void lineto(int x, int y);

Sample program using line(), linerel() and lineto() functions

Now, the program to demonstrate the above three line functions is given below through which you can understand easily.

#include<iostream.h> #include<conio.h> #include<graphics.h> void main() {    int gd=DETECT,gm,x,y;    clrscr();    initgraph(&gd,&gm,"c:\\TC\\bgi");   //INITIALISING GRAPHICS MODE     setlinestyle(0,0,3);    outtextxy(300,150,"LINE()");    line(350,60,200,200);     outtextxy(300,300," CURRENT POSITION");    linerel(320,350);    outtextxy(335,315,"LINEREL()");     outtextxy(30,30," CURRENT POSITION");    lineto(30,200);    outtextxy(70,45,"LINETO()");     getch();    closegraph(); }

So the code above illustrates the use of line(), linerel(), and lineto() graphics library functions. In the code above, the setlinestyle(0,0,3) function sets a solid line with a thick width. The line() function draws a line from (350,60) position to (200,200) position. The linerel() function in this program draws a line from the current position i.e (300,300) to a point (320,350) that is at a relative distance (20,50) from the current position onto the graphics screen. The lineto() function draws a line from the current position (30,30) to the position given in parameter i.e (30,200). The output of this program code is –

Draw a line in C++ graphics

So this is the output screenshot which shows the lines drawn by the functions used in the code. I hope you understood these three line functions of the C++ graphics library. Now we will learn about various lines that can be drawn using line functions.

Line drawing in C++

In this, we will learn how to draw various types of lines in C++ using line() function.

Line Types

So, first of all, we know that there are three types of lines namely horizontal, vertical and slant lines. To understand these lines lets take a sample line function which draws a line from position (x1,y1) to (x2,y2) –

void line(int x1, int y1, int x2, int y2);

  1. Horizontal line – In this type of lines the values of the y-axis coordinate remain the same and only the values of the x-axis vary.
    For example – line(20,100,200,100);
    This creates a horizontal line from position (20,100) to (200,100) on the graphics screen.
  2. Vertical line – In this type, the values of the x-axis remain unchanged and only the y-axis values change.
    For example – line(50,40,50,200);
    This creates a vertical line from position (50,40) to (50,200) on the graphics screen.
  3. Slant line – To draw a slant line we have to take the x-axis and y-axis values differently always. Because to draw slanted lines the x-y positions must be changed. So there is no restriction in assigning the values to get a slant line on the graphics screen.
    For example – line(20,30,60,100);
    This creates a slant line from position (20,30) to (60,100) on the graphics screen.

Line Styles

Now, we will learn about the setlinestyle function in detail. The syntax of setlinestyle() function is –

void setlinestyle(int linestyle, unsigned pattern, int thickness)

  • Linestyle –
    These values decide the style of a line to be drawn.
    1. Solid linestyle- 0
    2. Dotted linestyle- 1
    3. Centered linestyle- 2
    4. Dashed linestyle- 3
    5. User-defined linestyle- 4
    So, these are the five types of line styles you can use in your program.
  • Pattern –
    This is a 16-bit pattern that applies only if linestyle is USERBIT_LINE i.e. user-defined linestyle.
  • Thickness –
    1. Normal width – 1
    2. Thick width – 3
    So there are two types of thickness with integer values 1 and 3.

Program to draw various lines in C++

Now, we will see a program that uses each of these line styles to make you understand easily.

#include<iostream.h> #include<conio.h> #include<graphics.h> void main() {    int gd=DETECT,gm,x,y;    clrscr();    initgraph(&gd,&gm,"c:\\TC\\bgi");          int x1=25,y1=30,x2=60,y2=150;    outtextxy(x1-20,y1-10,"(x1,y1)");    outtextxy(x2-25,y2,"(x2,y2)");    line(x1,y1,x2,y2);     setlinestyle(0,0,3);    y1=230;    y2=350;    outtextxy(x1-20,y1-10,"(x1,y1)");    outtextxy(x2-25,y2,"(x2,y2)");    line(x1,y1,x2,y2);     outtextxy(135,10,"HORIZONTAL LINES");    outtextxy(142,30,"(NORMAL WIDTH)");    setlinestyle(0,0,1);    line(100,50,300,50);     setlinestyle(1,0,1);    line(100,70,300,70);     setlinestyle(2,0,1);    line(100,90,300,90);     setlinestyle(3,0,1);    line(100,110,300,110);     outtextxy(142,130,"(THICK WIDTH)");    setlinestyle(0,0,3);    line(100,150,300,150);     setlinestyle(1,0,3);    line(100,170,300,170);     setlinestyle(2,0,3);    line(100,190,300,190);     setlinestyle(3,0,3);    line(100,210,300,210);     outtextxy(445,20,"VERTICAL LINE");    outtextxy(405,40,"(NORMAL WIDTH)");    setlinestyle(0,0,1);    line(410,60,410,220);     setlinestyle(1,0,1);    line(440,60,440,220);     setlinestyle(2,0,1);    line(470,60,470,220);     setlinestyle(3,0,1);    line(510,60,510,220);     outtextxy(535,40,"(THICK WIDTH)");    setlinestyle(0,0,3);    line(540,60,540,220);     setlinestyle(1,0,3);    line(570,60,570,220);     setlinestyle(2,0,3);    line(600,60,600,220);     setlinestyle(3,0,3);    line(630,60,630,220);     outtextxy(200,250,"SLANT LINES");    //		/    outtextxy(110,265,"(NORMAL WIDTH)");    setlinestyle(0,0,1);    line(120,280,100,380);     setlinestyle(1,0,1);    line(150,280,130,380);     setlinestyle(2,0,1);    line(180,280,160,380);     setlinestyle(3,0,1);    line(210,280,190,380);     outtextxy(240,265,"(THICK WIDTH)");    setlinestyle(0,0,3);    line(240,280,220,380);     setlinestyle(1,0,3);    line(270,280,250,380);     setlinestyle(2,0,3);    line(300,280,280,380);     setlinestyle(3,0,3);    line(330,280,310,380);     //		\    outtextxy(360,265,"(NORMAL WIDTH)");    setlinestyle(0,0,1);    line(370,280,390,380);     setlinestyle(1,0,1);    line(400,280,420,380);     setlinestyle(2,0,1);    line(430,280,450,380);     setlinestyle(3,0,1);    line(460,280,480,380);     outtextxy(490,265,"(THICK WIDTH)");    setlinestyle(0,0,3);    line(490,280,510,380);     setlinestyle(1,0,3);    line(520,280,540,380);     setlinestyle(2,0,3);    line(550,280,570,380);     setlinestyle(3,0,3);    line(580,280,600,380);     getch();    closegraph(); }

So the C++ program above shows the various lines – horizontal, vertical, and slant lines. The output also shows solid, dotted, centered, dashed lines and both normal and thick width lines. The output of this program is –

Program to draw various lines in C++

So this is the output of the C++ program which draws various lines. I hope this tutorial helped you and solved your problems. Thanks for reading this tutorial.

Also read:

  • Remove the Last Line from a Text File in C++

Graphics to Draw Lines C++

Source: https://www.codespeedy.com/draw-a-line-in-cpp-graphics/

إرسال تعليق for "Graphics to Draw Lines C++"