Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
y m.x b
Slope
y2 y1
m
x2 x1
Intercept
b y1 m.x1
Interval Calculation
y
y m.x x
m
Line Drawing Algorithm
DDA Algorithm
Digital Differential Analyzer
Sample the line at unit intervals in one coordinate
Determine the corresponding integer values
nearest the line path in another co-ordinate
DDA Algorithm (left to right)
Slope yk 1 yk y
m
xk 1 xk x
d1 d 2 2m( xk 1) 2 yk 2b 1
Constant=2y + x(2b-1) Which is
independent of pixel position
pk 1 pk 2y 2x( yk 1 yk )
yk+1-yk = 0 if pk < 0
For Recursive calculation, initially
yk+1-yk = 1 if pk 0
p0 2y x Substitute b = y0 m.x0
and m = y/x in [1]
Algorithm Steps (|m|<1)
1. Input the two line endpoints and store the left endpoint in (x0,y0)
2. Plot first point (x0,y0)
3. Calculate constants x, y, 2y and 2 y- 2x, and obtain p0 = 2y x
4. At each xk along the line, starting at k=0, perform the following test:
If pk<0, the next point plot is (xk+1,yk) and
Pk+1 = pk + 2y
Otherwise, the next point to plot is (xk + 1, yk+1) and
Pk+1 = pk + 2y - 2x
5. Repeat step 4 x times
Whats the advantage?
Answer: involves only the calculation of constants x, y,
2y and 2y- 2x once and integer addition and
subtraction in each steps
Example
Endpoints (20,10) and (30,18)
Slope m = 0.8
x = 10, y = 8
P0 = 2y - x = 6
2y = 16, 2y-2x = -4
( x xc ) ( y yc ) r
2 2 2
y yc r 2 ( xc x)2
Problem (in above method)
Computational complexity
Spacing:
Non-uniform spacing of
plotted pixels
Adjustments (To fix problems)
Spacing problem (2 ways):
Interchange the role of x and y whenever the absolute value of the slope of
the circle tangent > 1
Use polar co-ordinate:
x xc r cos
y yc r sin
Equally spaced points are plotted along the circumference with fixed angular
step size.
step size chosen for depends on the application and display device.
Computation Problem:
Use symmetry of circle; i.e calculate for one octant and use symmetry for
others.
Circle Symmetry
Bresenhams Algorithm Could Be Adapted ??
Yes
How ?
Setting decision parameter for finding the closest
pixel to the circumference
And what to do For Non-linear equation of
circle ?
Comparison of squares of the pixel separation
distance avoids square root calculations
Midpoint Circle Algorithm
Circle function defined as:
f circle ( x, y ) x 2 y 2 r 2
1
pk f circle ( xk 1, yk )
2
1
( xk 1) 2 ( yk ) 2 r 2
2
1
pk 1 f circle ( xk 1 1, yk 1 )
2
1
[( xk 1) 1]2 ( yk 1 ) 2 r 2
2
pk 1 pk 2( xk 1) ( yk 1 yk ) ( yk 1 yk ) 1
2 2
yk+1 = yk if pk<0
yk+1 = yk-1 otherwise
Thus
Pk+1 = Pk + 2xk+1+1 if pk<0
Pk+1 = Pk + 2xk+1+1-2yk+1 otherwise
Also incremental evaluation of 2xk+1 and 2yk+1
2xk+1 = 2xk + 2
2yk+1 = 2yk 2 if pk >0
(x , y )=(0,10)
0 0
2x =0 0
2y =20 0
Digitize the circle (x-2)^2+(y-3)^2=25 in first
quadrant.
Solution:
P0=(1-r)= -4
(x , y )=(0,5)
0 0
2x =0 0
2y =100
Ellipse Generating Algorithms
Equation of ellipse:
d1 d2 constant
F1(x1,y1), F2(x2,y2)
( x x1 ) 2 ( y y1 ) 2 ( x x2 ) 2 ( y y2 ) 2 constant
General Equation
Ax 2 By 2 Cxy Dx Ey F 0
Simplified Form
2
y yc
2
x xc
1
r
x
r y
In polar co-ordinate
x xc rx cos
y yc r y sin
Ellipse function
f ellipse( x, y) ry x 2 rx y 2 rx ry
2 2 2 2
0,
if ( x, y) is inside the ellipse boundary
f ellipse( x, y) 0, if ( x, y) is on the ellipse boundary
0, if ( x, y) is outside the ellipse boundary
2
From ellipse tangent slope: dy
2ry x
2
dx 2rx y
between 1 and 2
ry ( xk 1) rx ( yk ) 2 rx ry
2r y 2r r
2 2 2 2 2
2 2
2
x x y
For next sample Incrementally:
1
p1k 1 f ellipse( xk 1 1, yk 1 ) Update x by adding
2 2ry2 to first equation
ry ( xk 1) 1 rx2 ( yk 1 ) 2 rx ry
2 2 1 2 2
and update y by
2
2
1
2 subtracting 2rx2 to
2 1
p1k 1 p1k 2ry ( xk 1) ry rx yk 1 yk
2 2
second equation
2 2
Thus increment
2ry 2 xk 1 ry2 , if p1k 0
increment 2
2 ry x k 1 ry
2
2 rx
2
yk 1 , if p1k 0
Initial value
1
p10 f ellipse1, ry
2
2
1
ry2 rx2 ry rx2 ry2 (0,ry)
2
1 2
p10 ry rx ry rx
2 2
4
In the region 2
1
p 2 k f ellipse( xk , yk 1)
2
1 2
ry ( xk ) rx ( yk 1) 2 rx ry
2 2 2 2
2
2
2
1 1
2
For simplification calculation of
p 2 k 1 p 2 k 2rx ( yk 1) rx ry xk 1 xk
2 2
p 2 k 1 p 2 k 2rx2 yk 1 rx2
Otherwise the next point along the ellipse is (xk+1,yk-1) and
2ry2 x 2rx2 y
Digitize the ellipse with parameter rx =8 and
ry =6 in first quadrant.
Solution:
Filled Area Primitives
Two basic approaches for area filling:
By determining overlap intervals for scan lines
Start from given interior position and filling
outwards
Scan Line Polygon Filled Algorithm
Intersection points of scan line with the
polygon edge are calculated.
Points are sorted from left to right.
Corresponding frame buffer position between
each intersection pair are set by specified
color.
A scan line pass through vertex, intersect two
polygon edges.
Scan line y
Intersect even number of edges
2 pairs of intersection points correctly find the
interior span
Scan line y
Intersect an odd number of edges(5)
Must count the vertex intersection as only one
point
How to distinguish these cases?
Scan line y
Intersecting edges are on the opposite side
Scan line y
Intersecting edges are on the same side.
By tracing around the boundary,
If the endpoint y values of two consecutive edges
monotonically increases or decreases count
middle vertex as single
Implementation
In determining edge intersection, we can set
up incremental set up calculation using fact
that slope of edge is constant.
Inside Outside test
Odd Even rule:
Odd edge means interior
Non zero winding number rule
Non zero winding number means interior
Scan line fill for Curved Area
Require more works than polygon filling due
to non-linear boundary.
We calculate the scan line intersection and fill
all the interior points.
Symmetries between the quadrant can be
used to reduce the calculation.
Boundary fill algorithm
Fill the region starting from the interior point
until the appropriate color boundary is
reached.
Two ways:
4 connected
8 connected
4 connected
8 connected
Using 4 connected flood fill algorithm