contouring in c ats 315. how contours really work contours may look like curves, but they are really...

146
Contouring in C ATS 315

Upload: gwendoline-hicks

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Contouring in C

ATS 315

How Contours Really Work

• Contours may LOOK like curves, but they are really just straight line segments.

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

How Contouring Works:

So What We Are Really Worried About…

So What We Are Really Worried About…

So What We Are Really Worried About…

The northwest corner is grid[i][j].

grid[i][j]

The northwest corner is grid[i][j].

grid[i][j] grid[i][j+1]

grid[i+1][j] grid[i+1][j+1]

The northwest corner is grid[i][j].

grid[i][j] grid[i][j+1]

grid[i+1][j] grid[i+1][j+1]

How the contour is drawn depends on the value of the contour and the

values at the four corners!

grid[i][j] grid[i][j+1]

grid[i+1][j] grid[i+1][j+1]

For simplicity, rename the values at the corners:

nw ne

sw se

Count the number of corners with values greater than the contour

line.

nw ne

sw se

CornersGreaterThanContour can be 0, 1, 2, 3, or 4.

nw ne

sw se

if (CornersGreaterThanContour==0) do nothing

nw ne

sw se

if (CornersGreaterThanContour==4) do nothing

nw ne

sw se

if (CornersGreaterThanContour==1)… there are four possibilities:

nw ne

sw se

if (CornersGreaterThanContour==1)… there are four possibilities:

nw ne

sw se

Possibility 1: Only the northwest corner is greater than the value of the contour.

if (CornersGreaterThanContour==1)… there are four possibilities:

nw ne

sw se

Possibility 2: Only the northeast corner is greater than the value of the contour.

if (CornersGreaterThanContour==1)… there are four possibilities:

nw ne

sw se

Possibility 3: Only the southeast corner is greater than the value of the contour.

if (CornersGreaterThanContour==1)… there are four possibilities:

nw ne

sw se

Possibility 4: Only the southwest corner is greater than the value of the contour.

if (CornersGreaterThanContour==2)… there are three possibilities:

nw ne

sw se

if (CornersGreaterThanContour==2)… there are three possibilities:

nw ne

sw se

Possibility 1: The line should be drawn from the west edge to the east edge.

Either:

Both ne and nw are bigger than contour…

or

Both ne and nw are smaller than contour.

if (CornersGreaterThanContour==2)… there are three possibilities:

nw ne

sw se

Possibility 2: The line should be drawn from the north edge to the south edge.

Either:

Both ne and se are bigger than contour…

or

Both ne and se are smaller than contour.

if (CornersGreaterThanContour==2)… there are three possibilities:

nw ne

sw se

Possibility 3: Two contour lines pass through this box.

Either:

Both nw and se are bigger than contour…

or

Both nw and se are smaller than contour.

if (CornersGreaterThanContour==3)… there are four possibilities:

nw ne

sw se

if (CornersGreaterThanContour==3)… there are four possibilities:

nw ne

sw se

Possibility 1: Only the northwest corner is less than the value of the contour.

if (CornersGreaterThanContour==3)… there are four possibilities:

nw ne

sw se

Possibility 2: Only the northeast corner is less than the value of the contour.

if (CornersGreaterThanContour==3)… there are four possibilities:

nw ne

sw se

Possibility 3: Only the southeast corner is less than the value of the contour.

if (CornersGreaterThanContour==3)… there are four possibilities:

nw ne

sw se

Possibility 4: Only the southwest corner is less than the value of the contour.

What will this program look like?

What will this program look like?

/* Compute CornersGreaterThanContour */

if (CornersGreaterThanContour == 0) { }

if (CornersGreaterThanContour == 1) { }

if (CornersGreaterThanContour == 2) { }

if (CornersGreaterThanContour == 3) { }

if (CornersGreaterThanContour == 4) { }

What will this program look like?

/* Compute CornersGreaterThanContour */

if (CornersGreaterThanContour == 0) {

/* Do nothing */

}

if (CornersGreaterThanContour == 1) { }

if (CornersGreaterThanContour == 2) { }

if (CornersGreaterThanContour == 3) { }

if (CornersGreaterThanContour == 4) {

/* Do nothing */

}

What will this program look like?

if (CornersGreaterThanContour == 1) { }

if (CornersGreaterThanContour == 2) { }

if (CornersGreaterThanContour == 3) { }

What will this program look like?

if (CornersGreaterThanContour == 1) {

if( nw > contour) { }

if( ne > contour) { }

if( se > contour) { }

if( sw > contour) { }

}

if (CornersGreaterThanContour == 2) { }

if (CornersGreaterThanContour == 3) { }

What will this program look like?

if (CornersGreaterThanContour == 2) { }

if (CornersGreaterThanContour == 3) { }

What will this program look like?

if (CornersGreaterThanContour == 2) {

if( (ne > contour && nw > contour) ||

(ne < contour && nw < contour)) { }

if( (ne > contour && se > contour) ||

(ne < contour && se < contour)) { }

if( (nw > contour && se > contour) ||

(nw < contour && se < contour)) { }

}

if (CornersGreaterThanContour == 3) { }

What will this program look like?

if (CornersGreaterThanContour == 3) { }

What will this program look like?

if (CornersGreaterThanContour == 3) {

if( nw < contour) { }

if( ne < contour) { }

if( se < contour) { }

if( sw < contour) { }

}

Deciding how to draw the contour

nw ne

sw se

Let’s say that you have determined that this is the kind of line you need to draw.

This line segment has a starting point and an ending point.

Deciding how to draw the contour

nw ne

sw se

What determines the location of the starting point?

Deciding how to draw the contour

