Line Drawing Techniques-Computer Graphics-Lecture Notes, Study notes of Computer Graphics

Computer Graphics involves technology to accept, process, transform and present information in a visual form that also concerns with producing images and animations using a computer. This course teach how to make your own design in computer using OpenGl. This lecture includes: Line, Drawing, Techniques, Collinear, Straight, Line, Different, Formalized, Intuitive, Points, Problem

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

parnashi
parnashi ๐Ÿ‡ฎ๐Ÿ‡ณ

4.4

(49)

71 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No.5 Line Drawing Techniques
Line
Aline, or straight line, is, roughly speaking, an (infinitely) thin, (infinitely) long, straight
geometrical object, i.e. a curve that is long and straight. Given two points, in Euclidean
geometry, one can always find exactly one line that passes through the two points; this
line provides the shortest connection between the points and is called a straight line.
Three or more points that lie on the same line are called collinear. Two different lines can
intersect in at most one point; whereas two different planes can intersect in at most one
line. This intuitive concept of a line can be formalized in various ways.
A line may have three forms with respect to slope i.e. it may have slope = 1 as shown in
following figure (a), or may have slope < 1 as shown in figure (b) or it may have slope >
1 as shown in figure (c). Now if a line has slope = 1 it is very easy to draw the line by
simply starting form one point and go on incrementing the x and y coordinates till they
reach the second point. So that is a simple case but if slope < 1 or is > 1 then there will be
some problem.
figure (a) figure (b) figure (c)
There are three techniques to be discussed to draw a line involving different time
complexities that will be discussed later. These techniques are:
๎˜‚Incremental line algorithm
๎˜‚DDA line algorithm
๎˜‚Bresenham line algorithm
Incremental line algorithm
This algorithm exploits simple line equation y = m x + b
Where m = dy / dx
and b = y โ€“ m x
Now check if |m| < 1 then starting at the first point, simply increment x by 1 (unit
increment) till it reaches ending point; whereas calculate y point by the equation for each
x and conversely if |m|>1 then increment y by 1 till it reaches ending point; whereas
calculate x point corresponding to each y, by the equation.
Now before moving ahead let us discuss why these two cases are tested. First if |m| is less
than 1 then it means that for every subsequent pixel on the line there will be unit
increment in x direction and there will be less than 1 increment in y direction and vice
versa for slope greater than 1. Let us clarify this with the help of an example:
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Line Drawing Techniques-Computer Graphics-Lecture Notes and more Study notes Computer Graphics in PDF only on Docsity!

Lecture No.5 Line Drawing Techniques

Line A line , or straight line , is, roughly speaking, an (infinitely) thin, (infinitely) long, straight geometrical object, i.e. a curve that is long and straight. Given two points, in Euclidean geometry, one can always find exactly one line that passes through the two points; this line provides the shortest connection between the points and is called a straight line. Three or more points that lie on the same line are called collinear. Two different lines can intersect in at most one point; whereas two different planes can intersect in at most one line. This intuitive concept of a line can be formalized in various ways.

A line may have three forms with respect to slope i.e. it may have slope = 1 as shown in following figure (a), or may have slope < 1 as shown in figure (b) or it may have slope > 1 as shown in figure (c). Now if a line has slope = 1 it is very easy to draw the line by simply starting form one point and go on incrementing the x and y coordinates till they reach the second point. So that is a simple case but if slope < 1 or is > 1 then there will be some problem.

figure (a) figure (b) figure (c)

There are three techniques to be discussed to draw a line involving different time complexities that will be discussed later. These techniques are:

 Incremental line algorithm  DDA line algorithm  Bresenham line algorithm

Incremental line algorithm This algorithm exploits simple line equation y = m x + b Where m = dy / dx and b = y โ€“ m x

Now check if |m| < 1 then starting at the first point, simply increment x by 1 (unit increment) till it reaches ending point; whereas calculate y point by the equation for each x and conversely if |m|>1 then increment y by 1 till it reaches ending point; whereas calculate x point corresponding to each y, by the equation.

Now before moving ahead let us discuss why these two cases are tested. First if |m| is less than 1 then it means that for every subsequent pixel on the line there will be unit increment in x direction and there will be less than 1 increment in y direction and vice versa for slope greater than 1. Let us clarify this with the help of an example :

docsity.com

Suppose a line has two points p1 (10, 10) and p2 (20, 18) Now difference between y coordinates that is dy = y2 โ€“ y1 = 18 โ€“ 10 = 8 Whereas difference between x coordinates is dx = x2 โ€“ x1 = 20 โ€“ 10 = 10 This means that there will be 10 pixels on the line in which for x-axis there will be distance of 1 between each pixel and for y-axis the distance will be 0.8.

Consider the case of another line with points p1 (10, 10) and p2 (16, 20) Now difference between y coordinates that is dy = y2 โ€“ y1 = 20 โ€“ 10 = 10 Whereas difference between x coordinates is dx = x2 โ€“ x1 = 16 โ€“ 10 = 6

This means that there will be 10 pixels on the line in which for x-axis there will be distance of 0.6 between each pixel and for y-axis the distance will be 1.

Now having discussed this concept at length let us learns the algorithm to draw a line using above technique, called incremental line algorithm:

