<?

/**
 * 
 * This file copyright (C) 2006 Barry Hunter (nearby@barryhunter.co.uk)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#INPUTS
#$west,$south,$east,$north represent the bbox
#$clat,$clong are the calculated center of the bbox
#$lat,$long are the lookat center

#OUTPUTS
#$west,$south,$east,$north potentially modified bbox

#CAVATS
# creates a new square bbox (as that what the grids need - will have a second version creating a no square bbox)


    
$uplat = ($clat $north) / 2.0;
    
$lolat = ($south $clat) / 2.0;

    
//is the lookat point outside the central square of the BBOX (hence large tilt)
    
if ($lat $uplat) {
        
$diflat = ($north $lat);
    } elseif (
$lat $lolat) {
        
$diflat = ($lat $south);        
    }
    
    
$uplong = ($clong $east) / 2.0;
    
$lolong = ($west $clong) / 2.0;

    if (
$long $uplong) {
        
$diflong = ($east $long);
    } elseif (
$long $lolong) {
        
$diflong = ($long $west);
    }
    
    
//find a suitable 'distance' from an edge
    
$dif abs(max($diflat,$diflong));
    
    
//if we have an off center view create a new square and recenter it 'in the foreground' 
    
if ($dif) {
        function 
interpolate_part($one,$two,$fraction) {
            
$big $two $one;
            
$small $fraction $big;
            return 
$one $small;
        }

        
$linelength sqrt(pow($lat $clat,2) + pow($long $clong,2));
        
$fraction $dif $linelength;

        
//find the point on the line between the lookat and the center point
        
$nlat interpolate_part($lat,$clat$fraction);
        
$nlong interpolate_part($long,$clong$fraction);

        
//and recenter the 'square' on that new point
        
$south $nlat $dif;
        
$north $nlat $dif;

        
$west $nlong $dif;
        
$east $nlong $dif;
    }
    
?>