You are on page 1of 34

COMPUTER GRAPHICS

LINE DRAWING ALGORITHM


Institute of Information and Communication Technology
University of Sindh, Jamshoro

Dr. Zeeshan Bhatti


BSIT-PIV
Chapter 2
BY: ZEESHAN BHATTI

POLYNOMIAL ALGORITHM

BY: ZEESHAN BHATTI

DDA (DIGITAL DIFFERENCE ANALYSER)


FOR LINE
Let endpoints coordinates are
(x0, y0) and (x1, y1) respectively.
Se we have

1 0
1 0

=
=

As already mentioned, in digital systems the coordinates of points are


represented by pixels positions. The pixels are considered to be
placed at a distance of unity from each other.
BY: ZEESHAN BHATTI

DDA FOR LINE


A very simple procedure for scan converting a straight line is given as
fallows:
Step 1: Plot the first pixel at (x,y) = (x0, y0)
Step 2: Increment the x and y values by dx and dy respectively to get
the next pixel. Since in digital systems pixel are a unit distance away,
we can consider either dx or dy to be unity and compute the other.
Plot pixel at ( x+dx, y+dy).
Step 3: Continue the above step until the final pixel (x1, y1) is
reached.

BY: ZEESHAN BHATTI

Depending on weather we select dx or dy to be unity , two different


situations can appear.
If we consider dx = 1, the following relations are used to compute the
next y-value.
yi = mxi + b

yi+1 = mxi+1 + b
=m (xi + x) + b
= yi +m x

If x = 1 , then
yi+1 = yi + m
BY: ZEESHAN BHATTI

Similarly, if we consider dy = 1, the following relations are used to


compute the next x-value.
If y = 1, then
Xi+1 = xi +

The above algorithm is called Digital Difference Analyzer (DDA). The


DDA is actually a mechanical device that solves differential equations
by simultaneously incrementing x and y by small steps proportional to
the first derivative of x and y.
In our algorithm when x is incremented by dx, y is incremented by dy.
BY: ZEESHAN BHATTI

USING SYMMETRY

BY: ZEESHAN BHATTI

For lines with |m| < 1, we are to increment the x value, at each
steps, by unity. For other lines, we increment the y-0values.
If x = 1,
then y = m, where

= m.

= .

If y = 1,
1

then x = , where

BY: ZEESHAN BHATTI

DDA ALGORITHM
Digital Differential Analyzer (DDA) algorithm is the simple line generation
algorithm which is explained step by step here.
Step 1 Get the input of two end points (X0,Y0) and (X1,Y1).
Step 2 Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Step 3 Based on the calculated difference in step-2, you need to identify the
number of steps to put pixel. If dx > dy, then you need more steps in x
coordinate; otherwise in y coordinate.
if (dx > dy)
Steps = absolute(dx);
else
Steps = absolute(dy);

BY: ZEESHAN BHATTI

Step 4 Calculate the increment in x coordinate and y coordinate.


Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;

Step 5 Put the pixel by successfully incrementing x and y


coordinates accordingly and complete the drawing of the line.
for(int v=0; v < Steps; v++)
{
x = x + Xincrement;
y = y + Yincrement;
putpixel(x,y);
}

BY: ZEESHAN BHATTI

10

PROBLEM: DRAW A LINE USING DDA ALGORITHM FOR


THE POINTS BETWEEN 2,5 AND 6, 9. DETERMINE THE
VALUES OF PIXEL AND PLOT THE GRAPH.
Step 1:

x0 = 2

x1 = 6

y0 = 5

y1 = 9

Step 2:

dx = x1 - x0
dx = 6-2 = 4

dx= 4

dy = y1 - y0
dy = 9 - 5 = 4

dy=4

BY: ZEESHAN BHATTI

11

Step 3:

Because dy is greater than & equal to dx i.e. 4=4, so we use the value of dy
Steps = dy
Steps = 4

Step 4:
xInc = dx / steps
yInc = dy / steps
xInc = 4 / 4 = 1

Step 5:

x = x + xInc

y = y + yInc

