52
Rasterizing Lines Robb T. Koether Pixel Coordinates Bresenham’s Midpoint Algorithm The Decision Function The Algorithm Examples Assignment Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney College Mon, Nov 9, 2009

Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

Embed Size (px)

Citation preview

Page 1: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing LinesLecture 26

Sections 7.8, 7.9

Robb T. Koether

Hampden-Sydney College

Mon, Nov 9, 2009

Page 2: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 3: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 4: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Pixels and Screen Coordinates

We think of the viewport as consisting of a rectangulararray of pixels.

Page 5: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Pixels and Screen Coordinates

A basic question is, what are the coordinates of thepixels?Are they integers, e.g., (3, 2)?Or, are they half-integers, e.g.

(31

2 , 212

)?

0 1 2 3

0123

0 1 2 30123

Integers Half-integers

Page 6: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Pixels and Screen Coordinates

OpenGL adopts the view that they are half-integers.We will conform to OpenGL’s view, so that our resultswill match OpenGL’s results.

Page 7: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 8: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing Lines

All lines will go from one intersection of grid lines toanother.

(0, 0)

(16, 7)

Page 9: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing Lines

In what follows, we will assume that the line has a slopebetween 0 and 1.That is, the change in x is greater than the change in y.The other cases are “easily” adapted from this case.

Page 10: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing Lines

When drawing a line on the screen, how do we decidewhich pixels to color?

(0, 0)

(16, 7)

Page 11: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing Lines

In each vertical column, choose the pixel whose centeris closest to the line.

Page 12: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Rasterizing Lines

In each vertical column, choose the pixel whose centeris closest to the line.

Page 13: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Lines and Pixels

In each vertical strip, choose the pixel whose center isclosest to the line.

(0, 0)

(16, 7)

Page 14: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Choosing the Pixel

Let the equation of the line be

y = mx + b.

Given that we have just colored pixel (px, py), the nextpixel to color is either (px + 1, py) or (px + 1, py + 1).Why?

Page 15: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Choosing the Pixel

What is the most efficient way to determine which pointto color?We could compute m(px + 1) + b and then compare it topy and py + 1 to see which is closer.Why is this not a good idea?

Page 16: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

The Decision Function

We need a function which, when evaluated, will tell uswhether to increment y or to keep y the same.Let the line go from A = (ax, ay) to B = (bx, by).The equation of the line can be written

y − ay = m(x− ax)

y − ay =(

by − ay

bx − ax

)(x− ax),

or

(bx − ax)(y − ay) = (by − ay)(x− ax).

Page 17: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 18: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

The Decision Function

Let W = bx − ax (the width).Let H = by − ay (the height).Note that W ≥ H ≥ 0 by our assumption.Then the equation can be written

−W (y − ay) + H(x− ax) = 0.

LetF (x, y) = −2W (y − ay) + 2H(x− ax).

The factor of 2 is introduced to avoid the fraction 12 later.

Page 19: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

The Decision Function

We will call F (x, y) the decision function.Note that

If F (x, y) < 0, then the line is below (x, y).If F (x, y) = 0, then the line passes through (x, y).If F (x, y) > 0, then the line is above (x, y).

Page 20: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

Suppose we have shaded pixel P = (px, py) (centeredat (px + 1

2 , py + 12)).

Now, in the next column, we need to decide whether toshade the lower pixel L = (px + 11

2 , py + 12) or the upper

pixel U = (px + 112 , py + 11

2).Let M be the point midway between U and L.Then M =

(px + 11

2 , py + 1).

Page 21: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

If the line is above M , then we should color U .If the line is below M , then we should color L.That is,

If F(px + 1 1

2 , py + 1)

> 0, then color U .If F

(px + 1 1

2 , py + 1)

< 0, then color L.

Page 22: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

In the first column of pixels, the midpoint isM1 =

(ax + 1

2 , ay + 1).

