Chapter - Two
Chapter - Two
(CoSc3072)
JIMMA UNIVERSITY
JIMMA INSTITUTE OF TECHNOLOGY
FACILITY OF COMPUTING AND INFORMATICS
Point Drawing in
OpenGL
Line Drawing
Algorithms
Circle Drawing
Algorithms
Fill-Area
Primitives
zero)
Computer Graphics- CoSc3072 02/11/25
8
Lines
Lines are a very common primitive and will be
supported by almost all graphics packages.
Lines are normally represented by the two end-points.
Point Drawing in
OpenGL
Line Drawing
Algorithms
Circle Drawing
Algorithms
Fill-Area
Primitives
y end y 0 c y mx
m 0 0
xend x 0 δy = m.δx
δx = (1/m).δy
Computer Graphics- CoSc3072 02/11/25
9 Digital Differential Analyzer
Algorithm
• The Digital Differential Analyzer helps
Point Drawing in us to interpolate the variables on an
OpenGL interval from one point to another point.
Line Drawing • We can use the digital Differential
Algorithms
Analyzer algorithm to perform
DDA Algorithm rasterization on polygons, lines, and
triangles
Bresenham’s • Is
Algorithm
an incremental scan conversion
method.
Circle Drawing
• Characterized by performing calculation
Algorithms
at each steps using results from the
Fill-Area
Primitives
previous steps.
Computer Graphics- CoSc3072 02/11/25
10 Digital Differential Analyzer
Algorithm
Steps In DDA:
1.Get the inputs to the two end points of the line( i.e initial and
Point Drawing in end points).
OpenGL
2.Calculate the difference between the two end points.
Line Drawing dx=X1-X0 and dy=Y1-Y0
Algorithms 3. Based on the calculated difference in step 2, you need to
identify the number of steps to put pixels. If dx>dy, you need
DDA Algorithm more steps in x coordinate otherwise in y coordinate.
if (absolute (dx)>absolute (dy) )
Point Drawing in
Given (x0,y0) = (10,10)
y end y 0 (13 10) 3
m 0.6
OpenGL
and (xend,yend) =
(15,13)
xend x0 (15 10) 5
Compute m
Line Drawing If |m| ≤ 1:
Algorithms δx = 1
δx = 1
δy = m
δy = 0.6
DDA Algorithm If |m| > 1:
δx = 1/m
δy = 1
Bresenham’s (xend,yend) = (15,13)
Algorithm Paint (x0,y0)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Point Drawing in
Given (x0,y0) = (10,10)
y end y 0 (13 10) 3
m 0.6
OpenGL
and (xend,yend) =
(15,13)
xend x0 (15 10) 5
Compute m
Line Drawing If |m| ≤ 1:
Algorithms δx = 1
δx = 1
δy = m
δy = 0.6
DDA Algorithm If |m| > 1:
δx = 1/m
δy = 1
Bresenham’s (xend,yend) = (15,13)
Algorithm Paint (x0,y0)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Point Drawing in
Given (x0,y0) = (10,10)
m
yend y0 (15 10) 5 1.25
OpenGL
and (xend,yend) =
xend x0 (14 10) 4
(14,15)
Compute m
Line Drawing δx = 0.8
If |m| ≤ 1:
Algorithms δx = 1 δy = 1
δy = m
DDA Algorithm
(xend,yend) = (14,15)
If |m| > 1:
δx = 1/m
δy = 1
Bresenham’s
Algorithm Paint (x0,y0)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Point Drawing in
Given (x0,y0) = (10,10)
m
yend y0 (15 10) 5
OpenGL
and (xend,yend) =
xend x0 (14 10) 4
(14,15)
Compute m
Line Drawing δx = 0.8
If |m| ≤ 1:
Algorithms δx = 1 δy = 1
δy = m
DDA Algorithm
(xend,yend) = (14,15)
If |m| > 1:
δx = 1/m
δy = 1
Bresenham’s
Algorithm Paint (x0,y0)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Computer Graphics- CoSc3072 02/11/25
26
Digital Differential Analyzer
Algorithm Example2
Paint (x0,y0)
Find successive pixel positions δx = 0.8
Point Drawing in xk+1 = (xk + δx) δy = 1
yk+1 = (yk + δy)
OpenGL For each position (xk,yk) plot a line point at
(round(xk),round(yk))
Bresenham’s
Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Exercise2
DDA Algorithm
Consider the above examples and
plotting the line using DDA
Bresenham’s algorithm:
Algorithm Given (x0,y0) = (2,2) and (xend,yend) = (6,12)
DDA Algorithm
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Circle Drawing
Algorithms
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives (x0,y0) = (10,10)
Fill-Area
Primitives
Line Drawing •For example, the following code will draw two
Algorithms
separate line segments:
•One line from (100,200) to (150,200)
DDA Algorithm •Another line from (150,250) to (200,250)
glLineWidth(3.0);
Bresenham’s glBegin(GL_LINES);
Algorithm glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
Circle Drawing glVertex2f(150.0, 250.0);
Algorithms glVertex2f(200.0, 250.0);
glEnd();
Fill-Area
Primitives
Algorithms glBegin(GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2);
DDA Algorithm glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
Bresenham’s glEnd(); (GL_LINES)
Algorithm
Circle Drawing
Algorithms
Fill-Area (GL_LINE_LOOP)
(GL_LINE_STRIP)
Primitives
Line Drawing
Algorithms
Circle Drawing
Algorithms
Basic Algorithms
Midpoint
Algorithms
We will examine a number of approaches to plotting
Fill-Area points on a circle, culminating in the most efficient
Primitives algorithm: the midpoint algorithm.
Line Drawing
Algorithms
Circle Drawing
Algorithms
Basic Algorithms
Circle Drawing p0 = -9
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
49 Midpoint Algorithm
Example
p0 < 0,
Point Drawing in
Plot (x1,y1) = (x0+1,y0) = (1,10)
OpenGL
p1 p0 2 x1 1 9 3 6
Line Drawing
Algorithms (10,10)
Circle Drawing p0 = -9
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
50 Midpoint Algorithm
Example
p1 < 0,
Plot (x2,y2) = (x1+1,y1) = (2,10)
Point Drawing in
OpenGL p 2 p1 2 x 2 1 6 5 1
Line Drawing
Algorithms (10,10)
Circle Drawing p1 = -6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
51 Midpoint Algorithm
Example
p2 < 0,
Plot (x3,y3) = (x2+1,y2) = (3,10)
Point Drawing in
OpenGL p 3 p 2 2 x 3 1 1 7 6
Line Drawing
Algorithms (10,10)
Circle Drawing p2 = -1
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
52 Midpoint Algorithm
Example
p3 ≥ 0
Plot (x4,y4) = (x3+1,y3-1) = (4,9)
Point Drawing in
OpenGL p 4 p 3 2 x 4 1 2 y 4 6 9 3
Line Drawing
Algorithms (10,10)
Circle Drawing p3 = 6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
53 Midpoint Algorithm
Example
p4 < 0,
Plot (x5,y5) = (x4+1,y4) = (5,9)
Point Drawing in
OpenGL p5 p 4 2 x5 1 3 11 8
Line Drawing
Algorithms (10,10)
Circle Drawing p4 = -3
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
54 Midpoint Algorithm
Example
p5 ≥ 0,
Plot (x6,y6) = (x5+1,y5-1) = (6,8)
Point Drawing in
OpenGL p6 p5 2 x6 1 2 y 6 8 3 5
Line Drawing
Algorithms (10,10)
Circle Drawing p5 = 8
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
55 Midpoint Algorithm
Example
p6 ≥ 0,
Plot (x7,y7) = (x6+1,y6-1) = (7,7)
Point Drawing in
OpenGL p7 p6 2 x7 1 2 y7 19 13 6
Line Drawing
Algorithms (10,10)
Circle Drawing p6 = 5
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
56 Midpoint Algorithm
Example
p7 ≥ 0,
Plot (x8,y8) = (x7+1,y7-1) = (8,6)
Point Drawing in
OpenGL p8 p7 2 x8 1 2 y8 22 11 11
Line Drawing
Algorithms (10,10)
Circle Drawing p7 = 6
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
57 Midpoint Algorithm
Example
p8 ≥ 0,
Plot (x9,y9) = (x8+1,y8-1) = (9,5)
Point Drawing in
OpenGL p9 p8 2 x9 1 2 y9 29 9 20
Line Drawing
Algorithms (10,10)
Circle Drawing p8 = 11
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
58 Midpoint Algorithm
Example
p9 ≥ 0,
Plot (x10,y10) = (x9+1,y9-1) = (10,4)
Point Drawing in
OpenGL
Line Drawing
Algorithms (10,10)
Circle Drawing p9 = 20
Algorithms
Basic Algorithms
Midpoint
Algorithms
Fill-Area
Primitives
(0,0)
Computer Graphics- CoSc3072 02/11/25
59
Bresenham’s Line-Drawing
Algorithm
Exercise Given (x0,y0) = (10,10) and (xend,yend) = (14,15)
Compute m
|m| ≥ 1 m = 1.25 ≥ 1
Point Drawing in
Plot (x0,y0)
OpenGL
Compute the first
Line Drawing decision variable:
Algorithms p0 2x y
DDA Algorithm
(xend,yend) = (14,15)
Exercise4
Bresenham’s Consider the above
Algorithm
examples and plotting the
line using Bresenham’s
Circle Drawing
algorithm:
Algorithms
Given (x0,y0) = (10,10) and
Circle Drawing P = -4
Algorithms Exercise5
Consider the above
Basic Algorithms example and express the
midpoint circle-drawing
algorithm for a circle
Midpoint centred at the origin.
Algorithms
(0,0) Given the center (0,0)
Fill-Area and radius r = 5
Primitives
Basic Algorithms
Exercise6
Midpoint Consider the above
Algorithms example and create a
OpenGL Circle solid sphere / circle with a
Drawing radius of 1 units.
Fill-Area
Primitives
Computer Graphics- CoSc3072 02/11/25
Any Question?