You are on page 1of 19

Chapter 2

Introduction
Graphic SW and HW provide subroutines to describe a scene in terms
of basic geometric structures called output primitives (a basic geometric
shape from which complex shapes can be constructed). Those functions in a
graphic package that we use to describe the various picture components are
called the graphic output primitives or simply primitives. Output
primitives are combined to form complex structures. Output primitives
describe the geometry of objects and – typically referred to as geometric
primitives. Examples: point, line each of the output primitives has its own set
of attributes.

Point
 A point is the most basic Output primitive in the graphic package. A point
contains location using x and y coordinate and the user may also pass other
Attributes of point is size and color.

Line
 A line is used to generate a straight line between any two end points. Usually a
line is provided with the location of two pixel points called the starting point and
the end point and it is up to computer to decide what pixels fall between these
two points so that a straight line is generated.
 Line is a collection (set) of point.

Attributes of line is Type or style, width, color,

Line Drawing Algorithms

What is Algorithms?

An algorithm is a set of instructions for solving a problem or accomplishing a task. One


common example of an algorithm is a recipe, which consists of specific instructions for
preparing a dish or meal.
What is Line segment?

A line segment is a part of a line that is bounded by two distinct end points, and
contains every point on the line that is between its endpoints

Digital Differential Analyzer (DDA) Line Drawing


Algorithms
A line contains two points. The point is an important element of a line. When a
computer need to determine the positions of pixel that falls between two given points
to generate a straight line it requires some algorithm. There are many methodologies
that used to draw the line. The most commonly used are DDA, Bresenahems and Mid-
point line drawing algorithm.

DDA Algorithm is the simplest line drawing algorithm. Given the starting and ending
coordinates of a line, DDA Algorithm attempts to generate the points between the
starting and ending coordinates.

Y=mx+c -Line question

Procedure
Given-

 Starting coordinates = (X0, Y0)


 Ending coordinates = (Xn, Yn)

The points generation using DDA Algorithm involves the following steps-

Step-01:

Calculate ΔX, ΔY and M from the given input.

These parameters are calculated as-

 ΔX = Xn – X0
 ΔY =Yn – Y0
 M = ΔY / ΔX
Step-02:

Find the number of steps or points in between the starting and ending coordinates.

if (absolute (ΔX) > absolute (ΔY))

Steps = absolute (ΔX);

else

Steps = absolute (ΔY);

Step-03:

Suppose the current point is (Xp, Yp) and the next point is (Xp+1, Yp+1).

Find the next point by following the below three cases-


Step-04:

Keep repeating Step-03 until the end point is reached or the number of generated new
points (including the starting and ending points) equals to the steps count.

PRACTICE PROBLEMS BASED ON DDA ALGORITHM

Problem-01:

Calculate the points between the starting point (5, 6) and ending point (8, 12).

Solution-

Given-

 Starting coordinates = (X0, Y0) = (5, 6)


 Ending coordinates = (Xn, Yn) = (8, 12)

Step-01:

Calculate ΔX, ΔY and M from the given input.

 ΔX = Xn – X0 = 8 – 5 = 3
 ΔY =Yn – Y0 = 12 – 6 = 6
 M = ΔY / ΔX = 6 / 3 = 2

Step-02:

Calculate the number of steps.


As |ΔX| < |ΔY| = 3 < 6, so number of steps = ΔY = 6

Step-03:

As M > 1, so case-03 is satisfied.

Now, Step-03 is executed until Step-04 is satisfied.

Round off (Xp+1,


Xp Yp Xp+1 Yp+1
Yp+1)

5 6 5.5 7 (6, 7)

6 8 (6, 8)

6.5 9 (7, 9)

7 10 (7, 10)

7.5 11 (8, 11)

8 12 (8, 12)
Advantages of DDA Algorithm

 It is the simplest algorithm and it does not require special skills for
implementation.
 It is a faster method for calculating pixel positions than the direct use
of equation y=mx + b. It eliminates the multiplication in the equation
by making use of raster characteristics, so that appropriate increments
are applied in the x or y direction to find the pixel positions along the
line path.

Disadvantages

 Floating point arithmetic in DDA algorithm is still time consuming.


 The algorithm is orientation dependent. Hence end point accuracy is
poor.
 Although DDA is fast, the accumulation of round-off error in successive
additions of floating point increment however can cause the calculation
pixel position to drift away from the true line path for long line
segment.
 Rounding-off in DDA is time consuming.

Bresenham's line algorithm


Definition

Bresenham's line algorithm is a line drawing algorithm that determines the


points of an n-dimensional raster that should be selected in order to form a
close approximation to a straight line between two points. It is commonly
used to draw line primitives in a bitmap image (e.g. on a computer screen), as
it uses only integer addition, subtraction and bit shifting, all of which are very
cheap operations in standard computer architectures. It is an incremental
error algorithm. It is one of the earliest algorithms developed in the field of
computer graphics. An extension to the original algorithm may be used for
drawing circles.
Procedure
Given-

 Starting coordinates = (X0, Y0)


 Ending coordinates = (Xn, Yn)

