Date post: | 22-Feb-2018 |
Category: |
Documents |
Upload: | zeeshan-bhatti |
View: | 223 times |
Download: | 0 times |
of 34
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
1/34
COMPUTER GRAPHICSLINE DRAWING ALGORITHM
Dr. Zeeshan Bhatti
BSIT-PIV
Chapter 2
Institute of Information and Communication TechnologyUniversity of Sindh, Jamshoro BY: ZEESHAN BHATTI 1
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
2/34
POLYNOMIAL ALGORITHM
BY: ZEESHAN BHATTI 2
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
3/34
DDA (DIGITAL DIFFERENCE ANALYSER)FOR LINE
Let endpoints coordinates are
(x0, y0) and (x1, y1) respectively.
Se we have
=
0
0
=
=
=
As already mentioned, in digital systems the coordinates of points arerepresented by pixels positions. The pixels are considered to beplaced at a distance of unity from each other.
BY: ZEESHAN BHATTI 3
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
4/34
DDA FOR LINE
A very simple procedure for scan converting a straight line is given asfallows:
Step 1: Plot the first pixel at (x,y) = (x0, y0)
Step 2: Increment the x and y values by dx and dy respectively to getthe next pixel. Since in digital systems pixel are a unit distance away,we can consider either dx or dy to be unity and compute the other.
Plot pixel at ( x+dx, y+dy).
Step 3: Continue the above step until the final pixel (x1, y1) isreached.
BY: ZEESHAN BHATTI 4
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
5/34
Depending on weather we select dx or dy to be unity , two differentsituations can appear.
If we consider dx = 1, the following relations are used to compute thenext y-value.
yi = mxi + b
yi+1 = mxi+1 + b
=m (xi + x) + b
= yi +mx
If x = 1 , then
yi+1 = yi + m
BY: ZEESHAN BHATTI 5
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
6/34
Similarly, if we consider dy = 1, the following relations are used tocompute the next x-value.
If y = 1, then
Xi+1 = xi +
The above algorithm is called Digital Difference Analyzer (DDA). TheDDA is actually a mechanical device that solves differential equationsby simultaneously incrementing x and y by small steps proportional tothe first derivative of x and y.
In our algorithm when x is incremented by dx, y is incremented by dy.
BY: ZEESHAN BHATTI 6
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
7/34
USING SYMMETRY
BY: ZEESHAN BHATTI 7
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
8/34
For lines with |m| < 1, we are to increment the xvalue, at eachsteps, by unity. For other lines, we increment the y-0values.
If x = 1,
then y = m, where
= m.
If y = 1,
then x =
, where
=
.
BY: ZEESHAN BHATTI 8
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
9/34
DDA ALGORITHM
Digital Differential Analyzer (DDA) algorithm is the simple line generation
algorithm which is explained step by step here.
Step 1 Get the input of two end points (X0,Y0) and (X1,Y1).
Step 2 Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Step 3 Based on the calculated difference in step-2, you need to identify thenumber of steps to put pixel. If dx > dy, then you need more steps in x
coordinate; otherwise in y coordinate.if (dx > dy)
Steps = absolute(dx);else
Steps = absolute(dy);
BY: ZEESHAN BHATTI 9
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
10/34
Step 4 Calculate the increment in x coordinate and y coordinate.Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;
Step 5 Put the pixel by successfully incrementing x and ycoordinates accordingly and complete the drawing of the line.
for(int v=0; v < Steps; v++)
{
x = x + Xincrement;
y = y + Yincrement;
putpixel(x,y);
}
BY: ZEESHAN BHATTI 10
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
11/34
PROBLEM: DRAW A LINE USING DDA ALGORITHM FORTHE POINTS BETWEEN 2,5 AND 6, 9. DETERMINE THE
VALUES OF PIXEL AND PLOT THE GRAPH.
Step 1:
x0 = 2 x1 = 6
y0 = 5 y1 = 9
Step 2:
dx = x1 - x0
dx = 6-2 = 4 dx= 4
dy = y1 - y0
dy = 9 - 5 = 4 dy=4
BY: ZEESHAN BHATTI 11
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
12/34
Step 3:
Because dy is greater than & equal to dx i.e. 4=4, so we use the value of dy
Steps = dySteps = 4
Step 4:
xInc = dx / steps
yInc = dy / steps
xInc = 4 / 4 = 1
yInc = 4 / 4 = 1
Step 5:
x = x + xInc y = y + yInc
2 +1 = 3 5+1 = 6
3 + 1 = 4 6+1 = 7
4+1 = 5 7+ 1 = 8
5 +1 = 6 8+1 = 9
BY: ZEESHAN BHATTI 12
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
13/34
BRESENHAMS LINE GENERATION
The Bresenham algorithm is anotherincremental scan conversion algorithm.The big advantage of this algorithm isthat, it uses only integer calculations.
Moving across the x axis in unitintervals and at each step choosebetween two different y coordinates.
For example, as shown in the followingillustration, from position (2, 3) youneed to choose between (3, 3) and (3,4). You would like the point that iscloser to the original line.
BY: ZEESHAN BHATTI 13
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
14/34
DDA requires one floating point addition per step
We can eliminate all fp through Bresenhams algorithm
Consider only 1 m 0
- Other cases by symmetry
Assume pixel centers are at half integers
If we start at a pixel that has been written, there are only twocandidates for the next pixel to be written into the frame buffer
BY: ZEESHAN BHATTI 14
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
15/34
CANDIDATE PIXELS
BY: ZEESHAN BHATTI 15
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
16/34
DECISION VARIABLE
BY: ZEESHAN BHATTI 16
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
17/34
SOLVING BRESENHAM
Let at any moment the Pixel I(xi, yi) as initial pixel and the next pixel is(xi+1, yi+1).
Since the pixel are at unit distance, therefore xi+1 = xi +1, but thevalue of yi+1 can be P(xi+1, yi) or Q (xi+1, yi+1) depending on thenearest point of interaction denoted by R(xi+1, yi+1 ), which is theactual location of the point on the line.
BY: ZEESHAN BHATTI 17
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
18/34
BRESENHAM LINE DRAWING ALGORITHM
Step 1:Get the values of x0,y0 and x1,y1dx = x
1
x0dy = y1y0
Step 2:
p = 2dydx
E (Q) = 2dy
NE (P) = 2* (dydx)
Step 3: Plot the Pixel at I(x0, y0)
BY: ZEESHAN BHATTI 18
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
19/34
Step 4:
while ( x0 < x1)
if ( p < 0) thenp = p + Ex0 = x0 +1
EndIFElse
p = p + NEx
0
= x0
+1y0 = y0+1
EndElse
PlotPixel (x0, y0)
EndWhile
BY: ZEESHAN BHATTI 19
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
20/34
PROBLEM: SOLVE BRESENHAM LINE DRAWING
ALGORITHM FOR VALUS (2, 3) (8, 7)
Step 1:Get the values of
x0
= 2, y0
=3
X1 = 8 , y1 = 7
dx = x1x0 = 8 - 2 = 6
dy = y1y0 = 7- 3 = 4
Step 2:
p = 2dydx = 2 (4)6 = 2
E (Q) = 2dy = 2(4) = 8
NE (P) = 2* (dydx) = 2(4-6) = -4
Step 3: Plot the Pixel at I(x0, y0) BY: ZEESHAN BHATTI 20
2 3
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
21/34
Step 4:
while ( x0 < x1)
if ( p < 0) thenp = p + Ex0 = x0 +1
EndIFElse
p = p + NEx
0
= x0
+1y0 = y0+1
EndElse
PlotPixel (x0, y0)
EndWhile
2 3
P P < 0
p = p + E
x0 = x0 +1
y0 = y0
P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1
X Y
2 (step 1) 3 (step 2) 4 (step 3)
2+(-4) = -2(step 4)
-2 4 4
-2 + 8 = 6
6 5 5
6 + (-4) = 2
2 6 6
2 +(-4) = -2
-2 7 6
-2 +8 = 66 8 7
6 + (-4) = 2
2 9 8
2 +(-4) = -2
-2 10 8
p = 2
E = 8NE = -4
BY: ZEESHAN BHATTI 21
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
22/34
PROBLEM: SOLVE BRESENHAM LINE DRAWING
ALGORITHM FOR VALUS (10, 16) (18, 20)
Step 1:Get the values of
x0
= 10, y0
=16
X1 = 18 , y1 = 20
dx = x1x0 = 18-10 = 8
dy = y1y0 = 20-16 = 4
Step 2:
p = 2dydx = 2 (4)8 = 0
E (Q) = 2dy = 2(4) = 8
NE (P) = 2* (dydx) = 2(4-8) = -8
Step 3: Plot the Pixel at I(x0, y0) = (10, 16) BY: ZEESHAN BHATTI 22
10 16
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
23/34
Step 4:
while ( x0 < x1)
if ( p < 0) thenp = p + Ex0 = x0 +1
EndIFElse
p = p + NEx0 = x0 +1y0 = y0+1
EndElse
PlotPixel (x0, y0)
EndWhile
p = 0
E = 8
NE = -8
10 16
P P < 0
p = p + E
x0 = x0 +1
y0 = y0
P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1
X Y
BY: ZEESHAN BHATTI 23
10 16
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
24/34
10 16
P P < 0
p = p + E
x0 = x0 +1
y0 = y0
P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1
X Y
0 11 17
0 + -8 = -8
-8 12 17
-8 + 8 = 0
0 13 18
0+ -8 = -8
-8 14 18
-8 + 8 = 0
0 15 19
0+ -8 = -8
-8 16 19
-8 + 8 = 0
0 17 20
0+ -8 = -8
-8 18 20
p = 0
E = 8
NE = -8
BY: ZEESHAN BHATTI 24
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
25/34
POLYNOMIAL METHOD FOR CIRCLES
The polynomial method is the simplest approach.
In polynomial, we solve the equation of a circle for known values on
one of the coordinates, and thus determine the calues of the othercoordinate.
The equation of a circle at centre (xc, yc) and radius r is given as
(x-xc)2 + (y - yc)
2 = r2
Therefore
= 2 2
BY: ZEESHAN BHATTI 25
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
26/34
SCAN CONVERSION - CIRCLES
BY: ZEESHAN BHATTI 26
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
27/34
SCAN CONVERSION - CIRCLES
BY: ZEESHAN BHATTI 27
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
28/34
SCAN CONVERSION - CIRCLES
BY: ZEESHAN BHATTI 28
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
29/34
CIRCLE MIDPOINT (FOR ONE OCTANT)
BY: ZEESHAN BHATTI 29
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
30/34
BRESENHAMS CIRCLE ALGORITHM
For simplicity We Assume that the center of a circle is at the origin, i.e.
xc = yc = 0
So, the equation of circle, i.e. (polynomial equation becomes)
x2 + y2 = r2
BY: ZEESHAN BHATTI 30
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
31/34
ALGORITHM FOR BRESENHAMS CIRCLE
BY: ZEESHAN BHATTI 31
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
32/34
PROBLEM: TRACE BRESENHAMS ALGORITHM
FOR DRAWING A CIRCLE OF RADIUS 6.To solve, first calculate the initial values of p0 of the decisionparameter, and then recalculating the p at each successive iteration.
Step 1:
x=0; // assumedy= radius;r = radius;
p0 = 32r = 3 -12 = -9
circlepoints(x,y);
Step 2:
-9 0 6
P P < 0p = p + 4x + 6
x0 = x0 +1
y0 = y0
P => 0p = p + 4(x-y)+10
x0 = x0 +1
y0 = y0 - 1
X Y
-9 + 4(0)+6 = -3
-3 1 6
-3 + 4(1)+6= 7
7 2 5
7 + 4 (2 5) +10 = 5
5 3 4
5 + 4 ( 3-4) +10 = 11
11 4 3BY: ZEESHAN BHATTI 32
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
33/34
PROBLEM: TRACE BRESENHAMS ALGORITHM
FOR DRAWING A CIRCLE OF RADIUS 11.To solve, first calculate the initial values of p0 of the decisionparameter, and then recalculating the p at each successive iteration.
Step 1:
x=0; // assumedy= radius;r = radius;
p0 = 32r =
circlepoints(x,y);
Step 2:
P P < 0p = p + 4x + 6
x0 = x0 +1
y0 = y0
P => 0p = p + 4(x-y)+10
x0 = x0 +1
y0 = y0 - 1
X Y
BY: ZEESHAN BHATTI 33
7/24/2019 Computer Graphics_Chapter 2 Line Draing Algorithms
34/34
COMPLETING THE CIRCLES BY
MIRRORING THE 8 OCTANTS