0% found this document useful (0 votes)
5 views21 pages

Note 2

Garphics

Uploaded by

deysarnabhahope
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views21 pages

Note 2

Garphics

Uploaded by

deysarnabhahope
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Computer Graphics

(Line-Drawing
Algorithms)
Sourav Pramanik
Assistant Professor, Dept. of Computer Science, New Alipore College
Line-Drawing Algorithm

y
• Line drawing is accomplished by calculating 5
intermediate positions along the line path ??
between two specified endpoint positions.
An output device is then directed to fill in
2
these positions between the endpoints.
 DDA (Digital Differential Analyzer)
Algorithm 2 3 4 5 6 7
x
 Bresenham’s Line Algorithm
Line: (2, 2) to (7, 7)
Which intermediate
pixels to turn on?
DDA Algorithm
• The digital differential analyser (DDA) is a scan-
conversion line algorithm based on using x or y.

• A line is sampled at unit intervals in one coordinate and


the corresponding integer values nearest the line path
are determined for the other coordinate.

• Need a lot of floating point arithmetic.


– 2 ‘round’s and 2 adds per pixel.

• Is there a simpler way ?


• Can we use only integer arithmetic ?
– Easier to implement in hardware.
Bresenham’s Line Algorithm
• Accurate and efficient
y
• Uses only incremental integer calculations
6

The method is described for a line segment with 5


a positive slope less than one
4
The method generalizes to line segments of other 3
slopes by considering the symmetry between
the various octants and quadrants of the xy 2
plane 1

0 1 2 3 4 5 6 7 8 9 10 x

𝒚 = 𝒎𝒙 + 𝒄
Bresenham’s Line Algorithm

𝒚𝒌 + 𝟏
𝒅𝟐
𝒚
𝒅𝟏 y 𝒙𝒌 + 𝟏, 𝒚
𝒚𝒌

For derivation, we should keep few thing in our minds, 𝒙𝒌 𝒙𝒌 + 𝟏 x


1. We are presently on (𝑥𝑘 , 𝑦𝑘 ) pixel. And every time x will increase by
1, so next x will be 𝑥𝑘 + 1
2. And y whether it will increase or not that’s what you have to decide.
3. So, what will be next x every time: 𝑥𝑛𝑒𝑥𝑡 = 𝑥𝑘 + 1
𝑦𝑛𝑒𝑥𝑡 = 𝑦𝑘 𝑜𝑟 𝑦𝑘 + 1
Bresenham’s Line Algorithm

𝑦 = 𝑚𝑥 + 𝑐
⇒ y = m 𝑥𝑘 + 1 + c
Decision Parameters
𝑑1 = 𝑦 − 𝑦𝑘 =m 𝑥𝑘 + 1 + c − 𝑦𝑘 −−−− −(1)
𝑖 𝑖𝑓 𝑑1 − 𝑑2 < 0, select pixel
𝑥𝑘 + 1, 𝑦𝑘
𝑑2 = (𝑦𝑘 +1) − 𝑦 = 𝑦𝑘 + 1 − m 𝑥𝑘 + 1 + c
=𝑦𝑘 + 1 − 𝑚 𝑥𝑘 + 1 − 𝑐 −− −(2) 𝑖𝑖 𝑖𝑓 𝑑1 − 𝑑2 > 0, select
pixel 𝑥𝑘 + 1, 𝑦𝑘 + 1
Bresenham’s Line Algorithm

𝑑1 − 𝑑2 = 𝑚 𝑥𝑘 + 1 + 𝑐 − 𝑦𝑘 − 𝑦𝑘 + 1 − 𝑚 𝑥𝑘 + 1 − 𝑐

= 𝑚 𝑥𝑘 + 1 + 𝑐 − 𝑦𝑘 − 𝑦𝑘 − 1 + 𝑚 𝑥𝑘 + 1 + 𝑐

= 2𝑚 𝑥𝑘 + 1 − 2𝑦𝑘 + 2𝑐 − 1 −−−−−−− −(3)

∆𝑦
In (3), there is a problem, 𝑚 is there, 𝑚 = ∆𝑥
, it may gives float results.
Bresenham’s Line Algorithm

Hence, multiplying ∆𝑥 both the side, we have


∆𝑦
∆𝑥 𝑑1 − 𝑑2 = ∆𝑥 2 𝑥 + 1 − 2𝑦𝑘 + 2𝑐 − 1
∆𝑥 𝑘

= 2∆𝑦 𝑥𝑘 + 1 − 2∆𝑥𝑦𝑘 + 2∆𝑥𝑐 − ∆𝑥


∆𝑥 𝑑1 − 𝑑2 = 2∆𝑦𝑥𝑘 + 2∆𝑦 − 2∆𝑥𝑦𝑘 + 2∆𝑥𝑐 − ∆𝑥