The points generation using Bresenham Line Drawing Algorithm involves the
following steps-

Step-01:

Calculate ΔX and ΔY from the given input.

These parameters are calculated as-

 ΔX = Xn – X0
 ΔY =Yn – Y0
Step-02:

Calculate the decision parameter Pk.

It is calculated as- Pk = 2ΔY – ΔX

Step-03:
Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).

Find the next point depending on the value of decision parameter Pk.

Follow the below two cases-


Step-04:
Keep repeating Step-03 until the end point is reached or number of iterations
equals to (ΔX-1) times.

PRACTICE PROBLEMS BASED ON BRESENHAM LINE


DRAWING ALGORITHM

Problem-01:

Calculate the points between the starting coordinates (9, 18) and ending coordinates (14,
22).
Solution-

Given-

 Starting coordinates = (X0, Y0) = (9, 18)


 Ending coordinates = (Xn, Yn) = (14, 22)

Step-01:

Calculate ΔX and ΔY from the given input.

 ΔX = Xn – X0 = 14 – 9 = 5
 ΔY =Yn – Y0 = 22 – 18 = 4

Step-02:

Calculate the decision parameter.

Pk

= 2ΔY – ΔX

=2x4–5

=3

So, decision parameter Pk = 3

Step-03:

As Pk >= 0, so case-02 is satisfied.

Thus,

 Pk+1 = Pk + 2ΔY – 2ΔX = 3 + (2 x 4) – (2 x 5) = 1


 Xk+1 = Xk + 1 = 9 + 1 = 10
 Yk+1 = Yk + 1 = 18 + 1 = 19

Similarly, Step-03 is executed until the end point is reached or number of iterations equals
to 4 times.

(Number of iterations = ΔX – 1 = 5 – 1 = 4)

Pk Pk+1 Xk+1 Yk+1

9 18

3 1 10 19

1 -1 11 20

-1 7 12 20

7 5 13 21

5 3 14 22
Problem-02:

Calculate the points between the starting coordinates (20, 10) and ending coordinates (30,
18).

Solution-

Given-

 Starting coordinates = (X0, Y0) = (20, 10)


 Ending coordinates = (Xn, Yn) = (30, 18)

Step-01:

Calculate ΔX and ΔY from the given input.

 ΔX = Xn – X0 = 30 – 20 = 10
 ΔY =Yn – Y0 = 18 – 10 = 8

Step-02:

Calculate the decision parameter.

Pk

= 2ΔY – ΔX

= 2 x 8 – 10

=6

So, decision parameter Pk = 6


Step-03:

As Pk >= 0, so case-02 is satisfied.

Thus,

 Pk+1 = Pk + 2ΔY – 2ΔX = 6 + (2 x 8) – (2 x 10) = 2


 Xk+1 = Xk + 1 = 20 + 1 = 21
 Yk+1 = Yk + 1 = 10 + 1 = 11

Similarly, Step-03 is executed until the end point is reached or number of iterations equals
to 9 times.

(Number of iterations = ΔX – 1 = 10 – 1 = 9)

Pk Pk+1 Xk+1 Yk+1

20 10

6 2 21 11

2 -2 22 12

-2 14 23 12

14 10 24 13

10 6 25 14

6 2 26 15
2 -2 27 16

-2 14 28 16

14 10 29 17

10 6 30 18
Advantages

The advantages of Bresenham Line Drawing Algorithm are-

 It is easy to implement.
 It is fast and incremental.
 It executes fast but less faster than DDA Algorithm.
 The points generated by this algorithm are more accurate than DDA
Algorithm.
 It uses fixed points only.

Disadvantages

The disadvantages of Bresenham Line Drawing Algorithm are-

 Though it improves the accuracy of generated points but still the resulted
line is not smooth.
 This algorithm is for the basic line drawing.
Midpoint Circle Algorithm
The Mid-point Subdivision algorithm is the extension of the Cyrus-
Beck algorithm. The Mid-Point line plotting algorithm was introduced
by “Pitway and Van Aken.” It is an incremental line drawing algorithm. In
this algorithm, we perform incremental calculations. The calculations are
based on the previous step to find the value of the next point. We perform
the same process for each step. By using the mid-point subdivision algorithm,
we can draw a line with close approximation between two points. The mid-
point subdivision line drawing algorithm subdivides the line at its midpoints
continuously.
The Mid-point Subdivision algorithm helps to compute or calculate the visible
areas of lines that appear in the window. This line plotting algorithm follows
the bisection method to divide the line into equal partitions.
Assuming we have just plotted the pixel P(xk, yk), we next need to determine whether
the pixel N (xk + 1, yk) or S(xk + 1, yk -1) is closer to the circle. So, Our decision parameter
is the circle function evaluated at the midpoint M(xk + 1, yk -1/2 ) between these two
pixels:
pk = F(circle) = (xk + 1)2+(yk -1/2)2 – r2
Case 1

