Transcript Document

Computer Graphics
Scan Conversion Lines
Faculty of Physical and Basic Education Computer
Dep.
2012-2013
Lecturer:
Azhee W. MD.
Line-Drawing Algorithms
 Line Equations
 Digital Differential analyzer(DDA) algorithm
 Bresenham’s Line Algorithm
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
2
Line Equations
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
3
Line Equations Example
Q) Find the equation of the line that passes through the points (1, 2)
and (-2, 4).
 if I have two points on a straight line, I can always find the slope
 Now I have the slope and two points. I know I can find the equation
(by solving first for "b“)
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
4
Line Equations (2)
 For any x interval δx along a line, we can compute y
interval δy
δy = m . δx
 Or compute x interval δx from y interval δy
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
5
DDA algorithm
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
6
DDA algorithm
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
7
Algorithm DDA
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
8
Algorithm DDA(2)
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
9
Example 1
 Consider the line from (0,0)to (6,6), use DDA to rasterize the line.
 Let us perform some initial calculations.
Initially x1=0 y1=0 x2=6 y2=6
 So our first step would be to evaluate the length
length =abs(x2-x1) 6-0= 6
length =abs(y2-y1) 6-0= 6
 Had (x2-x1) been equal (y2-y1):
dx=x2-x1/length 1
and dy=y2-y1/length 1
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
10
Example 1
 X=x1+0.5*sign(dx) 0+0.5*sign(1) x=0.5 sign(1)return 1
similar to y0.5
i
plot
1
x
y
0.5
0.5
2
(0,0)
1.5
1.5
3
(1,1)
2.5
2.5
4
(2,2)
3.5
3.5
5
(3,3)
4.5
4.5
6
(4,4)
5.5
5.5
7
(5,5)
6.5
6.5
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
11
Example 2
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
12
Example 2
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
13
Example 4
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
14
Home work
 Draw the line from (0,0) to (-8,-4) by using DDA algorithm?
 Draw the line from (0,-3) to ( 8,4) by using DDA algorithm?
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
15
Bersenham’s Line Algorithm
 Bersenham’s line algorithm is an efficient method for scan converting
straight lines , in that it use only integer addition ,subtraction and
multiplication by 2 and floating point
operations.
 The lines drawn are of superior quality as compared to ADD method
A2
Ai
Bi
A1
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
16
Bersenham’s Line Algorithm(2)
 we will keep on talking about Ai and Bi throughout the algorithm ,
Bersenham’s algorithm identifies the decision or test variable:
di=Bi-Ai
 When di<0 the closet pixel in the raster will be the pixel below the true line.
 Conversely ,when di>=0 the pixel above the true line is closet.
 Initially set
di=2dy-dx
dx=x2-x1
dy=y2-y1
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
17
Bersenham’s Line Algorithm(3)
 thereafter if di>=0
the x and y are incremented
Xi+1 =Xi+1
Yi+1 =Yi+1
di+1=di+2(dy-dx)
And
 if di<0 then only x incremented
Xi+1=Xi+1
di+1=di+2dy
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
18
Example one
 Q) Indicate which raster locations would be chosen by Bersenham’s algorithm when
scan converting a line from screen co-ordinates (1,1) to (8,5).
Solution
 First the straight values (initial values) must be found
dx=x2-x1
dy= y2-y1
8-1= 7
5-1= 4
Therefore
d=2dy-dx
2*4-7= 1 initial values
 Apply the above procedure again and again would get us the required solution
X
Y
d
1
1
1
2
2
-5
3
2
3
4
3
-3
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
19
Example 2
 Q) Indicate which raster locations would be chosen by Bersenham’s algorithm when
scan converting a line from screen co-ordinates (-2,3) to (8,10).
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
20
Homework

Draw the following lines using DDA
 (from left to right):
(-1, 2) and (7, 8)
(1, -3) and (6, 5)
 (from right to left)
(6, 2) and (-4, -3)
(9, 4) and (2, -5)
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
21
Homework 2
 Q) Indicate which raster locations would be chosen by Bersenham’s algorithm when
scan converting a line from screen co-ordinates (0,-3) to (8,4).
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
22
References
 Donald Hearn, M. Pauline Baker,
Computer Graphics with OpenGL, 3rd
edition, Prentice Hall, 2004
 Chapter 3
University of sulaimanyiah - Faculty of Physical and Basic Education - Computer Dep. 2012-2013
23