cs 376 introduction to computer graphics 02 / 02 / 2007 instructor: michael eckmann

23
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Upload: gage-ripley

Post on 14-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

CS 376Introduction to Computer Graphics

02 / 02 / 2007

Instructor: Michael Eckmann

Page 2: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Today’s Topics• Questions?• Line Drawing algorithms

– DDA lines

– Bresenham Line Drawing algorithm

• Circle drawing algorithm

Page 3: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Parametric Equation of a Line• This information appears in section 6.7 of text but is appropriate now

because it is used in the DDA algorithm (discussed next slide).

• Given 2 points on a line (x0, y

0) and (x

end, y

end)

• x = x0 + u (x

end - x

0)

• y = y0 + u (y

end - y

0)

• u is the parameter

• If we consider only the values 0 <= u <= 1, we get all the points on the line segment between (x

0, y

0) and (x

end, y

end).

• When u = 0, x=x0 and y = y

0

• When u = 1, x=xend

and y = yend

Page 4: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

DDA – digital differential analyzer• Consider a line with positive slope and x

0 < x

end

• If slope <=1 then we will sample the line at unit x intervals and compute y.

• If slope > 1 then we will sample the line at unit y intervals and compute x.

• What does that mean?

• In the DDA, the parameter u (from the parametric equation of a line) is 1/steps.

• Let's take a look at the code for this algorithm and “execute” it with actual values on the board.

Page 5: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

From Hearn & Baker's Computer Graphics with OpenGL, 3rd Edition.

void lineDDA (int x0, int y0, int xEnd, int yEnd)

{

int dx = xEnd - x0, dy = yEnd - y0, steps, k;

float xIncrement, yIncrement, x = x0, y = y0;

if (fabs (dx) > fabs (dy))

steps = fabs (dx);

else

steps = fabs (dy);

xIncrement = float (dx) / float (steps);

yIncrement = float (dy) / float (steps);

setPixel (round (x), round (y));

for (k = 0; k < steps; k++) {

x += xIncrement;

y += yIncrement;

setPixel (round (x), round (y));

}

}

Page 6: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

DDA – digital differential analyzer• It creates good lines. The lines aren't going to differ in “thickness” based on

angle, etc.

• Can you comment on any efficiency issues with this algorithm?

Page 7: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

DDA – digital differential analyzer• It creates good lines. The lines aren't going to differ in “thickness” based on

angle, etc.

• Problems with this algorithm are

– Rounding is expensive

– Floating point arithmetic is expensive

• So, a goal is to do all integer arithmetic and reduce / eliminate divides.

Page 8: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm• Bresenham's Algorithm is efficient (fast) and accurate.

• The key part of Bresenham's algorithm lies in the determination of which pixel to turn on based on whether the line equation would lie more near one pixel or the other.

Page 9: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm• Think of a line fully contained in the first octant (the area of the coordinate

plane between the positive x-axis and the line y=x.)

• Assume we have the pixel at (xk, y

k) turned on and we're trying to determine

between the following:

– Should (xk+1

, yk) or (x

k+1, y

k+1) be on?

– That is, the choice is between either the pixel one to the right of (xk, y

k)

or the pixel one to the right and one up.

• The decision can be made based on whether dlower

- dupper

is positive or not.

• The goal is to come up with an inexpensive way to determine the sign of

(dlower

– dupper

)

Page 10: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm• The y coordinate on the line is calculated by: y = m(x

k + 1) + b and

• dlower

= y – yk

• dupper

= yk+1

– y

• dlower

– dupper

= 2m(xk + 1) – 2y

k + 2b – 1

• Since m is < 1 (recall it's in the 1st octant) it is a non-integer. That's not good so, replace it with dy/dx, where dy is the change in y endpoints and dx is the change in x endpoints – these will be integer values divided --- but we'll get rid of the division.

• dlower

– dupper

= 2 ( dy / dx )(xk + 1) – 2y

k + 2b – 1

• Multiply both sides by dx and get:

• dx (dlower

– dupper

) = 2 dy (xk + 1) – 2 dx y

k + 2 dx b – dx

• dx (dlower

– dupper

) = 2 dy xk – 2 dx y

k + 2 dy + 2 dx b – dx

• 2 dy + 2 dx b – dx is a constant which is independent of each iteration

Page 11: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm• So we end up with

• dx (dlower

– dupper

) = 2 dy xk – 2 dx y

k + c

• We only need to determine the sign of (dlower

– dupper

)

• dx is positive so it doesn't have an effect on the sign (so now there's no need to divide) --- does that make sense?

• Let pk = dx (d

lower – d

upper ) p

k is the decision parameter

• So, we check if 2 dy xk – 2 dx y

k + c < 0 and if it is we turn on the lower pixel, so

(xk+1

, yk) should be the pixel turned on and

• otherwise (xk+1

, yk+1

) is turned on

• As the algorithm iterates though it needs to then calculate the next value of p, which

is pk+1

Page 12: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm• Recall that to calculate p

k we had: p

k = 2 dy x

k – 2 dx y

k + c

• So, pk+1

= 2 dy xk+1

– 2 dx yk+1

+ c

• If we subtract pk+1

– pk

= 2dy ( xk+1

– xk ) – 2dx ( y

k+1 – y

k )

• But, xk+1

= xk + 1 because of the octant we're in we always increment x by 1

• So, we have pk+1

– pk

= 2dy – 2dx ( yk+1

– yk )

• Then, pk+1

= pk

+ 2dy – 2dx ( yk+1

– yk )

• Where ( yk+1

– yk ) is either 1 or 0 (why?) depending on the sign of p

k . So, either

