Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mapdraw ats315. Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!"— Presentation transcript:

1 mapdraw ats315

2 Today’s goal: Drawing a map in the graphics window! Drawing a map in the graphics window!

3 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

4 The Map Files state.ats315.dat state.ats315.dat –nice state outlines usa_cnty.ats315.dat usa_cnty.ats315.dat –nice county outlines rd_int.ats315.dat rd_int.ats315.dat –U.S. Interstate Highways rivermap.ats315.dat rivermap.ats315.dat –major U.S. rivers

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

6 How the map files work: They are collections of line segments. They are collections of line segments. lat1 lon1 lat2 lon2 lat1 lon1 lat2 lon2 You don’t know in advance how many line segments there will be. You don’t know in advance how many line segments there will be.

7 Reading the map file: while (!feof(fmap)) { fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2, &lon2); /* Now do stuff with this information */ }

8 Reading the map file: What to do with these lats and lons is the tricky part! What to do with these lats and lons is the tricky part! while (!feof(fmap)) { fscanf (fmap, “%f %f %f %f”,&lat1, &lon1, &lat2, &lon2); /* Now do stuff with this information */ }

9 Why this is tricky: The map file is full of latitudes and longitudes of the line segments that make up the map. 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 longitude—rather it is a unit square! Your graphics window is NOT expressed in terms of latitude and longitude—rather it is a unit square! Therefore, you need to transform (lat,lon) into (x,y). Therefore, you need to transform (lat,lon) into (x,y).

10 Transforming coordinates: Is best done in a function. Is best done in a function. Can be done in two functions, but I’d prefer to see you do it with just one. Can be done in two functions, but I’d prefer to see you do it with just one.

11 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.

12 Transformations: Let’s focus on LATITUDE first. Let’s focus on LATITUDE first.

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

14 Transformations: 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. 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.

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

16 Transformations: You could write a function called transformlatitude(lat). You could write a function called transformlatitude(lat).

17 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!

18 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!

19 Transformations: Could look something like this: float transformlatitude(float lat) { float northernmost, southernmost. y; northernmost = 40.; southernmost = 30.; y = (lat – southernmost) / (northernmost – southernmost); return y; }

20 Transformations: 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; }

21 Transformations: 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 */ }

22 Transformations: 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 */ }

23 Transformations: 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 */ }

24 Ideally… you really should use just ONE function to transform the latitudes and longitudes into x and y: you really should use just ONE function to transform the latitudes and longitudes into x and y: transform(lat,lon,x,y); transform(lat,lon,x,y); But, to do this, you’ll need to think VERY carefully about which parameters are passed by VALUE and which are passed by ADDRESS! But, to do this, you’ll need to think VERY carefully about which parameters are passed by VALUE and which are passed by ADDRESS!

25 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????

26 Your Assignment: There is no specific assignment about this lecture. There is no specific assignment about this lecture. You DO have to draw a map—with “clipping”—for Friday. You DO have to draw a map—with “clipping”—for Friday. Therefore, start working on this NOW! Therefore, start working on this NOW!


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

Similar presentations


Ads by Google