7/29/2019 Lecture03 MidPoiint Circle Algo
1/65
Graphics Output Primitives
Drawing Line, Circle and Ellipse
7/29/2019 Lecture03 MidPoiint Circle Algo
2/65
Objectives
Introduction to Primitives
Points & Lines
Line Drawing Algorithms Digital Differential Analyzer (DDA)
Bresenhams Algorithm
Mid-Point Algorithm
Circle Generating Algorithms Properties of Circles
Bresenhams Algorithm
Mid-Point Algorithm
Ellipse Generating Algorithms Properties of Ellipse
Bresenhams Algorithm
Mid-Point Algorithm
Other Curves Conic Sections
Polynomial Curves
Spline Curves
7/29/2019 Lecture03 MidPoiint Circle Algo
3/65
Introduction
For a raster display, a picture is
completely specified by: intensity and position of pixels, or/and
set of complex objects
Shapes and contours can either be stored
in terms of pixel patterns (bitmaps) or as aset of basic geometric structures (for
example, line segments).
7/29/2019 Lecture03 MidPoiint Circle Algo
4/65
Introduction
Output primitives are the basic geometric
structures which facilitate or describe a
scene/picture. Example of these include:
points, lines, curves (circles, conics etc),
surfaces, fill colour, character string etc.
7/29/2019 Lecture03 MidPoiint Circle Algo
5/65
Points
A point is shown
by illuminating a
pixel on the
screen
7/29/2019 Lecture03 MidPoiint Circle Algo
6/65
Lines
A line segment is completely defined in
terms of its two endpoints.
A line segment is thus defined as:Line_Seg = { (x1, y1), (x2, y2) }
7/29/2019 Lecture03 MidPoiint Circle Algo
7/65
Lines
A line is
produced by
means of
illuminating aset of
intermediary
pixels between
the twoendpoints.
x
y
x1 x2
y1
y2
7/29/2019 Lecture03 MidPoiint Circle Algo
8/65
Lines
Lines is digitized into a set of discrete
integer positions that approximate the
actual line path.
Example: A computed line position of
(10.48, 20.51) is converted to pixel
position (10, 21).
7/29/2019 Lecture03 MidPoiint Circle Algo
9/65
Line
The rounding of coordinate values to
integer causes all but horizonatal and
vertical lines to be displayed with a stair
step appearance the jaggies.
7/29/2019 Lecture03 MidPoiint Circle Algo
10/65
Line Drawing Algorithms
A straight line segment is defined by the
coordinate position for the end points of
the segment.
Given Points (x1, y1) and (x2, y2)
7/29/2019 Lecture03 MidPoiint Circle Algo
11/65
Line
All line drawing algorithms make use ofthe fundamental equations:
Line Eqn. y= m.x+ b
Slope m = y2 y1 /x2x1= y/ x
y-intercept b = y1 m.x1 x-intervalx =y / m
y-interval y = mx
7/29/2019 Lecture03 MidPoiint Circle Algo
12/65
DDA Algorithm (Digital Differential Analyzer)
A line algorithm Based on calculating
either yor x using the above equations.
There are two cases:
Positive slop
Negative slop
7/29/2019 Lecture03 MidPoiint Circle Algo
13/65
DDA- Line with positive Slope
If m 1 then take x= 1
Compute successive yby
yk+1 = yk+ m (1)
Subscript ktakes integer values startingfrom 1, for the first point, and increases by1 until the final end point is reached.
Since 0.0 < m 1.0, the calculated yvalues must be rounded to the nearestinteger pixel position.
7/29/2019 Lecture03 MidPoiint Circle Algo
14/65
DDA
If m > 1, reverse the role ofxand yand take y
= 1, calculate successivexfrom
xk+1 =xk+ 1/m (2)
In this case, each computed x value is rounded
to the nearest integer pixel position.
The above equations are based on the
assumption that lines are to be processed fromleft endpoint to right endpoint.
7/29/2019 Lecture03 MidPoiint Circle Algo
15/65
DDA
In case the line is processed from Right
endpoint to Left endpoint, thenx= 1, yk+1 = yk m form 1 (3)
or
y= 1,xk+1 =xk1/m form > 1 (4)
7/29/2019 Lecture03 MidPoiint Circle Algo
16/65
DDA- Line with negative Slope
If m < 1,
use(1) [provided line is calculated from left to
right] and
use(3) [provided line is calculated from right to
left].
If m 1
use (2) or (4).
7/29/2019 Lecture03 MidPoiint Circle Algo
17/65
Merits + Demerits
Faster than the direct use of line Eqn.
It eliminates the multiplication in line Eqn.
For long line segments, the true line Pathmay be mislead due to round off.
Rounding operations and floating-pointarithmetic are still time consuming.
The algorithm can still be improved. Other algorithms, with better performance
also exist.
7/29/2019 Lecture03 MidPoiint Circle Algo
18/65
Code for DDA AlgorithmProcedure lineDDA(xa,ya,xb,yb:integer);
Vardx,dy,steps,k:integer
xIncrement,yIncrement,x,y:real;
begin
dx:=xb-xa;
dy:=yb-ya;
if abs(dx)>abs(dy) then steps:=abs(dx)
else steps:=abs(dy);
xIncrement:=dx/steps;
yIncrement:=dy/steps;
x:=xa;
y:=ya;
setPixel(round(x),round(y),1);
for k:=1 to steps dobegin
x:=x+xIncrement;
y:=y+yIncrement;
setPixel(round(x),round(y),1)
end
end; {lineDDA}
7/29/2019 Lecture03 MidPoiint Circle Algo
19/65
Bresenhams Line Algorithm
It is an efficient raster line generation
algorithm.
It can be adapted to display circles and
other curves.
The algorithm
After plotting a pixel position (xk, yk), what is
the next pixel to plot?
Consider lines with positive slope.
7/29/2019 Lecture03 MidPoiint Circle Algo
20/65
Bresenhams Line
For a positive slope, 0 < m < 1 and line is
starting from left to right.
After plotting a pixel position (xk
, yk
) we
have two choices for next pixel:
(xk +1, yk)
(xk +1, yk+1)
7/29/2019 Lecture03 MidPoiint Circle Algo
21/65
Bresenhams Line
At position xk +1, we
pay attention to the
intersection of the
vertical pixel and themathematical line
path.
7/29/2019 Lecture03 MidPoiint Circle Algo
22/65
Bresenhams Line
At position xk +1,
we label vertical
pixel separations
from themathematical line
path as
dlower , dupper.
7/29/2019 Lecture03 MidPoiint Circle Algo
23/65
Bresenhams Line
The y coordinate on the mathematical lineat xk+1 is calculated as
y = m(xk +1)+ b
then
dlower= y yk= m (xk+1) + b yk
anddupper=(yk+1) y
= yk+1 m(xk+1) b
7/29/2019 Lecture03 MidPoiint Circle Algo
24/65
Bresenhams Line
To determine which of the two pixels is closest to the linepath, we set an efficient test based on the differencebetween the two pixel separations
dlower
- dupper
= 2m (xk+1) 2y
k+ 2b - 1
= 2 (y / x) (xk+1) 2yk + 2b - 1
Consider a decision parameter pk such that
pk = x (dlower- dupper)
= 2y.xk 2x.yk + c
where
c = 2y + x(2b 1)
7/29/2019 Lecture03 MidPoiint Circle Algo
25/65
Bresenhams Line
Since x > 0, Comparing (dlower and dupper),
would tell which pixel is closer to the line path;
is it yk or yk + 1
If (dlower< dupper)
Then pk is negative
Hence plot lower pixel. Otherwise
Plot the upper pixel.
7/29/2019 Lecture03 MidPoiint Circle Algo
26/65
Bresenhams Line
We can obtain the values of successivedecision parameter as follows:
pk = 2y.xk 2x.yk + c
pk+1=2y.xk+12x.yk+1+c Subtracting these two equations
pk+1 pk= 2y (xk+1 xk) 2x ( yk+1
yk) But xk+1 xk = 1, Therefore
pk+1 = pk+2y 2x (yk+1 yk)
7/29/2019 Lecture03 MidPoiint Circle Algo
27/65
Bresenhams Line
( yk+1 yk) is either 0 or 1, depending on
the sign of pk (plotting lower or upper
pixel).
The recursive calculation of pk is
performed at integer x position, starting at
the left endpoint.
p0 can be evaluated as:
p0= 2y x
7/29/2019 Lecture03 MidPoiint Circle Algo
28/65
Bresenhams Line-Drawing
Algorithm form < 11. Input the two line end points and store the left end point
in (x0 , y0 ).
2. Load (x0 , y0 ) into the frame buffer; that is, plot the firstpoint.
3. Calculate the constants x, y, 2y, and 2y 2x,and obtain the starting value for the decision parameteras
p0= 2y x
4. At eachxkalong the line, starting at k= 0 , perform thefollowing test: Ifpk< 0,the next point to plot is (xk+1, ykand
pk+1=pk+2y
Otherwise, the next point to plot is (xk+1, yk+1) andpk+1=pk+2y2x
5. Repeat step 4, x1 times.
7/29/2019 Lecture03 MidPoiint Circle Algo
29/65
Summary
The constants 2yand 2y 2xare
calculated once for each line to be scan
converted.
Hence the arithmetic involves only integer
addition and subtraction of these two
constants.
7/29/2019 Lecture03 MidPoiint Circle Algo
30/65
Example
To illustrate the algorithm, we digitize the linewith endpoints (20,10) and (30,18). This line hasslope of 0.8, with
x= 10
y=8
The initial decision parameter has the value
p0= 2y x = 6
and the increments for calculating successivedecision parameters are
2 y = 16
2 y - 2x = -4
7/29/2019 Lecture03 MidPoiint Circle Algo
31/65
Example
We plot the initial point (x0 , y0)=(20,10) anddetermine successive pixel positions along the line
path from the decision parameter as
K pk (xk +1, yk +1) K pk (xk +1, yk +1)
0 6 (21,11) 5 6 (26,15)
1 2 (22,12) 6 2 (27,16)
2 -2 (23,12) 7 -2 (28,16)
3 14 (24,13) 8 14 (29,17)
4 10 (25,14) 9 10 (30,18)
7/29/2019 Lecture03 MidPoiint Circle Algo
32/65
Example
C G
7/29/2019 Lecture03 MidPoiint Circle Algo
33/65
Circle Generating Algorithms
A circle is defined as the set of points that are all at a
given distance rfrom a center point (xc, yc).
Circle function:
fcircle (x,y) = x2 + y2r2
7/29/2019 Lecture03 MidPoiint Circle Algo
34/65
34
Circle Equations
Polar form
x= rCos
y= rSin (r= radius of circle)
P=(rCos, rSin)
rSin)
rCos)x
y
r
7/29/2019 Lecture03 MidPoiint Circle Algo
35/65
7/29/2019 Lecture03 MidPoiint Circle Algo
36/65
36
Cartesian form
Use Pythagoras theorem
x2+ y2= r2
x
ry
y
x x
y
r
2 2,P x r x
Ci l l ith
7/29/2019 Lecture03 MidPoiint Circle Algo
37/65
37
Circle algorithms Step throughx-axis to determine y-values
Disadvantages: Not all pixel filled in
Square root function is very slow
Ci l G ti Al ith
7/29/2019 Lecture03 MidPoiint Circle Algo
38/65
Circle Generating Algorithms
Properties of Circle:
A circle is defined as the set of points that are all at a
given distance rfrom a center point (xc, yc).
For any circle point (x, y), this distance is expressed by the
Equation
(x xc)2
+ (y yc)2
= r2
We calculate the points by stepping along the x-axis in unit
steps from xc-rto xc+rand calculate y values as
Circle function:
fcircle (x,y) = x2 + y2r2
7/29/2019 Lecture03 MidPoiint Circle Algo
39/65
Circle Generating Algorithms
There are some problems with this approach:
1. Involves Considerable computation at each step.
2. Spacing between plotted pixel positions is not unifom
as in this Figure.
Positive half of a circle
plotted with eqn and
with (xc, yc) = (0, 0)
Ci l G ti Al ith
7/29/2019 Lecture03 MidPoiint Circle Algo
40/65
Circle Generating Algorithms
Problem (Spacing between plotted pixel
positions is not unifom ) can be removed
by adjusting the spacing by interchanging
x and y (stepping through y values and
calculating x values) whenever theabsolute value of the slope of the circle is
greater than 1.
But this increases the computation and
processing required by the algorithm.
Circle Generating Algorithms
7/29/2019 Lecture03 MidPoiint Circle Algo
41/65
Circle Generating Algorithms Problem (Spacing between plotted pixel positions
is not uniform ) can be removed by calculatingpoints along the circular boundary using polar
coordinates r and .
x = xc + r cos
y = yc + r sin
using a fixed angular step size, a circle is plotted
with equally spaced points along the
circumference.
7/29/2019 Lecture03 MidPoiint Circle Algo
42/65
Circle Generating Algorithms
Problem 1 can be overcome
by considering the symmetry
of circles as in Figure 3.
But it still requires a gooddeal of computation time.
Efficient Solutions
Midpoint Circle Algorithm
Mid i t Ci l D i
7/29/2019 Lecture03 MidPoiint Circle Algo
43/65
43
To determine the closest pixel position tothe specified circle path at each step.
For given radius r and screen centerposition (xc, yc), calculate pixel positions
around a circle path centered at thecoodinate origin (0,0).
Then, move each calculated position (x, y)to its proper screen position by adding xc toxand
y
cto y.
Along the circle section from x=0 to x=y inthe first quadrant, the gradient varies from0 to -1.
Midpoint Circle Drawing
Algorithm
7/29/2019 Lecture03 MidPoiint Circle Algo
44/65
Mid point Circle Algorithm
To apply the midpoint method, we define a
circle function:
Any point (x,y) on the boundary of thecircle with radius rsatisfies the equation
fcircle(x, y)= 0.
7/29/2019 Lecture03 MidPoiint Circle Algo
45/65
Mid point Circle Algorithm
If the points is in the interior of the circle,
the circle function is negative.
If the point is outside the circle, the circle
function is positive.
To summarize, the relative position of any
point (x,y) can be determined by checking
the sign of the circle function:
7/29/2019 Lecture03 MidPoiint Circle Algo
46/65
Mid point Circle Algorithm
The circle function tests in (3) are performed for the midpositions between pixels near the circle path at eachsampling step. Thus, the circle function is the decision
parameter in the midpoint algorithm, and we can set upincremental calculations for this function as we did in the
line algorithm.
7/29/2019 Lecture03 MidPoiint Circle Algo
47/65
Mid point Circle Algorithm
Figure 4 shows the midpoint between the two candidate
pixels at sampling positionxk+1. Assuming we have just
plotted the pixel at (xk, yk), we next need to determine
whether the pixel at position (xk+1, yk) or the one at
position (xk+1, yk1) is closer to the circle.
7/29/2019 Lecture03 MidPoiint Circle Algo
48/65
Choosing the Next Pixel
M
(x, y) (x+1, y)
(x+1, y-1)
E
SE
0)2/1,1( yxF
0)2/1,1( yxF
choose E
choose SE
)2/1,1()( yxFMFd
decision variable d
7/29/2019 Lecture03 MidPoiint Circle Algo
49/65
7/29/2019 Lecture03 MidPoiint Circle Algo
50/65
Mid point Circle Algorithm
Our decision parameter is the circle
function (2) evaluated at the midpoint
between these two pixels:
7/29/2019 Lecture03 MidPoiint Circle Algo
51/65
Mid point Circle Algorithm
Ifpk< 0, this midpoint is inside the circle
and the pixel on scan line ykis closer to
the circle boundary.
Otherwise, the midpoint is outside or on
the circle boundary, and we select the
pixel on scan line yk1.
Successive decision parameters areobtained using incremental calculations.
7/29/2019 Lecture03 MidPoiint Circle Algo
52/65
Mid point Circle Algorithm
We obtain a recursive expression for the next decision parameter byevaluating the circle function at sampling position xk+1 +1 =xk+ 2
where yk+1 is eitherykoryk-1,depending on the sign ofpk.
7/29/2019 Lecture03 MidPoiint Circle Algo
53/65
Mid point Circle Algorithm
Increments for obtainingpk+1 are either
2xk+1 +1 (ifpkis negative) or
2xk+1+1 2yk+1 (ifpkis positive)
Evaluation of the terms 2xk+1 and 2yk+1can also be done incrementally as:
7/29/2019 Lecture03 MidPoiint Circle Algo
54/65
Mid point Circle Algorithm
At the start position (0, r), these two terms
(2x, 2y) have the values 0 and 2r,
respectively.
Each successive value is obtained byadding 2 to the previous value of2xand
subtracting 2 from the previous value of
2y.
7/29/2019 Lecture03 MidPoiint Circle Algo
55/65
Mid point Circle Algorithm
The initial decision parameter is obtained
by evaluating the circle function at the start
position (x0 , y0)=(0, r):
7/29/2019 Lecture03 MidPoiint Circle Algo
56/65
Mid point Circle Algorithm
If the radius r is specified as an integer, we
can simply roundp0 to
since all increments are integers.
7/29/2019 Lecture03 MidPoiint Circle Algo
57/65
Summary of the Algorithm
As in Bresenhams line algorithm, the
midpoint method calculates pixel positions
along the circumference of a circle using
integer additions and subtractions,assuming that the circle parameters are
specified in screen coordinates. We can
summarize the steps in the midpoint circlealgorithm as follows.
Algorithm
7/29/2019 Lecture03 MidPoiint Circle Algo
58/65
7/29/2019 Lecture03 MidPoiint Circle Algo
59/65
Example
Given a circle radius r= 10, we
demonstrate the midpoint circle algorithm
by determining positions along the circle
octant in the first quadrant fromx= 0 tox= y. The initial value of the decision
parameter is
7/29/2019 Lecture03 MidPoiint Circle Algo
60/65
Example
For the circle centered on the coordinate origin,the initial point is (x0 , y0) =(0,10), and initialincrement terms for calculating the decisionparameters are
Successive decision parameter values andpositions along the circle path are calculatedusing the midpoint method as shown in the table.
Mid i t Ci l D i Al ith
7/29/2019 Lecture03 MidPoiint Circle Algo
61/65
61
Midpoint Circle Drawing Algorithm
Initial (x0, y
0) = (1,10)
Decision parameters are: 2x0 = 2, 2y0 = 20
k Pk (xk+1, yk+1) 2xk+1 2yk+1
0 -9 (1, 10) 2 201 -9+2+1=-6 (2, 10) 4 20
2 -6+4+1=-1 (3, 10) 6 20
3 -1+6+1=6 (4, 9) 8 18
4 6+8+1-18=-3 (5, 9) 10 18
5 -3+10+1=8 (6, 8) 12 16
6 8+12+1-16=5 (7, 7) 14 14
Example
7/29/2019 Lecture03 MidPoiint Circle Algo
62/65
62
Example
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10
i pi xi+1, yi+1 2xi+1 2yi+1
0 -9 (1, 10) 2 20
1 -6 (2, 10) 4 20
2 -1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 -3 (5, 9) 10 18
5 8 (6, 8) 12 16
6 5 (7, 7)
r= 10
p0 = 1r= -9 (ifris integer roundp0 = 5/4rto
integer)
Initial point (x0, y0) = (0, 10)
7/29/2019 Lecture03 MidPoiint Circle Algo
63/65
Example
A plot of the generated pixel positions in
the first quadrant is shown in Figure 5.
Mid i t Ci l D i Al ith
7/29/2019 Lecture03 MidPoiint Circle Algo
64/65
64
Midpoint Circle Drawing Algorithmvoid circleMidpoint (int xCenter, int yCenter, int radius){
int x = 0;Int y = radius;int f = 1 radius;
circlePlotPoints(xCenter, yCenter, x, y);while (x < y) {x++;if (f < 0)
f += 2*x+1;
else { y--;f += 2*(x-y)+1; }
}circlePlotPoints(xCenter, yCenter, x, y);
}
Mid i t Ci l D i Al ith
7/29/2019 Lecture03 MidPoiint Circle Algo
65/65
Midpoint Circle Drawing Algorithm
void circlePlotPoints (int xCenter, int yCenter,int x, int y){
setPixel (xCenter + x, yCenter + y);setPixel (xCenter x, yCenter + y);
setPixel (xCenter + x, yCenter y);setPixel (xCenter x, yCenter y);setPixel (xCenter + y, yCenter + x);setPixel (xCentery, yCenter + x);setPixel (xCenter + y, yCenterx);
setPixel (xCentery, yCenterx);}