Professional Documents
Culture Documents
6 (3, 3)
5
4
3
2
1
0
0 1 2 3 4 5 6
Line drawing (cont)
The thinnest line is of one-pixel wide. We will
concentrate on drawing a line of 1 pixel resolution.
The Cartesian slope-intercept equation for a straight
line is
y= m. x + b
m is the slope of the line and b is the y intercept.
Given the endpoints of a line segment.
m = y2-y1 / x2-x1
b= y1-m.x1
Symmetry
If we could draw lines with positive slope (0<=slope<=1) we
would be done.
For a line with negative slope (0>=slope>=-1)
(x,-y) (x,y)
450
(-x,-y) (x,-y)
(-y,-x) (y,-x)
Code for drawing a line
……….
DDA Illustration
Desired Line
(xi+1, Round(yj+m))
(xi, yj)
(xi+1, yj+m)
(xi, Round(yj))
y2
y1
x1 x2
Bresenham’s Line Algorithm
An accurate, efficient raster line drawing algorithm
developed by Bresenham, scan converts lines using
only incremental integer calculations that can be
adapted to display circles and other curves.
Note that along the circle section from x=0 to x=y in the first
octant, the slope of the curve varies from 0 to -1
yk X2+y2-r2=0
Yk-1 Midpoint between candidate
pixels at sampling position
Midpoint xk xk+1 Xk+3 xk+1 along a circular path
Midpoint Circle Algorithm
Assuming we have just plotted the pixel at (xk,yk) , we next
need to determine whether the pixel at position (xk + 1, yk-1) is
closer to the circle
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
If pk < 0 , this midpoint is inside the circle and the pixel on the
scan line yk is closer to the circle boundary. Otherwise, the
mid position is outside or on the circle boundary, and we select
the pixel on the scan line yk-1
OR
Pk+1 = Pk+2(xK+1) + (yK+12 – yk2) – (yk+1- yk)+1
Where yk+1 is either yk or yk-1, depending on the sign of pk
Increments for obtaining Pk+1:
2xk+1+1 if pk is negative
2xk+1+1-2yk+1 otherwise
Midpoint circle algorithm
Note that following can also be done incrementally:
2xk+1 = 2xk +2
2 yk+1 = 2yk – 2
At the start position (0,r) , these two terms have the values 2
p0 = 1-r
Midpoint Ellipse..
At each xk position in region 1, starting at k =
0, perform the following test. if p1k < 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
2
where( x0 , y0 ) is the last position calculated in region 1
At each yk position in region 2, starting at k=0,
perform the following test. if p 2k > 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
and ( xk + 1, yk − 1)
p 2 k +1 = p 2 k + 2ry2 xk +1 − 2rx2 yk +1 + rx2
Using the same incremental calculations for x and y
as in region 1. Continue until y=0
Midpoint Ellipse
For both regions, determine symmetry
points in the other three quadrants
Move each calculated pixel position (x,
y) onto the elliptical path that is
centered on ( xc , yc ) and plot the
coordinate values
x = x + xc , y = y + yc