Sei sulla pagina 1di 83

Graphics

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

 Rasterization or Scan Conversion is required in order to convert


vector data to Raster format for a scanline display device
 convert each line to a set of regular pixels.
 determine inside/outside areas when filling polygons.
 scan-convert curves

 The scan conversion process is interleaved with other processes to


deliver and improve the final image, some of which are not entirely
restricted to the discretization of vectors but are involved in
generally determining the colour of each pixel in the raster. e.g:
 shading & illumination,
 hidden surface removal
 texture mapping
 depth testing

To prepare the slides help was taken from Graphics Lab, Korea University
Points

 Single Coordinate Position


 Set the bit value(color code) corresponding to a
specified screen position within the frame buffer

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??

 Line drawing is accomplished by calculating intermediate positions


along the line path between specified end points.

 Precise definition of line drawing


Given two points P and Q in the plane, both with integer
coordinates, determine which pixels on a raster screen
should be on in order to make a picture of a unit-width line
segment starting from P and ending at Q.

To prepare the slides help was taken from Graphics Lab, Korea University
How does a machine draw Lines?

1. Give it a start and end position.


2. Figure out which pixels to colour in between these…
 How do we do this?
 Line-Drawing Algorithms: DDA, Bresenham’s Algorithm

To prepare the slides help was taken from Graphics Lab, Korea University
Lines

 Intermediate Positions between Two


Endpoints
 DDA, Bresenham’s line algorithms

Jaggies
= Aliasing

To prepare the slides help was taken from Graphics Lab, Korea University
Line Equation

 The equation of an infinite line (x2 , y2)


is given by

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

 The gradient m applies to the b


line as a whole but also to
every part of the line.

 So given any two points on the RISE y2  y1


line we can calculate the slope m 
RUN x2  x1

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)

 The thinnest line is of one-pixel wide. We will concentrate on


drawing a line of 1 pixel resolution.
 The Cartesian slope-intercept equation for a straight line is
y= m. x + b
m is the slope of the line and b is the y intercept.
Given the endpoints of a line segment.
m = y2-y1 / x2-x1
b= y1-m.x1

To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm

 Digital Differential Analyser: an algorithm for scan-converting lines


 Based on calculating the rate of change of x or y coordinates (Dx or Dy
respectively)
 For each part of the line the following holds true:

Dy
m  Dy  mDx
Dx

 If Dx = 1 i.e. 1 pixel then … Dy  m

 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

 Cartesian slope-intercept equation for a straight line


y = m.x + c……(1), where m is slope of the line, b is the y intercept
Endpoints of a line sgment be (x1, y1) & (x2, y2) then
m = (y2 – y1)/(x2 – x1) ……(2), b = y1 – m. x1 ……(3)
 For any given x interval ∆x along a line, we can compute the corresponding y & x
intervals ∆y & ∆x respectively as in eqn (2),
∆y = m ∆x ……(4), & ∆x = ∆y /m ……(5),
 These equations form the basis for determining the deflection voltages in analog
devices. For lines with slope magnitudes |m| < 1, ∆x can be set proportional to a
small horizontal deflection voltage and the corresponding vertical deflection is set
proportional to ∆y as calculated from eqn (4).
 For lines with slopes having magnitudes |m| > 1, ∆y can be set proportional to a
small vertical deflection voltage and the corresponding horizontal deflection is set
proportional to ∆x, calculated from eqn (5).
 For lines with |m| = 1, ∆x = ∆y and the horizontal & vertical deflection voltages
are equal

To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm

 Digital Differential Analyzer


 0 < Slope <= 1
 Unit x interval, ∆x = 1

 Subscript k takes integer values y2


starting from 1, for the 1st point, &
increases by 1 until the final endpt y1
is reached. Since m can be any real
no. between 0 & 1, the calculated y
values must be rounded to the
nearest integer. x1 x2

yk 1  yk  m

To prepare the slides help was taken from Graphics Lab, Korea University
DDA Algorithm

 Digital Differential Analyzer


 0 < Slope <= 1
 Unit x interval, ∆x = 1

 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

 Digital Differential Analyzer


 0 < Slope <= 1
 Unit x interval, ∆x = 1

 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

 Digital Differential Analyzer


 0 < Slope <= 1
 Unit x interval, ∆x = 1

 Slope > 1 y2
 Unit y interval, ∆y = 1

 -1 <= Slope < 0 y1


 Unit x interval, ∆x = -1
x1 x2
 Slope < -1 1
 Unit y interval, ∆y = -1 xk 1  xk 
m

To prepare the slides help was taken from Graphics Lab, Korea University
Digital differential analyser

lineDDA (xa, ya, xb, yb)


{
dx = xb – xa;
dy = yb – ya;
x = xa; y = ya;
If(abs(dx) > abs(dy))
del = abs(dx);
else
del = abs(dy);
delx = dx / del; dely = dy / del;
setPixel(ROUND(x), ROUND(y));
for( i=0; i<del; i++){
x += delx;
y += dely;
setPixel(ROUND(x), ROUND(y));
}
}

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

 e.g. p1 = (5, 10), p2 = (25, 20)