The line passes below M1.Indeed, the line passes below the center of the pixel(ax + 1

2 , ay + 12

).

For that first midpoint M1,

F1 = F

(ax +

12, ay + 1

)= −2W + H < 0.

Page 23: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

In the second column of pixels, the midpoint isM2 =

(ax + 11

2 , ay + 1).

The value of F is

F2 = F

(ax + 1

12, ay + 1

)= F1 + 2H

= 3H − 2W,

which may be positive or negative.We compute it, test it, and decide whether to shade Uor L.

Page 24: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

To find a recursive formula for the decision, considerwhat happens as we go from pixel Pi = (px, py) to pixelPi+1.That is, from x = px + 1

2 to x = px + 112 .

There are two cases:We shaded L in the previous step.We shaded U in the previous step.

Page 25: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

Suppose first that we shaded L.Then the midpoint considered wasMi =

(px + 1

2 , py + 1)

and Fi < 0.Because we shaded L, the next midpoint to consider isMi+1 =

(px + 11

2 , py + 1).

We calculate

Fi+1 = F

(px + 1

12, py + 1

)= Fi + 2H,

which may be positive or negative.

Page 26: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

px px + 2px + 1

py

py + 2

py + 1

P

U

L

Mi + 1Mi

Right - Right

Page 27: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

px px + 2px + 1

py

py + 2

py + 1

P

U

L

Mi + 1Mi

Right - Up

Page 28: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

Now suppose that we shaded U .Then the midpoint considered wasMi =

(px + 1

2 , py + 1)

and Fi > 0.Because we shaded U , the next midpoint to consider isMi+1 =

(px + 11

2 , py + 2).

We calculate

Fi+1 = F

(px + 1

12, py + 2

)= Fi − 2(W −H),

which may or may not be positive.

Page 29: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

px px + 2px + 1

py

py + 2

py + 1

P

U

L

Mi

Mi + 1

Up - Right

Page 30: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

px px + 2px + 1

py

py + 2

py + 1

P

U

L

Mi

Mi + 1

Up - Up

Page 31: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 32: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

Summarizing the two cases:If the previous value of F was negative, then the nextvalue is 2H larger.If the previous value of F was positive, then the nextvalue is 2(W −H) smaller.

This allows us to move quickly from one decision to thenext, since we need only add or subtract a constant.We do not need to evaluate F (x, y) each time.

Page 33: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

To start the process, the first midpoint to be consideredis

M2 =(

ax + 112, ay + 1

).

The value of F(ax + 11

2 , ay + 1)

is 3H − 2W .Therefore, the initial value of the decision function is3H − 2W .

Page 34: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

Bresenham’s Midpoint AlgorithmInitialize x = ax, y = ay, F = 3H − 2W .Repeat until x = bx.

Increment x.If F < 0, then

Add 2H to F .Else if F > 0, then

Subtract 2(W −H) from F .Increment y.

Page 35: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Midpoint Algorithm

This algorithm produces the coordinates of the gridpoint to the lower left of the pixel to be shaded.

Page 36: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 37: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Example

Rasterize the line from (0, 0) to (16, 7).W = 16, H = 7.2H = 14, 2(W −H) = 18, 3H − 2W = −11.

x F ∆y y x F ∆y y x F ∆y y

0 6 121 7 132 8 143 9 154 105 11

Page 38: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Example

Rasterize the line from (0, 0) to (16, 7).W = 16, H = 7.2H = 14, 2(W −H) = 18, 3H − 2W = −11.

x F ∆y y x F ∆y y x F ∆y y

0 -11 6 9 12 -31 3 7 -9 13 112 -15 8 5 14 -73 -1 9 -13 15 74 13 10 15 -5 11 -17

Page 39: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Example

Rasterize the line from (0, 0) to (16, 7).W = 16, H = 7.2H = 14, 2(W −H) = 18, 3H − 2W = −11.

x F ∆y y x F ∆y y x F ∆y y

