RiverRat  - An MIT iCampus project http://web.mit.edu/riverrat/

Main Page | Software Documentation | Hardware Documentation | People | Contact | Wiki

Main Page | Class Hierarchy | Class List | File List | Class Members

Map.java

00001 package riverrat;
00002 
00009 public class Map {
00010         static final boolean DEBUG = false;
00011         
00012         private Fix nw;
00013         private Fix se;
00014         
00015         private boolean constrain_height = true;
00016         private boolean constrain_width = false;
00017         
00022         Map() {
00023                 nw = new Fix();
00024                 se = new Fix();
00025         }
00026         
00034         Map(Fix f1, Fix f2) {
00035                 float northlat, southlat, eastlon, westlon;
00036                 if(f1.getLon() > f2.getLon()) {
00037                         //f1's lon is farther east, so make it the east side
00038                         eastlon = f1.getLon();
00039                         westlon = f2.getLon();
00040                 } else if(f1.getLon() < f2.getLon()) {
00041                         //f2's lon is farther east, so make it the east side
00042                         eastlon = f2.getLon();
00043                         westlon = f1.getLon();
00044                 } else {
00045                         //both lon's are the same
00046                         eastlon = f1.getLon();
00047                         westlon = f1.getLon();
00048                 }
00049                 if(f1.getLat() > f2.getLat()) {
00050                         //f1's lat is farther noth, so make it the north side
00051                         northlat = f1.getLat();
00052                         southlat = f2.getLat();
00053                 } else if(f1.getLat() < f2.getLat()) {
00054                         northlat = f2.getLat();
00055                         southlat = f1.getLat();
00056                 } else {
00057                         northlat = f1.getLat();
00058                         southlat = f1.getLat();
00059                 }
00060                 nw = new Fix(northlat, westlon);
00061                 se = new Fix(southlat, eastlon);
00062         }
00063         
00070         public float getAspect() {
00071                 return (getWidth()/getHeight());
00072         }
00073         
00079         public boolean isFixedAspect() {
00080                 return (constrain_height && constrain_width);
00081         }
00082         
00088         public void setAspect(float asp) {
00089                 if(constrain_height && constrain_width) {
00090                         //this should throw an exception or something
00091                         return;
00092                 } else if(constrain_height && !constrain_width) {
00093                         setWidth(asp*getHeight());
00094                 } else if(!constrain_height && constrain_width) {
00095                         setHeight(getWidth()/asp);
00096                 }
00097         }
00098         
00099         
00100         private void setWidth(float newwidth) {
00101                 if(constrain_width) return;
00102                 se.setLon(getWestLon() + newwidth);
00103         }
00104         
00105         private void setHeight(float newheight) {
00106                 if(constrain_height) return;
00107                 se.setLat(getNorthLat() - newheight);
00108         }
00109         
00118         public float getLonFromX(float x) {
00119                 if(x > 1.0f) x = 1.0f;
00120                 if(x < 0.0f) x = 0.0f;
00121                 return ((getWidth()*x)+getEastLon());
00122         }
00123         
00132         public float getLatFromY(float y) {
00133                 if(y > 1.0f) y = 1.0f;
00134                 if(y < 0.0f) y = 0.0f;
00135                 return ((getHeight()*(-y))+getSouthLat());
00136         }
00137         
00145         public float getXFromLon(float lon) {
00146                 if(DEBUG) {
00147                         System.out.println("getXFromLon(): called with lon "+lon);
00148                 }
00149                 if((nw == null) || (se == null)) {
00150                         if(DEBUG) {
00151                                 System.out.println("getXFromLon(): map has null corners, returning -1");
00152                         }
00153                         return -1;
00154                 }
00155                 if(DEBUG) {
00156                         System.out.println("getXFromLon(): returning "+((lon-getWestLon())/getWidth()));
00157                 }
00158                 return (float)(lon - getWestLon())/(getWidth());
00159         }
00160         
00168         public float getYFromLat(float lat) {
00169                 if(DEBUG) {
00170                         System.out.println("getYFromLat(): called with lat "+lat);
00171                 }
00172                 if((nw == null) || (se == null)) {
00173                         if(DEBUG) {
00174                                 System.out.println("getYFromLat(): map has null corners, returning -1");
00175                         }
00176                         return -1;
00177                 }
00178                 if(DEBUG) {
00179                         System.out.println("getYFromLat(): returning "+((lat-getSouthLat())/getHeight()));
00180                 }
00181                 return 1.0f - (float)((lat - getSouthLat())/(getHeight()));
00182         }
00183         
00189         public float getWidth() {
00190                 return (float) Math.abs(nw.getLon() - se.getLon());
00191         }
00192 
00198         public float getHeight(){
00199                 return (float) Math.abs(nw.getLat() - se.getLat());
00200         }
00201         
00207         public float getNorthLat() {
00208                 return (float) nw.getLat();
00209         }
00210         
00216         public float getSouthLat() {
00217                 return (float) se.getLat();
00218         }
00219         
00225         public float getWestLon() {
00226                 return (float) nw.getLon();
00227         }
00228         
00234         public float getEastLon() {
00235                 return (float) se.getLon();
00236         }
00237 
00238 }
 

Brought to you by the RiverRat team.