nw ne

sw se

What determines the location of the starting point?

INTERPOLATION!

Deciding how to draw the contour

nw ne

sw se

Suppose we are drawing the 1000 mb contour.

nw = 999.9

ne = 1022.1

Deciding how to draw the contour

nw ne

sw se

Suppose we are drawing the 1000 mb contour.

nw = 982.2

ne = 1001.1

Deciding how to draw the contour

nw ne

sw se

startlat = ???

startlon = ???

Deciding how to draw the contour

nw ne

sw se

startlat = depends on:•latitude of nw•latitude of ne•value at nw•value at ne•value of contour

startlon = depends on:•longitude of nw•longitude of ne•value at nw•value at ne•value of contour

Deciding how to draw the contour

nw ne

sw se

interp(gridlatitude[i][j], &startlat, gridlatitude[i][j+1], nw, contour, ne);

interp(gridlongitude[i][j], &startlon, gridlongitude[i][j+1], nw, contour, ne);

Deciding how to draw the contour

nw ne

sw se

How about the endlat and endlon?

Deciding how to draw the contour

nw ne

sw se

interp(gridlatitude[i][j+1], &endlat, gridlatitude[i+1][j+1], ne, contour, se);

interp(gridlongitude[i][j+1], &endlon, gridlongitude[i+1][j+1], ne, contour, se);

Deciding how to draw the contour

nw ne

sw se

Once you have (startlat, startlon) and (endlat, endlon):

1. tranform

2. clip

3. gline

How this produces contours

How this produces contours

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

How this produces contours

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

for(contour=mincontour;contour<=maxcontour;contour=contour+conint)

i=0

i=1

i=2

i=3

j=0 j=1 j=2 j=3

Great, I can picture what this looks like, but how do I do it?

The Steps

• Open the window

• Draw the base map

• Read the sao.cty file

• Read the current_sao.wxp file

• Produce grids of objectively analyzed data.

• Contour (like this….)

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

For every “square” on the map…

(Notice that there are

(NUMROWS-1)x(NUMCOLS-1) squares…)

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

For every possible contour level…

(We’ll discuss how to figure this out shortly.)

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

Determine the number of corners on this square

that are greater than the current contour level.

(This will take about 5 lines of code.)

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

For each of the 5 possible values of

CornersGreaterThanContour, you’ll need the

elaborate “if” statements discussed earlier.

for(i=0;i<NUMROWS-1;i++) {for(j=0;j<NUMCOLS-1;j++) {

for(contour=mincon;contour<=maxcon;contour=contour+conint) {

CornersGreaterThanContour = ?????;

if (CornersGreaterThanContour==0) { }if (CornersGreaterThanContour==1) { }if (CornersGreaterThanContour==2) { }if (CornersGreaterThanContour==3) { }if (CornersGreaterThanContour==4) { }

}

}}

Where do you get these values?

mincon, maxcon, conint

mincon, maxcon, conint

• You could just prompt the user for these three values.

• Better: prompt the user for conint, and compute mincon and maxcon!

• But, to do this, you first need to be able to compute max and min of the grid!

max and min

• Suppose you have a 2D grid of floating point numbers.

• min needs to be the lowest value in the grid, and max needs to be the highest.

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

max and min

min = 100000000.;

max = -100000000.;

for(i=0;i<NUMROWS;i++) {

for(j=0;j<NUMCOLS;j++) {

if (grid[i][j] < min) min = grid[i][j];

if (grid[i][j] > max) max = grid[i][j];

}

}

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

max and minmin = 100000000.;max = -100000000.;

for(i=0;i<NUMROWS;i++) {for(j=0;j<NUMCOLS;j++) {

if (grid[i][j] < min) min = grid[i][j];if (grid[i][j] > max) max = grid[i][j];

}}

• Set min to a very high number and max to a very low number.

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

max and min

min = 100000000.;

max = -100000000.;

for(i=0;i<NUMROWS;i++) {

for(j=0;j<NUMCOLS;j++) {

if (grid[i][j] < min) min = grid[i][j];

if (grid[i][j] > max) max = grid[i][j;

}

}

• For every element of the 2D array…

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

max and min

min = 100000000.;

max = -100000000.;

for(i=0;i<NUMROWS;i++) {

for(j=0;j<NUMCOLS;j++) {

if (grid[i][j] < min) min = grid[i][j];

if (grid[i][j] > max) max = grid[i][j];

}

}

• If grid[i][j] < min, min=grid[i][j] !

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

max and min

min = 100000000.;

max = -100000000.;

for(i=0;i<NUMROWS;i++) {

for(j=0;j<NUMCOLS;j++) {

if (grid[i][j] < min) min = grid[i][j];

if (grid[i][j] > max) max = grid[i][j];

}

}

• If grid[i][j] > max, max=grid[i][j] !

22.5 25.5 28.3

21.5 15.5 29.0

29.1 18.2 19.9

But Sadly…

• min mincon

• max maxcon

But Happily…

• mincon = (float) (( (int)(min/conint) + 1) * conint)• maxcon = (float) (( (int)(max/conint) ) * conint)

You can work out the math and see that this works! (maxcon is easier than mincon)

Grading Assignment 15

• Properly determine mincon and maxcon from a grid of data, prompting the user for the contour interval.

Grading Assignment 15

• Correctly determining most of the things you need to draw contours, but not getting good output:

Grading Assignment 15

• Successfully contouring temperature fields:

Grading Assignment 15

• Successfully contouring any grid of data chosen by the user:

Suggested “Impressive” Things

• Variable domains

• Variable colors for the contours

• Nice labels for the plot (NOT contour labels!)