/**************************************************************************
 * Great Circle Computations
 * author: thomas.albrecht@udo.edu
 * last changes: 2011-08-05
 *
 * based on
 * Snyder, J.P. Map Projections: A Working Manual. US Geological Survey
 *		Professional Paper. 1987.
 *
/**************************************************************************/


// computes the great circle distance between two points
// result is given in degrees
function haversineDistance(p1,p2){
	slat = Math.sin((p1.latitude/180*Math.PI-p2.latitude/180*Math.PI)/2);
	slon = Math.sin((p1.longitude/180*Math.PI-p2.longitude/180*Math.PI)/2);
	dist = 2*Math.asin(Math.sqrt(slat*slat+Math.cos(p1.latitude/180*Math.PI)
  					*Math.cos(p2.latitude/180*Math.PI)*slon*slon));
	return dist/Math.PI*180;
}

// computes vertices that connect the two points p1 and p2
function computeHaversineVertices(p1,p2,numVertices){
	var points = [];
	if(!numVertices)
		numVertices = 30;
	lat1 = p1.latitude/180*Math.PI; lat2 = p2.latitude/180*Math.PI;
	lon1 = p1.longitude/180*Math.PI;lon2 = p2.longitude/180*Math.PI;
	dist = haversineDistance(p1,p2)/180*Math.PI;
	for(i=0;i<=numVertices;i++){
		u = Math.sin((1-(1/numVertices)*i)*dist)/Math.sin(dist);
		v = Math.sin(1/numVertices*i*dist)/Math.sin(dist);
		x = u*Math.cos(lat1)*Math.cos(lon1)+v*Math.cos(lat2)*Math.cos(lon2);
    y = u*Math.cos(lat1)*Math.sin(lon1)+v*Math.cos(lat2)*Math.sin(lon2);
		z = u*Math.sin(lat1)+v*Math.sin(lat2);
		point = new Microsoft.Maps.Location(Math.atan2(z,Math.sqrt(x*x+y*y))
			/Math.PI*180,Math.atan2(y,x)/Math.PI*180);
		points.push(point);
	}
	return points;
}
		 

