26
mapdraw mapdraw ats315 ats315

Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Embed Size (px)

DESCRIPTION

Previous Skills: Basic graphic commands (opening windows, drawing lines) Basic graphic commands (opening windows, drawing lines) Reading files Reading files The “while” statement The “while” statement functions functions

Citation preview

Page 1: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

mapdrawmapdrawats315ats315

Page 2: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Today’s goal:Today’s goal: Drawing a Drawing a

map in the map in the graphics graphics window!window!

Page 3: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Previous Skills:Previous Skills: Basic graphic commands Basic graphic commands

(opening windows, drawing lines)(opening windows, drawing lines) Reading filesReading files The “while” statementThe “while” statement functionsfunctions

Page 4: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

The Map FilesThe Map Files state.ats315.datstate.ats315.dat

– nice state outlinesnice state outlines usa_cnty.ats315.datusa_cnty.ats315.dat

– nice county outlinesnice county outlines rd_int.ats315.datrd_int.ats315.dat

– U.S. Interstate HighwaysU.S. Interstate Highways rivermap.ats315.datrivermap.ats315.dat

– major U.S. riversmajor U.S. rivers

Page 5: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Where are the map Where are the map files?files? /usr/local/wxp/etc//usr/local/wxp/etc/

Page 6: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

How the map files How the map files work:work: They are collections of line They are collections of line

segments.segments. lat1 lon1 lat2 lon2lat1 lon1 lat2 lon2 You don’t know in advance how You don’t know in advance how

many line segments there will be.many line segments there will be.

Page 7: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Reading the map file:Reading the map file:while (!feof(fmap)) {

fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2, &lon2);

/* Now do stuff with this information */}

Page 8: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Reading the map file:Reading the map file:

What to do with these lats and What to do with these lats and lons is the tricky part!lons is the tricky part!

while (!feof(fmap)) {fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2,

&lon2);/* Now do stuff with this information */

}

Page 9: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Why this is tricky:Why this is tricky: The map file is full of latitudes and The map file is full of latitudes and

longitudes of the line segments longitudes of the line segments that make up the map.that make up the map.

Your graphics window is NOT Your graphics window is NOT expressed in terms of latitude and expressed in terms of latitude and longitude—rather it is a unit longitude—rather it is a unit square!square!

Therefore, you need to transform Therefore, you need to transform (lat,lon) into (x,y).(lat,lon) into (x,y).

Page 10: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transforming Transforming coordinates:coordinates: Is best done in a Is best done in a functionfunction.. Can be done in two functions, but Can be done in two functions, but

I’d prefer to see you do it with I’d prefer to see you do it with just one.just one.

Page 11: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

Latitude corresponds to the y direction.Latitude corresponds to the y direction. Longitude corresponds to the x direction.Longitude corresponds to the x direction.

Page 12: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

Let’s focus on LATITUDE first.Let’s focus on LATITUDE first.

Page 13: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

You need some kind of function that You need some kind of function that converts the latitude to a number converts the latitude to a number between 0 and 1.between 0 and 1.

Page 14: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

More to the point, you need this function to More to the point, you need this function to convert JUST THE PART OF THE MAP YOU convert JUST THE PART OF THE MAP YOU WANT to a number between 0 and 1.WANT to a number between 0 and 1.

Page 15: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

For example, here clearly some For example, here clearly some latitudes are outside the (0,1) range latitudes are outside the (0,1) range for y, and so they aren’t plotted.for y, and so they aren’t plotted.

Page 16: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

You could write a function called You could write a function called transformlatitude(lat).transformlatitude(lat).

Page 17: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

Your “northernmost” latitude becomes 1.Your “northernmost” latitude becomes 1. YOU get to pick your northernmost latitude!YOU get to pick your northernmost latitude!

Page 18: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

Your “southernmost” latitude becomes 0.Your “southernmost” latitude becomes 0. YOU get to pick your southernmost latitude!YOU get to pick your southernmost latitude!

Page 19: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:

Could look something like this:Could look something like this:

float transformlatitude(float lat) {float northernmost, southernmost. y;

northernmost = 40.;southernmost = 30.;y = (lat – southernmost) / (northernmost –

southernmost);

return y;}

Page 20: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:Interpolates your “lat” between 0 and 1 if it is between Interpolates your “lat” between 0 and 1 if it is between

northernmost and southernmost:northernmost and southernmost:

float transformlatitude(float lat) {float northernmost, southernmost. y;

northernmost = 40.;southernmost = 30.;y = (lat – southernmost) / (northernmost –

southernmost);

return y;}

Page 21: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:Then your map reading program could call Then your map reading program could call

transformlatitude to get a value on the unit square:transformlatitude to get a value on the unit square:

while (!feof(fmap)) {fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2,

&lon2);y1 = transformlatitude (lat1);y2 = transformlatitude (lat2);/* Now do this with the longitudes as well! *//* Now do stuff with this information */

}

Page 22: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:A similar function could be written for longitudes then:A similar function could be written for longitudes then:

while (!feof(fmap)) {fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2,

&lon2);y1 = transformlatitude (lat1);y2 = transformlatitude (lat2);x1 = transformlongitude (lon1);x2 = transformlongitude (lon2);/* Now do stuff with this information */

}

Page 23: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Transformations:Transformations:Then decide what you are going to do with this new Then decide what you are going to do with this new

(x1,y1) and (x2,y2)!(x1,y1) and (x2,y2)!

while (!feof(fmap)) {fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2,

&lon2);y1 = transformlatitude (lat1);y2 = transformlatitude (lat2);x1 = transformlongitude (lon1);x2 = transformlongitude (lon2);/* Now do stuff with this information */

}

Page 24: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Ideally…Ideally… you really should use just ONE function you really should use just ONE function

to transform the latitudes and to transform the latitudes and longitudes into x and y:longitudes into x and y:

transform(lat,lon,x,y);transform(lat,lon,x,y);

But, to do this, you’ll need to think But, to do this, you’ll need to think VERY carefully about which VERY carefully about which parameters are passed by VALUE and parameters are passed by VALUE and which are passed by ADDRESS!which are passed by ADDRESS!

Page 25: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Which one is it?Which one is it? transform(lat,lon,x,y);transform(lat,lon,x,y); transform(&lat,&lon,x,y);transform(&lat,&lon,x,y); transform(lat,lon,&x,&y);transform(lat,lon,&x,&y); transform(&lat,&lon,&x,&y);transform(&lat,&lon,&x,&y);

Or even something else????Or even something else????

Page 26: Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

Your Assignment:Your Assignment: There is no specific assignment There is no specific assignment

about this lecture.about this lecture. You DO have to draw a map—with You DO have to draw a map—with

“clipping”—for Friday.“clipping”—for Friday. Therefore, start working on this Therefore, start working on this

NOW!NOW!