/**
    A representation of the geometry of a map of an areay of land for
    which a mission pertains.

    @author RPD
*/
public class StrikeMap {
    ScreenLoc dim;
    LatLong upperleftcorner;
    LatLong lowerrightcorner;

    public LatLong ScreenToMapCoords(ScreenLoc sl) {
        double maplongW=upperleftcorner.GetLong();
        double maplongE=lowerrightcorner.GetLong();
        double maplatN=upperleftcorner.GetLat();
        double maplatS=lowerrightcorner.GetLat();
        double mlat = maplatN - (((double) sl.GetY()) / (double) dim.GetY()) * (maplatN - maplatS);
        double mlong = maplongW + (((double) sl.GetX()) / (double) dim.GetX()) * (maplongE - maplongW);
        LatLong ll=new LatLong(mlat, mlong);
        return ll;
    }

    // lat, NS, y
    public ScreenLoc MapToScreenCoords(LatLong ll) {
        double maplongW=upperleftcorner.GetLong();
        double maplongE=lowerrightcorner.GetLong();
        double maplatN=upperleftcorner.GetLat();
        double maplatS=lowerrightcorner.GetLat();
        
        // round these?
        int my = dim.Y - (int) (((ll.GetLat() - maplatS)/(maplatN - maplatS)) * (double) dim.GetY());
        int mx = (int) (((ll.GetLong() - maplongW)/(maplongE - maplongW)) * (double) dim.GetX());
//        System.err.println(ll + " maps to " + mx + ", " + my);
//        System.err.println("maplongW " + maplongW + " maplongE " + maplongE);
//        System.err.println("maplatS " + maplatS + " maplatN " + maplatN);

        //System.out.println( " MY : " + my + "MX: " + mx );
        if(mx < 0) mx=0;
        if(mx >= dim.GetX()) mx=dim.GetX()-1;
        if(my < 0) my=0;
        if(my >= dim.GetY()) my=dim.GetY()-1;

        ScreenLoc sl=new ScreenLoc(mx, my);
        return sl;
    }

    public StrikeMap(double maplongW, double maplongE, double maplatN, double maplatS, int maxx, int maxy) {
        upperleftcorner=new LatLong(maplatN, maplongW);
        lowerrightcorner=new LatLong(maplatS, maplongE);
        dim=new ScreenLoc(maxx, maxy);
    }
}
