Scan Conversion

44
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam Scan Conversion CS123 1 of 44 Scan Conversion - 10/16/12

Transcript of Scan Conversion

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Scan Conversion CS123

1 of 44 Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Line Drawing

Draw a line on a raster screen between two points

Why is this a difficult problem?

What is β€œdrawing” on a raster display?

What is a β€œline” in raster world?

Efficiency and appearance are both important

Problem Statement

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

2 of 44

Scan Converting Lines

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Final step of rasterisation (process of taking geometric shapes and converting them into an array of pixels stored in the framebuffer to be displayed)

Takes place after clipping occurs

All graphics packages do this at the end of the rendering pipeline

Takes triangles and maps them to pixels on the screen

Also takes into account other properties like lighting and shading, but we’ll focus first on algorithms for line scan conversion

3 of 44

What is Scan Conversion?

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Special cases: Horizontal Line:

Draw pixel P and increment x coordinate value by 1 to get next pixel.

Vertical Line:

Draw pixel P and increment y coordinate value by 1 to get next pixel.

Diagonal Line:

Draw pixel P and increment both x and y coordinate by 1 to get next pixel.

What should we do in general case?

Increment x coordinate by 1 and choose point closest to line.

But how do we measure β€œclosest”?

4 of 44

Finding the next pixel:

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Why can we use vertical distance as a measure of which point is closer?

… because vertical distance is proportional to actual distance

Similar triangles show that true distances to line (in blue) are directly proportional to vertical distances to line (in black) for each point

Therefore, point with smaller vertical distance to line is closest to line

5 of 44

Vertical Distance

(π‘₯1, 𝑦1)