Incremental_Line (Point p1, Point p2) dx = p2.x โ€“ p1.x dy = p2.y โ€“ p1.y m = dy / dx x = p1.x y = p1.y b = y โ€“ m * x if |m| < 1 for counter = p1.x to p2.x drawPixel (x, y) x = x + 1 y = m * x + b else for counter = p1.y to p2.y drawPixel (x, y) y = y + 1 x = ( y โ€“ b ) / m

Discussion on algorithm: Well above algorithm is quite simple and easy but firstly it involves lot of mathematical calculations that is for calculating coordinate using equation each time secondly it works only in incremental direction.

We have another algorithm that works fine in all directions and involving less calculation mostly only addition; which will be discussed in next topic.

Digital Differential Analyzer (DDA) Algorithm: DDA abbreviated for digital differential analyzer has very simple technique. Find difference dx and dy between x coordinates and y coordinates respectively ending points of a line. If |dx| is greater than |dy|, than |dx| will be step and otherwise |dy| will be step.

if |dx|>|dy| then step = |dx| else

docsity.com

actual line, using only integer math. Assuming that the slope is positive and less than 1, moving 1 step in the x direction, y either stays the same, or increases by 1. A decision function is required to resolve this choice.

If the current point is (xi , y (^) i), the next point can be either (xi +1,y (^) i) or (xi +1,y (^) i +1). The actual position on the line is (x (^) i +1, m(xi +1)+c). Calculating the distance between the true point, and the two alternative pixel positions available gives:

d 1 = y - y (^) i = m * (x+1)+b-y (^) i d 2 = y (^) i + 1 - y = y (^) i + 1 โ€“ m ( xi + 1 ) - b

Let us magically define a decision function p, to determine which distance is closer to the true point. By taking the difference between the distances, the decision function will be positive if d1 is larger, and negative otherwise. A positive scaling factor is added to ensure that no division is necessary, and only integer math need be used.

pi = dx (d 1 -d 2 ) pi = dx (2m * (xi +1) + 2b โ€“ 2y (^) i -1 ) pi = 2 dy (xi+1) โ€“2 dx y (^) i + dx (2b-1 ) ------------------ (i) pi = 2 dy xi โ€“ 2 dx y (^) i + k ------------------ (ii) where k=2 dy + dx (2b-1)

Then we can calculate pi+1 in terms of pi without any xi , y (^) i or k.

pi+1 = 2 dy xi+1 โ€“ 2 dx y (^) i+1 + k pi+1 = 2 dy (xi + 1) - 2 dx y (^) i+1 + k since xi+1 = x (^) i + 1 pi+1 = 2 dy xi + 2 dy- 2 dx y (^) i+1 + k ------------------ (iii) Now subtracting (ii) from (iii), we get pi+1 - pi = 2 dy - 2 dx (y (^) i+1 - y (^) i ) pi+1 = pi + 2 dy - 2 dx (y (^) i+1 - y (^) i ) If the next point is: (xi +1,y (^) i ) then

d 1 <d 2 => d 1 -d 2 < => pi < => p (^) i+1= p (^) i + 2 dy

If the next point is: (xi +1,y (^) i +1) then

d 1 >d 2 => d 1 -d 2 > => pi > => p (^) i+1= p (^) i + 2 dy - 2 dx

The p (^) i is our decision variable, and calculated using integer arithmetic from pre-computed constants and its previous value. Now a question is remaining how to calculate initial value of pi. For that use equation (i) and put values (x 1 , y 1 )

pi = 2 dy (x 1 +1) โ€“ 2 dx y (^) i + dx (2b-1 ) where b = y โ€“ m x implies that

docsity.com

pi = 2 dy x 1 +2 dy โ€“ 2 dx y (^) i + dx ( 2 (y 1 โ€“ mx 1 ) -1 ) pi = 2 dy x 1 +2 dy โ€“ 2 dx y (^) i + 2 dx y 1 โ€“ 2 dy x 1 - dx pi = 2 dy x 1 +2 dy โ€“ 2 dx y (^) i + 2 dx y1 โ€“ 2 dy x1 - dx

there are certain figures will cancel each other shown in same different colour

p (^) i = 2 dy - dx

Thus Bresenham's line drawing algorithm is as follows: dx = x 2 -x 1 dy = y 2 -y (^1) p = 2dy-dx c1 = 2dy c2 = 2(dy-dx) x = x (^1) y = y (^1) plot (x,y,colour) while (x < x 2 ) x++; if (p < 0) p = p + c 1 else p = p + c 2 y++ plot (x,y,colour) Again, this algorithm can be easily generalized to other arrangements of the end points of the line segment, and for different ranges of the slope of the line.

Improving performance

Several techniques can be used to improve the performance of line-drawing procedures. These are important because line drawing is one of the fundamental primitives used by most of the other rendering applications. An improvement in the speed of line-drawing will result in an overall improvement of most graphical applications.

Removing procedure calls using macros or inline code can produce improvements. Unrolling loops also may produce longer pieces of code, but these may run faster.

The use of separate x and y coordinates can be discarded in favour of direct frame buffer addressing. Most algorithms can be adapted to calculate only the initial frame buffer address corresponding to the starting point and to replaced:

X++ with Addr++ Y++ with Addr+=XResolution

Fixed point representation allows a method for performing calculations using only integer arithmetic, but still obtaining the accuracy of floating point values. In fixed point, the fraction part of a value is stored separately, in another integer:

M = Mint.Mfrac

docsity.com