y 2  y1 20  10
m   0.5 p1 (5, 10)
x 2  x1 25  5

 Starting at p1, we move right


by one pixel and down by 0.5
pixels each time.

 But we can’t move by “half-a


p2 (25, 20)
pixel” so in pixel coordinates –
we need to round-off to the
nearest pixel value

To prepare the slides help was taken from Graphics Lab, Korea University
Rounding Off

X Y Rounded { x, y }

 Note that the actual pixel position is 1.0 0.33 { 1, 0 }


actually stored as a REAL number
(in C/C++/java a float or a double) 2.0 0.66 { 2, 0 }

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

 If we could draw lines with positive slope (0<=slope<=1) we would be


done.
 For a line with negative slope (0>=slope>=-1)
We negate all Y values
 For a line with slope > 1 or slope <-1
we just swap x and y axes

(-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

 One disadvantage of DDA is the ROUNDing part which


can be expensive

 Bresenham’s Algorithm is based on essentially the same


principles but is completely based on integer variables

 One of the earliest algorithms in computer graphics

To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm

 An accurate, efficient raster line drawing algorithm developed


by Bresenham, scan converts lines using only incremental
integer calculations that can be adapted to display circles and
other curves.

 Keeping in mind the symmetry property of lines, lets derive a


more efficient way of drawing a line.

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)

Choices are(xk +1, yk) and (xk+1, yK+1)


d1 = y – yk = m(xk + 1) + b – yk
d2 = (yk + 1) – y = yk + 1- m(xk + 1) – b
 The difference between these 2 separations is
d1-d2 = 2m(xk + 1) – 2 yk + 2b – 1

 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

 The sign of Pk is the same as the sign of d1 - d2, since Δx > 0.


Parameter c is a constant and has the value 2Δy + Δx(2b-1)
(independent of pixel position)

 If pixel at yk is closer to line-path than pixel at yk +1


(i.e, if d1 < d2) then pk is negative. We plot lower pixel in such a case.
Otherwise , upper pixel will be plotted.

To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s algorithm (cont)

 At step k + 1, the decision parameter can be evaluated as,


pk+1 = 2Δyxk+1 - 2Δxyk+1 + c

 Taking the difference of pk+ 1 and pk we get the following.


pk+1 – pk = 2Δy(xk+1- xk)-2Δx(yk+1 – yk)

 But, xk+1 = xk +1, so that


pk+1 = pk + 2Δy - 2 Δx(yk+1 – yk)

 Where the term yk+1 - yk is either 0 or 1, depending on the sign of parameter


pk

To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm

 The first parameter p0 is directly computed


p0 = 2 Δyxk - 2 Δxyk + c = 2 Δyxk – 2 Δy + Δx (2b-1)

 Since (x0,y0) satisfies the line equation , we also have


y0 = Δy/ Δx * x0 + b

 Combining the above 2 equations , we will have


p0 = 2Δy – Δx
The constants 2Δy and 2Δy-2Δx are calculated once for each time to be
scan converted

To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham’s Line Algorithm

 So, the arithmetic involves only integer addition and subtraction of 2


constants

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

At each xk along the line, starting at k=0, perform the


following test:
If pk < 0 , the next point is (xk+1, yk) and
pk+1 = pk + 2Δy
Otherwise
Point to plot is (xk+1, yk+1)
pk+1 = pk + 2Δy - 2Δx

Repeat step 4 (above step) Δx times

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

 NOTE! The algorithm and derivation above


assumes slopes are less than 1. for other
slopes we need to adjust the algorithm slightly

To prepare the slides help was taken from Graphics Lab, Korea University
DDA versus Bresenham’s Algorithm

 DDA works with floating point arithmetic


 Rounding to integers necessary

 Bresenham’s algorithm uses integer arithmetic


 Constants need to be computed only once

 Bresenham’s algorithm generally faster than


DDA

To prepare the slides help was taken from Graphics Lab, Korea University
Bresenham Example

 Let’s have a go at this


 Let’s plot the line from (20, 10) to (30, 18)
 First off calculate all of the constants:
 Δx: 10
 Δy: 8
 2Δy: 16
 2Δy - 2Δx: -4

 Calculate the initial decision parameter p0:


 p0 = 2Δy – Δx = 6

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

 Go through the steps of the Bresenham line


drawing algorithm for a line going from (21,12)
to (29,16)

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

 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

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

you need to step for x instead of


y (as we saw in DDA) -Step for x instead
- Swap x0 for x1
of y
- Step for y
instead of x - Increment by x

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

 where r is the radius of the circle


 So, we can write a simple circle drawing
algorithm by solving the equation for y at unit x
intervals using:

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…)

 However, unsurprisingly this is not a brilliant


solution!
 Firstly, the resulting circle has large gaps where
