Open In App

Line Attributes in Computer Graphics

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Computer Graphics is an important topic in the Computer Science domain. It is a type of coding practice that will print the necessary output images. One should have a good imagination to master Computer Graphics. Computer Graphics mainly can be written in C programming language or C++ programming language. Using any one of the programming languages, users can develop eye-catching output images, It generally uses the computer graphics card to produce the images. There are color options also. Using calculation & proper use of programming knowledge, users can develop any structure. From a simple car to Eiffel Tower, everything can be derived using computer graphics. There are many inbuilt functions in the Computer Graphics. The line is one of them.

Line Attributes In Computer Graphics:

The line is one of the major inbuilt functions in computer graphics, This helps to make more interactive & interesting images. There are many inbuilt functions are present. Like there are Circle, Arch, Eclipse, etc. All of this help to make a structure. Proper use of those functions helps to draw out particular images.

As the line is the function there some attributes or arguments are present. These help to draw a line in a better position. There are mainly four coordinates. Two are the starting coordinates & the two are for ending coordinates.

Syntax: 

line(int X1, int Y1, int X2, int Y2);

  • int X1: This is the starting coordinate of the line. This is the horizontal coordinate of any line. This is always the integer in nature. This is the starting X coordinate of the lines.
  • int Y1: This also falls under the starting coordinates. This is the vertical coordinate of any line. This is always the integer in nature. This is the starting Y coordinate of the lines.
  • int X2: This is the ending coordinate of the line. This is the horizontal coordinate of any line. This is also an integer in nature. This is the ending X coordinate of the lines.
  • int Y2: This also falls under the ending coordinates. This is the vertical coordinate of any line. This is also an integer in nature. This is the ending Y coordinate of the lines.

Associated Functions With Line Attribute:

The Line function can only be able to draw lines. Whenever there is a need to draw a line in computer graphics, we need to take the help of the Line function. But there is also some need to customize the line. Suppose, if the user wants to draw a Red car, then the lines will definitely be in Red color. Or a line needs to be thick in someplace. So, for that purpose, we need to make a width line there. All these things can’t able to do by a simple line function. For that purpose, we need to have some more functions.

  • setcolor(color): This is the function that is needed to make a colorful line. Using this function along with the Line() function, users can able to draw a colorful line. There in this function, we have to provide the color name as the argument. This function needs to be placed before the line function. This will help to provide the appropriate output.
  •  setlinestyle(int linestyle, unsigned pattern, int thickness): This function basically used for two main reasons. One is by the help of this function, we can able to draw different lines. Like we can able to draw the dotted line also. This function also helps to make a thick like. The last argument is used for making a width line. The first two functions are needed when we have to make some other line pattern.

Types Of Lines In Computer Graphics:

It is time to know about the types of lines that can be derived using the line attribute in computer graphics. Users can derive mainly three types of lines by playing with the attribute values of the lines.

Horizontal Lines:

  • Step 1: Here, we will first include the necessary header file for working on the computer graphics. 
  • Step 2: Then we color the line using setcolor() function. This will help to color any diagram easily. Here, we have colored the line Green.
  • Step 3: Now, we will use the line attributes. As this is the horizontal line, we have to keep same the values of Y coordinates. This means the Y1 & Y2 will be the same. The distance will depend upon the difference between X1 & X2.
  • Step 4: We have to close the graph using closegraph() function.

Example 1:

C




// Horizontal Line code
#include <stdio.h>
 
// Required Header File
#include <conio.h>
#include <graphics.h>
int main()
{
 
    // Configuring The Graphics File
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Coloring The Line
    setcolor(GREEN);
 
    // Horizontal Line
    line(100, 300, 400, 300);
    getch();
 
    // Closing The Graphics
    closegraph();
    return 0;
}


Output:

 

Vertical Lines:

  • Step 1: Here again, we will first include the necessary header file for working on the computer graphics. 
  • Step 2: Then we color the line using setcolor() function. This will help to color any diagram easily. Here, we have colored the line Blue.
  • Step 3: Now, we will use the line attributes. As this is the vertical line, we have to keep same the values of X coordinates. This means the X1 & X2 will be the same. The distance will depend upon the difference between Y1 & Y2.
  • Step 4: We have to close the graph using the closegraph() function.