= 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 + 2∆𝑦 + 2∆𝑥𝑐 − ∆𝑥 ∴ 𝑝𝑘 = 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 ------(4)


This is new
decision
variable. So, we Difference in 𝑥 and 𝑦 for a line is
give the name remain same.
as 𝑝𝑘
Bresenham’s Line Algorithm
Successive decision parameter
Now, from (4), we can write
• 𝑝𝑘 may change for next pixel.
∴ 𝑝𝑛𝑒𝑥𝑡 = 2∆𝑦𝑥𝑛𝑒𝑥𝑡 − 2∆𝑥𝑦𝑛𝑒𝑥𝑡
• That means for next pixel we
have to take decision. 𝑝𝑛𝑒𝑥𝑡 − 𝑝𝑘 = 2∆𝑦𝑥𝑛𝑒𝑥𝑡 − 2∆𝑥𝑦𝑛𝑒𝑥𝑡 − 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘

How much it change every time?

𝑝𝑛𝑒𝑥𝑡 − 𝑝𝑘 = 2∆𝑦𝑥𝑛𝑒𝑥𝑡 − 2∆𝑥𝑦𝑛𝑒𝑥𝑡 − 2∆𝑦𝑥𝑘 + 2∆𝑥𝑦𝑘

= 2∆𝑦 𝑥𝑛𝑒𝑥𝑡 − 𝑥𝑘 − 2∆𝑥 𝑦𝑛𝑒𝑥𝑡 − 𝑦𝑘


Bresenham’s Line Algorithm

Case-I: if 𝑝𝑛𝑒𝑥𝑡 − 𝑝𝑘 < 0, then we should


Case-II: if 𝑝𝑛𝑒𝑥𝑡 − 𝑝𝑘 ≥ 0,
remain on the same 𝑦𝑘 .
∴ 𝑝𝑛𝑒𝑥𝑡
∴ 𝑝𝑛𝑒𝑥𝑡
= 𝑝𝑘 + 2∆𝑦 𝑥𝑘 + 1 − 𝑥𝑘
= 𝑝𝑘 + 2∆𝑦 𝑥𝑘 + 1 − 𝑥𝑘
− 2∆𝑥 𝑦𝑘 + 1 − 𝑦𝑘
− 2∆𝑥 𝑦𝑘 − 𝑦𝑘 (𝑖. 𝑒. , 𝑦𝑛𝑒𝑥𝑡 = 𝑦𝑘 )
= 𝑝𝑘 + 2∆𝑦 − 2∆𝑥
= 𝑝𝑘 + 2∆𝑦
Bresenham’s Line Algorithm

• What should be the initial value of 𝒑𝒌

𝑝𝑘 = 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 + 2∆𝑦 + 2∆𝑥𝑐 − ∆𝑥

⇒ 𝑝1 = 2∆𝑦𝑥1 − 2∆𝑥𝑦1 + 2∆𝑦 + 2∆𝑥𝑐 − ∆𝑥


