CH2 - Drawing Algorithms
CH2 - Drawing Algorithms
• A scene can be displayed by loading pixel arrays into the frame buffer or by
scan converting basic geometric structure specifications into pixel patterns.
• We also want to be able to retrieve the current frame-buffer intensity setting for a specified location
using the low-level function
Line Drawing
• Line drawing Is accomplished by calculating intermediate positions along
the line path between two specified endpoint positions.
• An output device is then directed to fill in these positions between the endpoints.
• Digital devices display a straight line segment by plotting discrete points
between the two endpoints.
• Discrete coordinate positions along the line path are calculated from the equation
of the line.
• For a raster video display, the line color (intensity) is then loaded into the frame
buffer at the corresponding pixel coordinates.
• Reading from the frame buffer, the video controller then "plots" the screen pixels.
• Screen locations are referenced with integer values, so plotted positions may only
approximate actual line positions between two specified endpoints.
Line Drawing
• For example:
• A computed line position of (10.48, 20.51), would be converted to pixel position
(10, 21).
• Thus rounding of coordinate values to integers causes lines to be displayed with
aliasing effect or stair-case appearance ("the jaggies").
• More effective techniques for smoothing raster lines are based on adjusting
pixel intensities along the line paths (anti-aliasing).
Line Drawing Algorithms
• The Cartesian slope-intercept equation for a straight line with representing the slope of the line
and as the y-intercept is:
• Given that the two endpoints of a line segment are specified at positions and , we can determine
values for the slope and y-intercept with the following calculations:
• Algorithms for displaying straight lines are based on the line equation and the calculations given in
equations and .
• For any given interval along a line, we can compute the corresponding interval from equation as:
• Subscript takes integer values starting from , for the first point, and increases by until the final
endpoint is reached.
• Since can be any real number between and , the calculated values must be rounded to the nearest
integer.
• For lines with a positive slope greater than , we reverse the roles of and , i.e., we sample at unit
intervals and calculate each succeeding value as:
Digital Differential Analyzer (DDA)
Algorithm
• Equations and are based on the assumption that lines are to be
processed from the left endpoint to the right endpoint.
• If this processing is reversed, so that the starting endpoint is at the
right, then either we have and
• When the start endpoint is at the right (for the same slope), we set and obtain
positions from equation .
• Similarly, when ,
• We use and equation
… … … … (eq. 1)
… … … … … … (eq. 2)
• Put
Bresenham’s Line Drawing Algorithm
Working of Bresenham's Algorithm:
• Now, take decision variable
… … … … … … … … … … … … …… (eq. 3)
… … … … … … … … … … (eq. 4)
where,
• Similarly,
… … … … … … … … … (eq. 5)
Bresenham’s Line Drawing Algorithm
Working of Bresenham's Algorithm:
• Now subtract (eq. 4) from (eq. 5), we get
• Put:
… … … … … …… (eq. 6)
Bresenham’s Line Drawing Algorithm
Working of Bresenham's Algorithm:
• Now if choose pixel T, it means that
as
• then, putting this in (eq. 6)
• But Þ
Þ
• Thus
Bresenham’s Line Drawing Algorithm
Bresenham's Line-Drawing Algorithm for
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Load (x0, y0) into the frame buffer; that is, plot the first point.
3. Calculate constants Dx, Dy, 2Dy, and 2Dy – 2Dx, and obtain the starting value for the
decision parameter as
Otherwise (if di > 0), the next point to plot is (xi+1, yi+1) and
di+1 = di + 2Dy – 2Dx
5. Repeat step 4 Dx times.
Bresenham’s Line Drawing Algorithm
Bresenham's Line-Drawing Algorithm for
Algorithm: Algorithm for scan converting a line from to with and .
1 1 7 4 -6 8 1 1,1
2 2 -5 2,2
3 3 3,2
4 3 -3 4,3
5 5 5,3
6 4 -1 6,4
7 7 7,4
8 5 1 8,5
Mid-Point Algorithm
• Mid-point algorithm is due to Bresenham which was modified by
Pitteway and Van Aken.
• Assume that you have already put the point P at
coordinate and the slope of the line is as shown
in the illustration.
• Now you need to decide whether to put the next
point at or .
• This can be chosen by identifying the intersection
point closest to the point or .
• If the intersection point is closest to the point then
is considered as the next point; otherwise .
Mid-Point Algorithm
• To determine which point to choose, first calculate the mid-point
• If the intersection point of the line with the vertical line connecting and is
below , then take as the next point; otherwise take as the next point.
• In order to check this, we need to consider the implicit line equation:
• We could use this equation to calculate the position of points on a circle circumference by
stepping along the axis in unit steps from to and calculating the corresponding values at each
position as:
• When a display is generated with these equations using a fixed angular step size, a circle is
plotted with equally spaced points along the circumference.
• Computation can be reduced by considering the symmetry of circles.
• The shape of the circle is similar in each quadrant.
• We can generate the circle section in the second quadrant of the by noting that the two circle sections are
symmetric with respect to the axis.
• And circle sections in the third and fourth quadrants can be obtained from sections in the first and second
quadrants by considering symmetry about the axis.
• Determining pixel positions along a circle circumference using either or equations requires a
good deal of computation time.
• The Cartesian equation involves multiplications and square-root calculations, while the parametric
equations contain multiplications and trigonometric calculations.
Circle Drawing – Circle Generating
Algorithms
• More efficient circle algorithms are based on incremental calculation of
decision-parameters, as in the Bresenham line algorithm, which involves
only integer operations.
• 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.
• This method is more easily applied to other conics; and for an integer
circle radius, the midpoint approach generates the same pixel positions
as the Bresenham circle algorithm.
• Also, the error involved in locating pixel positions along any conic section
using the midpoint test is limited to one-half the pixel separation.
Circle Drawing – Midpoint Circle
Algorithm
• As in the raster line algorithm, we sample at unit intervals and
determine the closest pixel position to the specified circle path at each
step.
• For a given radius and screen center position , we can first set up our algorithm
to calculate pixel positions around a circle path centered at the coordinate origin
.
• Then each calculated position is moved to its proper screen position by adding
to and to .
Circle Drawing – Midpoint Circle
Algorithm
• Along the circle section from to in the first quadrant, the slope of the
curve varies from to .
• Therefore, we can take unit steps in the positive direction over this octant and
use a decision parameter to determine which of the two possible positions is
closer to the circle path at each step.
• Positions in the other seven octants are then obtained by symmetry.
Circle Drawing – Midpoint Circle
Algorithm
• To apply the midpoint method, we define a circle function:
• Any point on the boundary of the circle with radius satisfies the equation .
• If the point is in the interior of the circle, the circle function is negative.
• And if the point is outside the circle, the circle function is positive.
• The circle-function tests are performed for the mid positions between
pixels near the circle path at each sampling step.
• Thus, the circle function is the decision parameter in the midpoint algorithm,
and we can set up incremental calculations for this function as we did in the line
algorithm.
Circle Drawing – Midpoint Circle
Algorithm
• Figure below shows the midpoint between the two candidate pixels at sampling position .
• Assuming we have just plotted the pixel at ,
we next need to determine whether the pixel at position
or the one at position is
closer to the circle.
• Our decision parameter is the circle function evaluated at the midpoint between these
two pixels:
• If , this midpoint is inside the circle and the pixel on scan line is closer to the circle boundary.
• Otherwise, the mid position is outside or on the circle boundary, and we select the pixel on scan line
.
• Successive decision parameters are obtained using incremental calculations.
Circle Drawing – Midpoint Circle
Algorithm
• We obtain a recursive expression for the next decision parameter by evaluating the circle
function at sampling position :
or
• At the start position , these two terms have the values and , respectively.
• Each successive value is obtained by adding to the previous value and subtracting from the
previous value of
Circle Drawing – Midpoint Circle
Algorithm
• The initial decision parameter is obtained by evaluating the circle
function at the start position :
or
where and
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position onto the circular path centered on and plot the coordinate values: ,
.
(a) Ellipse generated about foci (b) Ellipse centered at (xc, yc) with semimajor
F1 and F2. axis rx, and semiminor axis ry.
Ellipse – Generating Algorithms
• If the distances to the two foci from any point on the ellipse are labeled and , then
the general equation of an ellipse can be stated as:
()
Ellipse – Generating Algorithm
• Symmetry considerations can be used to further reduce computations.
• An ellipse in standard position is symmetric between quadrants, but
unlike a circle, it is not symmetric between the two octants of a
quadrant.
•
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
• Regions 1 and 2 can be processed in various ways.
• We can start at position (0, ry) and step clockwise along the elliptical path in the
first quadrant, shifting from unit steps in to unit steps in when the slope
becomes less than 1.
• Alternatively, we could start at (rx, 0) and select points in a counterclockwise
order, shifting from unit steps in to unit steps in when the slope becomes
greater than –1.
• With parallel processors, we could calculate pixel positions in the two
regions simultaneously.
• As an example of a sequential implementation of the midpoint
algorithm, we take the start position at (0, ry) and step along the ellipse
path in clockwise order throughout the first quadrant.
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
• We define an ellipse function from () with (xc, yc) = (0, 0) as
2 2 2 2 2 2
𝑓 ellipse ( 𝑥 , 𝑦 )=𝑟 𝑦 𝑥 +𝑟 𝑥 𝑦 − 𝑟 𝑥 𝑟 𝑦
()
which has the following properties:
{ }
¿ 0 if ( x , y ) is inside the ellipse boundary
𝑓 ellipse ( 𝑥 , 𝑦 ) ¿ 0 i f ( x , y ) is on the ellipse boundary
¿ 0 if ( x , y ) is outside the ellipse boundary ()
• Thus, the ellipse function fellipse serves as the decision parameter in the
midpoint algorithm.
• At each sampling position, we select the next pixel along the ellipse path
according to the sign of the ellipse function evaluated at the midpoint between
the two candidate pixels.
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
• Starting at (0, ry), we take unit steps in the direction until we reach the boundary between
Region 1 and Region 2.
• Then we switch to unit steps in the direction over the remainder of the curve in the first
quadrant.
• At each step, we need to test the value of the slope of the curve.
• The ellipse slope is calculated from () as
2
𝑑𝑦 2𝑟𝑦 𝑥
=−
𝑑𝑥 2
2𝑟 𝑥 𝑦 ()
2 𝑟 2𝑦 𝑥 =2𝑟 2𝑥 𝑦
• At the boundary between Region 1 and Region 2,
●
Assuming position has been selected at the previous step,
– we determine the next position along the ellipse path by evaluating the
decision parameter (that is, the ellipse function ()) at this midpoint:
𝑝 1𝑘 = 𝑓 ellipse ( 𝑥𝑘 +1 , 𝑦 𝑘 −
1
2 )
( ) ()
2
2 2 2 1 2 2
¿𝑟 𝑦 ( 𝑥 𝑘 +1 ) +𝑟 𝑥 𝑦𝑘− − 𝑟𝑥 𝑟 𝑦
2
– If , the midpoint is inside the ellipse and the pixel on scan line is closer to
the ellipse boundary.
– Otherwise, the mid-position is outside or on the ellipse boundary, and we
select the pixel on scan line .
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
or
[( ) ( )]
2 2
2 2 2 1 1
𝑝 1 𝑘+1= 𝑝 1𝑘 +2 𝑟 ( 𝑥 𝑘+1 ) +𝑟 +𝑟 𝑦 𝑘 +1 − − 𝑦𝑘−
𝑦 𝑦 𝑥
2 2 ()
where is either or depending on the sign of .
– Decision parameters are incremented by the following amounts:
{ }
2 2
2 𝑟 𝑦 𝑥 𝑘+1 +𝑟 𝑦 , 𝑖𝑓𝑝 1 𝑘< 0
𝑖𝑛𝑐𝑟𝑒𝑚𝑒𝑛𝑡 =
2 𝑟 2𝑦 𝑥𝑘 +1+ 𝑟 2𝑦 − 2 𝑟 2𝑥 𝑦 𝑘+1 , 𝑖𝑓𝑝 1𝑘 ≥ 0
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
or
2 2 1 2
𝑝 1 0=𝑟 𝑦 − 𝑟 𝑥 𝑟 𝑦 + 𝑟
4 𝑥
()
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
● Over region 2, we sample at unit steps in the negative direction, and the midpoint is now
taken between horizontal pixels at each step.
● For this region, the decision parameter is evaluated as
𝑝 2 𝑘= 𝑓 ellipse ( 𝑥𝑘 +
1
2 )
, 𝑦𝑘− 1
()
( )
2
2 1 2 2 2 2
¿𝑟 𝑦 𝑥𝑘 + + 𝑟𝑥 ( 𝑦 𝑘−1 ) − 𝑟𝑥 𝑟 𝑦
2
● If p2k > 0, the mid-position is outside the ellipse boundary, and we select the pixel at .
● If p2k 0, the midpoint is inside or on the ellipse boundary, and we select pixel position
xk+1.
● To determine the relationship between successive decision parameters in region 2, we
evaluate the ellipse function at the next sampling step yk+1 – 1 = yk – 2:
𝑝 2 𝑘+1= 𝑓 ellipse (𝑥 1
𝑘+1 +
2
, 𝑦 𝑘+1 − 1 )
( )
2
1 2
¿𝑟
2
𝑦 𝑥𝑘 +1 +
2
=𝑟
2
𝑥 [( 𝑦𝑘− 1) −1] −𝑟
2
𝑥 𝑟
2
𝑦 ()
[( ) ( )]
2 2
1 1
or 2
𝑝 2𝑘+1= 𝑝 2𝑘 +2 𝑟 ( 𝑦 𝑘 − 1 ) +𝑟 +𝑟
𝑥
2
𝑥
2
𝑦 𝑥 𝑘+1 +
2
− 𝑥 𝑘+
2
()
( ) ( )
2
1 2 1 2 2 2 2
𝑝 20= 𝑥 0 + , 𝑦 0 −1 =𝑟 𝑦 𝑥 0 + +𝑟 𝑥 ( 𝑦 0 −1 ) −𝑟 𝑥 𝑟 𝑦
2 2 ()
Algorithm:
1. Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the origin as:
3. At each xk position in region 1, starting at k = 0, perform the following test: If p1k < 0, the next point along
2 2
𝑝centered
the ellipse 1 𝑘+1=on 𝑝 (0, +2
1𝑘 0) is 𝑟
(x𝑦k+1𝑥
, y𝑘+
k ) +
and
1 𝑟 𝑦
𝑟 2𝑦the
2 2
𝑝 1the
Otherwise, = 𝑝point
𝑘+1next 1𝑘 +2along 𝑥 𝑘+ 1−2
ellipse is 𝑟
(x𝑥k +𝑦1,𝑘+1
yk +𝑟
– 1) 𝑦and
2 2 2 2 2 2
2
with 𝑟 𝑦 𝑥 𝑘 +1 =2 𝑟 𝑥 𝑘 +2 𝑟 𝑦 , 2 𝑟 𝑥 𝑦 𝑘 +1 =2 𝑟 𝑥 𝑦 𝑘 − 2 𝑟 𝑘
𝑟 2𝑦 𝑥 ≥ 2𝑟 2𝑥 𝑦
and continue2until .
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
Algorithm:
4. Calculate the initial value of the decision parameter in region 2 using the last point (x0, y0) calculated in region 1 as
( )
2
2 1 2 2 2 2
𝑝 20=𝑟 𝑦 𝑥0+ +𝑟 𝑥 ( 𝑦 0 − 1 ) − 𝑟 𝑥 𝑟 𝑦
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 2 2
𝑝 2𝑘+1= 𝑝 2𝑘 − 2 𝑟 𝑥 𝑦 𝑘+1 +𝑟 𝑥
2 𝑟 2𝑥 =0 2 𝑟 2𝑦 =72
(with increment )
2 𝑟 2𝑥 𝑦 =2 𝑟 2𝑥 𝑟 𝑦 − 2 𝑟 2𝑥 =− 128
(with increment )
● For region 1: The initial point for the ellipse centered on the origin is , and the initial
decision parameter value is
2 2 1 2
𝑝 1 0=𝑟 𝑦 − 𝑟 𝑥 𝑟 𝑦 + 𝑟 =− 332
4 𝑥
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
●
The remaining positions along the ellipse path in the first
quadrant are then calculated as
k p2k (xk+1, yk+1)
2 745 (8, 0) -- --
Ellipse – Generating Algorithm
Midpoint Ellipse Algorithm
• Positions along an elliptical path centered on the origin with rx = 8 and ry = 6 using
• the midpoint algorithm to calculate pixel addresses in the first quadrant.
Polygon Drawing – Filled Area Primitives
Polygon Drawing – Filled Area Primitives
• Polygons are the areas enclosed by single closed loops of line
segments, where the line segments are specified by the vertices at their
endpoints.
• Polygons are typically drawn with the pixels in the interior filled in, but
you can also draw them as outlines or a set of points.
76
Polygon Drawing – Filled Area Primitives
• There are two basic approaches to area filling on raster systems:
1. To determine the overlap intervals for scan lines that cross the area.
2. To start from a given interior position and paint outward from this point until
encounter the specified boundary conditions.
77
Polygon Drawing – Filled Area Primitives
Filling Polygons – Inside-outside test
• An important issue that arises when filling polygons is that of deciding
whether a particular point is interior or exterior to a polygon.
• A rule called the odd-parity (or the odd-even rule) is usually applied to test
whether a point is interior or not.
• A half-line starting from the particular point and extending to infinity is drawn in
any direction such that no polygon vertex intersects with the line.
79
Polygon Drawing
The scan-line polygon filling algorithm
• The third step accepts a sorted list of points and connects them
according to the odd-parity rule.
• For example, given the list , it draws the lines .
• A decision must be taken as to whether the edges should be displayed or not:
given that and, should we display the line or just the interior points ?
• Step 1 can be optimized by making use of a sorted edge table.
• Entries in the edge table are sorted on the basis of their lower value.
• Next, edges sharing the same low value are sorted on the basis of their higher
value.
80
Polygon Drawing
The scan-line polygon filling algorithm
• A pair of markers are used to denote the range of ‘active’ edges in the
table that need to be considered for a particular scan-line.
• This range starts at the top of the table, and moves progressively downwards as
higher scan-lines are processed.
81
Polygon Drawing
The scan-line polygon filling algorithm
An Example: Consider the polygon in Right. The edge table and edge list
for such a polygon would be:
Edge Table Edge List Given Polygon
82
Polygon Drawing
The scan-line polygon filling algorithm
An Example: Cont.…
• Note that in the above table the horizontal lines are not added to the
edge list.
• The reason for this is discussed below.
• The active edges for scan-line 3 would be 3, 5, 7, 9, these are sorted in order of
their values, in this case 9, 7, 5, 3.
• The polygon fill routine would proceed to fill the intersections between and and
to .
• The next scan-line is calculated in the same manner.
• In this the values of do not change (since the line is vertical; it is incremented by ).
• The active edge at scan-line are and (correct order).
83
Polygon Drawing
General-purpose filling algorithms
Boundary-fill Algorithm
• Sometimes we come across an object where we want to fill the area
and its boundary with different colors.
• This makes use of coherence properties of the boundary of a
primitive/figure:
• given a point inside the region the algorithm recursively plots the surrounding
pixels until the primitive boundary is reached.
• Given the FillColor, the BoundaryColor and a point inside the boundary,
the following algorithm recursively sets the four adjacent pixels (2
horizontal and 2 vertical) to the FillColor.
84
Polygon Drawing
General-purpose filling algorithms
Boundary-fill Algorithm: 4-connected
• Regions which can be completely filled with this algorithm are called 4-connected regions.
• Some regions cannot be filled using this algorithm.
• Such regions are called 8-connected and algorithms filling such areas consider the four diagonally adjacent
pixels as well as the horizontal and vertical ones.
85
Polygon Drawing
General-purpose filling algorithms
Boundary-fill Algorithm: 8-connected
• Care must be taken to ensure that the boundary does not contain holes, which will cause the fill to ‘leak’.
• The 8-connected algorithm is particularly vulnerable. 86
Polygon Drawing
General-purpose filling algorithms
Flood-fill Algorithm:
• Sometimes we come across an object where we want to fill the area and its boundary
with different colors.
• The flood-fill algorithm is used to fill a region which has the same color and whose
boundary may have more than one color.
87
Polygon Drawing
General-purpose filling algorithms
Pattern Filling
• Any of the filling algorithms discussed thus far can be modified to use a given pattern when filling.
• The effects of using this with the scan-line and boundary fill algorithms respectively is shown below:
88
Polygon Drawing
General-purpose filling algorithms
Pattern Filling
• Patterned fills can be achieved by changing statements into where an
m by n matrix that defines the fill pattern.
89