Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Raster Graphics
To prepare the slides help was taken from Graphics Lab, Korea University
Output Primitives
Points
Lines
DDA Algorithm
Bresenham’s Algorithm
Polygons
Scan-Line Polygon Fill
Inside-Outside Tests
Boundary-Fill Algorithm
Antialiasing
To prepare the slides help was taken from Graphics Lab, Korea University
Scan Conversion
To prepare the slides help was taken from Graphics Lab, Korea University
Points
y
setPixel (x, y)
To prepare the slides help was taken from Graphics Lab, Korea University
Pixel Co-ordinates
A pixel (10, 6)
Y
X
A point (-12.3, 10.3, 0)
Z
Y
SCREEN COORDINATES
a.k.a device co-ordinates, pixel co-ordinates
MODELLING CO-ORDINATES On display hardware we deal with finite,
discrete coordinates
Mathematically vectors are defined in
X, Y values in positive integers
an infinite, “real-number” cartesian co-
ordinate system 0,0 is measured from top-left usually with +Y
pointing down
To prepare the slides help was taken from Graphics Lab, Korea University
WHERE TO DRAW A LINE??
To prepare the slides help was taken from Graphics Lab, Korea University
How does a machine draw Lines?
To prepare the slides help was taken from Graphics Lab, Korea University
Lines
Jaggies
= Aliasing
To prepare the slides help was taken from Graphics Lab, Korea University
Line Equation
y m x b
m is a constant which b is the value of x
represents the where the line (x1, y1)
slope/gradient of the line intersects the y-axis
To prepare the slides help was taken from Graphics Lab, Korea University
6 (3, 3)
5
4
3
2
1
0
0 1 2 3 4 5 6
To prepare the slides help was taken from Graphics Lab, Korea University
Line drawing (cont)
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm
Dy
m Dy mDx
Dx
i.e. for each pixel we move right (along the x axis), we need to move down
(along the y-axis) by m pixels
In pixels, the gradient represents how many pixels we step upwards (Dy) for
every step to the right (Dx)
To prepare the slides help was taken from Graphics Lab, Korea University
Line drawing
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm
yk 1 yk m
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm
Slope > 1 y2
Unit y interval, ∆y = 1
y1
x1 x2
1
xk 1 xk
m
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm
Slope > 1 y1
Unit y interval, ∆y = 1
y2
-1 <= Slope < 0
Unit x interval, ∆x = -1
x1 x2
yk 1 yk m
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm
Slope > 1 y2
Unit y interval, ∆y = 1
To prepare the slides help was taken from Graphics Lab, Korea University
Digital differential analyser
To prepare the slides help was taken from Graphics Lab, Korea University
DDA ( Digital Differential
Algorithm )
m<1
To prepare the slides help was taken from Graphics Lab, Korea University
DDA ( Digital Differential
Algorithm )
m>1
To prepare the slides help was taken from Graphics Lab, Korea University
DDA ( Digital Differential
Algorithm )
m>1
To prepare the slides help was taken from Graphics Lab, Korea University
Example
To prepare the slides help was taken from Graphics Lab, Korea University
Rounding Off
X Y Rounded { x, y }
3.0 0.99 { 3, 1 }
But we Round Off to the nearest
whole number just before we draw 4.0 1.32 { 4, 1 }
the pixel.
e.g. if m=.33 …
To prepare the slides help was taken from Graphics Lab, Korea University
DDA Illustration
Desired Line
(xi+1, Round(yj+m))
(xi, yj)
(xi+1, yj+m)
(xi, Round(yj))
y2
y1
x1 x2
To prepare the slides help was taken from Graphics Lab, Korea University
Symmetry
(-y,x) (y,x)
(x,-y) (x,y)
450
(-x,-y)
(x,-y)
(-y,-x) (y,-x)
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm
Starting from the left end point (x0,y0) of a given line , we step to
each successive column (x position) and plot the pixel whose
scan-line y value closest to the line path
Assuming we have determined that the pixel at (xk,yk) is to
be displayed, we next need to decide which pixel to plot in
column xk+1.
To prepare the slides help was taken from Graphics Lab, Korea University
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham's line algorithm
y = mx + b
d2
y = m(x+1) + b
y d1
x x+1
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Line Algorithm
(cont)
A decision parameter pk for the kth step in the line algorithm can
be obtained by rearranging above equation so that it involves
only integer calculations
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm
Define
Pk = Δx ( d1 - d2) = 2Δyxk - 2 Δxyk + c
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s algorithm (cont)
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm
Input the two end points and store the left end
point in (x0,y0)
Load (x0,y0) into the frame buffer (plot the first
point)
Calculate the constants Δx, Δy, 2Δy and 2Δy-2Δx
and obtain the starting value for the decision
parameter as
p0 = 2Δy- Δx
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
The Bresenham Line
Algorithm
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1. Input the two line end-points, storing the left end-point in (x0, y0)
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 2Dy Dx
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:
pk 1 pk 2Dy
To prepare the slides help was taken from Graphics Lab, Korea University
The Bresenham Line Algorithm
(cont…)
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2Dy 2Dx
5. Repeat step 4 (Δx – 1) times
To prepare the slides help was taken from Graphics Lab, Korea University
DDA versus Bresenham’s Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Example
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Example (cont…)
k pk (xk+1,yk+1)
18
0 6 (21, 11)
17
1 2 (22, 12)
16
2 -2 (23, 12)
15 3
14 (24, 13)
14 4
10 (25, 14)
5
13 6 (26, 15)
6
12 2 (27, 16)
7
11 -2 (28, 16)
8
14 (29, 17)
10 9
10 (30, 18)
20 21
To prepare 22help was
the slides 23 taken
24from25 26Lab, 27
Graphics 28 29
Korea University 30
Bresenham Exercise
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Exercise (cont…)
18 k pk (xk+1,yk+1)
17 0
16 1
2
15
3
14
4
13 5
12 6
11 7
8
10
20 21 22 23 24 25 26 27 28 29 30
To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Line Algorithm
Summary
To prepare the slides help was taken from Graphics Lab, Korea University
Other Quadrants
-Swap y0 for y1
-Step for x instead
of y Step for y
instead of x
Note that this only applies to - Increment by -x
lines with a positive gradient
-Increment by -y
But you can easily write a -Swap y0 for y1
separate case for each other
case
Increment by -y
Also if the gradient is too steep Swap x0 for x1
To prepare the slides help was taken from Graphics Lab, Korea University
A Simple Circle Drawing
Algorithm
The equation for a circle is:
x y r
2 2 2
y r 2 x2
To prepare the slides help was taken from Graphics Lab, Korea University
A Simple Circle Drawing Algorithm
(cont…)
y0 20 2 0 2 20
y1 202 12 20
y2 202 22 20
y19 20 2 19 2 6
y20 20 2 20 2 0
To prepare the slides help was taken from Graphics Lab, Korea University
A Simple Circle Drawing Algorithm
(cont…)
To prepare the slides help was taken from Graphics Lab, Korea University
Eight-Way Symmetry
(-x, y) (x, y)
(-y, x) (y, x)
R
2
(-y, -x) (y, -x)
(x,y)
r
yc ө
xc
For any circle point (x,y), the distance relationship is expressed by
the Pythagorean theorem in Cartesian coordinate as:
( x xc ) 2 ( y yc ) 2 r 2
To prepare the slides help was taken from Graphics Lab, Korea University
Circle Generating Algorithm
We could use this equation to calculate the points on a circle
circumference by stepping along x-axis in unit steps from xc–r
to xc+r and calculate the corresponding y values as
y yc r 2 ( xc x)2
To prepare the slides help was taken from Graphics Lab, Korea University
The problems:
Involves many computation at
each step
Spacing between plotted pixel
positions is not uniform
Adjustment: interchanging x & y
(step through y values and
calculate x values)
Involves many computation too!
To prepare the slides help was taken from Graphics Lab, Korea University
Another way:
Calculate points along a circular boundary using polar
coordinates r and ө
x = xc + r cos ө
y = yc + r sin ө
To prepare the slides help was taken from Graphics Lab, Korea University
Consider symmetry of circles
Shape of the circle is similar in each quadrant
To prepare the slides help was taken from Graphics Lab, Korea University
Circle sections in adjacent octants within 1 quadrant are
symmetric with respect to the 45° line dividing the 2
octants
(-y,x) (y,x)
(x,y)
(-x,y)
450
(-x,-y) (x,-y)
(-y,-x) (y,-x)
To prepare the slides help was taken from Graphics Lab, Korea University
Midpoint Circle Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
Midpoint Circle Algorithm
Along a circle section from x=0 to x=y in the 1st quadrant, the
slope (m) of the curve varies from 0 to -1.0
i.e. we can take unit steps in the +ve x direction over the
octant & use decision parameter to determine which 2 possible
positions is vertically closer to the circle path
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
(cont…)
Let’s re-jig the equation of the circle slightly to
give us:
f circ ( x, y ) x 2 y 2 r 2
The equation evaluates as follows:
0, if ( x, y ) is inside the circle boundary
f circ ( x, y ) 0, if ( x, y ) is on the circle boundary
0, if ( x, y ) is outside the circle boundary
By evaluating this function at the midpoint
between the candidate pixels we can make our
decision
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
(cont…)
Assuming we have just plotted the pixel at (xk,yk)
so we need to choose between (xk+1,yk) and
(xk+1,yk-1)
Our decision variable can be defined as:
pk f circ ( xk 1, yk 1 )
2
( xk 1) 2 ( yk 1 ) 2 r 2
If pk < 0 the midpoint is inside the 2 circle and and
the pixel at yk is closer to the circle
Otherwise the midpoint is outside and yk-1 is
closer
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
(cont…)
To ensure things are as efficient as possible we
can do all of our calculations incrementally
First consider:
pk 1 f circ xk 1 1, yk 1 1 2
or:
[( xk 1) 1] yk 1 12
2 r
2
2
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
(cont…)
The first decision variable is given as: (x , y =(0,r))
p0 f circ (1, r 1 )
o o
2
1 (r ) r
1 2 2
2
5 r
4
Then if pk < 0 then the next decision variable is
given as:
pk 1 pk 2 xk 1 1
If pk > 0 then the decision variable is:
pk 1 pk 2 xk 1 1 2 yk 1
To prepare the slides help was taken from Graphics Lab, Korea University
The Mid-Point Circle Algorithm
( x0 , y0 ) (0, r )
Calculate the initial value of the decision parameter as:
p 5 r
Starting with k = 0 at each 4 xk, perform the following test.
0 position
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
To prepare the slides help was taken from Graphics Lab, Korea University
The Mid-Point Circle Algorithm
(cont…)
Otherwise the next point along the circle is (xk+1, yk-1) and:
pk 1 pk 2 xk 1 1 2 yk 1
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular path
centred at (xc, yc) to plot the coordinate values:
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
Example
To see the mid-point circle algorithm in action
lets use it to draw a circle centred at (0,0) with
radius 10
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
Example (cont…)
0 1 2 3 4 5 6 7 8 9 10
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
Exercise
Use the mid-point circle algorithm to draw the
circle centred at (0,0) with radius 15
To prepare the slides help was taken from Graphics Lab, Korea University
Mid-Point Circle Algorithm
Example (cont…)
16
k pk (xk+1,yk+1) 2xk+1 2yk+1
15
14
0
13
1
12
2
11
3
10
4
9
8 5
7 6
6 7
5 8
4 9
3 10
2 11
1 12
0
To0prepare
1 2the 3 4 help
slides 5 was
6 taken 10 11 Lab,
7 8from9Graphics 12 13 14University
Korea 15 16
Mid-Point Circle Algorithm
Summary
The key insights in the mid-point circle algorithm
are:
Eight-way symmetry can hugely reduce the work in
drawing a circle
Moving in unit steps along the x axis at each point
along the circle’s edge we need to choose between
two possible y coordinates
To prepare the slides help was taken from Graphics Lab, Korea University
Filling Polygons
To prepare the slides help was taken from Graphics Lab, Korea University
Scan-Line Polygon Fill
Algorithm
10 Scan Line
0
2 4 6 8 10 12 14 16
To prepare the slides help was taken from Graphics Lab, Korea University
Scan-Line Polygon Fill
Algorithm
The basic scan-line algorithm is as follows:
Find the intersections of the scan line with all edges
of the polygon
Sort the intersections by increasing x coordinate
Fill in all pixels between pairs of intersections that lie
interior to the polygon
To prepare the slides help was taken from Graphics Lab, Korea University
Scan-Line Polygon Fill Algorithm
(cont…)
To prepare the slides help was taken from Graphics Lab, Korea University
Line Drawing Summary
To prepare the slides help was taken from Graphics Lab, Korea University
Polygon Scan-conversion
For each scan line, fill between start and end pixel
To prepare the slides help was taken from Graphics Lab, Korea University
Area Filling
To prepare the slides help was taken from Graphics Lab, Korea University
Antialiasing
Simple
perspective
projected texture With some
antialiasing
To prepare the slides help was taken from Graphics Lab, Korea University
Antialiasing
To prepare the slides help was taken from Graphics Lab, Korea University
Supersampling Example
To prepare the slides help was taken from Graphics Lab, Korea University
But we don’t actually render
this hi-res line. Instead we
downsample it using a “trick”
To prepare the slides help was taken from Graphics Lab, Korea University
Anti-aliasing with
Accumulation
To prepare the slides help was taken from Graphics Lab, Korea University