Title: mapdraw
1mapdraw
2Todays goal
- Drawing a map in the graphics window!
3Previous Skills
- Basic graphic commands (opening windows, drawing
lines) - Reading files
- The while statement
- functions
4The Map Files
- state.ats315.dat
- nice state outlines
- usa_cnty.ats315.dat
- nice county outlines
- rd_int.ats315.dat
- U.S. Interstate Highways
- rivermap.ats315.dat
- major U.S. rivers
5Where are the map files?
6How the map files work
- They are collections of line segments.
- lat1 lon1 lat2 lon2
- You dont know in advance how many line segments
there will be.
7Reading the map file
while (!feof(fmap)) fscanf (fmap, f f f
f,lat1, lon1, lat2, lon2) / Now do stuff
with this information /
8Reading the map file
while (!feof(fmap)) fscanf (fmap, f f f
f,lat1, lon1, lat2, lon2) / Now do stuff
with this information /
- What to do with these lats and lons is the tricky
part!
9Why this is tricky
- The map file is full of latitudes and longitudes
of the line segments that make up the map. - Your graphics window is NOT expressed in terms of
latitude and longituderather it is a unit
square! - Therefore, you need to transform (lat,lon) into
(x,y).
10Transforming coordinates
- Is best done in a function.
- Can be done in two functions, but Id prefer to
see you do it with just one.
11Transformations
- Latitude corresponds to the y direction.
- Longitude corresponds to the x direction.
12Transformations
- Lets focus on LATITUDE first.
13Transformations
- You need some kind of function that converts the
latitude to a number between 0 and 1.
14Transformations
- More to the point, you need this function to
convert JUST THE PART OF THE MAP YOU WANT to a
number between 0 and 1.
15Transformations
- For example, here clearly some latitudes are
outside the (0,1) range for y, and so they arent
plotted.
16Transformations
- You could write a function called
transformlatitude(lat).
17Transformations
- Your northernmost latitude becomes 1.
- YOU get to pick your northernmost latitude!
18Transformations
- Your southernmost latitude becomes 0.
- YOU get to pick your southernmost latitude!
19Transformations
- Could look something like this
float transformlatitude(float lat) float
northernmost, southernmost. y northernmost
40. southernmost 30. y (lat
southernmost) / (northernmost
southernmost) return y
20Transformations
- Interpolates your lat between 0 and 1 if it is
between northernmost and southernmost
float transformlatitude(float lat) float
northernmost, southernmost. y northernmost
40. southernmost 30. y (lat
southernmost) / (northernmost
southernmost) return y
21Transformations
- Then your map reading program could call
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
/
22Transformations
- 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 /
23Transformations
- Then decide what you are going to do with this
new (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 /
24Ideally
- you really should use just ONE function to
transform the latitudes and longitudes into x and
y - transform(lat,lon,x,y)
- But, to do this, youll need to think VERY
carefully about which parameters are passed by
VALUE and which are passed by ADDRESS!
25Which one is it?
- transform(lat,lon,x,y)
- transform(lat,lon,x,y)
- transform(lat,lon,x,y)
- transform(lat,lon,x,y)
- Or even something else????
26Your Assignment
- There is no specific assignment about this
lecture. - You DO have to draw a mapwith clippingfor
Friday. - Therefore, start working on this NOW!