(π‘₯2, 𝑦2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Basic Algorithm: Find equation of line that connects two points P and Q

Starting with leftmost point , increment π‘₯𝑖 by 1 to calculate 𝑦𝑖 = π‘š βˆ— π‘₯𝑖 + B where π‘š = slope, B = y intercept

Draw pixel at (π‘₯𝑖 , Round(𝑦𝑖)) where Round (𝑦𝑖) = .5 + 𝑦𝑖

Incremental Algorithm:

Each iteration requires a floating-point multiplication Modify algorithm to use deltas

(𝑦𝑖+1 βˆ’ 𝑦𝑖) = π‘š βˆ— (π‘₯𝑖+1 βˆ’ π‘₯𝑖) + B

𝑦𝑖+1 = 𝑦𝑖 + π‘š βˆ— (π‘₯𝑖+1 βˆ’ π‘₯𝑖)

If π‘₯ = π‘₯𝑖+1 βˆ’ π‘₯𝑖 = 1, then 𝑦𝑖+1 = 𝑦𝑖 + π‘š

At each step, we make incremental calculations based on preceding step to find next y value

6 of 44

Strategy 1 – Incremental Algorithm (1/3)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 7 of 44

Strategy 1 – Incremental Algorithm (2/3)

(π‘₯𝑖 + 1, π‘…π‘œπ‘’π‘›π‘‘ 𝑦𝑖 + π‘š )

(π‘₯𝑖 , 𝑦𝑖)

(π‘₯𝑖 , π‘…π‘œπ‘’π‘›π‘‘ 𝑦𝑖 )

(π‘₯𝑖 + 1, 𝑦𝑖 + π‘š)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 8 of 44

Sample Code and Problems (3/3)

void Line(int x0, int y0, int x1, int y1) {

int x, y;

float dy = y1 – y0;

float dx = x1 – x0;

float m = dy / dx;

y = y0;

for (x = x0; x < x1; ++x) {

WritePixel( x, Round(y) );

y = y + m;

}

}

Rounding takes time

Since slope is fractional, need special case for vertical lines (dx = 0)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Assume that line’s slope is shallow and positive (0 < slope < 1); other slopes can be handled by suitable reflections about principle axes

Call lower left endpoint (π‘₯0, 𝑦0) and upper right endpoint (π‘₯1, 𝑦1)

Assume that we have just selected pixel 𝑃 at (π‘₯𝑃, 𝑦𝑃)

Next, we must choose between pixel to right (E pixel), or one right and one up (NE pixel)

Let Q be intersection point of line being scan-converted and vertical line π‘₯ = π‘₯𝑃 +1

9 of 44

Strategy 2 – Midpoint Line Algorithm (1/3)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 10 of 44

Strategy 2 – Midpoint Line Algorithm (2/3)

Previous pixel

E pixel

NE pixel

Midpoint M Q

𝑃 = (π‘₯𝑃, 𝑦𝑃)

π‘₯ = π‘₯𝑃 + 1 (dashed)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Line passes between E and NE

Point that is closer to intersection point 𝑄 must be chosen

Observe on which side of line midpoint 𝑀 lies: E is closer to line if midpoint 𝑀 lies

above line, i.e., line crosses bottom half

NE is closer to line if midpoint 𝑀 lies below line, i.e., line crosses top half

Error (vertical distance between chosen pixel and actual line) is always ≀ .5

11 of 44

Strategy 2- Midpoint Line Algorithm (3/3)

For line shown, algorithm chooses NE as next pixel.

Now, need to find a way to calculate on which side of line midpoint lies

E pixel

NE pixel

𝑀

𝑄

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Line equation as function: 𝑓 π‘₯ = 𝑦 = π‘šπ‘₯ + 𝐡 =𝑑𝑦

𝑑π‘₯π‘₯ + 𝐡

Line equation as implicit function: 𝑓 π‘₯, 𝑦 = π‘Žπ‘₯ + 𝑏𝑦 + 𝑐 = 0 Avoids infinite slopes, provides symmetry between x and y

So from above, 𝑦 βˆ™ 𝑑π‘₯ = 𝑑𝑦 βˆ™ π‘₯ + 𝐡 βˆ™ 𝑑π‘₯

𝑑𝑦 βˆ™ π‘₯ βˆ’ 𝑦 βˆ™ 𝑑π‘₯ + 𝐡 βˆ™ 𝑑π‘₯ = 0 ∴ π‘Ž = 𝑑𝑦, 𝑏 = βˆ’π‘‘π‘₯, 𝑐 = 𝐡 βˆ™ 𝑑π‘₯

Properties (proof by case analysis): 𝑓 π‘₯π‘š, π‘¦π‘š = 0 when any point π‘š is on line

𝑓 π‘₯π‘š, π‘¦π‘š < 0 when any point π‘š is above line

𝑓 π‘₯π‘š, π‘¦π‘š > 0 when any point π‘š is below line

Our decision will be based on value of function at midpoint 𝑀 at (π‘₯𝑃 + 1, 𝑦𝑃 + .5)

12 of 44

General Line Equation

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Decision Variable 𝑑 :

We only need sign of f(π‘₯𝑃 + 1, 𝑦𝑃 + .5) to see where the line lies, and then pick nearest pixel.

𝑑 = f(π‘₯𝑃 + 1, 𝑦𝑃 + .5)

if 𝑑 > 0 choose pixel NE

if 𝑑 < 0 choose pixel E

if 𝑑 = 0 choose either one consistently

How do we incrementally update 𝑑?

On basis of picking E or NE, figure out location of 𝑀 for the next pixel, and corresponding value 𝑑 for next grid line.

We can derive 𝑑 for the next pixel based on our current decision.

13 of 44

Decision Variable

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Increment M by one in x direction:

π‘‘π‘œπ‘™π‘‘ = π‘Ž π‘₯𝑃 + 1 + 𝑏 𝑦𝑃 + .5 + 𝑐

𝑑𝑛𝑒𝑀 = 𝑓(π‘₯𝑃 + 2, 𝑦𝑃 + .5)

= π‘Ž π‘₯𝑃 + 2 + 𝑏 𝑦𝑃 + .5 + 𝑐

𝑑𝑛𝑒𝑀 βˆ’ π‘‘π‘œπ‘™π‘‘ is the incremental difference E 𝑑𝑛𝑒𝑀 = π‘‘π‘œπ‘™π‘‘ + π‘Ž β†’ E = π‘Ž = 𝑑𝑦 (2 slides back)

We can compute value of decision variable at next step incrementally without computing F(M) directly 𝑑𝑛𝑒𝑀 = π‘‘π‘œπ‘™π‘‘ + E = π‘‘π‘œπ‘™π‘‘ + 𝑑𝑦

E can be thought of as correction or update factor to take π‘‘π‘œπ‘™π‘‘ to 𝑑𝑛𝑒𝑀

It is referred to as forward difference

14 of 44

Incrementing Decision Variable if E was chosen:

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Increment M by one in both x and y directions:

𝑑𝑛𝑒𝑀 = 𝑓 π‘₯𝑃 + 2, 𝑦𝑃 + 1.5

= π‘Ž π‘₯𝑃 + 2 + 𝑏 𝑦𝑃 + 1.5 + 𝑐

NE = 𝑑𝑛𝑒𝑀 βˆ’ π‘‘π‘œπ‘™π‘‘

𝑑𝑛𝑒𝑀 = π‘‘π‘œπ‘™π‘‘ + π‘Ž + 𝑏 β†’ NE = π‘Ž + 𝑏 = 𝑑π‘₯ βˆ’ 𝑑𝑦

Thus, incrementally,

𝑑𝑛𝑒𝑀 = π‘‘π‘œπ‘™π‘‘ + NE = π‘‘π‘œπ‘™π‘‘ + 𝑑π‘₯ βˆ’ 𝑑𝑦

15 of 44

If NE was chosen:

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

At each step, algorithm chooses between 2 pixels based on sign of decision variable calculated in previous iteration.

It then updates decision variable by adding either E or NE to old value depending on choice of pixel. Simple additions only!

First pixel is first endpoint (π‘₯0, 𝑦0), so we can directly calculate initial value of d for choosing between E and NE.

16 of 44

Summary (1/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

First midpoint for first 𝑑 = π‘‘π‘ π‘‘π‘Žπ‘Ÿπ‘‘ is at (π‘₯0 + 1, 𝑦0 + .5)

f (π‘₯0 + 1, 𝑦0 + .5)

= π‘Ž π‘₯0 + 1 + 𝑏 𝑦0 + .5 + 𝑐

= π‘Žπ‘₯0 + 𝑏𝑦0 + π‘Ž +𝑏

2+ c

= f π‘₯0, 𝑦0 + π‘Ž +𝑏

2

But π‘₯0, 𝑦0 is point on line, so f π‘₯0, 𝑦0 = 0

Therefore, π‘‘π‘ π‘‘π‘Žπ‘Ÿπ‘‘ = π‘Ž +𝑏

2= 𝑑𝑦 βˆ’

𝑑π‘₯

2

use π‘‘π‘ π‘‘π‘Žπ‘Ÿπ‘‘to choose second pixel, etc.

To eliminate fraction in π‘‘π‘ π‘‘π‘Žπ‘Ÿπ‘‘: redefine f by multiplying it by 2; 𝑓 π‘₯, 𝑦 = 2 π‘Žπ‘₯ + 𝑏𝑦 + 𝑐

This multiplies each constant and decision variable by 2, but does not change sign

Note: this is identical to β€œBresenham’s algorithm”, though derived by different means. That won’t be true for circle and ellipse scan conversion.

17 of 44

Summary (2/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

void MidpointLine(int x0, int y0, int x1, int y1) { int dx = (x1 - x0), dy = (y1 - y0); int d = 2 * dy - dx; int incrE = 2 * dy; int incrNE = 2 * (dy - dx); int x = x0, y = y0; WritePixel(x, y); while (x < x1) { if (d <= 0) d = d + incrE; // East Case else { d = d + incrNE; ++y; } // Northeast Case ++x; WritePixel(x, y); } }

18 of 44

Example Code

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Version 1: really bad For π‘₯ from βˆ’π‘… π‘‘π‘œ 𝑅:

𝑦 = 𝑅2 βˆ’ π‘₯2;

WritePixel(round(π‘₯), round(𝑦));

WritePixel(round(π‘₯), round(βˆ’π‘¦));

Version 2: slightly less bad For π‘₯ from 0 to 360:

WritePixel(round(𝑅 cos (π‘₯)), round(𝑅 sin (π‘₯)));

19 of 44

Scan Converting Circles

(17, 0)

(0, 17)

(17, 0)

(0, 17)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 20 of 44

Version 3 β€” Use Symmetry

R

(x0 + a, y0 + b)

(x-x0)2 + (y-y0)

2 = R2

(x0, y0)

Symmetry:

If π‘₯0 + π‘Ž, 𝑦0 + 𝑏 is on circle centered at π‘₯0, 𝑦0 :

Then π‘₯0 Β± π‘Ž, 𝑦0 Β± 𝑏 and π‘₯0 Β± 𝑏, 𝑦0 Β± π‘Ž are also on the circle

Hence there is 8-way symmetry

Reduce the problem to finding the pixels for 1/8 of the circle.

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Scan top right 1/8 of circle of radius 𝑅

Circle starts at π‘₯0, 𝑦0 + 𝑅

Let’s use another incremental algorithm with decision variable evaluated at midpoint

21 of 44

Using the Symmetry

(x0, y0)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

x = x0, y = y0 + R; WritePixel(x, y); for (x = x + 1; (x – x0) > (y – y0); x++) { if (decision_var < 0) { // move east update decision variable } else { // move south east update decision variable y--; } WritePixel(x, y); }

Note: can replace all occurrences of π‘₯0, 𝑦0with 0, shifting coordinates by βˆ’π‘₯0, βˆ’π‘¦0

22 of 44

The incremental algorithm – a sketch

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Decision variable

negative if we move E, positive if we move SE (or vice versa).

Follow line strategy: Use implicit equation of circle

𝑓 π‘₯, 𝑦 = π‘₯2 + 𝑦2 βˆ’ 𝑅2 = 0

𝑓 π‘₯, 𝑦 is zero on circle, negative inside, positive outside

If we are at pixel π‘₯, 𝑦 examine π‘₯ + 1, 𝑦 and π‘₯ + 1, 𝑦 βˆ’ 1

Compute f at the midpoint.

23 of 44

What we need for the Incremental Algorithm

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Evaluate 𝑓 π‘₯, 𝑦 = π‘₯2 + 𝑦2 βˆ’ 𝑅2 at the point:

π‘₯ + 1, 𝑦 βˆ’1

2

We are asking: β€œIs 𝑓 𝑀 =

𝑓 π‘₯ + 1, 𝑦 βˆ’1

2= (π‘₯ + 1)2+(𝑦 βˆ’

1

2)2βˆ’π‘…2

positive or negative?” (it is zero on circle)

24 of 44

The Decision Variable

If negative, midpoint inside circle, choose E vertical distance to the circle is less at (π‘₯ + 1, 𝑦) than at

π‘₯ + 1, 𝑦 βˆ’ 1

If positive, opposite is true, choose SE

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Decision based on vertical distance

Ok for lines, since d and dvert are proportional

For circles, not true:

𝑑 π‘₯ + 1, 𝑦 , πΆπ‘–π‘Ÿπ‘ = π‘₯ + 1 2 + 𝑦2 βˆ’ 𝑅

𝑑 π‘₯ + 1, 𝑦 βˆ’ 1 , πΆπ‘–π‘Ÿπ‘ = π‘₯ + 1 2 + (𝑦 βˆ’ 1)2βˆ’ 𝑅

Which d is closer to zero? (i.e., which value below is closest to R?):

π‘₯ + 1 2 + 𝑦2 or π‘₯ + 1 2 + (𝑦 βˆ’ 1)2

25 of 44

The right decision variable?

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

We could ask instead: β€œIs π‘₯ + 1 2 + 𝑦2 or π‘₯ + 1 2 + (𝑦 βˆ’ 1)2 closer to 𝑅2?”

The two values in equation above differ by:

π‘₯ + 1 2 + 𝑦2 βˆ’ π‘₯ + 1 2 + 𝑦 βˆ’ 1 2 = 2𝑦 βˆ’ 1

26 of 44

Alternate Phrasing (1/3)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

The second value, which is always less, is closer if its difference from R2 is less than:

1

2(2𝑦 βˆ’ 1)

i.e., if 𝑅2 βˆ’ π‘₯ + 1 2 + 𝑦 βˆ’ 1 2 <1

2(2𝑦 βˆ’ 1)

then 0 < 𝑦 βˆ’1

2+ π‘₯ + 1 2 + 𝑦 βˆ’ 1 2 βˆ’ R2

0 < π‘₯ + 1 2 + 𝑦2 βˆ’ 2𝑦 + 1 + 𝑦 βˆ’1

2βˆ’ 𝑅2

0 < π‘₯ + 1 2 + 𝑦2 βˆ’ 𝑦 +1

2βˆ’ 𝑅2

0 < π‘₯ + 1 2 + (𝑦 βˆ’1

2)2+

1

4βˆ’ 𝑅2

27 of 44

Alternate Phrasing (2/3)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

The radial distance decision is whether

𝑑1 = π‘₯ + 1 2 + 𝑦 βˆ’1

2

2

+1

4βˆ’ 𝑅2

is positive or negative.

The vertical distance decision is whether

𝑑2 = π‘₯ + 1 2 + 𝑦 βˆ’1

2

2βˆ’ 𝑅2

is positive or negative; 𝑑1and 𝑑2 are ΒΌ apart.

The integer 𝑑1 is positive only if 𝑑2 + ΒΌ is positive (except special case where 𝑑2 = 0: remember you’re using integers).

28 of 44

Alternate Phrasing (3/3)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

How can we compute the value of

𝑓 π‘₯, 𝑦 = π‘₯ + 1 2 + 𝑦 βˆ’1

2

2βˆ’ 𝑅2

at successive points? (vertical distance approach)

Answer: Note that 𝑓 π‘₯ + 1, 𝑦 βˆ’ 𝑓 π‘₯, 𝑦

= Δ𝐸 π‘₯, 𝑦 = 2π‘₯ + 3

and that 𝑓 π‘₯ + 1, 𝑦 βˆ’ 1 βˆ’ 𝑓(π‘₯, 𝑦)

= Δ𝑆𝐸 π‘₯, 𝑦 = 2π‘₯ βˆ’ 2𝑦 + 5

29 of 44

Incremental Computation Revisited (1/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

If we move E, update d = f(M) by adding 2π‘₯ + 3

If we move SE, update d by adding 2π‘₯ βˆ’ 2𝑦 + 5

Forward differences of a 1st degree polynomial are constants and those of a 2nd degree polynomial are 1st degree polynomials

this β€œfirst order forward difference,” like a partial derivative, is one degree lower

30 of 44

Incremental Computation (2/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

The function Ξ”E π‘₯, 𝑦 = 2π‘₯ + 3 is linear, hence amenable to incremental computation:

Ξ”E π‘₯ + 1, 𝑦 βˆ’ Ξ”E π‘₯, 𝑦 = 2 Ξ”E π‘₯ + 1, 𝑦 βˆ’ 1 βˆ’ Ξ”E π‘₯, 𝑦 = 2

Similarly Ξ”SE π‘₯ + 1, 𝑦 βˆ’ Ξ”SE π‘₯, 𝑦 = 2

Ξ”SE π‘₯ + 1, 𝑦 βˆ’ 1 βˆ’ Ξ”SE π‘₯, 𝑦 = 4

31 of 44

Second Differences (1/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

For any step, can compute new Ξ”E π‘₯, 𝑦 from old Ξ”E π‘₯, 𝑦 by adding appropriate second constant increment – update delta terms as we move. This is also true of Ξ”SE π‘₯, 𝑦 .

Having drawn pixel π‘Ž, 𝑏 , decide location of new pixel at π‘Ž + 1, 𝑏 or

(π‘Ž + 1, 𝑏 – 1), using previously computed Ξ”(π‘Ž, 𝑏)

Having drawn new pixel, must update Ξ”(π‘Ž, 𝑏) for next iteration; need to find

either Ξ”(π‘Ž + 1, 𝑏) π‘œπ‘Ÿ Ξ”(π‘Ž + 1, 𝑏 – 1) depending on pixel choice

Must add Δ𝐸(π‘Ž, 𝑏) or Δ𝑆𝐸(π‘Ž, 𝑏) to Ξ”(π‘Ž, 𝑏)

So we… Look at 𝑑 to decide which to draw next, update π‘₯ and 𝑦

Update d using Δ𝐸(π‘Ž, 𝑏) π‘œπ‘Ÿ Δ𝑆𝐸(π‘Ž, 𝑏)

Update each of Δ𝐸(π‘Ž, 𝑏) π‘Žπ‘›π‘‘ Δ𝑆𝐸(π‘Ž, 𝑏) for future use

Draw pixel

32 of 44

Second Differences (2/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

MidpointEighthCircle(R) { /* 1/8th of a circle w/ radius R */ int x = 0, y = R; int deltaE = 2 * x + 3; int deltaSE = 2 * (x - y) + 5; float decision = (x + 1) * (x + 1) + (y - 0.5) * (y - 0.5) – R*R; WritePixel(x, y); while ( y > x ) { if (decision > 0) { // Move East x++; WritePixel(x, y); decision += deltaE; deltaE += 2; deltaSE += 2; // Update delta } else { // Move SouthEast y--; x++; WritePixel(x, y); decision += deltaSE; deltaE += 2; deltaSE += 4; // Update delta } } }

33 of 44

Midpoint Eighth Circle Algorithm

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Uses floats!

1 test, 3 or 4 additions per pixel

Initialization can be improved

Multiply everything by 4: No Floats!

Makes the components even, but sign of decision variable remains same

Questions

Are we getting all pixels whose distance from the circle is less than Β½?

Why is y > x the right criterion?

What if it were an ellipse?

34 of 44

Analysis

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Aligned Ellipses

Non-integer primitives

General conics

Patterned primitives

35 of 44

Other Scan Conversion Problems

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Equation is π‘₯2

π‘Ž2 +𝑦2

𝑏2 = 1 i.e., 𝑏2π‘₯2 + π‘Ž2𝑦2 = π‘Ž2𝑏2

Computation of Δ𝐸 and Δ𝑆𝐸 is similar

Only 4-fold symmetry

When do we stop stepping horizontally and switch to vertical?

36 of 44

Aligned Ellipses

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

When absolute value of slope of ellipse is more than 1:

How do you check this? At a point π‘₯, 𝑦 for which 𝑓 π‘₯, 𝑦 = 0, a vector perpendicular to the curve is 𝛻𝑓 π‘₯, 𝑦 , which is

[πœ•π‘“

πœ•π‘₯π‘₯, 𝑦 ,

πœ•π‘“

πœ•π‘¦π‘₯, 𝑦 ]

This vector points more right than up when πœ•π‘“

πœ•π‘₯π‘₯, 𝑦 βˆ’

πœ•π‘“

πœ•π‘¦π‘₯, 𝑦 > 0

37 of 44

Direction Changing Criterion (1/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

In our case, πœ•π‘“

πœ•π‘₯π‘₯, 𝑦 = 2𝑏2π‘₯ and

πœ•π‘“

πœ•π‘¦π‘₯, 𝑦 = 2π‘Ž2𝑦

so we check for 2𝑏2π‘₯ βˆ’ 2π‘Ž2𝑦 > 0

𝑏2π‘₯ βˆ’ π‘Ž2𝑦 > 0

This, too, can be computed incrementally

38 of 44

Direction Changing Criterion (2/2)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Now in ENE octant, not ESE octant

This problem is an artifact of aliasing, remember filter?

39 of 44

Problems with aligned ellipses

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 40 of 44

Patterned line from P to Q is not same as patterned line from Q to P.

Patterns can be cosmetic or geometric

Cosmetic: Texture applied after transformations

Geometric: Pattern subject to transformations

Cosmetic patterned line

Geometric patterned line

Patterned Lines

P Q

P Q

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam 41 of 44

Geometric vs. Cosmetic

+

Geometric (Perspectivized/Filtered)

Cosmetic (Real-World Contact Paper)

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Non-Integer Primitives

Initialization is harder Endpoints are hard, too

making Line (𝑃, 𝑄) and Line (𝑄, 𝑅) join properly is a good test

Symmetry is lost

General Conics

Very hard – the octant-changing test is tougher, the difference computations are tougher, etc. Do it only if you have to

42 of 44

Non-Integer Primitives and General Conics

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

What's the difference between these two solutions? Under which circumstances is the right one "better"?

43 of 44

Generic Polygons

Balsa Demo

Scan Conversion - 10/16/12

CS123 | INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam

Rapid Prototyping is becoming cheaper and more prevalent

3D printers lay down layer after layer to produce solid objects

We have an RP lab across the street in Barus and Holley

http://www.youtube.com/watch?v=ZboxMsSz5Aw

http://www.youtube.com/watch?v=Ah1067HXNdM&NR=1

44 of 44

Scan Converting Arbitrary Solids

Scan Conversion - 10/16/12