1/3/2023
1
EI-OE-401 Open Elective IV
(Computer Graphics and CAD/CAM)
Module - 2
Point and Lines
Prof. Pardeep Kumar
Department of Instrumentation,
Kurukshetra University, Kurukshetra-136119
pardeepk@kuk.ac.in
• Points and lines, Line drawing algorithms, midpoint circle and ellipse
algorithms. Filled area primitives: scan line polygon fill algorithm, boundary-
fill and flood fill algorithms.
• Translation, scaling, rotation, reflection and shear transformations, matrix
representations and homogeneous coordinates, composite transforms,
transformation between coordinate systems. 2-D Viewing: The viewing
pipeline, viewing coordinate reference frame, window to viewport coordinate
transformation, viewing functions, Cohen-Sutherland and Cyrus beck line
clipping algorithms
• Reference Books:
Computer Graphics by Hearn & Donald, PHI
Computer Graphic by Plastock, McGraw Hill
Principle of Interactive graphics by Newman.W Spraul R.F., McGraw Hill
Procedural Elements of computer graphics by Rogers D.F., McGraw Hill
Module - I Syllabus
2
January 3, 2023 DOI, Kurukshetra University
There will be
continuous
evaluation in the
form of
Assignments,
quizzes etc.
After
completion
there will be a
class test
1/3/2023
2
• How line-drawing routine works?
– need to calculate which pixels need to be colored
– line to draw: from A = (ax, ay) to B = (bx, by) (integers)
• ideal vs. actual line?
Line Drawing
3
January 3, 2023 DOI, Kurukshetra University
• We are going to analyze how this process is
achieved.
• Some useful definitions:
– Rasterization: Process of determining which pixels
provide the best approximation to a desired line on the
screen.
– Scan Conversion: Combining the rasterization and
generating the picture in scan line order.
• General requirements:
– Straight lines must appear as straight lines.
– They must start and end accurately
– Lines should have constant brightness along their length
– Lines should be drawn quickly/rapidly
Line Drawing Algorithms
4
January 3, 2023 DOI, Kurukshetra University
?
?
?
?
1/3/2023
3
• For horizontal, vertical and 45º
lines, the choice of raster
elements is obvious.
• These lines exhibit constant
brightness along the length:
• However, for any other
orientation the choice is more
difficult:
Line Drawing Algorithms
5
January 3, 2023 DOI, Kurukshetra University
?
?
?
?
?
Rasterization of straight lines.
6
January 3, 2023 DOI, Kurukshetra University
• Rasterization yields uneven brightness: Horizontal
and vertical lines appear brighter than the 45º lines.
For doing so, we would need:
1. Calculation of square roots
(increasing CPU time)
2. Multiple brightness levels
Compromises:
1. Calculate only an approximate line
2. Use integer arithmetic
3. Use incremental methods
OR
1/3/2023
4
• A line in Computer graphics typically refers to line segment,
• which is a portion of straight line that extends indefinitely in opposite
direction.
• It is defined by its two end points & the slope intercept equation for a line:
y = mx + b (1)
where, m = Slope of the line
b = the y intercept of a line
Line Drawing Algorithms
7
January 3, 2023 DOI, Kurukshetra University
x
y
P1(x1,y1)
P2(x2,y2)
b
0
• The two endpoints of a line
segment are specified at
positions (x1,y1) and (x2,y2).
• We can determine the value for slope m & b intercept as
m = y2-y1/x2-x1 = Δy/ Δx (2)
And, b = y1 – mx1 (3)
• For a given x interval Δx along a line, we can compute the corresponding y
interval Δy from
Δy = m Δx (4)
• Similarly, we can obtain x interval Δx by Δy:
Δx = Δy/m (5)
Contd. ……
8
January 3, 2023 DOI, Kurukshetra University
1/3/2023
5
Conditions….
1. If |m| < 1,
– then for every integer value of x between and excluding x1 and x2, calculate the
corresponding value of y using equation
– Δy = m Δx & scan convert (x, y).
2. If |m| > 1,
– then for every integer value of y between and excluding y1 and y2, calculate the
corresponding value of x using equation
– Δx = Δy / m & scan convert (x, y).
3. If |m| = 1 i.e Δx = Δy.
– In each case, a smooth line with slope m is generated between the specific endpoints.
January 3, 2023 DOI, Kurukshetra University 9
Line Algorithm example
• Example 1: The endpoints of line are(0,0) & (6,18). Compute each value of y
as x steps from 0 to 6 and plot the result.
• Solution : Equation of line is y= mx +b
m = y2-y1/x2-x1 = 18-0/6-0 = 3
Next the y intercept b is found by plugging y1& x1 into the equation
y = 3x + b, 0 = 3(0) + b.
Therefore, b=0, so the equation for the line is
y= 3x.
January 3, 2023 DOI, Kurukshetra University 10
1/3/2023
6
Line Drawing Algorithm
• Algorithm 1: Direct Scan Conversion
1. Start at the pixel from the left-hand endpoint xl
2. Step along the pixels horizontally until we
reach the right-hand end of the line, xr
3. For each pixel compute the corresponding y
value
4. round this value to the nearest integer to select
the nearest pixel
January 3, 2023 DOI, Kurukshetra University 11
The equation of a straight line is given by: y = m.x + b
x = xl;
while (x <= xr)
{
ytrue = m*x + b;
y = Round (ytrue);
PlotPixel (x, y);
/* Set the pixel at (x,y) on */
x = x + 1;
}
The algorithm performs a floating-point multiplication for every step in x.
This method therefore requires an enormous number of floating-point
multiplications, addition and rounding and is therefore expensive.
© 2001 Andrés Iglesias. See: http://personales.unican.es/iglesias
Simple Line Drawing- explained
• Equation for the line: y = m (x – ax) + ay
• What's the line slope m ?
– m = (by – ay)/(bx – ax) calculated once
– increment x, from ax to bx in steps of one pixel
• for each pixel
– calcúlate y = round (m (x – ax) + ay)
– color the resulting pixel (x, y)
• Requires a multiplication, a subtraction, an addition, and a
round for each pixel –
• Expensive! Slow!
January 3, 2023 DOI, Kurukshetra University 12
1/3/2023
7
Drawbacks….
• While this approach is mathematically sound, it involves floating-point
computation (multiplication & addition) in every step that uses the line
equation since m & b are generally real numbers.
• The challenge is to find a way to achieve the same goal as quickly as
possible.
January 3, 2023 DOI, Kurukshetra University 13
Algorithm 2: Digital Differential Analyzer (DDA)
– The digital differential analyzer (DDA) algorithm is an
incremental scan-conversion method.
– Such an approach is characterized by performing
calculations at each step using results from the
preceding step.
– Suppose, at step i we have calculated (xi, yi) to be a
point on the line.
– Since the next point (xi+1,yi+1) should satisfy
Δy/Δx= m
where Δy = yi+1 – yi &
Δx = xi+1 – xi.
We have, yi+1 = yi + mΔx
yi+1 = yi + Δy (1)
or xi+1 = xi + Δy/m (2)
Line Drawing Algorithm
14
January 3, 2023 DOI, Kurukshetra University
DDA uses repeated addition
𝑑𝑦
𝑑𝑥
= 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
or
∆𝑦
∆𝑥
=
𝑦2−𝑦1
𝑥2−𝑥1
We need only compute m
once, as the start of the scan-
conversion.
The DDA algorithm runs rather
slowly because it requires real
arithmetic (floating-point
operations).
1/3/2023
8
Line Drawing Algorithms
1. DDA algorithm for lines with -1 < m < 1
x = xl;
ytrue = yl;
while (x <= xr)
{
ytrue = ytrue + m;
y = Round (ytrue);
PlotPixel (x, y);
x = x + 1;
}
January 3, 2023 DOI, Kurukshetra University 15
Example: Third quadrant
• Switching the roles of x and y when m >1
– Gaps occur when m > 1
– Reverse the roles of x and y using a unit step in y,
and 1/m for x.
Contd…
16
January 3, 2023 DOI, Kurukshetra University
Limitations of DDA:
(1) The rounding operation & floating point arithmetic are time
consuming procedures.
(2) The accumulation of round-off error in successive addition of
floating point increment can cause the calculated pixel
position to drift away from the true line path for long line
segment.
1/3/2023
9
Algorithm: (x1,y1) (x2,y2) are the end points
and dx, dy are the float variables.
(i) If abs(x2-x1) > abs(y2-y1) then
length = abs(x2-x1)
else
length = abs(y2-y1)
endif
(ii) dx = (x2-x1)/length
dy = (y2-y1)/length
(iii) x = x1 + 0.5
y = y1 + 0.5
(iv) i = 0
(v) Plot (trunc(x), trunc(y))
(vi) x = x + dx
y = y + dy
i = i + 1
(viii) If i < length then go to step (v)
(ix) Stop
Digital Differential Analyzer (DDA)
17
January 3, 2023 DOI, Kurukshetra University
Example 2 Scan convert a line having end points
(3,2) & (4,7) using DDA.
Solution: x2 - x1 = 4-3 = 1
y2 - y1 = 7-2 = 5
As, abs(x2-x1) < abs(y2-y1) then
length = y2-y1 = 5
dx = (x2-x1)/ length = 1/5 = .2
dy = (y2-y1)/ length = 5/5 = 1
x1 y1 x2 y2 L dx dy i x y Result Plot
3 2 4 7 5 .2 1 0 3.5 2.5 3.5, 2.5 3,2
1 3.7 3.5 3.7,3.5 3,3
2 3.9 4.5 3.9,4.5 3,4
3 4.1 5.5 4.1,5.5 4,5
4 4.3 6.5 4.3,6.5 4,6
5 4.5 7.5 4.5,7.5 4,7
• Algorithm 3: Bresenham’s algorithm (1965)
– Bresenham, J.E. Algorithm for computer control of a digital plotter, IBM Systems Journal, January
1965, pp. 25-30.
– This algorithm uses only integer arithmetic, and runs significantly faster.
Line Drawing Algorithms
18
January 3, 2023 DOI, Kurukshetra University
,0)
(0 0)
,
(1
,
(1 1)
(0,1)
X
Y
/
1 2
?
?
1/2 ≤ m ≤ 1
0 ≤ m ≤ 1/2
Plot (1,1)
Plot (1,0)
Key idea: distance between
the actual line and the nearest
grid locations (error).
Initialize error:
e = -1/2
Error is given by:
e = e + m
Reinitialize error:
when e > 0
1/3/2023
10
Line Drawing Algorithms
19
January 3, 2023 DOI, Kurukshetra University
Example: m = 3/8
If e < 0 below
else above below below above above
Reinitialize
error:
e = 1/4 -1
= -3/4
error
0
Error: e = e + m
Initial value:
e = - 1/2
e = -1/2+3/8
=-1/8
e = -1/8+3/8
=1/4
e = -3/4 +3/8
=-3/8
What is the question?
– Which pixel (L or U) do we set next?
– uses no floating point arithmetic and no rounding.
– Incremental approach
• Assume a line with 0 < slope < 1
• Define extents of the line segment in x and y:
– ∆x = bx – ax, and ∆y = by – ay.
– 0 < ∆y < ∆x. Why?
• Line equation: ∆y/∆x = (y-ay)/(x-ax)
– –∆x*(y-ay) + ∆y*(x – ax) = 0
• Define F(x, y) = –2 ∆x*(y - ay) + 2 ∆y*(x – ax)
– equation of line is F(x, y) = 0
Bresenham Technique
20
January 3, 2023 DOI, Kurukshetra University
1
U
M’’
Ideal line
M’
M
L
Q
P=(px, py )
1/3/2023
11
• Grid: Points on the grid are centers of pixels
• Let P a pixel which is currently set (chosen)
• Next step pixel: Candidates L (low) and U (up)
• Midpoint between them M = (px+1, py+1/2)
• if ideal line below M
– Set next pixel L
– (px+1, py)
• Else set next pixel U
– (px+1, py+1)
• how do we decide?
Bresenham Technique
21
January 3, 2023 DOI, Kurukshetra University
1
U
M’’
Ideal line
M’
M
L
Q
P=(px, py )
• Case 1: line below M F(Mx, My) < 0
– Next M is M’ = (px+2, py+1/2)
– next F: F(px+2, py+1/2) = -2∆x(py+1/2 – ay) + 2∆y(px+2-ax)
• Calculate the change in F, from this step to next step:
– ∆F = F(px+2, py+1/2) - F(px+1, py+1/2)
– ∆F = 2∆y(px+2-ax) - 2∆y(px+1-ax) = 2∆y
• F(M’x, M’y) = F(Mx,My) + 2∆y (constant integer)
• Case 2: line above M F(Mx, My) > 0
– Next M is M” = (px+2, py+3/2)
– F = F(px+2, py+3/2) - F(px+1, py+1/2) = -2(∆x-∆y)
– F(M”x, M”y) = F(Mx, My) – 2(∆x-∆y)
• In either case, F changes by a constant integer:
– 2∆y if y was not incremented (negative F)
– -2(∆x-∆y) if y was incremented (positive F)
Bresenham Technique
22
January 3, 2023 DOI, Kurukshetra University
1
U
M’’
Ideal line
M’
M
L
Q
P=(px, py )
1/3/2023
12
Calculate the initial point value
• M = (ax+1, ay+1/2)
– F(ax, ay) = -2∆x(ay+1/2-ay) + 2∆y(ax+1-ax) = 2∆y-∆x
– Initial value: F = 2∆y-∆x
• For (i = ax; x <= bx; x++)
{ Set pixel (x, y)
If F< 0 {
F += 2∆y; // no change in y
else
{ y++;
F += 2(∆y-∆x)
}}
}
Bresenham algorithm
23
January 3, 2023 DOI, Kurukshetra University
• Until now: ax < bx and slope < 1
– Other cases: (slope >1; negative slopes)
• Exercises
– tricks: replace roles of X and Y, and ∆x with
–∆x
• Drawing Patterned lines:
– Patterned lines (dotted or dashed lines)
• store the desired line pattern in a bit mask
• When a pixel is selected, consult the bit
mask to check whether it should be drawn.
• Another approach
Bresenham Line Algorithm
24
January 3, 2023 DOI, Kurukshetra University
1/3/2023
13
Bresenham Line Algorithm
25
January 3, 2023 DOI, Kurukshetra University
for (0<m<1)
d2
d1
x
Xk Xk+1 Xk+2
yk+2
yk+1
yk
If d2 > d1
Plot yk
else
Plot yk +1
➢ Is it possible to compute and compare
d1 and d2 using only integer operations?
y = m (xk+ 1) + b
d2 = (yk + 1) − y = yk +1 − m (xk + 1) − b
d1 = y − yk= m (xk + 1) + b − yk
d1−d2 = 2m (xk +1)− 2yk + 2b − 1
d1−d2 = 2 dy/dx (xk +1)− 2yk + 2b − 1
dx(d1−d2) = 2 dy (xk +1)− 2 dx yk + dx (2b − 1)
dx(d1−d2) = 2 dy xk +2 dy− 2 dx yk + dx (2b − 1)
Let 2dy + dx(2b−1) = c
We have:
dx(d1−d2) = 2 dy xk - 2 dx yk + c
Bresenham Line Algorithm
26
January 3, 2023 DOI, Kurukshetra University
d2
d1
x
Xk Xk+1 Xk+2
yk+2
yk+1
y
k
1/3/2023
14
• since 0 < m < 1, dx > 0
– dx(d1−d2) has the same sign as of (d1−d2), so
– if (d1−d2) < 0,
– then yk +1 = yk
– else yk +1 = yk +1
• Let Pk= (d1−d2) = 2dy xk − 2dx yk + c
where c = 2dy + dx(2b−1), hence
• Pk+1= 2dy(xk +1)- 2dx(yk+1)+ c
Bresenham Line Algorithm
27
January 3, 2023 DOI, Kurukshetra University
Subtracting
each other as
shown on next
page…….
Selection criterion
for yk +1
• Continued from previous page: To derive Pk+1 from Pk
– Pk+1-Pk= 2dy(xk +1- xk )- 2dx(yk+1-yk)
– Pk+1=Pk + 2dy- 2dx = Pk + 2(dy- dx)
• At initial value when k = 0, let’s consider again the previous equation
– P0= (d1−d2) = 2dy x0 − 2dx y0 + c,
• substitute value of c here
– P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx(2b−1)
• further at k = 0, form y0 = m x0 + b,
– b = y0 - m x0
• substitute the b in above equation
Bresenham Line Algorithm
28
January 3, 2023 DOI, Kurukshetra University
1/3/2023
15
– P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx (2(y0 - m x0 ) −1)
– P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx (2(y0 - dy/dx x0 ) −1)
– P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + 2 dx y0 - 2 dy x0 − dx
– P0= (d1−d2) = 2dy − dx
• (at the starting point of line or initial condition)
Bresenham Line Algorithm
29
January 3, 2023 DOI, Kurukshetra University
• Given end points (x0, y0) (x1, y1)
dx = x1−x0, dy=y1−y0
• Starting with an end point (x0, y0):
1. Compute P0 = 2dy −dx
2. For each k, staring with k = 0
if (Pk < 0)
the next point is (Xk+1, Yk),
Pk+1 = Pk + 2 dy,
else
the next point is (Xk+1, Yk+1),
Pk+1 = Pk + 2dy − 2dx
3. Repeat step 2 x1−x0 times
Bresenham’s Line Algorithm
30
January 3, 2023 DOI, Kurukshetra University
1/3/2023
16
• Given end points (x0, y0) (x1, y1)
dx = x1−x0, dy=y1−y0
• Starting with an end point (x0, y0):
1. Compute P0 = 2dy −dx
2. For each k, staring with k = 0
if (Pk < 0)
the next point is (Xk+1, Yk),
Pk+1 = Pk + 2 dy,
else
the next point is (Xk+1, Yk+1),
Pk+1 = Pk + 2dy − 2dx
3. Repeat step 2 x1−x0 times
Bresenham’s Line Algorithm
31
January 3, 2023 DOI, Kurukshetra University
void line(int x0, int y0, int x1, int y1, int value)
{ int dx = x1-x0,dy = y1 – y0;
int d = 2 * dy-dx; /* Initial Value of d
int incrL = 2*dy; /* increment used
incrU = 2*(dy-dx); /* increment used
int x = x0, y = y0;
putpixel (x,y, value) /* start p
while (x < x1)
{ if (d <= ))
{ /*choose L */
d +=incrL;
x++;}
else { d += incrU; /* Choose U */
x++;
y++; }
putpixel (x, y, value); /* selected pixel clo
} /* while */
} /* Midpoint Line */
void line(int x0, int y0, int x1, int y1, int value)
{ int dx = x1-x0,dy = y1 – y0;
int d = 2 * dy-dx; /* Initial Value of d*/
int incrL = 2*dy; /* increment used for move to L*/
incrU = 2*(dy-dx); /* increment used for move to U*/
int x = x0, y = y0;
putpixel (x,y, value) /* start pixel */
while (x < x1) {
if (d <= ))
{ /*choose L */
d +=incrL;
x++;}
else { d += incrU; /* Choose U */
x++;
y++; }
putpixel (x, y, value); /* selected pixel closest to the line */
} /* while */
} /* Midpoint Line */
Bresenham algorithm
32
January 3, 2023 DOI, Kurukshetra University
1/3/2023
17
Bresenham algorithm
33
January 3, 2023 DOI, Kurukshetra University
January 3, 2023
DOI, Kurukshetra University
34
Any Question?
Thanks!