Example 2: 

C




// Vertical Lines code in Computer Graphics
#include <stdio.h>
 
// Required Header File
#include <conio.h>
#include <graphics.h>
int main()
{
    // Configuring The Graphics File
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
    // Coloring The Line
    setcolor(BLUE);
    // Vertical Line
    line(600, 200, 600, 400);
    getch();
    // Closing The Graphics
    closegraph();
    return 0;
}


Output:

 

Tangent Lines:

  • Step 1: Here again, we will first include the necessary header file for working on the computer graphics. 
  • Step 2: Then we color the line using setcolor() function. This will help to color any diagram easily. Here, we have colored the line Red.
  • Step 3: Now, we will use the line attributes. As this is the tangent line, all the values must not be the same as each other. This means the X1, Y1, X2 & Y2 will not be the same. The distance will depend upon the difference between X1 & Y1, X2 & Y2.
  • Step 4: We have to close the graph using the closegraph() function.

Example 3: 

C




// Tangent Lines code for Computer Graphics
#include <stdio.h>
 
// Required Header File
#include <conio.h>
#include <graphics.h>
int main()
{
 
    // Configuring The Graphics File
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Coloring The Line
    setcolor(RED);
 
    // Tangent Line
    line(250, 400, 450, 500);
    getch();
 
    // Closing The Graphics
    closegraph();
    return 0;
}


Output:

 

Width Of Line In Computer Graphics:

  • Step 1: Here again, we will first include the necessary header file for working on the computer graphics.
  • Step 2: Then we have to first draw a normal line with the help of setlinestyle() & line() function. There we need to provide the zero values to the first two arguments of the setlinestyle() function. As the goal is to make a width line. There is no need to draw patterns of lines. in the third argument, we have to provide the value of 1. This implements a normal thick line as the output.
  • Step 3: Now, we will use the line attributes. As this is the horizontal line, we have to keep same the values of Y coordinates. This means the Y1 & Y2 will be the same. The distance will depend upon the difference between X1 & X2.
  • Step 4: Now, again we have to use the setlinestyle() function. This time, the last argument value will not be as 1. As the goal is to create a thick line. So, the value 10 is provided there.
  • Step 5: Now again we have to draw a horizontal line there. So, we have to use the same theory of the Horizontal line.
  • Step 6: We have to close the graph using the closegraph() function.

Example 4:

C




// Width of Line code in computer graphics
#include <stdio.h>
// Required Header File
#include <conio.h>
#include <graphics.h>
int main()
{
 
    // Configuring The Graphics File
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Normal Width Line
    setlinestyle(0, 0, 1);
    line(300, 400, 700, 400);
 
    // Width Line
    setlinestyle(0, 0, 10);
    line(300, 700, 700, 700);
    getch();
 
    // Closing The Graphics
    closegraph();
    return 0;
}


Output:

 

Color Of Line In Computer Graphics:

  • Step 1: Here first, we need to provide the header file to start the implementation of the program.
  • Step 2: Then we have to use the setcolor() function. Here, at first, we need to draw a Green line. So, we have provided Green as the argument there.
  • Step 3: Now, as we are drawing the horizontal line, we have to keep same the values of Y. The values of X will change here.
  • Step 4: Now, again we have to use the setcolor() function. Here, we need to draw a Yellow line. So, we have provided Yellow as the argument in the setcolor() function.
  • Step 5: Again we have to draw a horizontal line for creating a difference with the above line.
  • Step 6: We have to close the graph using the closegraph() function.

Example 5:

C




// Color Of Line In Computer Graphics
#include <stdio.h>
 
// Required Header File
#include <conio.h>
#include <graphics.h>
 
int main()
{
 
    // Configuring The Graphics File
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Coloring The Line With Green
    setcolor(GREEN);
    line(300, 400, 700, 400);
 
    // Coloring The Line With Yellow
    setcolor(YELLOW);
    line(300, 700, 700, 700);
    getch();
 
    // Closing The Graphics
    closegraph();
    return 0;
}


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads