Uploaded by Umair Naqvi

chapter 3

advertisement
Graphics
OutPut Primitives
By : Engr. Syed Atir Iftikhar
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Basic Graphics System
CGVR
Output device
Input devices
Image formed in FB
[Edward Angel, Interactive computer Graphics, 2009]
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Raster Graphics
Compile by : Engr. Syed Atir Iftikhar
CGVR
Graphics Lab @ Korea University
Frame Buffer
Compile by : Engr. Syed Atir Iftikhar
CGVR
Graphics Lab @ Korea University
Frame Buffer Refresh

CGVR
Refresh Rate

Usually 30~75 Hz
cgvr.korea.ac.kr
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Color CRT
cgvr.korea.ac.kr
Compile by : Engr. Syed Atir Iftikhar
CGVR
Graphics Lab @ Korea University
LINE-DRAWING ALGORITHMS
CGVR
Graphics Lab @ Korea University
LINE-DRAWING ALGORITHMS
CGVR
Graphics Lab @ Korea University
LINE-DRAWING ALGORITHMS
Compile by : Engr. Syed Atir Iftikhar
CGVR
Graphics Lab @ Korea University
LINE-DRAWING ALGORITHMS
CGVR

LINE-DRAWING ALGORITHMS
 The Cartesian slope-intercept equation for a
straight line is

y=m.x+b
 with m representing the slope of the line and b
as they intercept. Given that the two endpoints
of a line segment are specified at positions (x ,
y ,) and (x , y ), as shown in Fig. we can
determine values for the slope m and y intercept
b with the following calculations:
1
1
2
2
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
LINE-DRAWING ALGORITHMS
CGVR

Numerical example of finding slope m:
 (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
By  Ay 96  41
55
m


 0.5392
Bx  Ax 125  23 102
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Output Primitives
CGVR

Points
 Lines

DDA Algorithm
 Bresenham’s Algorithm

Polygons

Scan-Line Polygon Fill
 Inside-Outside Tests
 Boundary-Fill Algorithm
 Antialiasing
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Points

CGVR
Single Coordinate Position

Set the bit value(color code) corresponding to a
specified screen position within the frame buffer
y
setPixel (x, y)
x
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Lines

CGVR
Intermediate Positions between Two
Endpoints

DDA, Bresenham’s line algorithms
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
CGVR
cgvr.korea.ac.kr
Graphics Lab @ Korea University
DDA Algorithm
CGVR

The digital differential analyzer (DDA) is a scanconversion line algorithm based on calculating
either y or x,

Case 1:

Consider first a line with positive slope, as
shown in Fig. 3-3. If the slope is less than or
equal to 1, we sample at unit x intervals ( x = 1)
and compute each successive y value as
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA Algorithm

CGVR
Digital Differential Analyzer

0 < Slope <= 1

Unit x interval = 1
Subscript k takes integer values starting y2
from 1, for the first point, and increases
y1
by 1 until the final endpoint is reached.
Since m can be any real number between
0 and 1, the calculated y values must

be rounded to the nearest integer.
x1
x2
y k 1  y k  m
Equation 3.6
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA Algorithm

CGVR
Digital Differential Analyzer

0 < Slope <= 1


Unit x interval = 1
Slope > 1

y2
Unit y interval = 1
For lines with a positive slope greater than y1
1 we reverse the roles of x and y. That is, we
sample at unit y intervals ( y = 1) and calculate
each succeeding x value as

x1
x k 1
x2
1
 xk 
m
Equation 3.7
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA Algorithm

CGVR
Digital Differential Analyzer

0 < Slope <= 1


Slope > 1


Unit x interval = 1
y1
Unit y interval = 1
y2
-1 <= Slope < 0

Unit x interval = -1
x1

Equations 3-6 and 3-7 are based on the assumption
that lines are to be processed from the left endpoint
to the right endpoint (Fig. 3-3). If this processing is reversed,
so that the starting endpoint is at the right, then either
we have
x = -1
x2
y k 1  y k  m
Equation 3.8
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA Algorithm

CGVR
Digital Differential Analyzer

Slope >= 1


0 < Slope < 1


y2
Unit y interval = 1
-1 <= Slope < 0


Unit x interval = 1
y1
Unit x interval = -1
Slope < -1

Unit y interval = -1
or (when the slope is greater than 1)
we have y = -1 with
x1
x k 1
x2
1
 xk 
m
Compile by : Engr. Syed Atir Iftikhar
Equation 3.9
Graphics Lab @ Korea University
DDA Algorithm

CGVR
Advantages

DDA algorithm is a faster method for
calculating the pixel positions than the
direct use of line equation.
• It eliminates multiplication by making use of
raster characteristics.
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA Algorithm
CGVR

Disadvantages
•
DDA algorithm runs slowly because it
requires real arithmetic (floating point
operations) and rounding operations
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
DDA versus Bresenham’s Algorithm
CGVR
The Bresenham line algorithm has the following
advantages:


An fast incremental algorithm
Uses only integer calculations
Comparing this to the DDA algorithm, DDA has the
following problems:


Accumulation of round-off errors can make the
pixelated line drift away from what was intended
The rounding operations and floating point arithmetic
involved are time consuming
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Bresenham’s Algorithm
cgvr.korea.ac.kr
CGVR
Graphics Lab @ Korea University
Bresenham’s Line Algorithm

CGVR
Midpoint Line Algorithm

Decision variable
d  F M 
NE
1

 F  x p  1, y p  
2

Q
1

 a x p  1  b y p    c
2


d > 0 : choose NE


d new
M
P(xp, yp)
E
3

 F  x p  2 , y p   : dnew= dold+a+b
2

d <= 0 : choose E

d new
1

 F  x p  2, y p  
2

: dnew= dold+a
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)