More Related Content

PPT
Lecture _Line Scan Conversion.ppt
PPTX
lecture 1.pptx
PDF
Notes_456_Windowing2_7 (1).pdf
PPTX
Rasterization.pptx
PDF
3Dshape Analysis Matching Ajmmmmmmmmmmmmm
PPTX
Study on Fundamentals of Raster Scan Graphics
PPTX
Elliptic Curve Cryptography
Lecture _Line Scan Conversion.ppt
lecture 1.pptx
Notes_456_Windowing2_7 (1).pdf
Rasterization.pptx
3Dshape Analysis Matching Ajmmmmmmmmmmmmm
Study on Fundamentals of Raster Scan Graphics
Elliptic Curve Cryptography

Similar to Notes_456_Lines_Drawing2_4 (1).pdf (20)

PDF
Computer graphics notes watermark
PDF
Computer Graphics involves technology to access. The Process transforms and p...
PPTX
B. SC CSIT Computer Graphics Unit1.2 By Tekendra Nath Yogi
PPTX
Computer Graphics Unit 1
PDF
Unit-2 raster scan graphics,line,circle and polygon algorithms
PDF
Computer graphics notes 2 tutorials duniya
PPTX
L-5 (Line Drawing Algorithms Computer graphics).pptx
PPTX
Digital Differential Analyzer Line Drawing Algorithm
PPTX
Spline interpolation numerical methods presentation
PPT
03.Scan Conversion.ppt
PDF
C012271015
PPT
Line drawing algo.
PDF
Notes_456_PolygonClipping2_10 (1).pdf
PDF
47549379 paper-on-image-processing
PDF
Computer Graphics Unit 2
PDF
mesh generation techniqure of structured gridpdf
PPTX
Learning multifractal structure in large networks (Purdue ML Seminar)
PDF
Computer graphics notes
PPTX
Output primitives in Computer Graphics
Computer graphics notes watermark
Computer Graphics involves technology to access. The Process transforms and p...
B. SC CSIT Computer Graphics Unit1.2 By Tekendra Nath Yogi
Computer Graphics Unit 1
Unit-2 raster scan graphics,line,circle and polygon algorithms
Computer graphics notes 2 tutorials duniya
L-5 (Line Drawing Algorithms Computer graphics).pptx
Digital Differential Analyzer Line Drawing Algorithm
Spline interpolation numerical methods presentation
03.Scan Conversion.ppt
C012271015
Line drawing algo.
Notes_456_PolygonClipping2_10 (1).pdf
47549379 paper-on-image-processing
Computer Graphics Unit 2
mesh generation techniqure of structured gridpdf
Learning multifractal structure in large networks (Purdue ML Seminar)
Computer graphics notes
Output primitives in Computer Graphics
Ad

