Line Generation Algorithms
Line Generation Algorithms
Algorithms
VECGEN Algorithm
The concept of drawing a line or a curve,
stepping along a row index (y) or a column
index (x) to determine the corresponding
column index or row index respectively is
known as Digital Differential Analyzer (DDA).
The line equation works well for gentle slope
but when the slope is sharp the role of x and
y are interchanged as well as slope m
replaced by 1/m. This method is known as
VECGEN method.
VECGEN Algorithm
Advantage :
It is a faster method for calculating pixel
position then simple line algorithm.
It eliminates continues multiplication in
equation y=mx+b so that appropriate
increments are applied on x or y.
Disadvantage :
Round off error in successive additions can
cause the pixel positions to drift away from the
true line path for long line segments.
The rounding operations and floating point
arithmetic are still time consuming.
Bresenhams Algorithm
The line generating algorithm developed by
Bresenham is accurate and faster; which converts
scan lines using incremental integer calculations
that can be used to draw not only lines but also
used to display circles and other curves.
Bresenhams Algorithm
Let us assume that we are
drawing a line y = mx+b that
passes through a point (x0,y0).
Here 0 < m < 1. Let us also
assume that the last pixel
turned on is (xk,yk) and the
decision to be made is for the
next step that is for the vertical
Now let us assume a vertical
distance xk+1
row of pixels which passes
through horizontal distance
xk+1. There are three vertical
points, (xk+1, yk), (xk+1, y) and
(xk+1, yk+1), fall on this
Bresenhams Algorithm
The equation of this vertical line is,
y= m(xk+1) + b
Therefore, d1 = y yk
= m(xk+1) + b yk
d2 = yk +1 y
= yk +1 - (m(xk+1) + b)
= yk +1 - m(xk+1) b
The difference among these two points can be obtained as
d1-d2,
d1 - d2 = [m(xk+1) + b yk] [yk +1 - m(xk+1) b]
= m(xk)+ m + b yk yk 1 + m(xk) + m + b
= 2m( xk) +2m + 2b - 2yk - 1
= 2m(xk + 1) + 2b - 2yk - 1
Bresenhams Algorithm
So,
d1 - d2 = 2m(xk + 1) + 2b - 2yk 1
[after substituting m =dy/dx ]
= 2 (dy/dx)(xk + 1) + 2b - 2yk 1
dx (d1 - d2)
= 2 dy (xk + 1) + 2b dx - 2yk dx
dx
= 2 dy xk + 2 dy + 2b dx - 2yk dx dx
Gk = 2 dy xk - 2yk dx + c
where c = 2 dy + 2b dx dx and it is independent of
the positions of pixel, and Gk is a decision
parameter for the kth step.
Bresenhams Algorithm
If Gk is positive, that is dx(d1 - d2) > 0, and d1 >
d2. Therefore, the upper pixel at position yk + 1 is
closer to the line than the pixel yk, hence the pixel
at position yk + 1 will be activated.
In case of negative Gk, that is dx(d1 - d2) < 0, (d2
> d1) and therefore the pixel at position yk will be
activated.
Bresenhams Algorithm
Now for the (k + 1)st step,
Gk+1 = 2 dy (xk+1) 2(yk+1) dx + c
Gk+1 - Gk = [2 dy (xk+1) 2(yk+1) dx + c] [2 dy xk 2yk dx + c]
= 2 dy (xk+1) 2(yk+1) dx + c 2 dy xk + 2yk
dx c
= 2 dy (xk+1 - xk) - 2dx (yk+1 - yk)
= 2 dy - 2dx (yk+1 - yk)
[since xk+1 - xk = 1]
Bresenhams Algorithm
At this stage the basic algorithm is defined. We
only need to calculate the initial value for
parameter p0.
G0 = 2 dy x0 2 dx y0 + 2 dy + 2 dx b dx
For the initial point on the line:
y0 = mx0 + b
Therefore,
b = y0 (dy/dx) x0
Bresenhams Algorithm
Substituting the above for b we get:
G0 = 2 dy x0 2 dx y0 + 2 dy + 2 dx [ y0 (dy/dx)
x0 ] dx
= 2 dy x0 2 dx y0 + 2 dy + 2 dx y0 2 dx
(dy/dx) x0 dx
= 2 dy x0 2 dx y0 + 2 dy + 2 dx y0 2 dy x0
dx
= 2 dy x0 2 dy x0 2 dx y0 + 2 dx y0 + 2 dy
dx
= 2 dy dx
Bresenhams Algorithm
Bresenham's Line-Drawing Algorithm for |m|
<1
Step 1: Input the two line endpoints and store the
left endpoint in (x0,y0)
Step 2: Load (x0,y0) into the frame buffer; that is,
plot the first point.
Step 3: Calculate constants dx, dy, 2dy, and 2dy
2dx, and obtain the starting value for the decision
parameter as
G0 = 2dy - dx
Bresenhams Algorithm
Step 4: At each xk along the line, starting at k =
0, perform the following test:
If Gk < 0, the next point to plot is (xk +1, yk)
and
Gk +1 = Gk + 2dy
Otherwise, the next point to plot is (xk +1, yk
+1)
Gk +1 = Gk + 2dy - 2dx
Step 5: Repeat Step 4 dx number of times.
Circle Generation
Algorithms
Midpoin
t
yi-1
x2 + y 2 r 2 = 0
xi xi+1 xi+2
A method for direct distance comparison is to test the
halfway position between two pixels to determine if
this midpoint is inside or outside the circle boundary.
Assuming that we have just plotted the pixels at (xi ,
yi).
pi f circle ( xi 1, yi 12 )
( xi 1) 2 ( yi 12 ) 2 r 2
If pi < 0, the midpoint is inside the circle and the
pixel yi is closer to the circle boundary.
If pi 0, the midpoint is outside the circle and the
pixel yi - 1 is closer to the circle boundary.
pi 1 f circle ( xi 1 1, yi 1 )
1
2
OR
Note:
xi+1 = xi +1
( xi 2) 2 ( yi 1 12 ) 2 r 2
x = x xc and y = y yc
= 2x + 2 and 2y
= 2y 2
Bresenhams Circle
Algorithm
General Principle
The circle function:
Consider only
45 90
f circle ( x, y ) x 2 y 2 r 2
and
0 if (x,y) is inside the circle boundary
Bresenhams Circle
Algorithm
Bresenhams Circle
Algorithm
Define: D(si) = distance of p3 from circle
D(ti) = distance of p2 from circle
i.e. D(si) = (xi + 1)2 + yi2 r2
[always +ve]
D(ti) = (xi + 1)2 + (yi 1)2 r2 [always -ve]
Decision Parameter pi = D(si) + D(ti)
so, if pi < 0 then the circle is closer to p3 (point
above)
if pi 0 then the circle is closer to p2 (point below)
Bresenhams Circle
Algorithm
x0 = 0
y0 = r
p0 = [12 + r2 r2] + [12 + (r-1)2 r2] = 3 2r
if pi < 0 then
yi+1 = yi
pi+1 = pi + 4xi + 6
xi+1 = xi + 1
else if pi 0 then
yi+1 = yi 1
pi+1 = pi + 4(xi yi) + 10
Stop when xi yi and determine symmetry points in
the other octants
F1
d1
P=(x,y)
F2
d2
Ellipse Properties
Expressing distances d1 and d2 in terms of the focal
coordinates F1 = (x1, x2) and F2 = (x2, y2), we have:
( x x1 ) 2 ( y y1 ) 2 ( x x2 ) 2 ( y y2 ) 2 constant
ry
rx
y yc
x xc
1
Cartesian coordinates:
rx
ry
Polar coordinates:
x xc rx cos
y yc ry sin
Ellipse Algorithms
Symmetry between quadrants
Not symmetric between the two octants of a quadrant
Thus, we must calculate pixel positions along the
elliptical arc through one quadrant and then we obtain
positions in the remaining 3 quadrants by symmetry
(-x, y)
(x, y)
ry
rx
(-x, -y)
(x, -y)
Ellipse Algorithms
f ellipse ( x, y ) ry2 x 2 rx2 y 2 rx2 ry2
Decision parameter:
Slope = -1
ry
rx
2ry2 x
dy
Slope
2
dx
2rx y
Ellipse Algorithms
Slope = -1
1
ry 2
rx
dx
2ry x 2rx y
Midpoin
t
yi-1
xi
xi+1 xi+2
p1i 1 f ellipse ( xi 1 1, yi 1 12 )
ry2 ( xi 2) 2 rx2 ( yi 1 12 ) 2 rx2 ry2
OR
2ry2 xi 1 ry2
increment
2
2
2
2
r
x
2
r
y i 1
y
x yi 1
if p1i 0
if p1i 0
Region 2
Over region 2, step in the negative y direction and midpoint
is taken between horizontal pixels at each step.
yi
Midpoin
t
yi-1
xi
xi+1 xi+2
Decision parameter:
p 2i f ellipse ( xi 12 , yi 1)
ry2 ( xi 12 ) 2 rx2 ( yi 1) 2 rx2 ry2
p 2i 1 f ellipse ( xi 1 12 , yi 1 1)
ry2 ( xi 1 12 ) 2 rx2 ( yi 2) 2 rx2 ry2
OR
2
r
y
r
y i 1
x i 1
x
if p 2i 0
if p 2i 0
p 20 f ellipse ( x0 12 , y0 1)
ry2 ( x0 12 ) 2 rx2 ( y0 1) 2 rx2 ry2
p 2i 1 p 2i 2rx2 yi 1 rx2
Line Styles
Solid lines, dashed lines, dotted lines and
thick lines are used as different line styles
in graphics applications.
A dashed line can be generated by interdash spaces between the dashes.
Dashes and inter-dash spaces are equal in
length whereas a dotted line can be
generated by very short length dashes.
Thick Lines
If you need such a line segment with a
thickness of three pixels, you need to put
there a vertical span of three pixels for
each value of .
Thus, a line with any width , can be
generated just by putting a vertical span
of (w/2) pixels on both the sides of the
central line.
Thick Lines
Let P1(x1,y1) and P2(x2,y2) be the end points of a
line segment with width , which is to be drawn.
Here two parallel line segments will be drawn
from a central line(x1,y1) and (x2,y2) at a
distance(w/2).
The coordinates of end points of the upper and
lower line boundaries are [(x1,y1 + wy), (x2,y2 +
wy)] and [(x1,y1 - wy), (x2,y2 - wy)] respectively.
Where the wy is as below:
Thick Lines
When the slope is steep that is greater
than 1, the role of x and y in the above
equation will get change and the equation
would be written as follows:
Line Caps
There are three basic techniques or caps
which are used in graphics applications:
butt cap, round cap, and projecting square
cap.
Line Caps
In the projecting square cap, the portion of
line segment is just extended to give a line
segment an effect of square.
In the round cap, the upper and lower line
boundaries are joined by a circular arc with
a semi-circle of the diameter of the width of
the thick line segment.
In the butt cap the end positions of the thick
lines are adjusted in such a way that the line
segment appears with square ends.
Round Joint
In the round joint, the exterior boundaries are connected with a
circular arch with a diameter of total thickness of two thick
lines, which are joined.
Bevel Joint
The bevel joint is one where butt cap is used to draw line
segment and the little triangle that is created at the joint is
filled with color of these two line segments.
Character Generation
Three methods for generating
character:
1. Stroke method
2. Bitmap method
Stroke
Method
Bitmap
Character Generation
3. Stardust method
Aliasing
The effect created when rasterization is
performed over a discrete series of pixels.
In particular, when lines or edges do not
necessarily align directly with a row or
column of pixels, that line may appear
unsmooth and have a stair-step edge
appearance.
Anti aliasing utilizes blending techniques
to blur the edges of the lines and provide
the viewer with the illusion of a smoother
line.
Aliasing
Antialiasing
The process of removing one or more of
the unwanted artifact is called antialiasing or smoothing.
Antialiasing