CGIP - Mod1 - PPT - 3 (Line, Circ Algo)
CGIP - Mod1 - PPT - 3 (Line, Circ Algo)
CS 010 703
CRT
(maxx,maxy)
Cartesian equation:
y = mx + b
y2
where y1
m – slope x1 x2
b – y-intercept
y 2− y 1 Δy
m= =
x 2 − x 1 Δx
Slope
+ve -ve
if |m| = 1
= 45° 45°
45°
if |m| 1
-45° < < 45°
°
°
if |m| 1
45° < < 90° or
°
-90° < < -45° outside
°
11
Straight line segment with 5 sampling positions
along the x axis and y axis
y2
y1
x1 x2
The Cartesian slope intercept equation for straight line is
y = m x + b (1)
(x1,y1) & (x2,y2) be the two endpoints of the line, then
y1 = mx1 + b (2) y2 = mx2 + b (3)
Eqn(3) – (2) ( y2 – y1) = m (x2 – x1)
m = (y2-y1)/(x2-x1)
m = ∆y/ ∆x
For any given x interval (∆x) along a line, compute the
corresponding y interval (∆y) as ∆y = m ∆x
Similarly the x interval (∆x) is obtained as ∆x = ∆y /m
The above equations form the basis for determining deflection
voltages in analog devices.
Digital Differential Analyzer Algorithm(DDA)
Scan conversion algorithm based on calculating either ∆x or ∆y.
Sample the line at unit intervals in one coordinate and determine
the corresponding integer values nearest the line path for the
other coordinate. (Left to right)
Case 1:
Line with positive slope less than or equal to 1 (m <=1),
we sample at unit x intervals (∆x =1) and determine the successive
y value as
∆y = m ∆x
yk+1- yk = m ∆x
yk+1 = yk + m yk+1- yk = m (∆x =1)
k takes integer values starting from 1 and increases by 1 until the
final point is reached.
m can be any number between 0 and 1.
Calculated y values must be rounded off to the nearest integer
Case 2:
For lines with positive slope greater than 1 (m>1) , sample
at unit y intervals (∆y = 1) and calculate each successive x value as
xk+1= xk + 1/m ∆y = m ∆x
∆x = ∆y/m
xk+1- xk = ∆y/m
xk+1- xk = 1/m (∆y =1)
Equations used when lines are processed from (right to left)
1. (m <= 1) ∆x = -1 & yk+1 = yk – m
2. (m > 1) ∆y = -1 & xk+1 = xk – 1/m
|m| = 1
y=x
m=1
c=0 y
x y 8
0 0 7
1 1 6
2 2 5
4
3 3
3
4 4
2
5 5
1
6 6
0
7 7 0 1 2 3 4 5 6 7 8 x
8 8
16
|m | 1
y=½x+1
m=½
y
c=1
8
x y round(y)
7
0 1 1 6
1 1.5 2 5
2 2 2 4
3 2.5 3 3
4 3 3 2
5 3.5 4 1
6 4 4 0
7 4.5 5 0 1 2 3 4 5 6 7 8 x
8 5 5 17
The DDA Method
Uses differential equation of the line : m
If slope |m| 1 then increment x in steps of 1 pixel and
find corresponding y-values.
If slope |m| 1 then increment y in steps of 1 pixel and
find corresponding x-values.
19
step through in x step through in y
The DDA Approach
Desired line
(xi+1,round(yi+m))
(xi,yi) (xi+1,yi+m)
(xi,round(yi))
20
Steps for DDA Algorithm
1. Accept as input the two end point pixel positions.
P1(xa , ya) P2 (xb , yb)
2. Horizontal and vertical differences between the endpoint
positions are computed & assigned to two parameters
namely dx and dy.
dx= xb – xa, dy = yb – ya,
3. If abs(dx) >abs(dy) ,then steps =abs( dx)
else steps =abs( dy)
4. xincr = dx/steps yincr = dy/steps
(if steps = abs(dx) , xincr = 1, yincr = dy/dx = m)
(if steps = abs(dy) , yincr = 1, xincr = dx/dy = 1/m)
5. Assign x = xa and y = ya
6. Plot (x, y)
7. x= x + xincr & y= y + yincr
8. Loop through 6 and 7 steps times.
DDA Algorithm
Advantages
• It is simple and easy to implement algorithm
• DDA algorithm is a faster method for calculating pixel positions
than the direct use of line equation y= mx+b.
• It eliminates multiplication by making use of raster characteristics,
incremental operations
Disadvantages
• DDA algorithm runs slowly because it requires real arithmetic
(floating point operations) and rounding operations.
• Rounding off errors
• As it is orientation dependent, so it has poor endpoint accuracy.
• Due to the limited precision in the floating point representation it
produces cumulative error.
void LineDDA(int x0, int y0, int x1, int y1)
{
int dx = x1 – x0, dy = y1 – y0, steps;
double x = x0;
double y = y0;
setPixel(round(x), round(y),1);
Advantages
-uses only integer calculations
- more efficient
Jack Bresenham worked
for 27 years at IBM
before entering
academia. Bresenham
developed his famous
algorithms at IBM in the
early 1960s
Bresenham’s Line Algorithm
A
B
Start position
27
The Big Idea
Note: Move across the x axis in unit intervals and at each
step choose between two different y coordinates
2 3 4 5
Deriving Bresenham’s Line Algorithm
At sample position xk+1 the vertical yk+1
separations from the mathematical line dupper
are labelled dupper and dlower y
dlower
The y coordinate on the mathematical yk
line at xk+1 is:
y m( xk 1) b
So, dupper and dlower are given as: xk+1
d lower y yk
m( xk 1) b yk
and:
d upper ( yk 1) y
yk 1 m( xk 1) b
Deriving Bresenham’s Line Algorithm
Decision is based on difference between the two pixel positions:
dlower – dupper = [m (xk + 1) + b – yk] – [yk + 1 – m (xk + 1) – b]
= m xk + m + b – y k – y k – 1 + m x k + m + b
= 2m xk + 2m – 2 yk + 2 b– 1
= 2m (xk + 1) – 2 yk + 2 b – 1
(m = ∆y/∆x, where ∆x and ∆y are differences between the end-points )
Decision parameter at step k , pk is:
pk = Δx (dlower – dupper )
= Δx [2m (xk + 1) – 2 yk + 2 b – 1]
= Δx [2(Δy/Δx) (xk + 1) – 2 yk + 2 b – 1]
= 2Δy (xk + 1) – 2Δx yk + 2Δx b – Δx
= 2Δy xk – 2Δx yk + [2Δy + Δx (2 b – 1)]
= 2Δy xk – 2Δx yk + c
(where c is a constant , c = 2Δy + Δx (2 b – 1) )
Deriving Bresenham’s Line Algorithm
(Sign of decision parameter pk is same as that of (dlower – dupper ))
if pk is negative, then choose lower pixel, (xk +1, yk )
if pk is positive ,then choose upper pixel, (xk +1, yk +1)
Similarly
if ( p 0), ( y y 1)
k k 1 k
p p 2 y 2 x ( y y )
k 1 k k 1 k
p 2 y 2 x ( y 1 y )
k k k
p 2 y 2 x
k
p p 2 y 2 x , ( p 0)
k 1 k k
Deriving Bresenham’s Line Algorithm
The first decision parameter p0 is evaluated at (x0, y0) as:
p k x ( d lower d upper )
x ( 2 m ( x k 1) 2b 2 y k 1)
p0 x ( 2 m ( x 0 1) 2b 2 y 0 1)
x ( 2 mx 0 2 m 2b 2 y 0 1)
x ( 2 ( mx 0 b y 0 ) 2 m 1)
( y mx b , so ( mx 0 b y 0 ) 0 , m y / x )
( 2 m 1) x 2 mx x
2 ( y / x ).x x
p0 2 y x
Bresenham’s Line Algorithm - (for |m| < 1)
1. Input the two line end-points (x0, y0) and (x1, y1) , storing the left
end-point in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants
Δx = (x1 – x0 ), Δy =( y1 - y0), 2Δy, and (2Δy - 2Δx)
and get the first value for the decision parameter as:
p0 2y x
0 6 (21,11) 18
1 2 (22,12) 17
2 -2 (23,12) 16
3 14 (24,13) 15
4 10 (25,14) 14
5 6 (26,15) 13
6 2 (27,16) 12
7 -2 (28,16) 11
8 14 (29,17) 10
9 10 (30,18) 20 21 22 23 24 25 26 27 28 29 30 31 32 36
void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1
{
int dx = abs(x1 – x0), dy = abs(y1 – y0);
int p = 2 * dy – dx;
int x, y, xend;
if (x0 > x1) { // determines which point to use as start, which as end
x = x1;
y = y1;
xend = x0;
}
else {
x = x0;
y = y0;
xend = x1;
}
setPixel(x,y,1);
while (x < xend) {
x++;
if (p < 0) p = p+ 2* dy;
else {
y++;
p= p + 2 * (dy-dx);
}
setPixel(x, y,1);
} 37
}
Bresenham’s Line - Special Cases
Bresenhams algorithm is generalised to lines with arbitrary slopes by
considering the symmetry between the various octants and quadrants of
xy plane of the coordinate system.
For line with +ve slope (m > 1), we interchange the roles of x and y
direction i.e., step along y directions in unit steps and calculate
successive x values nearest the line path.
For +ve slope to plot from right to left, both x and y decreases in steps
For line with –ve slopes the procedures are similar except that now,
one coordinate decreases as the other increases.
Special cases can be handled separately
– Horizontal lines (y = 0)
– Vertical lines (x = 0)
– Diagonal lines (|x| = |y|)
Points loaded directly into the frame-buffer without processing them
38
using the line-plotting algorithms.
Bresenham’s Line Algorithm
Advantages
• Only involves integer calculations.
• It outperforms DDA in terms of speed.
• Using a shift register, we may implement multiplication by two in
hardware.
• Involves less expensive operations such as addition and
subtraction.
• More accurate.
Disadvantages
• Though it improves the accuracy of generated points but still the
resulting line is not smooth.
• This algorithm is for the basic line drawing. It cannot handle
diminishing jaggies.
Circle Generating Algorithms
Polar form
x = rCosy = rSin (r = radius of circle)
= 0°
while ( < 360°) y
x = rCos
y = rSin
setPixel(x,y) P=(rCos, rSin)
= + 1°
end while r rSin
x
rCos
Disadvantages
To find a complete circle varies
from 0° to 360°
The calculation of trigonometric
functions is very slow
40
Cartesian Form
Use Pythagoras theorem
x 2 + y 2 = r2
y
r y
P x, r 2 x 2
x r y
Disadvantages x x
To find a complete circle
varies from 0° to 360°
The calculation of square and
square roots is very slow
41
Circle algorithms
Step through x-axis to determine y-values
Disadvantages:
– Not all pixel filled in
42
– Square root function is very slow
Circle Symmetry
Use 8-fold symmetry and only compute pixel positions
for the 45° sector.
(-x, y) (x, y)
p1 p3
yk du
yk - 1 dl
p2
xk xk+ 1
pk+1 = pk + 4 xk + 6 ( pk < 0 )
pk +1 = pk + 4 xk – 4 yk + 10 ( pk ≥ 0 )
p0 = 3 – 2r
Bresenham’s Circle Algorithm
1. Initial values:- point (0, r) move circle origin at (0,0) by
x = x – xc and y = y – yc
x0 = 0, y0 = r
2. Initial decision parameter, p0 = 3 – 2r
3. At each xk position, starting at k = 0, along circle centered on (0,
0) perform the following test:
if ( pk < 0), the next point is upper pixel (xk + 1, yk ) and
pk+1 = pk + 4xk + 6
else if (pk ≥ 0 ), the next point is lower pixel (xk+1, yk -1) and
pk+1 = pk + 4(xk – yk) + 10
4. Determine symmetry points in the other 7 octants
5. Move pixel positions (x, y) onto the circular path centered on
(xc , yc) and plot the coordinates: x = x + xc , y = y + yc
6. Repeat steps 3 to 5 until (x ≥ y) 50
Example- Bresenham’s Circle
r = 10
p0 = 3 – 2r = -17
Initial point (x0, y0) = (0, 10)
10
k pk xk+1, yk+1 9
8
0 -17 (1, 10) 7
1 -11 (2, 10) 6
2 -1 (3, 10) 5
4
3 13 (4, 9) 3
4 -5 (5, 9) 2
5 17 (6, 8) 1
0
6 11 (7,7) 0 1 2 3 4 5 6 7 8 9 10
51
Bresenham’s Circle Algorithm
Advantages:
• Due to its integer arithmetic - addition, subtraction and
multiplication, it is faster
• There is no need for squares, square roots and trigonometric
functions
• This algorithm is more efficient and accurate than any other
circle drawing algorithm as it avoids the round off function
Disadvantages:
• No accuracy of the generating points.
• This algorithm cannot generate complex and highly
graphical
images.
• There is no significant enhancement with respect to 52
Mid-Point Circle Algorithm
Similarly to the case with lines, this is an
incremental algorithm for drawing
circles – the Mid-point circle algorithm
In the mid-point circle algorithm we use
eight-way symmetry
So only calculate the points for the top
right eighth of a circle, and then use
symmetry to plot the rest of the points in The mid-point circle
algorithm was developed
7 other octants. by Jack Bresenham.
Mid-Point Circle Algorithm (cont…)
Assume that we have just
plotted point (xk, yk)
The next point is a
choice between (xk+1, yk) and
(xk+1, yk-1)
Choose the point that is nearest
to the actual circle
Mid-Point Circle Algorithm (cont…)
The equation of circle evaluates as follows:
2 2 2
f circ ( x, y ) x y r
pk f circ ( xk 1, yk 1 )
2
( xk 1) 2 ( yk 1 ) 2 r 2
2
If pk < 0, midpoint is inside the circle , yk is closer to circle
If pk > 0 the midpoint is outside and yk-1 is closer
To do calculations incrementally consider:
p
k 1
f x
circ k 1
1, y
k 1
1
2
[( x 1) 1]2 ( y 1 )2 r 2
k k 1 2
Mid-Point Circle Algorithm (cont…)
p p (( x 1) 1) 2 ( y 1 )2 r 2
k 1 k k k 1 2
[( x 1) 2 ( y 1 ) 2 r 2 ]
k k 2
( x 1) 2 2( x 1) 1 ( y )2 y 1
k k k 1 k 1 4
2
( x 1) y 2 y 1
k k k 4
2( x 1) [( y ) 2 y 2 ] [( y ) y ] 1
k k 1 k k 1 k
now
2 2
pk 1 pk 2( xk 1) ( yk 1 yk ) ( yk 1 yk ) 1
xk 1 xk 1
when( pk 0), y k 1 yk (upperpixel )
pk 1 pk 2( xk 1) ( yk2 yk2 ) ( y k yk ) 1
pk 1 pk 2( xk 1) 0 0 1
pk 2 xk 1 1
Mid-Point Circle Algorithm (cont…)
when( pk 0), yk 1 yk 1(lowerpixel)
pk 1 pk 2( xk 1) [( yk 1) 2 yk2 ][( yk 1) yk ]1
[(a 2 b 2 ) (a b)(a b)]also[((a b)(a b)) (a b)) (a b)(a b 1)]
[a ( yk 1),b yk ]
pk 2( xk 1) [( yk 1) yk ][( yk 1) yk ][( yk 1) yk ]1
pk 2( xk 1) [( yk 1) yk 1][( yk 1) yk ]1
pk 2( xk 1) [(2 yk 2)(1)]1
pk 2( xk 1) 2( yk 1) 1
pk 2 xk 1 2 yk 1 1
where 2 xk 1 2( xk 1)
2 xk 2
2 y k 1 2( y k 1)
2 yk 2
Mid-Point Circle Algorithm (cont…)
Initial point (x0, y0) = (0, r)
initial decision parameter p0 is given as:
p0 f circ (1, r 1 )
2
1 (r 1 )2 r 2
2
5 r
4
(if r is integer , round p0 = 5/4 – r to integer)
Then if pk < 0 then next decision variable pk+1 is:
pk 1 pk 2 xk 1 1
1 -6 (2, 10) 4 20 6
5
2 -1 (3, 10) 6 20 4
3 6 (4, 9) 8 18 3
4 -3 (5, 9) 10 18 2
1
5 8 (6, 8) 12 16 0
6 5 (7, 7) 0 1 2 3 4 5 6 7 8 9 10
Mid-Point Circle Algorithm
Advantages:
• Due to its integer arithmetic - addition, subtraction and
multiplication, it is faster
• There is no need for squares, square roots and trigonometric
functions
• This algorithm is efficient for scan conversion for drawing
geometric curves on raster display.
Disadvantages:
• It is time consuming
• Distance between the pixels is not equal so a smooth circle will not
be got.
• No accuracy of the generating points.