Recently uploaded (20)

PDF
Abrasive, erosive and cavitation wear.pdf
PDF
Java Basics-Introduction and program control
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
Applications of Equal_Area_Criterion.pdf
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
Feature types and data preprocessing steps
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PPTX
Software Engineering and software moduleing
PDF
Design Guidelines and solutions for Plastics parts
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Abrasive, erosive and cavitation wear.pdf
Java Basics-Introduction and program control
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Amdahl’s law is explained in the above power point presentations
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
Applications of Equal_Area_Criterion.pdf
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
August -2025_Top10 Read_Articles_ijait.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Feature types and data preprocessing steps
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
20250617 - IR - Global Guide for HR - 51 pages.pdf
Software Engineering and software moduleing
Design Guidelines and solutions for Plastics parts
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Ad

Notes_456_Lines_Drawing2_4 (1).pdf

  • 1. 1/3/2023 1 EI-OE-401 Open Elective IV (Computer Graphics and CAD/CAM) Module - 2 Point and Lines Prof. Pardeep Kumar Department of Instrumentation, Kurukshetra University, Kurukshetra-136119 pardeepk@kuk.ac.in • Points and lines, Line drawing algorithms, midpoint circle and ellipse algorithms. Filled area primitives: scan line polygon fill algorithm, boundary- fill and flood fill algorithms. • Translation, scaling, rotation, reflection and shear transformations, matrix representations and homogeneous coordinates, composite transforms, transformation between coordinate systems. 2-D Viewing: The viewing pipeline, viewing coordinate reference frame, window to viewport coordinate transformation, viewing functions, Cohen-Sutherland and Cyrus beck line clipping algorithms • Reference Books: Computer Graphics by Hearn & Donald, PHI Computer Graphic by Plastock, McGraw Hill Principle of Interactive graphics by Newman.W Spraul R.F., McGraw Hill Procedural Elements of computer graphics by Rogers D.F., McGraw Hill Module - I Syllabus 2 January 3, 2023 DOI, Kurukshetra University There will be continuous evaluation in the form of Assignments, quizzes etc. After completion there will be a class test
  • 2. 1/3/2023 2 • How line-drawing routine works? – need to calculate which pixels need to be colored – line to draw: from A = (ax, ay) to B = (bx, by) (integers) • ideal vs. actual line? Line Drawing 3 January 3, 2023 DOI, Kurukshetra University • We are going to analyze how this process is achieved. • Some useful definitions: – Rasterization: Process of determining which pixels provide the best approximation to a desired line on the screen. – Scan Conversion: Combining the rasterization and generating the picture in scan line order. • General requirements: – Straight lines must appear as straight lines. – They must start and end accurately – Lines should have constant brightness along their length – Lines should be drawn quickly/rapidly Line Drawing Algorithms 4 January 3, 2023 DOI, Kurukshetra University ? ? ? ?
  • 3. 1/3/2023 3 • For horizontal, vertical and 45º lines, the choice of raster elements is obvious. • These lines exhibit constant brightness along the length: • However, for any other orientation the choice is more difficult: Line Drawing Algorithms 5 January 3, 2023 DOI, Kurukshetra University ? ? ? ? ? Rasterization of straight lines. 6 January 3, 2023 DOI, Kurukshetra University • Rasterization yields uneven brightness: Horizontal and vertical lines appear brighter than the 45º lines. For doing so, we would need: 1. Calculation of square roots (increasing CPU time) 2. Multiple brightness levels Compromises: 1. Calculate only an approximate line 2. Use integer arithmetic 3. Use incremental methods OR
  • 4. 1/3/2023 4 • A line in Computer graphics typically refers to line segment, • which is a portion of straight line that extends indefinitely in opposite direction. • It is defined by its two end points & the slope intercept equation for a line: y = mx + b (1) where, m = Slope of the line b = the y intercept of a line Line Drawing Algorithms 7 January 3, 2023 DOI, Kurukshetra University x y P1(x1,y1) P2(x2,y2) b 0 • The two endpoints of a line segment are specified at positions (x1,y1) and (x2,y2). • We can determine the value for slope m & b intercept as m = y2-y1/x2-x1 = Δy/ Δx (2) And, b = y1 – mx1 (3) • For a given x interval Δx along a line, we can compute the corresponding y interval Δy from Δy = m Δx (4) • Similarly, we can obtain x interval Δx by Δy: Δx = Δy/m (5) Contd. …… 8 January 3, 2023 DOI, Kurukshetra University
  • 5. 1/3/2023 5 Conditions…. 1. If |m| < 1, – then for every integer value of x between and excluding x1 and x2, calculate the corresponding value of y using equation – Δy = m Δx & scan convert (x, y). 2. If |m| > 1, – then for every integer value of y between and excluding y1 and y2, calculate the corresponding value of x using equation – Δx = Δy / m & scan convert (x, y). 3. If |m| = 1 i.e Δx = Δy. – In each case, a smooth line with slope m is generated between the specific endpoints. January 3, 2023 DOI, Kurukshetra University 9 Line Algorithm example • Example 1: The endpoints of line are(0,0) & (6,18). Compute each value of y as x steps from 0 to 6 and plot the result. • Solution : Equation of line is y= mx +b m = y2-y1/x2-x1 = 18-0/6-0 = 3 Next the y intercept b is found by plugging y1& x1 into the equation y = 3x + b, 0 = 3(0) + b. Therefore, b=0, so the equation for the line is y= 3x. January 3, 2023 DOI, Kurukshetra University 10
  • 6. 1/3/2023 6 Line Drawing Algorithm • Algorithm 1: Direct Scan Conversion 1. Start at the pixel from the left-hand endpoint xl 2. Step along the pixels horizontally until we reach the right-hand end of the line, xr 3. For each pixel compute the corresponding y value 4. round this value to the nearest integer to select the nearest pixel January 3, 2023 DOI, Kurukshetra University 11 The equation of a straight line is given by: y = m.x + b x = xl; while (x <= xr) { ytrue = m*x + b; y = Round (ytrue); PlotPixel (x, y); /* Set the pixel at (x,y) on */ x = x + 1; } The algorithm performs a floating-point multiplication for every step in x. This method therefore requires an enormous number of floating-point multiplications, addition and rounding and is therefore expensive. © 2001 Andrés Iglesias. See: http://personales.unican.es/iglesias Simple Line Drawing- explained • Equation for the line: y = m (x – ax) + ay • What's the line slope m ? – m = (by – ay)/(bx – ax) calculated once – increment x, from ax to bx in steps of one pixel • for each pixel – calcúlate y = round (m (x – ax) + ay) – color the resulting pixel (x, y) • Requires a multiplication, a subtraction, an addition, and a round for each pixel – • Expensive! Slow! January 3, 2023 DOI, Kurukshetra University 12
  • 7. 1/3/2023 7 Drawbacks…. • While this approach is mathematically sound, it involves floating-point computation (multiplication & addition) in every step that uses the line equation since m & b are generally real numbers. • The challenge is to find a way to achieve the same goal as quickly as possible. January 3, 2023 DOI, Kurukshetra University 13 Algorithm 2: Digital Differential Analyzer (DDA) – The digital differential analyzer (DDA) algorithm is an incremental scan-conversion method. – Such an approach is characterized by performing calculations at each step using results from the preceding step. – Suppose, at step i we have calculated (xi, yi) to be a point on the line. – Since the next point (xi+1,yi+1) should satisfy Δy/Δx= m where Δy = yi+1 – yi & Δx = xi+1 – xi. We have, yi+1 = yi + mΔx yi+1 = yi + Δy (1) or xi+1 = xi + Δy/m (2) Line Drawing Algorithm 14 January 3, 2023 DOI, Kurukshetra University DDA uses repeated addition 𝑑𝑦 𝑑𝑥 = 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 or ∆𝑦 ∆𝑥 = 𝑦2−𝑦1 𝑥2−𝑥1 We need only compute m once, as the start of the scan- conversion. The DDA algorithm runs rather slowly because it requires real arithmetic (floating-point operations).
  • 8. 1/3/2023 8 Line Drawing Algorithms 1. DDA algorithm for lines with -1 < m < 1 x = xl; ytrue = yl; while (x <= xr) { ytrue = ytrue + m; y = Round (ytrue); PlotPixel (x, y); x = x + 1; } January 3, 2023 DOI, Kurukshetra University 15 Example: Third quadrant • Switching the roles of x and y when m >1 – Gaps occur when m > 1 – Reverse the roles of x and y using a unit step in y, and 1/m for x. Contd… 16 January 3, 2023 DOI, Kurukshetra University Limitations of DDA: (1) The rounding operation & floating point arithmetic are time consuming procedures. (2) The accumulation of round-off error in successive addition of floating point increment can cause the calculated pixel position to drift away from the true line path for long line segment.
  • 9. 1/3/2023 9 Algorithm: (x1,y1) (x2,y2) are the end points and dx, dy are the float variables. (i) If abs(x2-x1) > abs(y2-y1) then length = abs(x2-x1) else length = abs(y2-y1) endif (ii) dx = (x2-x1)/length dy = (y2-y1)/length (iii) x = x1 + 0.5 y = y1 + 0.5 (iv) i = 0 (v) Plot (trunc(x), trunc(y)) (vi) x = x + dx y = y + dy i = i + 1 (viii) If i < length then go to step (v) (ix) Stop Digital Differential Analyzer (DDA) 17 January 3, 2023 DOI, Kurukshetra University Example 2 Scan convert a line having end points (3,2) & (4,7) using DDA. Solution: x2 - x1 = 4-3 = 1 y2 - y1 = 7-2 = 5 As, abs(x2-x1) < abs(y2-y1) then length = y2-y1 = 5 dx = (x2-x1)/ length = 1/5 = .2 dy = (y2-y1)/ length = 5/5 = 1 x1 y1 x2 y2 L dx dy i x y Result Plot 3 2 4 7 5 .2 1 0 3.5 2.5 3.5, 2.5 3,2 1 3.7 3.5 3.7,3.5 3,3 2 3.9 4.5 3.9,4.5 3,4 3 4.1 5.5 4.1,5.5 4,5 4 4.3 6.5 4.3,6.5 4,6 5 4.5 7.5 4.5,7.5 4,7 • Algorithm 3: Bresenham’s algorithm (1965) – Bresenham, J.E. Algorithm for computer control of a digital plotter, IBM Systems Journal, January 1965, pp. 25-30. – This algorithm uses only integer arithmetic, and runs significantly faster. Line Drawing Algorithms 18 January 3, 2023 DOI, Kurukshetra University ,0) (0 0) , (1 , (1 1) (0,1) X Y / 1 2 ? ? 1/2 ≤ m ≤ 1 0 ≤ m ≤ 1/2 Plot (1,1) Plot (1,0) Key idea: distance between the actual line and the nearest grid locations (error). Initialize error: e = -1/2 Error is given by: e = e + m Reinitialize error: when e > 0
  • 10. 1/3/2023 10 Line Drawing Algorithms 19 January 3, 2023 DOI, Kurukshetra University Example: m = 3/8 If e < 0 below else above below below above above Reinitialize error: e = 1/4 -1 = -3/4 error 0 Error: e = e + m Initial value: e = - 1/2 e = -1/2+3/8 =-1/8 e = -1/8+3/8 =1/4 e = -3/4 +3/8 =-3/8 What is the question? – Which pixel (L or U) do we set next? – uses no floating point arithmetic and no rounding. – Incremental approach • Assume a line with 0 < slope < 1 • Define extents of the line segment in x and y: – ∆x = bx – ax, and ∆y = by – ay. – 0 < ∆y < ∆x. Why? • Line equation: ∆y/∆x = (y-ay)/(x-ax) – –∆x*(y-ay) + ∆y*(x – ax) = 0 • Define F(x, y) = –2 ∆x*(y - ay) + 2 ∆y*(x – ax) – equation of line is F(x, y) = 0 Bresenham Technique 20 January 3, 2023 DOI, Kurukshetra University 1 U M’’ Ideal line M’ M L Q P=(px, py )
  • 11. 1/3/2023 11 • Grid: Points on the grid are centers of pixels • Let P a pixel which is currently set (chosen) • Next step pixel: Candidates L (low) and U (up) • Midpoint between them M = (px+1, py+1/2) • if ideal line below M – Set next pixel L – (px+1, py) • Else set next pixel U – (px+1, py+1) • how do we decide? Bresenham Technique 21 January 3, 2023 DOI, Kurukshetra University 1 U M’’ Ideal line M’ M L Q P=(px, py ) • Case 1: line below M F(Mx, My) < 0 – Next M is M’ = (px+2, py+1/2) – next F: F(px+2, py+1/2) = -2∆x(py+1/2 – ay) + 2∆y(px+2-ax) • Calculate the change in F, from this step to next step: – ∆F = F(px+2, py+1/2) - F(px+1, py+1/2) – ∆F = 2∆y(px+2-ax) - 2∆y(px+1-ax) = 2∆y • F(M’x, M’y) = F(Mx,My) + 2∆y (constant integer) • Case 2: line above M F(Mx, My) > 0 – Next M is M” = (px+2, py+3/2) – F = F(px+2, py+3/2) - F(px+1, py+1/2) = -2(∆x-∆y) – F(M”x, M”y) = F(Mx, My) – 2(∆x-∆y) • In either case, F changes by a constant integer: – 2∆y if y was not incremented (negative F) – -2(∆x-∆y) if y was incremented (positive F) Bresenham Technique 22 January 3, 2023 DOI, Kurukshetra University 1 U M’’ Ideal line M’ M L Q P=(px, py )
  • 12. 1/3/2023 12 Calculate the initial point value • M = (ax+1, ay+1/2) – F(ax, ay) = -2∆x(ay+1/2-ay) + 2∆y(ax+1-ax) = 2∆y-∆x – Initial value: F = 2∆y-∆x • For (i = ax; x <= bx; x++) { Set pixel (x, y) If F< 0 { F += 2∆y; // no change in y else { y++; F += 2(∆y-∆x) }} } Bresenham algorithm 23 January 3, 2023 DOI, Kurukshetra University • Until now: ax < bx and slope < 1 – Other cases: (slope >1; negative slopes) • Exercises – tricks: replace roles of X and Y, and ∆x with –∆x • Drawing Patterned lines: – Patterned lines (dotted or dashed lines) • store the desired line pattern in a bit mask • When a pixel is selected, consult the bit mask to check whether it should be drawn. • Another approach Bresenham Line Algorithm 24 January 3, 2023 DOI, Kurukshetra University
  • 13. 1/3/2023 13 Bresenham Line Algorithm 25 January 3, 2023 DOI, Kurukshetra University for (0<m<1) d2 d1 x Xk Xk+1 Xk+2 yk+2 yk+1 yk If d2 > d1 Plot yk else Plot yk +1 ➢ Is it possible to compute and compare d1 and d2 using only integer operations? y = m (xk+ 1) + b d2 = (yk + 1) − y = yk +1 − m (xk + 1) − b d1 = y − yk= m (xk + 1) + b − yk d1−d2 = 2m (xk +1)− 2yk + 2b − 1 d1−d2 = 2 dy/dx (xk +1)− 2yk + 2b − 1 dx(d1−d2) = 2 dy (xk +1)− 2 dx yk + dx (2b − 1) dx(d1−d2) = 2 dy xk +2 dy− 2 dx yk + dx (2b − 1) Let 2dy + dx(2b−1) = c We have: dx(d1−d2) = 2 dy xk - 2 dx yk + c Bresenham Line Algorithm 26 January 3, 2023 DOI, Kurukshetra University d2 d1 x Xk Xk+1 Xk+2 yk+2 yk+1 y k
  • 14. 1/3/2023 14 • since 0 < m < 1, dx > 0 – dx(d1−d2) has the same sign as of (d1−d2), so – if (d1−d2) < 0, – then yk +1 = yk – else yk +1 = yk +1 • Let Pk= (d1−d2) = 2dy xk − 2dx yk + c where c = 2dy + dx(2b−1), hence • Pk+1= 2dy(xk +1)- 2dx(yk+1)+ c Bresenham Line Algorithm 27 January 3, 2023 DOI, Kurukshetra University Subtracting each other as shown on next page……. Selection criterion for yk +1 • Continued from previous page: To derive Pk+1 from Pk – Pk+1-Pk= 2dy(xk +1- xk )- 2dx(yk+1-yk) – Pk+1=Pk + 2dy- 2dx = Pk + 2(dy- dx) • At initial value when k = 0, let’s consider again the previous equation – P0= (d1−d2) = 2dy x0 − 2dx y0 + c, • substitute value of c here – P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx(2b−1) • further at k = 0, form y0 = m x0 + b, – b = y0 - m x0 • substitute the b in above equation Bresenham Line Algorithm 28 January 3, 2023 DOI, Kurukshetra University
  • 15. 1/3/2023 15 – P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx (2(y0 - m x0 ) −1) – P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + dx (2(y0 - dy/dx x0 ) −1) – P0= (d1−d2) = 2dy x0 − 2dx y0 + 2dy + 2 dx y0 - 2 dy x0 − dx – P0= (d1−d2) = 2dy − dx • (at the starting point of line or initial condition) Bresenham Line Algorithm 29 January 3, 2023 DOI, Kurukshetra University • Given end points (x0, y0) (x1, y1) dx = x1−x0, dy=y1−y0 • Starting with an end point (x0, y0): 1. Compute P0 = 2dy −dx 2. For each k, staring with k = 0 if (Pk < 0) the next point is (Xk+1, Yk), Pk+1 = Pk + 2 dy, else the next point is (Xk+1, Yk+1), Pk+1 = Pk + 2dy − 2dx 3. Repeat step 2 x1−x0 times Bresenham’s Line Algorithm 30 January 3, 2023 DOI, Kurukshetra University
  • 16. 1/3/2023 16 • Given end points (x0, y0) (x1, y1) dx = x1−x0, dy=y1−y0 • Starting with an end point (x0, y0): 1. Compute P0 = 2dy −dx 2. For each k, staring with k = 0 if (Pk < 0) the next point is (Xk+1, Yk), Pk+1 = Pk + 2 dy, else the next point is (Xk+1, Yk+1), Pk+1 = Pk + 2dy − 2dx 3. Repeat step 2 x1−x0 times Bresenham’s Line Algorithm 31 January 3, 2023 DOI, Kurukshetra University void line(int x0, int y0, int x1, int y1, int value) { int dx = x1-x0,dy = y1 – y0; int d = 2 * dy-dx; /* Initial Value of d int incrL = 2*dy; /* increment used incrU = 2*(dy-dx); /* increment used int x = x0, y = y0; putpixel (x,y, value) /* start p while (x < x1) { if (d <= )) { /*choose L */ d +=incrL; x++;} else { d += incrU; /* Choose U */ x++; y++; } putpixel (x, y, value); /* selected pixel clo } /* while */ } /* Midpoint Line */ void line(int x0, int y0, int x1, int y1, int value) { int dx = x1-x0,dy = y1 – y0; int d = 2 * dy-dx; /* Initial Value of d*/ int incrL = 2*dy; /* increment used for move to L*/ incrU = 2*(dy-dx); /* increment used for move to U*/ int x = x0, y = y0; putpixel (x,y, value) /* start pixel */ while (x < x1) { if (d <= )) { /*choose L */ d +=incrL; x++;} else { d += incrU; /* Choose U */ x++; y++; } putpixel (x, y, value); /* selected pixel closest to the line */ } /* while */ } /* Midpoint Line */ Bresenham algorithm 32 January 3, 2023 DOI, Kurukshetra University
  • 17. 1/3/2023 17 Bresenham algorithm 33 January 3, 2023 DOI, Kurukshetra University January 3, 2023 DOI, Kurukshetra University 34 Any Question? Thanks!