∆𝑦
⇒ 𝑝1 = 2∆𝑦𝑥1 − 2∆𝑥𝑦1 + 2∆𝑦 + 2∆𝑥 𝑦1 − 𝑥 − ∆𝑥
∆𝑥 1
⇒ 𝑝1 = 2∆𝑦𝑥1 − 2∆𝑥𝑦1 + 2∆𝑦 + 2∆𝑥𝑦1 − 2∆𝑦𝑥1 − ∆𝑥
𝑝1 = 2∆𝑦 − ∆𝑥
Bresenham’s Line Algorithm
Algorithm Bresenham (𝑥1 , 𝑦1 , 𝑥2 , 𝑦2 )
Draw a line from (2,1) to (8,3)
{
𝑥 = 𝑥1 ; 𝑦 = 𝑦1
∆𝑥 = 𝑥2 − 𝑥1 ; ∆𝑦 = 𝑦2 − 𝑦1 x y p
𝑝 = 2∆𝑦 − ∆𝑥
While (𝑥 ≤ 𝑥2 ) { 2 1 -2
dx=8-2=6
Putpixel (x,y); dy=3-1=2 3 1 2
x++; P_o=2*2-6=-2 4 2 -6
If (𝑝 < 0){ P>0=2+(2*2-12)=-6
𝑝 = 𝑝 + 2∆𝑦 P<0=-6+4=-2 5 2 -2
} 6 2 2
Else { 7 3 -6
𝑝 = 𝑝 + 2∆𝑦 − 2∆𝑥;
y++; 8 3 -2
}
}
Mid-Point Line Generation Algorithm

Mid-Point Line drawing algorithm is a different way to represent Bresenham’s


algorithm.

As discussed earlier, for any given/calculated previous pixel P(Xp, Yp), there are two
candidates for the next pixel closest to the line, E(Xp+1, Yp) and NE(Xp+1, Yp+1)
(E stands for East and NE stands for North-East).
Mid-Point Line Generation Algorithm

In Mid-Point algorithm we do following.


1. Find middle of two possible next points.
Middle of E(Xp+1, Yp) and NE(Xp+1, Yp+1)
is M(Xp+1, Yp+1/2).
2. If M is above the line, then choose E as
next point.
3. If M is below the line, then choose NE as
next point.
Mid-Point Line Generation Algorithm

How to find if a point is above a line or


below a line?
Below are some assumptions to keep
algorithm simple.
1. We draw line from left to right.
2. x1 < x2 and y1< y2
3. Slope of the line is between 0 and 1. We
draw a line from lower left to upper right.
Mid-Point Line Generation Algorithm
Cases other than above assumptions can be handled using reflection.
Let us consider a line y = mx + B.

We can re-write the equation as :


y = (dy/dx)x + B or
(dy)x + B(dx) - y(dx) = 0
Let F(x, y) = (dy)x - y(dx) + B(dx) -----(1)

Let we are given two end points of a line (under above


assumptions)
-> For all points (x,y) on the line, the solution to F(x, y)
is 0.

-> For all points (x,y) above the line, F(x, y) result in a
negative number.

-> And for all points (x,y) below the line, F(x, y) result in
a positive number.
Mid-Point Line Generation Algorithm
This relationship is used to determine the relative
position of M
M = (Xp+1, Yp+1/2)
From (2), we have
∴𝑑=𝐹 𝑀
So our decision parameter d is, = 𝐹 𝑥𝑝 + 1, 𝑦𝑝 + 1ൗ2
d = F(M) = F(Xp+1, Yp+1/2) ---------(2) = 𝑎 𝑥𝑝 + 1 + 𝑏 𝑦𝑝 + 1ൗ2
+ 𝑐; 𝑎𝑡 𝑀
How to efficiently find new value of d from its old value? If 𝑑 > 0
For simplicity, let as write F(x, y) as ax + by + c. choose NE
Else
Where a = dy
choose E
b = -dx Set d_old=d
c = B*dx
We got these values from above equation (1)
Mid-Point Line Generation Algorithm
Case 1: If E is chosen then for next point :
d_new = F(Xp+2, Yp +1/2)
= a(Xp+2) + b(Yp +1/2) + c

d_old = a(Xp+1) + b(Yp +1/2) + c

Difference (Or delta) of two distances:


DEL_d = d_new – d_old
= a(Xp+2)- a(Xp+1)+ b(Yp +1/2)- b(Yp +1/2)+ c-c
= a(Xp) +2a – a(Xp) – a
= a.
Therefore, d_new = d_old + dy. (as a = dy)
Mid-Point Line Generation Algorithm
Case 2: If NE is chosen then for next point :
d_new = F(Xp+2, Yp+3/2)
= a(Xp+2) + b(Yp+3/2) + c

d_old = a(Xp+1) + b(Yp+1/2) + c

Difference (Or delta) of two distances:


DEL_d = d_new –d_old
= a(Xp+2)- a(Xp+1)+ b(Yp+3/2)- b(Yp+1/2)+ c-c
= a(Xp) + 2a – a(Xp) – a + b(Yp) + 3/2b – b(Yp) -1/2b
=a+b
Therefore, d_new = d_old + dy – dx. (as a = dy , b = -dx)
Mid-Point Line Generation Algorithm

Calculation For initial value of decision parameter d_0:


Let’s get rid of function and see
d_0 = F(X1+1 , Y1+1/2) what we end up with for all the
= a(X1 + 1) + b(Y1 + 1/2) +c variables:
= aX1+ bY1 + c + a + b/2 d_0=d_start=2dy-dx
= F(X1,Y1) + a + b/2 (d_new)_E=d_old+2dy
(d_new)_NE=d_old+2(dy-dx)
= a + b/2 (as F(X1, Y1) = 0 )
d_0 = dy – dx/2. (as a = dy, b = -dx)

(So the solution is to multiply 2 with all the variables)


Input (X1,Y1) and (X2,Y2)

dy = Y2- Y1; dx = X2 - X1
// initial value of

// decision parameter d
d = 2dy - dx; x = X1 , y = Y1

// plot initial given point


Plot(x , y)

// iterate through value of X


while(x < X2)
x = x+1

// 'E' is chosen
if (d <= 0)
d = d + 2dy

// 'NE' is chosen
else
d = d + 2(dy – dx)
y = y+1
Plot(x,y)

You might also like