Docsity

Line Drawing Algorithm and Ellipse Equations, Lecture notes of Computer Science

Toggle sidebar
bg1
COMPILED BY: SAMUEL G.
1
COMPUTER GRAPHICS
Chapter Two
Graphics Primitives
1. Introduction
All graphics packages construct pictures from basic building blocks known as
graphics primitives. Primitives that describe the geometry, or shape, of these building
blocks are known as geometric primitives. They can be anything from 2-D primitives
such as points, lines and polygons to more complex 3-D primitives such as spheres
and polyhedra (a polyhedron is a 3-D surface made from a mesh of 2-D polygons).
In the following sections we will examine some algorithms for drawing different
primitives, and where appropriate we will introduce the routines for displaying
these primitives in OpenGL.
2. OpenGL Point Drawing Primitives
The most basic type of primitive is the point. Many graphics packages, including
OpenGL, provide routines for displaying points. We have already seen the
OpenGL routines for point drawing in the simple OpenGL program introduced in
Chapter 1. To recap, we use the pair of functions glBegin … glEnd, using the
symbolic constant GL_POINTS to specify how the vertices in between should be
interpreted. In addition, the function glPointSize can be used to set the size of the
points in pixels. The default point size is 1 pixel. Points that have a size greater
than one pixel are drawn as squares with a side length equal to their size. For
example, the following code draws three 2-D points with a size of 2 pixels.
glPointSize(2.0);
glBegin(GL_POINTS);
glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
glVertex2f(150.0, 250.0);
glEnd();
Note that when we specify 2-D points, OpenGL will actually create 3-D points with
the third coordinate (the z-coordinate) equal to zero. Therefore, there is not really
any such thing as 2-D graphics in OpenGL – but we can simulate 2-D graphics by
using a constant z-coordinate.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Line Drawing Algorithm and Ellipse Equations and more Lecture notes Computer Science in PDF only on Docsity!

Chapter Two

Graphics Primitives

1. Introduction

All graphics packages construct pictures from basic building blocks known as

graphics primitives. Primitives that describe the geometry , or shape, of these building

blocks are known as geometric primitives. They can be anything from 2 - D primitives

such as points, lines and polygons to more complex 3-D primitives such as spheres

and polyhedra (a polyhedron is a 3-D surface made from a mesh of 2-D polygons).

In the following sections we will examine some algorithms for drawing different

primitives, and where appropriate we will introduce the routines for displaying

these primitives in OpenGL.

2. OpenGL Point Drawing Primitives

The most basic type of primitive is the point. Many graphics packages, including

OpenGL, provide routines for displaying points. We have already seen the

OpenGL routines for point drawing in the simple OpenGL program introduced in

Chapter 1. To recap, we use the pair of functions glBegin … glEnd , using the

symbolic constant GL_POINTS to specify how the vertices in between should be

interpreted. In addition, the function glPointSize can be used to set the size of the

points in pixels. The default point size is 1 pixel. Points that have a size greater

than one pixel are drawn as squares with a side length equal to their size. For

example, the following code draws three 2 - D points with a size of 2 pixels.

glPointSize(2.0);

glBegin(GL_POINTS);

glVertex2f(100.0, 200.0);

glVertex2f(150.0, 200.0);

glVertex2f(150.0, 250.0);

glEnd();

Note that when we specify 2-D points, OpenGL will actually create 3-D points with

the third coordinate (the z - coordinate) equal to zero. Therefore, there is not really

any such thing as 2-D graphics in OpenGL – but we can simulate 2-D graphics by

using a constant z - coordinate.

3. Line Drawing Algorithms

Lines are a very common primitive and will be supported by almost all graphics

packages. In addition, lines form the basis of more complex primitives such as

polylines (a connected sequence of straight-line segments) or polygons (2-D objects

formed by straight-line edges).

Lines are normally represented by the two end-points of the line, and points (x,y)

along the line must satisfy the following slope-intercept equation:

y = mx + c …………………………………………………………………… (1)

where m is the slope or gradient of the line, and c is the coordinate at which the line

intercepts the y - axis. Given two end-points (x 0 ,y 0 ) and (xend,yend) , we can calculate

values for m and c as follows:

0

x x

y y m end

end

−

c = y 0 − mx 0 …………………………………………………………………… (3)

Furthermore, for any given x - interval δx , we can calculate the corresponding y -

interval δy :

