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:
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: nwne swse
Count the number of corners with values greater than the contour line. nwne swse
CornersGreaterThanContour can be 0, 1, 2, 3, or 4. nwne swse
if (CornersGreaterThanContour==0) do nothing nwne swse
if (CornersGreaterThanContour==4) do nothing nwne swse
if (CornersGreaterThanContour==1)… there are four possibilities: nwne swse
if (CornersGreaterThanContour==1)… there are four possibilities: nwne swse Possibility 1: Only the northwest corner is greater than the value of the contour.
if (CornersGreaterThanContour==1)… there are four possibilities: nwne swse Possibility 2: Only the northeast corner is greater than the value of the contour.
if (CornersGreaterThanContour==1)… there are four possibilities: nwne swse Possibility 3: Only the southeast corner is greater than the value of the contour.
if (CornersGreaterThanContour==1)… there are four possibilities: nwne swse Possibility 4: Only the southwest corner is greater than the value of the contour.
if (CornersGreaterThanContour==2)… there are three possibilities: nwne swse
if (CornersGreaterThanContour==2)… there are three possibilities: nwne swse 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: nwne swse 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: nwne swse 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: nwne swse
if (CornersGreaterThanContour==3)… there are four possibilities: nwne swse Possibility 1: Only the northwest corner is less than the value of the contour.
if (CornersGreaterThanContour==3)… there are four possibilities: nwne swse Possibility 2: Only the northeast corner is less than the value of the contour.
if (CornersGreaterThanContour==3)… there are four possibilities: nwne swse Possibility 3: Only the southeast corner is less than the value of the contour.
if (CornersGreaterThanContour==3)… there are four possibilities: nwne swse Possibility 4: Only the southwest corner is less than the value of the contour.
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 nwne swse 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 nwne swse What determines the location of the starting point?
Deciding how to draw the contour nwne swse What determines the location of the starting point? INTERPOLATION!
Deciding how to draw the contour nwne swse Suppose we are drawing the 1000 mb contour. nw = ne =
Deciding how to draw the contour nwne swse Suppose we are drawing the 1000 mb contour. nw = ne =
Deciding how to draw the contour nwne swse startlat = ??? startlon = ???
Deciding how to draw the contour nwne swse 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 nwne swse 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 nwne swse How about the endlat and endlon?
Deciding how to draw the contour nwne swse 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 nwne swse Once you have (startlat, startlon) and (endlat, endlon): 1.tranform 2.clip 3.gline
How this produces contours
i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
How this produces contours i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=3
for(contour=mincontour;contour<=maxcontour;contour=contour+conint) i=0 i=1 i=2 i=3 j=0j=1j=2j=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
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
max and min min = ; max = ; 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]; }
max and min min = ; max = ; 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
max and min min = ; max = ; 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…
max and min min = ; max = ; 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] !
max and min min = ; max = ; 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] !
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!)