2 +1 = 3

5+1 = 6

3+1=4

6+1 = 7

4+1 = 5

7+ 1 = 8

5 +1 = 6

8+1 = 9

yInc = 4 / 4 = 1
BY: ZEESHAN BHATTI

12

BRESENHAMS LINE GENERATION


The Bresenham algorithm is another
incremental scan conversion algorithm.
The big advantage of this algorithm is
that, it uses only integer calculations.
Moving across the x axis in unit
intervals and at each step choose
between two different y coordinates.
For example, as shown in the following
illustration, from position (2, 3) you
need to choose between (3, 3) and (3,
4). You would like the point that is
closer to the original line.

BY: ZEESHAN BHATTI

13

DDA requires one floating point addition per step

We can eliminate all fp through Bresenhams algorithm


Consider only 1 m 0
- Other cases by symmetry
Assume pixel centers are at half integers
If we start at a pixel that has been written, there are only two
candidates for the next pixel to be written into the frame buffer

BY: ZEESHAN BHATTI

14

CANDIDATE PIXELS

BY: ZEESHAN BHATTI

15

DECISION VARIABLE

BY: ZEESHAN BHATTI

16

SOLVING BRESENHAM
Let at any moment the Pixel I(xi, yi) as initial pixel and the next pixel is
(xi+1, yi+1).
Since the pixel are at unit distance, therefore xi+1 = xi +1, but the
value of yi+1 can be P(xi+1, yi) or Q (xi+1, yi+1) depending on the
nearest point of interaction denoted by R(xi+1, yi+1 ), which is the
actual location of the point on the line.

BY: ZEESHAN BHATTI

17

BRESENHAM LINE DRAWING ALGORITHM


Step 1:
Get the values of x0,y0 and x1,y1
dx = x1 x0
dy = y1 y0
Step 2:
p = 2dy dx
E (Q) = 2dy
NE (P) = 2* (dy dx)
Step 3: Plot the Pixel at I(x0, y0)

BY: ZEESHAN BHATTI

18

Step 4:

while ( x0 < x1)


if ( p < 0) then
p = p + E
x0 = x0 +1
EndIF
Else
p = p + NE
x0 = x0 +1
y0 = y0+1
EndElse

PlotPixel (x0, y0)


EndWhile
BY: ZEESHAN BHATTI

19

PROBLEM: SOLVE BRESENHAM LINE DRAWING


ALGORITHM FOR VALUS (2, 3) (8, 7)
Step 1:
Get the values of
x0 = 2, y0=3
X1 = 8 , y 1 = 7
dx = x1 x0 = 8 - 2 = 6
dy = y1 y0 = 7- 3 = 4
Step 2:
p = 2dy dx = 2 (4) 6 = 2
E (Q) = 2dy = 2(4) = 8
NE (P) = 2* (dy dx) = 2(4-6) = -4
Step 3: Plot the Pixel at I(x0, y0)

BY: ZEESHAN BHATTI

20

P<0
p = p + E
x0 = x0 +1
y0 = y0

p=2
E = 8
NE = -4
2

P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1

(step 1)

3 (step 2)

4 (step 3)

2+(-4) = -2

Step 4:

(step 4)

-2

while ( x0 < x1)


if ( p < 0) then
p = p + E
x0 = x0 +1
EndIF
Else
p = p + NE
x0 = x0 +1
y0 = y0+1
EndElse

PlotPixel (x0, y0)

-2 + 8 = 6
6
6 + (-4) = 2

2
2 +(-4) = -2
-2
-2 +8 = 6
6
6 + (-4) = 2
2

EndWhile

2 +(-4) = -2
-2

10

8
BY: ZEESHAN BHATTI

21

PROBLEM: SOLVE BRESENHAM LINE DRAWING


ALGORITHM FOR VALUS (10, 16) (18, 20)
Step 1:
Get the values of
x0 = 10, y0=16
X1 = 18 , y1 = 20
dx = x1 x0 = 18-10 = 8
dy = y1 y0 = 20-16 = 4
Step 2:
p = 2dy dx = 2 (4) 8 = 0
E (Q) = 2dy = 2(4) = 8
NE (P) = 2* (dy dx) = 2(4-8) = -8
Step 3: Plot the Pixel at I(x0, y0) = (10, 16)