δy = m.δx …………………………………………………………………… (4)

δx = ( 1 /m).δy ………………………….………………………………………… (5)

These equations form the basis of the two line-drawing algorithms described

below: the DDA algorithm and Bresenham’s algorithm.

3.1. DDA Line-Drawing Algorithm

The Digital Differential Analyser (DDA) algorithm operates by starting at one end-

point of the line, and then using Eqs. (4) and (5) to generate successive pixels until

the second end-point is reached. Therefore, first, we need to assign values for δx

and δy.

Before we consider the actual DDA algorithm, let us consider a simple first

approach to this problem. Suppose we simply increment the value of x at each

iteration (i.e. δx = 1), and then compute the corresponding value for y using Eqs.

Let us consider an example of applying the DDA algorithm for drawing a straight-

line segment. Referring to see Figure 2 , we first compute a value for the gradient

m :

( )

( )

0

0 = = −

x x

y y m end

end

Now, because |m| ≤ 1, we compute δx and δy as follows:

δx = 1

δy = 0.

Using these values of δx and δy we can now start to plot line points:

  • Start with (x 0 ,y 0 ) = (10,10) – colour this pixel
  • Next, (x 1 ,y 1 ) = (10+1,10+0.6) = (11,10.6) – so we colour pixel (11,11)
  • Next, (x 2 ,y 2 ) = (11+1,10.6+0.6) = (12,11.2) – so we colour pixel (12,11)
  • Next, (x 3 ,y 3 ) = (12+1,11.2+0.6) = (13,11.8) – so we colour pixel (13,12)
  • Next, (x 4 ,y 4 ) = (13+1,11.8+0.6) = (14,12.4) – so we colour pixel (14,12)
  • Next, (x 5 ,y 5 ) = (14+1,12.4+0.6) = (15,13) – so we colour pixel (15,13)
  • We have now reached the end-point (xend,yend) , so the algorithm terminates

Figure 2 - The Operation of the DDA Line-Drawing Algorithm

The DDA algorithm is simple and easy to implement, but it does involve floating

point operations to calculate each new point. Floating point operations are time-

consuming when compared to integer operations. Since line-drawing is a very

common operation in computer graphics, it would be nice if we could devise a

faster algorithm which uses integer operations only. The next section describes

such an algorithm.

3.2. Bresenham’s Line-Drawing Algorithm

Bresenham’s line-drawing algorithm provides significant improvements in

efficiency over the DDA algorithm. These improvements arise from the

observation that for any given line, if we know the previous pixel location, we only

have a choice of 2 locations for the next pixel. This concept is illustrated in Figure

3 : given that we know (xk,yk) is a point on the line, we know the next line point

must be either pixel A or pixel B. Therefore we do not need to compute the actual

floating-point location of the ‘true’ line point; we need only make a decision

between pixels A and B.

p (^) k =  x ( dlower − dupper )= 2  yxk − 2  xyk + d …………………………… (10)

where d is a constant that has the value 2  y + 2 cx − x. Note that the sign of pk

will be the same as the sign of (dlower – dupper) , so if pk is positive we choose pixel A

and if it is negative we choose pixel B. In addition, pk can be computed using only

integer calculations, making the algorithm very fast compared to the DDA

algorithm.

An efficient incremental calculation makes Bresenham’s algorithm even faster. (An

incremental calculation means we calculate the next value of pk from the last one.)

Given that we know a value for pk , we can calculate pk+1 from Eq. (10) by observing

that:

  • Always xk+1 = xk+ 1
  • If pk < 0, then yk+1 = yk , otherwise yk+1 = yk+ 1

Therefore we can define the incremental calculation as:

p (^) k + 1 = pk + 2  y , if pk < 0 …………………………………………………… (11)

p (^) k + 1 = pk + 2  y − 2  x , if pk ≥ 0 …………………………………………… (12)

The initial value for the decision variable, p 0 , is calculated by substituting xk = x 0

and yk = y 0 into Eq. (10), which gives the following simple expression:

p (^) 0 = 2  y − x …………………………………………………………… (13)

So we can see that we never need to compute the value of the constant d in Eq.

Summary

To summarise, we can express Bresenham’s algorithm as follows:

  • Plot the start-point of the line (x 0 ,y 0 )
  • Compute the first decision variable:

o p (^) 0 = 2  y − x

  • For each k, starting with k= 0 :

