Direct Use of Line Equation in Computer Graphics

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Direct Use of Line Equation in Computer Graphics

Computer Graphics | Direct Use of Line Equation: In this tutorial, we are going to learn about
the Direct Use of Line Equation in Computer Graphics, 2D line and properties of good line
drawing algorithms.
Computer Graphics | Direct Use of Line Equation
The standard line equation, as we all know is used for drawing a line. It is given by: y = mx + c.
We are discussing here in 2D so we all know that there are 2 axes: x and y. Both of the axes are
required to give the equation of any 2D shape. The line is a straight path joining 2 points in the x-
y plane. If both the points are given then we can find the equation of a line.
Properties of Line Drawing Algorithm

The following are the properties that a line must hold in any line drawing algorithm,

i. Line must be straight


ii. Line must terminate accurately
iii. Line must have constant density
iv. Density must be independent of its length
v. Line must be drawn very fast

Line Drawing Algorithms

There are some set of rules and steps which help draw a line. These algorithms are given below,

1. Direct Use of line equation


2. DDA (Digital Differential Analyzer)
3. Bresenham's Algorithm

Direct use of Line Equation

This is the simplest form of drawing a line. We all know that the equation of the line is y = mx +
c. Here m is slope and c is the length from origin to the point where the line cuts y-axis. In this
method, we will be having the start and endpoint of the line and by the help of that points, we'll
calculate the other points which lie on the line. We have to find the slope of the line by using the
given points.

Example:

We have given two points X and Y. The coordinates of X are (0, 0) and the coordinates
of Y are (5, 15). The slope of the line will be,

m = (15 - 0) / (5-0)
m=3

Now let us put the slope in the line equation.

y = 3x + c
Origin point is (0,0). So,

c=0

Putting c=0 in the above equation.

y = 3x

Now we will calculate the intermediate points.

Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15

We got the intermediate points which are, (1, 3), (2, 6), (3, 9), (4, 12) and finally (5, 15)

Now we'll plot these points on the graph.

Now we'll plot these points on the graph.

Hence, we have plotted the points that lie between the given points through the standard line
equation. By doing so with a very small gap between these pints will give us the entire line.
write a code for direct line drawing algorithm?
Digital Differential Analyzer (DDA) Algorithm

Step 1: Read the input of the 2 end points of the line as (x1, y1) & (x2, y2) such that x1 != x2 and
y1 != y2
Step 2: Calculate dx = x2 – x1 and dy = y2 – y1
Step 3:
if(dx>=dy)
step=dx
else
step=dy
Step 4: xin = dx / step & yin = dy / step
Step 5: x = x1 + 0.5 & y = y1 + 0.5
Step 6: for(k = 0; k < step; k++)
{
x = x + xin
y = y + yin
putpixel(x, y)
}

Program for DDA Line Drawing Algorithm in C

#include <graphics.h> printf("Enter the value of x1 and y1 : ");


#include <stdio.h> scanf("%f%f",&x1,&y1);
#include <math.h> printf("Enter the value of x2 and y2: ");
#include <dos.h> scanf("%f%f",&x2,&y2);
   
void main( ) dx=abs(x2-x1);
{ dy=abs(y2-y1);
float x,y,x1,y1,x2,y2,dx,dy,step;  
int i,gd=DETECT,gm; if(dx>=dy)
  step=dx;
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); else
  step=dy;
dx=dx/step;
dy=dy/step; x=x+dx;
x=x1; y=y+dy;
y=y1; i=i+1;
  delay(100);
i=1; }
while(i<=step)  
{ closegraph();
putpixel(x,y,5); }
Outptut
Program for DDA Line Drawing Algorithm in C++

#include <graphics.h> step=dx;


#include <iostream.h> else
#include <math.h> step=dy;
#include <dos.h>  
  dx=dx/step;
void main( ) dy=dy/step;
{  
float x,y,x1,y1,x2,y2,dx,dy,step; x=x1;
int i,gd=DETECT,gm; y=y1;
  i=1;
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); while(i<=step)
  {
cout<<"Enter the value of x1 and y1 : "; putpixel(x,y,5);
cin>>x1>>y1; x=x+dx;
cout<<"Enter the value of x2 and y2: "; y=y+dy;
cin>>x2>>y2; i=i+1;
  delay(100);
dx=abs(x2-x1); }
dy=abs(y2-y1);  
  closegraph();
if(dx>=dy) }

Write Advantages and disadvantages of DDA Algorithm and Compare Bresenham algorithm?
The advantages of DDA Algorithm are-

 It is a simple algorithm.
 It is easy to implement.
 It avoids using the multiplication operation which is costly in terms of time complexity.
The disadvantages of DDA Algorithm are-

 There is an extra overhead of using round off( ) function.


 Using round off( ) function increases time complexity of the algorithm.
 Resulted lines are not smooth because of round off( ) function.

The points generated by this algorithm are not accurate.

Bresenham Algorithm

Bresenham Algorithm was developed by J.E.Bresenham in 1962 and it is much accurate and
much more efficient than DDA.

It scans the coordinates but instead of rounding them off it takes the incremental value in account
by adding or subtracting and therefore can be used for drawing circle and curves.
Therefore if a line is to be drawn between two points x and y then next coordinates will be( xa+1,
ya) and (xa+1, ya+1) where a is the incremental value of the next coordinates and difference
between these two will be calculated by subtracting or adding the equations formed by them.

Comparision

 DDA uses floating points where as Bresenham algorithm use fixed points.
 DDA round off the coordinates to nearest integer but Bresenham algorithm does not.
 Bresenham algorithm is much accurate and efficient than DDA.
 Bresenham algorithm can draw circles and curves with much more accuracy than DDA.
 DDA uses multiplication and division of equation but Bresenham algorithm uses
subtraction and addition only

Digital Differential Analyzer,Line Bresenhams Line Drawing Algorithm


Drawing Algorithm

Arithmetic DDA algorithm uses floating points i.e. Bresenhams algorithm uses fixed points i.e. integer
Real Arithmetic Arithmetic.

Operations DDA algorithms uses multiplication and Bresenhams algorithm uses only subtraction and addition
division in its operations. in its

Speed DDA algorithm is rather slowly than Bresenhams algorithm is faster than DDA algorithm in
Bresenhams algorithm in line drawing line drawing because it performs only addition and
because it uses real arithmetic (floating subtraction in its calculations and uses only integer
point operations). arithmetic so it runs significantly faster.

Accuracy DDA algorithm is not as accurate and Bresenhm algorithm is more accurate and efficient as
& efficient as Bresenhm algorithm. than DDA algorithm.
Efficiency

Drawing DDA algorithm can draw circles and Bresenhm algorithm can draw circles and curves with
curves but that are not as accurate as much more accuracy than DDA algorithm.
Bresenhm algorithm.

Round off DDA algorithm round off the coordinates Bresenhm algorithm does not round off but takes the
to integer that is nearest to the line. incremental value in its operation.

Bresenhm algorithm is less expensive than DDA


algorithm as it uses only addition and subtraction.

You might also like