• pk+1

= pk + 2dy (when p

k < 0)

• pk+1

= pk + 2dy – 2dx (when p

k >= 0)

Page 13: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Bresenham's Line Algorithm/* Bresenham line-drawing procedure for |m| < 1.0. */

void lineBres (int x0, int y0, int xEnd, int yEnd)

{

int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0);

int p = 2 * dy - dx;

int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx);

int x, y;

/* Determine which endpoint to use as start position. */

if (x0 > xEnd) {

x = xEnd;

y = yEnd;

xEnd = x0;

}

else {

x = x0;

y = y0;

}

setPixel (x, y);

while (x < xEnd) {

x++;

if (p < 0)

p += twoDy;

else {

y++;

p += twoDyMinusDx;

}

setPixel (x, y);

}

}

/* From Hearn & Baker's Computer Graphics with OpenGL, 3rd Edition */

Page 14: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms

Page 15: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms• So, we only need to calculate the coordinates of an eighth of the circle and plot the

following:

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

• Bresenham's Line Algorithm has been adapted to circles by deciding which pixel is closest to the actual circle at each sampling step.

• It should be able to draw circles with any center, but to make calculations easier, compute the pixel coordinates based on the same radius but centered at the origin. Then when you plot the pixel, add in the center coordinates.

• Consider only the octant between the lines x=0 and y=x. The slope of the curve varies from 0 (at the top) to -1 at the 45 degree angle. This implies that we should do unit steps on the x-axis and calculate y (I leave this for you to convince yourself that this is the right approach.)

• At each x, the decision is between which y is correct among two possible y's, the one to the right or the one to the right and down.

Page 16: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms

Page 17: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms• The decision is always between the pixel one to the right or one to the right and one

down. Respectively, yk or y

k-1 .

• The midpoint circle algorithm is a way to determine it using only integer arithmetic.

• Define a circle function as f(x,y) = x2 + y2 – r2

• A point x,y on the circle with radius r will cause f(x,y) = 0

• A point x,y in the circle's interior will cause f(x,y) < 0.

• A point x,y outside the circle will cause f(x,y) > 0.

• The point we use is the midpoint between the two pixel choices. And f(x,y) as defined above is our decision variable p

k

• The point is (xk + 1, y

k – ½)

• If pk is < 0, it is inside the circle and closer to y

k

• Otherwise choose yk-1

Page 18: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms• p

k = f(x

k + 1, y

k – ½) = (x

k + 1)2 + (y

k – ½)2 – r2

• If pk is < 0, it is inside the circle and closer to y

k

• Otherwise choose yk-1

• pk+1

= f(xk+1

+ 1, yk+1

– ½) = (xk+1

+ 1)2 + (yk+1

– ½)2 – r2

• pk+1

= ((xk + 1) + 1)2 + (y

k+1 – ½)2 – r2 (p

k+1can be expressed in terms of p

k )

• Similar to how we determined the recursive decision variable computation in Bresenham's Line Algorithm, we do for the Circle Algorithm as well: skipping a bunch of steps leads to:

• pk+1

= pk + 2x

k+1 + 1 (when p

k < 0)

• pk+1

= pk + 2x

k+1 + 1 – 2y

k+1 (otherwise)

• Assuming we start at point (0,r), we'll have p0 = f(0 + 1, r – ½) = f(1, r – ½) which yields

5/4 – r (or 1– r for integer radii because of rounding)

• Let's plot some points as an example for a circle of radius 6, and center (-2, 4)

• We do calculations with center 0,0 and for each point we plot, we add in the center.

Page 19: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithm Example

Center = (-2, 4)

K

0 -5 (1, 6) (-1, 10) 2 121 -2 (2, 6) (0, 10) 4 122 3 (3, 5) (1, 9) 6 103 0 (4, 4) (2, 8) 8 8

Pk

(xk+1

, yk+1

) Center + (xk+1

, yk+1

) 2xk+1

2yk+1

• p0 = 1– r

• pk+1

= pk + 2x

k+1 + 1 (when p

k < 0)

• pk+1

= pk + 2x

k+1 + 1 – 2y

k+1 (otherwise)

• Plot: (-1, 10), (0, 10), (1, 9), (2, 8)

• Also, plot points in the other 7 octants for each of these four.

• e.g. The other 7 points for (-1, 10) to plot are:

•(-3, 10), (-1,-2), (-3,-2), (4,5), (4, 3), (-8, 5), (-8, 3)

•These were calculated by adding the center (-2,4) to (-1,6), (1,-6), (-1,-6), (6, 1), (6,-1), (-6,1), and (-6,-1)

Page 20: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Circle Algorithms• There's a figure in the text book (figure 3-20) and it looks to me like the red circle

line is drawn incorrectly but the pixels are correct. It is showing the center of the circle at the bottom left corner of the 0,0 pixel box, but it should be in the center of the 0,0 pixel box.

• I noticed it because when x=3, based on the drawn red circle, the algorithm would “turn on” (3,9) not (3,10), because the midpoint is clearly outside the red circle there.

• I didn't verify it, but I trust the calculations in the table which says that p2 = -1 which

would be inside the circle.

Page 21: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Ellipse Algorithm• We can generalize the circle algorithm to generate ellipses but

with only 4-way symmetry.

• The slope of the ellipse changes from > -1 to < -1 in one quadrant so that we will have to switch from – unit samples along the x-axis and calculate y– to unit samples along the y-axis and calculate x

• I'm not as concerned about the details of ellipses as I am with

you understanding the details of the line and circle algorithms.

Page 22: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Ellipse Algorithms

Page 23: CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Ellipse Algorithms