the slope approaches the vertical
 Secondly, the calculations are not very efficient
 The square (multiply) operations
 The square root operation – try really hard to avoid
these!
 We need a more efficient, more accurate
solution

To prepare the slides help was taken from Graphics Lab, Korea University
Eight-Way Symmetry

 The first thing we can notice to make our circle


drawing algorithm more efficient is that circles
centred at (0, 0) have eight-way symmetry

(-x, y) (x, y)

(-y, x) (y, x)

R
2
(-y, -x) (y, -x)

(-x, -y) (x, -y)


To prepare the slides help was taken from Graphics Lab, Korea University
Circle Generating Algorithm
 Properties of Circle
 Circle is defined as the set of points that are all at a given distance
r from a center point (xc,yc)

(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 ө

 Using fixed angular step size, a circle is plotted with


equally spaced points along the circumference

 Problem: trigonometric calculations are still time


consuming

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

 i.e. if we determine the curve positions in the 1st quadrant,


we can generate the circle section in the 2nd quadrant of
the xy plane (the 2 circle sections are symmetric with
respect to the y axis)

 The circle section in the 3rd and 4th quadrant can be


obtained by considering symmetry about the x axis

 One step further  symmetry between octants

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)

 Calculation of a circle point (x, y) in 1 octant yields the


circle points for the other 7 octants

To prepare the slides help was taken from Graphics Lab, Korea University
Midpoint Circle Algorithm

 As in raster algorithm, we sample at unit intervals & determine


the closest pixel position to the specified circle path at each
step

 For a given radius, r and screen center position (xc,yc) , we can


set up our algorithm to calculate pixel positions around a circle
path centered at the coordinate origin (0,0)

 Each calculated position (x, y) is moved to its proper screen


position by adding xc to x and yc to y

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

 Positions in the other 7 octants are obtained by symmetry

To prepare the slides help was taken from 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 mid-point circle
the top right eighth of a circle, and algorithm was
then use symmetry to get the rest developed by Jack
Bresenham, who we
of the points heard about earlier.
Bresenham’s patent
for the algorithm can
To prepare the slides help was taken from Graphics Lab, Korea University be viewed here.
Mid-Point Circle Algorithm
(cont…)
 Assume that we have
just plotted point (xk, yk) (xk, yk) (xk+1, yk)
 The next point is a
choice between (xk+1, yk)
and (xk+1, yk-1) (xk+1, yk-1)

 We would like to choose


the point that is nearest to
the actual circle
 So how do we make this choice?

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

 where y is either y 2 y -12depending on the


or
pk 1  pk  2( xk  1)  ( yk 1 k yk )  ( yk 1  yk )  1
k+1 k
sign of pk

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

 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 )
 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:

6. Repeat steps 3 to 5 until x >= y


x  x  xc y  y  yc

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…)

10 k pk (xk+1,yk+1) 2xk+1 2yk+1


9
8
0 -9 (1,10) 2 20
7 1 -6 (2,10) 4 20
6 2 -1 (3,10) 6 20
5
3 6 (4,9) 8 18
4
4 -3 (5,9) 10 18
3
2 5 8 (6,8) 12 16
1 6 5 (7,7) 14 14
0

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

 So we can figure out how to draw lines and


circles
 How do we go about drawing polygons?
 We use an incremental algorithm known as the
scan-line algorithm

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

 Over the last couple of lectures we have looked


at the idea of scan converting lines
 The key thing to remember is this has to be
FAST
 For lines we have either DDA or Bresenham
 For circles the mid-point algorithm

To prepare the slides help was taken from Graphics Lab, Korea University
Polygon Scan-conversion

 First scan convert all edges very coarsely, increment


along y and don’t worry about gaps

 For each scan line, fill between start and end pixel

To prepare the slides help was taken from Graphics Lab, Korea University
Area Filling

A simple recursive flood-fill algorithm: user selects a pixel S. S


and all adjacent pixels of the same colour are changed, and the
adjacent pixels of those etc… leads to some wasted processing
Recursive Flood Fill
1. Let orig_col = original colour
of S
2. Let current_pixel = S
3. Change Colour of
current_pixel to new_col
4. For all adjacent pixels p
IF (colour == orig_col)
THEN let p be the
current_pixel
Repeat from Step 3
To prepare the slides help was taken from Graphics Lab, Korea University
Aliasing

 Distortion of information due to low-frequency sampling

sampling frequency What we “see”

 In raster images – leads to jagged edges with staircase effect.


Commonly occurs when a pattern has details that are smaller than
a pixel in width
 We can reduce effects by antialiasing methods to compensate for
undersampling

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

Effectively: BLUR edges to make them look smooth

To prepare the slides help was taken from Graphics Lab, Korea University
Supersampling Example

Use twice the


available resolution
4 pixels (2x2 square)
for every 1 in the
original

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”

Average out the colours in the 4 pixel squares


To prepare the slides help was taken from Graphics Lab, Korea University
Antialiasing by polygon
boundary

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

Potrebbero piacerti anche