o If pk < 0:

▪ Plot (xk+ 1 ,yk)

▪ p (^) k + 1 = pk + 2  y

o Otherwise:

▪ Plot (xk+ 1 ,yk+ 1 )

▪ p (^) k + 1 = pk + 2  y − 2  x

The steps given above will work for lines with positive | m| < 1. For | m | > 1 we

simply swap the roles of x and y. For negative slopes one coordinate decreases at

each iteration whilst the other increases.

Exercise

Consider the example of plotting the line shown in Figure 2 using Bresenham’s

algorithm:

  • First, compute the following values:

o Δ x = 5

o Δ y = 3

o 2Δ y = 6

o 2Δ y - 2Δ x = - 4

o p 0 = 2  y − x = 2  3 − 5 = 1

  • Plot (x 0 ,y 0 ) = (10,10)
  • Iteration 0:

o p 0 ≥ 0, so

▪ Plot (x 1 ,y 1 ) = (x 0 + 1 ,y 0 + 1 ) = (11,11)

▪ p 1 = p 0 + 2  y − 2  x = 1 − 4 =− 3

  • Iteration 1:

o p 1 < 0, so

▪ Plot (x 2 ,y 2 ) = (x 1 + 1 ,y 1 ) = (12,11)

▪ p 2 = p 1 + 2  y =− 3 + 6 = 3

  • Iteration 2:

o p 2 ≥ 0, so

▪ Plot (x 3 ,y 3 ) = (x 2 + 1 ,y 2 + 1 ) = (13,12)

▪ p 3 = p 2 + 2  y − 2  x = 3 − 4 =− 1

  • Iteration 3:

o p 3 < 0, so

▪ Plot (x 4 ,y 4 ) = (x 3 + 1 ,y 3 ) = (14,12)

▪ p 4 = p 3 + 2  y =− 1 + 6 = 5

  • Iteration 4:

o p 4 ≥ 0, so

▪ Plot (x 5 ,y 5 ) = (x 4 + 1 ,y 4 + 1 ) = (1 5 ,1 3 )

glBegin(GL_LINES);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

glBegin(GL_LINE_STRIP);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

glBegin(GL_LINE_LOOP);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glEnd();

We can see that GL_LINES treats the vertices as pairs of end-points. Lines are

drawn separately and any extra vertices (i.e. a start-point with no end-point) are

ignored. GL_LINE_STRIP will create a connected polyline , in which each vertex is

joined to the one before it and after it. The first and last vertices are only joined to

one other vertex. Finally, GL_LINE_LOOP is the same as GL_LINE_STRIP except

that the last point is joined to the first one to create a loop.

4. Circle-Drawing Algorithms

Some graphics packages allow us to draw circle primitives. Before we examine

algorithms for circle-drawing we will consider the mathematical equations of a

circle. In Cartesian coordinates we can write:

2 2 2 ( x − xc ) +( y − yc ) = r …………………………………………………… ( 14 )

where (xc,yc) is the centre of the circle. Alternatively, in polar coordinates we can

write:

x = xc + r cos …………………………………………………………… ( 15 )

y = yc + r sin …………………………………………………………… ( 16 )

In the following sections we will examine a number of approaches to plotting

points on a circle, culminating in the most efficient algorithm: the midpoint

algorithm.

4.1. Plotting Points Using Cartesian Coordinates

As a first attempt at a circle-drawing algorithm we can use the Cartesian

coordinate representation. Suppose we successively increment the x - coordinate

and calculate the corresponding y - coordinate using Eq. ( 14 ):

2 2 y = ycr − xc − x …………………………………………………… ( 17 )

This would correctly generate points on the boundary of a circle. However, like

the first attempt at a line-drawing algorithm we saw in Section 3.1 (see Figure 1 )

we would end up with ‘holes’ in the line – see Figure 4. We would have the same

problem if we incremented the y - coordinate and plotted a calculated x - coordinate.

As with the DDA line-drawing algorithm we can overcome this problem by

calculating and checking the gradient: if |m| ≤ 1 then increment x and calculate y ,

and if |m | > 1 then increment y and calculate x. However, with the DDA algorithm

we only needed to compute and check the gradient once for the entire line, but for

circles the gradient changes with each point plotted, so we would need to compute

and check the gradient at each iteration of the algorithm. This fact, in addition to

the square root calculation in Eq. (17), would make the algorithm quite inefficient.

