Vous êtes sur la page 1sur 80

Output Primitives

Points and Lines


Line Drawing Algorithms
DDA Algorithm
Bresenhams Line Algorithm
Midpoint Circle Algorithm
Midpoint Ellipse Algorithm
Filled Area Primitives
Points and Lines
Point is the fundamental element of picture
representation.
It is the position in the plan defined as either
pair or triplets of number depending upon the
dimension.
Two points represent line or edge and 3 or
more points a polygon.
Curved lines are represented by the short
straight lines.
Line Drawing Algorithms
Slope-Intercept Equation

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

For |m|<1 (|y|< |x|)


Sample line at unit interval in x co-ordinate
yk 1 yk m x xk 1 xk 1
For |m|>1 (|y|> |x|)
Sample line at unit interval in y co-ordinate
1
xk 1 xk y yk 1 yk 1
m
DDA Algorithm (right to left)
Slope yk 1 yk y
m
xk 1 xk x

For |m|<1 (|y|< |x|)


Sample line at unit interval in x co-ordinate
yk 1 yk m x xk 1 xk 1
For |m|>1 (|y|> |x|)
Sample line at unit interval in y co-ordinate
1
xk 1 xk y yk 1 yk 1
m
DDA Algorithm
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
4. If |x| > |y| steps = |x| else steps = |y|
5. Calculate XInc = |x| / steps and YInc = |y| / steps
6. At each xk along the line, starting at k=0, Plot the next pixel at (xk + XInc, yk
+ YInc)
7. Repeat step 6 steps times
Pseudo Code
Void lineDDA(int xa, int ya, int xb, int yb)
{
int dx = xb xa, dy = yb ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;

if( abs (dx) > abs (dy) ) steps = abs (dx);


else steps = abs (dy);
xIncrement = dx / (float) steps;
yIncrement = dy / (float) steps;
setPixel (ROUND (x), ROUND (y));
for (k=0; k<steps; k++){
x += xIncrement;
y += yIncrement;
setPixel (ROUND(x), ROUND(y));
}
}
Use DDA algorithm for rasterizing line (0,0) to
(6,6).
Use DDA algorithm for rasterizing line (0,0) to
(4,6).
Bresenhams Line Algorithm
Uses only incremental integer
calculations
Which pixel to draw ?
(11,11) or (11,12) ?
(51,50) or (51,49) ?
Answered by Bresenham
For |m|<1
Start from left end point (x0,y0) step to each
successive column (x samples) and plot the pixel
whose scan line y value is closest to the line path.
After (xk,yk) the choice could be (xk+1,yk) or
(xk+1,yk+1)
y m( xk 1) b
Then
d1 y yk
m( xk 1) b yk
And
d 2 ( yk 1) y
yk 1 m( xk 1) b
Difference between separations

d1 d 2 2m( xk 1) 2 yk 2b 1
Constant=2y + x(2b-1) Which is
independent of pixel position

Defining decision parameter


pk x(d1 d 2 ) [1]
2y.xk 2x. yk c
Sign of pk is same as that of d1-d2 for x>0 (left to right sampling)

pk 1 2y.xk 1 2x. yk 1 c c eliminated here

pk 1 pk 2y( xk 1 xk ) 2x( yk 1 yk ) because xk+1 = xk + 1

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

Plot (x0,y0) = (20,10)


Use Bresenhams algorithm for rasterizing the
line (5,5) to (13,9)
Digitize a line with endpoints (15,18) and
(10,15) using BLA.
Digitize a line with endpoints (15,15) and
(10,18) using BLA.
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, 2x and 2 x- 2y, and obtain p0 = 2x y
4. At each xk along the line, starting at k=0, perform the following test:
If pk<0, the next point plot is (xk, yk+1) and
Pk+1 = pk + 2x
Otherwise, the next point to plot is (xk + 1, yk+1) and
Pk+1 = pk + 2x - 2y
5. Repeat step 4 x times
Use BLA algorithm for rasterizing line (0,0) to
(4,6).
Circle-Generating Algorithms (Basic
Foundations)
Circle Equation:

( x xc ) ( y yc ) r
2 2 2

Points along circumference could be calculated by stepping along x-


axis:

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