If pk < 0, it implies midpoint is inside the circle and select yk.


Otherwise, it implies midpoint is outside. or midpoint is on the circle boundary. In such
case we select the pixel yk – 1
Now we will derive the value of pk+1
pk+1 = (xk+1+1)2 + (yk+1 -1/2)2 – r2
Further,
pk+1– pk = (xk+1+1)2 + (yk+1 –1/2)2 – r2
-[(xk+1)2 + (yk –1/2)2 – r2]
= ((xk+1)+1)2 + (yk+1 –1/2)2
– (xk+1)2 – (yk –1/2)2
= (xk+1)2 + 1 +2(xk+1) + yk+12 +(1/4) – yk+1
– (xk+1)2 – yk2 – (1/4) + yk
= 2(xk+1) + yk+12 – yk2 – yk+1 + yk +1
= 2(xk+1) +( yk+12 – yk2 ) – (yk+1 – yk) +1

pk+1 = pk + 2(xk+1)+( yk+12 – yk2 ) – (yk+1 – yk) +1 …..(ii)

Initial Decision parameter

Now let us calculate the initial decision parameter


For that put (0,r)
Put this in (i) i.e. pk
So, pk = (xk+1)2 + (yk -1/2)2 – r2
Also, p0 = (0+1)2 + (r -1/2)2 – r2
= 1 + r2 + ¼ – r – r2
=1+¼–r
…..(initial decision parameter)
For simplification we can take it as
P0=1-r
Case 2

Now If pk ≥ 0 that means midpoint is outside the circle and S is closest pixel so we
will choose S (xk+1, yk-1)
Putting coordinates of S in (ii) ,we have ,
pk+1 = pk + 2(xk+1)+( yk-12 – yk2 ) – (yk-1 – yk) +1
So, by putting the values pk+1 = pk + 2(xk+1)+(( yk -1)2 – yk2 ) – ((yk-1) – yk) +1.
Further, simplifying pk+1 = pk + 2(xk+1)+yk 2+1-2yk – yk2 – yk+1 + yk +1
Then, we get pk+1 = pk + 2(xk+1)-2yk +2 + 1

Finally, pk+1 = pk + 2(xk+1)-2(yk -1) + 1


Actually , (xk+1 = xk+1)
Also, (yk-1 = yk-1)
Therefore,
pk+1 = pk + 2xk+1 – 2yk-1 + 1
And if pk < 0 that means midpoint is inside the circle and N is closest pixel so we
will choose N (xk+1 , yk)
i.e. yk+1 = yk
Now Also put coordinates of N in (ii)
pk+1 = pk + 2(xk+1)+( yk2 – yk2 ) – (yk – yk) +1
= pk + 2(xk+1)+( yk2 – yk2 ) – (yk – yk) +1
= pk + 2(xk+1) +1
as xk+1 = xk+1 , therefore,
pk+1 = pk + 2xk+1 +1
This is the complete derivation.
Mid-point Circle Drawing Algorithm

1.Input radius r and circle center (x, y,), and plot first point as (0,r)

2.Then, calculate the initial value of the decision parameter as

p0=1-r
3. Also, At each xk position, starting at k = 0, perform the following test:
If pk < 0. Then, the next point along the circle centered on (0,0) is

(xk +1, yk) and


pk+1 = pk + 2(xk+1)+( yk+12 – yk2 ) – (yk+1 – yk) +1
Otherwise, the next point along the circle is

(xk + 1, yk – 1) and

pk+1 = pk + 2(xk+1)-2(yk -1) + 1


4.Then Determine symmetry points in the other seven octants.

5. Also, Move each calculated pixel position (x, y) onto the circular path centered on (xc,
yc) and plot the coordinate values:

x=x+xc, y=y+yc

6. Keep on Repeating steps 3 to 5 until x >= y.


Assignment

1. Calculate the required points Draw a line using DDA Algorithm


From(0,0) to (4,6)
2. Calculate the required points Draw a line using DDA Algorithm
from(1,1) to (8,7)
3. Calculate the required points Draw a line Using DDA Algorithm from
(2,3) to (9,8)
4. Calculate the required points Draw a line from (1,1) to (8,7) using
Bresenham’s Line Algorithm
5. Calculate the required points and Draw a line from (2,3) to (5,8) using
Bresenham’s Line Algorithm
6. Calculate the required points and Draw a line from (3,3) to (10,15) using
Bresenham’s Line Algorithm
7. Calculate the required points to plot the following circle using the
midpoint circle algorithm.
Radius(r)=10 Centre=(3,4)
Hint , Initial Point (Xk, Yk) = (0,10)
8. Calculate the required points to plot the following circle using the
midpoint circle algorithm.
Radius(r) =8 Centre= (10, 10)

Note:
What is required?
 Show each point calculation step by step
 Form table
 Draw a required object.

You might also like