CGVR
Initial Value of d
1
1


F  x0  1, y 0    a  x0  1  b y 0    c
2
2


1
 F  x0 , y 0   a  b
2
 F  x , y   2 a x  b y  c 
 d  2a  b

Update d
 x  ,

if d  0, then  y   ,
 d   2 a  b 

 x  ,
if d  0, then 
d   2a
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)
CGVR
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
Input the two line end-points, storing the left end-point
in (x0, y0)

1.
2.
Plot the point (x0, y0)
3.
Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx)
and get the first value for the decision parameter as:
p0  2y  x
4.
At each xk along the line, starting at k = 0, perform the
following test. If pk < 0, the next point to plot is
(xk+1, yk) and: p  p  2y
k 1
k
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)
CGVR
Otherwise, the next point to plot is (xk+1, yk+1) and:

pk 1  pk  2y  2x
5.
Repeat step 4 (Δx) times
The algorithm and derivation above assumes slopes are less than 1. for other s
lopes we need to adjust the algorithm slightly.
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Adjustment
CGVR
For m>1, we will find whether we will increment x
while incrementing y each time.
After solving, the equation for decision parameter
pk will be very similar, just the x and y in the
equation will get interchanged.
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)
CGVR

EXAMPLE :

To illustrate the algorithm, we digitize the line
with endpoints (20, 10) and (30,18). This line
has a slope of 0.8, with