Any point (x,y) satisfies following conditions

0, if ( x, y) is inside the circle boundary



f circle ( x, y) 0, if ( x, y) is on the circle boundary
0,
if ( x, y) is outside the circle boundary
Decision parameter is the circle function; evaluated as:

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

At start position (x0,y0) = (0,r)


2x0 = 0 and 2y0 = 2r
Initial decision parameter
1
p0 f circle (1, r )
1 22 2
1 (r ) r
5 2
r
4
For r specified as an integer, round p0 to
P0 = 1-r
(because all increments are integers)
Algorithm
1. Input radius r and circle center (xc, yc) and obtain the first point on the circumference of
a circle centered on the origin as
(x0,y0) = (0,r)
2. Calculate the initial value of the decision parameter as
P0 = 5/4 r
3. At each xk position, starting at k = 0, perform the following test:
If pk < 0, the next point along the circle centered on (0,0) is (xk+1,yk) and
Pk+1 = pk + 2xk+1 + 1
Otherwise, the next point along the circle is (xk+1,yK-1) and
Pk+1 = pk + 2xk+1 + 1 -2yk+1
Where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk-2
4. Determine the symmetry points in the other seven octants.
5. Move each calculated pixel position (x,y) onto the circular path
centered on (xc,yc) and plot the co-ordinate values:
x = x + xc, y = y+yc
6. Repeat steps 3 through 5 until x y
Given radius =10 use mid-point circle drawing
algorithm to determine the pixel in the first
quadrant.
Solution:
P = 1-r = -9
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

At boundary region (slope = -1): 2ry x 2rx y


2 2

Start from (0,ry), take x samples to boundary Slope=-1

between 1 and 2

Switch to sample y from boundary between 1


and 2
(i.e whenever 2ry 2 x 2rx 2 y )
For increment calculation;
In the region 1 Initially:
1
p1k f ellipse( xk 1, yk )
2
1
2r x 0
y
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

For next sample


1
p 2 k 1 f ellipse( xk 1 , yk 1 1)
2
2
2 1
ry xk 1 rx2 yk 1 1 rx ry
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

2 2 p20 can be done by selecting


pixel positions in counter
Initially 1
clockwise order starting at (rx,0)
p 20 f ellipse x0 , y0 1 and unit samples to positive y
2 direction until the boundary
2
1 between two regions
p 20 ry2 x0 rx2 ( y0 1) 2 rx2 ry2
2
Algorithm
1. Input rx,ry, and the ellipse center(xc,yc) and obtain the first point on an ellipse centered on the origin as
(x0,y0) = (0,ry)
2. Calculate the initial value of the decision parameter in region 1 as
1
p10 ry2 rx2 ry rx2
4
3. At each xk position in region 1, starting at k = 0, perform the following test: If p1 k < 0, the next point along the
ellipse centered on (0,0) is (xk+1,yk) and

p1k 1 p1k 2ry2 xk 1 ry2


Otherwise, the next point along the ellipse is (xk+1,yk-1) and

p1k 1 p1k 2ry2 xk 1 2rx2 yk 1 ry2


With

2ry2 xk 1 2ry2 xk 2ry2 , 2rx2 yk 1 2rx2 yk 2rx2


and continue until
2ry2 x 2rx2 y
4. Calculate the initial value of decision parameter in region 2 using the last point (x 0,y0) calculated in region 1 as
2
1
p 20 r x0 rx2 ( y0 1) 2 rx2 ry2
2
y
2
5. At each yk position in region 2, starting at k = 0, perform the following test: If p2k>0, the next point along the
ellipse centered on (0,0) is (xk,yk-1) and

p 2 k 1 p 2 k 2rx2 yk 1 rx2
Otherwise the next point along the ellipse is (xk+1,yk-1) and

p2k 1 p2k 2ry2 xk 1 2rx2 yk 1 rx2


Using the same incremental calculations for x and y as in region 1.
6. Determine the symmetry points in the other three quadrants.
7. Move each calculated pixel position (x,y) onto the elliptical path centered on (xc,yc) and plot the co-ordinate
values:
X = x + xc, y = y+ yc
8. Repeat the steps for region 1 until

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

Vous aimerez peut-être aussi