0 -11 0 6 9 1 12 -3 01 3 1 7 -9 0 13 11 12 -15 0 8 5 1 14 -7 03 -1 0 9 -13 0 15 7 14 13 1 10 1 15 -5 0 11 -17 0

Page 40: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Example

Rasterize the line from (0, 0) to (16, 7).W = 16, H = 7.2H = 14, 2(W −H) = 18, 3H − 2W = −11.

x F ∆y y x F ∆y y x F ∆y y

0 -11 0 0 6 9 1 2 12 -3 0 51 3 1 0 7 -9 0 3 13 11 1 52 -15 0 1 8 5 1 3 14 -7 0 63 -1 0 1 9 -13 0 4 15 7 1 64 13 1 1 10 1 1 45 -5 0 2 11 -17 0 5

Page 41: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Lines and Pixels

(0, 0)

(16, 7)

Page 42: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Another Example

Rasterize the line from (3, 5) to (16, 14).W = 13, H = 9.2H = 18, 2(W −H) = 8, 3H − 2W = 1.

x F ∆y y x F ∆y y x F ∆y y

3 8 134 9 145 10 156 117 12

Page 43: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Another Example

Rasterize the line from (3, 5) to (16, 14).W = 13, H = 9.2H = 18, 2(W −H) = 8, 3H − 2W = 1.

x F ∆y y x F ∆y y x F ∆y y

3 1 8 13 13 -14 -7 9 5 14 175 11 10 -3 15 96 3 11 157 -5 12 7

Page 44: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Another Example

Rasterize the line from (3, 5) to (16, 14).W = 13, H = 9.2H = 18, 2(W −H) = 8, 3H − 2W = 1.

x F ∆y y x F ∆y y x F ∆y y

3 1 1 8 13 1 13 -1 04 -7 0 9 5 1 14 17 15 11 1 10 -3 0 15 9 16 3 1 11 15 17 -5 0 12 7 1

Page 45: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Another Example

Rasterize the line from (3, 5) to (16, 14).W = 13, H = 9.2H = 18, 2(W −H) = 8, 3H − 2W = 1.

x F ∆y y x F ∆y y x F ∆y y

3 1 1 5 8 13 1 8 13 -1 0 124 -7 0 6 9 5 1 9 14 17 1 125 11 1 6 10 -3 0 10 15 9 1 136 3 1 7 11 15 1 107 -5 0 8 12 7 1 11

Page 46: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Example

(3, 5)

(16, 14)

Page 47: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Algorithm

Bresenham’s Algorithmvoid bresenham(Point2D A, Point2D B){

int y = A.y;int W = B.x - A.x;int H = B.y - A.y;int H2 = 2*H;int WH2 = 2*(W - H);int F = 3*H - 2*W;for (int x = A.x; x < B.x; x++){

setPixel(x, y);if (F < 0)

F += H2;else{

F -= WH2;y++;

}}

}

Page 48: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Bresenham’s Algorithm

Example (Bresenham’s Algorithm)The code.The executable.

Page 49: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

OpenGL’s Line Drawer

Example (OpenGL’s Line Drawer)The code.The executable.

Page 50: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Freehand Drawing

Example (Freehand Drawing)The code.The executable.

Page 51: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Outline

1 Pixel Coordinates

2 Bresenham’s Midpoint AlgorithmThe Decision FunctionThe AlgorithmExamples

3 Assignment

Page 52: Rasterizing Lines - Lecture 26 Sections 7.8, 7people.hsc.edu/faculty-staff/robbk/Coms331/Lectures/Lectures 2009...Rasterizing Lines Lecture 26 Sections 7.8, 7.9 Robb T. Koether Hampden-Sydney

RasterizingLines

Robb T.Koether

PixelCoordinates

Bresenham’sMidpointAlgorithmThe DecisionFunction

The Algorithm

Examples

Assignment

Homework

HomeworkRead Section 7.8 – rasterization.Read Section 7.9 – Bresenham’s algorithm.