mapdraw ats315. today’s goal: drawing a map in the graphics window! drawing a map in the graphics...
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 functionsTRANSCRIPT
mapdrawmapdrawats315ats315
Today’s goal:Today’s goal: Drawing a Drawing a
map in the map in the graphics graphics window!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
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
Where are the map Where are the map files?files? /usr/local/wxp/etc//usr/local/wxp/etc/
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.
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 */}
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 */
}
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).
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.
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.
Transformations:Transformations:
Let’s focus on LATITUDE first.Let’s focus on LATITUDE first.
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.
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.
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.
Transformations:Transformations:
You could write a function called You could write a function called transformlatitude(lat).transformlatitude(lat).
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!
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!
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;}
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;}
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 */
}
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 */
}
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 */
}
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!
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????
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!