Figure 4 - Circle-Drawing by Plotting Cartesian Coordinates

Figure 5 - Circle-Drawing by Plotting Polar Coordinates

4.3. Taking Advantage of the Symmetry of Circles in Plotting

We can improve the efficiency of any circle-drawing algorithm by taking

advantage of the symmetry of circles. As illustrated in Figure 6 , when we compute

the Cartesian coordinates x and y of points on a circle boundary we have 4 axes of

symmetry (shown in blue): we can generate a point reflected in the y - axis by

negating the x - coordinate; we can generate a point reflected in the x - axis by

negating the y - coordinate, and so on. In total, for each point computed, we can

generate seven more through symmetry. Computing these extra points just by

switching or negating the coordinates of a single point is much more efficient than

computing each boundary point separately. This means that we only need to

compute boundary points for one octant (i.e. one eighth) of the circle boundary –

shown in red in Figure 6

In addition to generating extra points, the 4-way symmetry of circles has another

advantage if combined with the Cartesian plotting algorithm described in Section

4.1. Recall that this algorithm resulted in holes in some parts of the circle boundary

(see Figure 4 ), which meant that a time-consuming gradient computation had to

be performed for each point. In fact, the problem of holes in the boundary only

occurs when the gradient is greater than 1 for computing x- coordinates, or when

the gradient is less than or equal to 1 for computing y - coordinates. Now that we

know we only need to compute points for one octant of the circle we do not need

to perform this check. For example, in the red octant in Figure 6 , we know that the

gradient will never become greater than one, so we can just increment the y -

coordinate and compute the corresponding x - coordinates

Figure 6 - Four-Way Symmetry of Circles

However, even with these efficiency improvements due to symmetry, we still need

to perform a square root calculation (for Cartesian plotting) or a trigonometric

calculation (for polar plotting). It would be nice if there were an algorithm for

circle-drawing that used integer operations only, in the same way that

Bresenham’s algorithm does for line-drawing.

4.4. Midpoint Circle-Drawing Algorithm

The midpoint algorithm takes advantage of the symmetry property of circles to

produce a more efficient algorithm for drawing circles. The algorithm works in a

similar way to Bresenham’s line-drawing algorithm, in that it formulates a

decision variable that can be computed using integer operations only.

The midpoint algorithm is illustrated in Figure 7. Recall that we only need to draw

one octant of the circle, as the other seven octants can be generated by symmetry.

In Figure 7 we are drawing the right-hand upper octant – the one with coordinate

(y,x) in Figure 6 , but the midpoint algorithm would work with slight modifications

whichever octant we chose. Notice that in this octant, when we move from one

pixel to try to draw the next pixel there is only a choice of two pixels: A and B , or

(xk+1,yk) and (xk+1,yk- 1 ). Therefore we don’t need to calculate a real-valued coordinate

(^2 ) pk = fcircxk + 1 , yk − 0. 5 = xk + 1 + yk − 0. 5 − r ………………….. (20)

An incremental calculation for pk+1 can be derived by subtracting pk from pk+1 and

simplifying – the result is:

pk + 1 = pk + 2 xk + 1 + 1 , if pk < 0 …………………………………………… ( 2 1)

pk + 1 = pk + 2 xk + 1 + 1 + 2 yk + 1 , if pk ≥ 0 …………………………………… ( 2 2)

The initial value of the decision variable, p 0 , is calculated by evaluating it at the

starting point (0,r) :

p = fcirc ( r − ) = +( r − ) − r = − r

(^22) 0.^ ……………………………^ (23)

If r is an integer, then all increments are integers and we can round Eq. (23) to the

nearest integer:

p 0 = 1 – r …………………………………………………………………… (24)

Summary

To summarise, we can express the midpoint circle-drawing algorithm for a circle

centred at the origin as follows:

  • Plot the start-point of the circle (x 0 ,y 0 ) = (0,r)
  • Compute the first decision variable:

o p (^) 0 = 1 − r

  • For each k, starting with k= 0:

o If pk < 0:

▪ Plot (xk+ 1 ,yk)

▪ pk + 1 = pk + 2 xk + 1 + 1

o Otherwise:

▪ Plot (xk+ 1 ,yk - 1 )

▪ pk + 1 = pk + 2 xk + 1 + 1 − 2 yk + 1

Example

For example, given a circle of radius r =10, centred at the origin, the steps are:

  • First, compute the initial decision variable:

o p 0 = 1 − r =− 9

  • Plot (x 0 ,y 0 ) = (0, r ) = (0,10)
  • Iteration 0:

o p 0 < 0, so

▪ Plot (x 1 ,y 1 ) = (x 0 + 1 ,y 0 ) = ( 1 ,1 0 )

▪ p 1 = p 0 + 2 x 1 + 1 =− 9 + 3 =− 6

  • Iteration 1:

o p 1 < 0, so

▪ Plot (x 2 ,y 2 ) = (x 1 + 1 ,y 1 ) = (2,1 0 )

▪ p 2 = p 1 + 2 x 2 + 1 =− 6 + 5 =− 1

  • Iteration 2:

o p 2 < 0, so

▪ Plot (x 3 ,y 3 ) = (x 2 + 1 ,y 2 ) = (3,1 0 )

▪ p 3 = p 2 + 2 x 3 + 1 =− 1 + 7 = 6

  • Iteration 3:

o p 3 ≥ 0, so

▪ Plot (x 4 ,y 4 ) = (x 3 + 1 ,y 3 - 1 ) = (4, 9 )

▪ p 4 =^ p 3 +^2 x 4 +^1 −^2 y 4 =^6 −^9 =−^3

  • Iteration 4:

o p 4 < 0, so

▪ Plot (x 5 ,y 5 ) = (x 4 + 1 ,y 4 ) = (5, 9 )

▪ p 5 = p 4 + 2 x 5 + 1 =− 3 + 11 = 8

  • Iteration 5:

o p 5 ≥ 0, so

▪ Plot (x 6 ,y 6 ) = (x 5 + 1 ,y 5 - 1 ) = (6,8)

▪ p 6 = p 5 + 2 x 6 + 1 − 2 y 6 = 8 − 3 = 5

  • Etc. 5. Ellipse-Drawing Algorithms

Some graphics packages may provide routines for drawing ellipses, although as

we will see ellipses cannot be drawn as efficiently as circles. The equation for a 2 -

D ellipse in Cartesian coordinates is:

2 2

y

c

x

c

r

y y

r

x x …………………………………………………… ( 25 )

Alternatively, in polar coordinates, we can write:

just 2 pixels to plot at each iteration. But ellipses only have 2-way symmetry, which

means we have to draw an entire quadrant (see Figure 9 ).

Therefore the midpoint algorithm for ellipses is:

  • At each point, compute gradient m :

o If |m| ≤ 1 we increment x and decide between candidate y positions

o If |m| >1 we increment y and decide between candidate x positions

Again, a fast incremental algorithm exists to compute a decision function, but

because of the gradient calculation the midpoint ellipse-drawing algorithm is not

as efficient as the midpoint circle drawing algorithm.

Figure 9 - Two-Way Symmetry of Ellipses

6. Fill-Area Primitives

The most common type of primitive in 3-D computer graphics is the fill-area

primitive. The term fill-area primitive refers to any enclosed boundary that can be

filled with a solid colour or pattern. However, fill-area primitives are normally

polygons , as they can be filled more efficiently by graphics packages. Polygons are

2 - D shapes whose boundary is formed by any number of connected straight-line

segments. They can be defined by three or more coplanar vertices ( coplanar points

are positioned on the same plane). Each pair of adjacent vertices is connected in

sequence by edges. Normally polygons should have no edge crossings: in this case

they are known as simple polygons or standard polygons (see Figure 10 ).

Figure 10 - A Polygon with an Edge Crossing

Polygons are the most common form of graphics primitive because they form the

basis of polygonal meshes , which is the most common representation for 3-D

graphics objects. Polygonal meshes approximate curved surfaces by forming a

mesh of simple polygons. Some examples of polygonal meshes are shown in Figure

Figure 11 - Examples of Polygonal Mesh Surfaces

6.1. Convex and Concave Polygons

We can differentiate between convex and concave polygons:

  • Convex polygons have all interior angles ≤ 180 o
  • Concave polygons have at least one interior angle > 180o

The difference between convex and concave polygons is shown in Figure 12. Many

graphics packages (including OpenGL) require all fill-area polygons to be convex

polygons. The reason for this, as we will see in Chapter 3, is that it is much more

efficient to fill a polygon if we know it is convex. Some packages even require that

all fill-area primitives are triangles, as this makes filling even more

straightforward.