x = 10 ,
y=8
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Example – Draw a line from (20,10) to (30,18)
x = 10
CGVR
y=8
initial decision parameter has the value p0 = 2 y – x = 6
(30,18)
Increment for calculating successive decision parameter are
2 y = 16, 2 y – 2 x) = -4
(20,10)
k pk (xk+1,yk+1)
0
1
2
3
4
5
6
7
8
9
6
2
-2
14
10
6
2
-2
14
10
19
(21,11)
18
(22,12)
17
(23,12)
16
(24,13)
15
(25,14)
14
(26,15)
13
(27,16)
12
(28,16)
11
(29,17)
10
(30,18)
20 21 22 23 24 25 26 27 28 29 30 31 32
Compile by : Engr. Syed Atir Iftikhar Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)
CGVR
20 , 10
Pk
(X k+1 , Y k+1)
0
6
21 , 11
6 + 16 -20 = 2
1
2
22 , 12
2 + 16 – 20 = -2
2
-2
23 , 12
-2 +16 = 14
3
14
24 , 13
14+ 16 – 20 = 10
4
10
25 , 14
10 + 16 - 20 = 6
5
6
26 , 15
6 +16 -20 = 2
6
2
27 , 16
2 + 16 – 20 = -2
7
-2
28 , 16
-2 +16 = 14
8
14
29 , 17
14 + 16 -20 = 10
9
10
30 , 18
10 +16 - 20 = 6
If pk < 0, the next point to plot is (xk+1, yk) and: pk 1 
Compile by : Engr. Syed Atir Iftikhar
Pk + 2
y–2
K
x
pk  2y
Graphics Lab @ Korea University
Bresenham’s Algorithm(cont.)

CGVR
Go through the steps of the Bresenham line
drawing algorithm for a line going from (21,12)
to (29,16)
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Circle :


CGVR
Drawing a circle on the screen is a little complex than
drawing a line. There are two popular algorithms for
generating a circle − Bresenham’s Algorithm and
Midpoint Circle Algorithm. These algorithms are
based on the idea of determining the subsequent points
required to draw the circle. Let us discuss the algorithms
in detail −
In computer graphics, the midpoint circle algorithm is
an algorithm used to determine the points needed for
rasterizing a circle. Bresenham's circle algorithm is
derived from the midpoint circle algorithm.
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Mid-Point Circle Algorithm
Similarly to the case with lines, there
is an incremental algorithm for
drawing circles – the mid-point circle
algorithm
In the mid-point circle algorithm we
use eight-way symmetry so only
ever calculate the points for the top
right eighth of a circle, and then use
symmetry to get the rest of the
points
Compile by : Engr. Syed Atir Iftikhar
CGVR
The mid-point circle
algorithm was developed
by Jack Bresenham, who
we heard about earlier.
Bresenham’s patent for
the algorithm can be
v
i
e
d
Graphics Lab @ Korea University
Mid-Point Circle Algorithm
cgvr.korea.ac.kr
CGVR
Graphics Lab @ Korea University
Mid-Point Circle Algorithm
cgvr.korea.ac.kr
CGVR
Graphics Lab @ Korea University
Mid-Point Circle Algorithm
cgvr.korea.ac.kr
CGVR
Graphics Lab @ Korea University
The Mid-Point Circle Algorithm
1.
CGVR
MID-POINT CIRCLE ALGORITHM
Input radius r and circle centre (xc, yc), then set the
coordinates for the first point on the circumference of a
circle centred on the origin as:
( x0 , y0 )  (0, r )
2.
Calculate the initial value of the decision parameter as:
p0  1  r
3.
Starting with k = 0 at each position xk, perform the
following test. If pk < 0, the next point along the circle
centred on (0, 0) is (xk+1, yk) and:
pk 1  pk  2 xk 1  1
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
The Mid-Point Circle Algorithm (cont…)
CGVR
Otherwise the next point along the circle is (xk+1, yk-1)
and:
pk 1  pk  2 xk 1  1  2 yk 1
5.
Where 2x k+1 = 2xk + 2 and 2yk+1 = 2yk - 2
Determine symmetry points in the other seven octants
Move each calculated pixel position (x, y) onto the
circular path centred at (xc, yc) to plot the coordinate
values:
x  x  xc
y  y  yc
6.
Repeat steps 3 to 5 until x >= y
4.
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Mid-Point Circle Algorithm Example
CGVR
To see the mid-point circle algorithm in action lets
use it to draw a circle centred at (0,0) with radius
10
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Mid-Point Circle Algorithm Exercise
CGVR
Use the mid-point circle algorithm to draw the
circle centred at (0,0) with radius 15
Compile by : Engr. Syed Atir Iftikhar
Graphics Lab @ Korea University
Download