BY: ZEESHAN BHATTI

22

p=0
E = 8
NE = -8

P<0
p = p + E
x0 = x0 +1
y0 = y0

P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1

10

16

Step 4:

while ( x0 < x1)


if ( p < 0) then
p = p + E
x0 = x0 +1
EndIF
Else
p = p + NE
x0 = x0 +1
y0 = y0+1
EndElse

PlotPixel (x0, y0)


EndWhile
BY: ZEESHAN BHATTI

23

p=0
E = 8
NE = -8

P<0
p = p + E
x0 = x0 +1
y0 = y0

P => 0
p = p + NE
x0 = x0 +1
y0 = y0+1

10

16

11

17

12

17

13

18

14

18

15

19

16

19

17

20

18

20

0 + -8 = -8

-8
-8 + 8 = 0
0

0+ -8 = -8
-8
-8 + 8 = 0
0
0+ -8 = -8
-8
-8 + 8 = 0
0
0+ -8 = -8
-8

BY: ZEESHAN BHATTI

24

POLYNOMIAL METHOD FOR CIRCLES


The polynomial method is the simplest approach.

In polynomial, we solve the equation of a circle for known values on


one of the coordinates, and thus determine the calues of the other
coordinate.
The equation of a circle at centre (xc, yc) and radius r is given as
(x-xc)2 + (y - yc)2 = r2
Therefore

BY: ZEESHAN BHATTI

25

SCAN CONVERSION - CIRCLES

BY: ZEESHAN BHATTI

26

SCAN CONVERSION - CIRCLES

BY: ZEESHAN BHATTI

27

SCAN CONVERSION - CIRCLES

BY: ZEESHAN BHATTI

28

CIRCLE MIDPOINT (FOR ONE OCTANT)

BY: ZEESHAN BHATTI

29

BRESENHAMS CIRCLE ALGORITHM


For simplicity We Assume that the center of a circle is at the origin, i.e.

xc = yc = 0
So, the equation of circle, i.e. (polynomial equation becomes)
x 2 + y2 = r 2

BY: ZEESHAN BHATTI

30

ALGORITHM FOR BRESENHAMS CIRCLE

BY: ZEESHAN BHATTI

31

PROBLEM: TRACE BRESENHAMS ALGORITHM


FOR DRAWING A CIRCLE OF RADIUS 6.
To solve, first calculate the initial values of p0 of the decision
parameter, and then recalculating the p at each successive iteration.
Step 1:
x=0; // assumed
y= radius;
r = radius;
p0 = 3 2r = 3 -12 = -9
circlepoints(x,y);
Step 2:

-9

P<0
p = p + 4x + 6
x0 = x0 +1
y0 = y0

P => 0
p = p + 4(x-y)+10
x0 = x0 +1
y0 = y0 - 1

-9 + 4(0)+6 = -3
-3
-3 + 4(1)+6= 7
7
7 + 4 (2 5) +10 = 5
5
5 + 4 ( 3-4) +10 = 11
11

BY: ZEESHAN BHATTI

32

PROBLEM: TRACE BRESENHAMS ALGORITHM


FOR DRAWING A CIRCLE OF RADIUS 11.
To solve, first calculate the initial values of p0 of the decision
parameter, and then recalculating the p at each successive iteration.
Step 1:
x=0; // assumed
y= radius;
r = radius;
p0 = 3 2r =
circlepoints(x,y);

P<0
p = p + 4x + 6
x0 = x0 +1
y0 = y0

P => 0
p = p + 4(x-y)+10
x0 = x0 +1
y0 = y0 - 1

Step 2:

BY: ZEESHAN BHATTI

33

COMPLETING THE CIRCLES BY


MIRRORING THE 8 OCTANTS

BY: ZEESHAN BHATTI

34

You might also like