package com.base.core.utiles;


import com.base.core.source.LatLon;
import com.base.core.source.RadiansDic;

public class CalDistance {
	private static double					arc2degree = 57.295779513;//角度弧度换算单位
	/*public static double Distance(double lat1, double lon1, double lat2, double lon2){
		double a,b;
		double lat = (lat1 + lat2) / 2;
		double radious = RadiansDic.queryradians(lat);
		//double radious = Parameters.R;
		lat1 = lat1 * Math.PI / 180.0;//转换成弧度
		lat2 = lat2 * Math.PI / 180.0;//转换成弧度
		a = lat1 - lat2;
		b = (lon1 - lon2) * Math.PI / 180.0;
		double dis,sa2,sb2;
		sa2 = Math.sin(a / 2.0);
		sb2 = Math.sin(b / 2.0);
		dis = 2 * radious * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
		return dis;
	}*/
	public static double Distance(double lat1, double lon1, double lat2, double lon2){
		LatLon p1 = LatLon.fromDegrees(lat1,lon1);
		LatLon p2 = LatLon.fromDegrees(lat2,lon2); 
		double dis = LatLon.ellipsoidalDistance(p1, p2, 6378137.0, 6356752.0);
		return dis;
	}
	//得到经度差值
	public static double CalDisLon(double lat1, double lon1, double lat2, double dis){
		double lat = (lat1 + lat2) / 2;
		//double radious = Parameters.R;
		double radious = RadiansDic.queryradians(lat);
		lat1 = lat1 * Math.PI / 180.0;//转换成弧度
		lat2 = lat2 * Math.PI / 180.0;//转换成弧度
		double a = lat1 - lat2;
		
		double sa2 = Math.sin(a / 2.0);
		double alfa = Math.sin(dis / (radious * 2));
		double temp = alfa * alfa - sa2 * sa2;
		double sb2 = Math.sqrt(temp / (Math.cos(lat1) * Math.cos(lat2)));
		double b = Math.asin(sb2) * 2.0;
		double dislon = b * arc2degree;
		return dislon;
	}
	
	public static double calDisLon(double lat1, double dis){
		lat1 = lat1 * Math.PI / 180.0;//转换成弧度
		double radious = RadiansDic.queryradians(lat1);
		double alfa = Math.sin(dis / (radious * 2));
		double temp = alfa * alfa;
		double sb2 = Math.sqrt(temp / (Math.cos(lat1) * Math.cos(lat1)));
		double b =  Math.abs(Math.asin(sb2) * 2.0);
		double dislon = b * arc2degree;
		return dislon;
	}
	public static double calDisLat(double lat1, double dis){
		lat1 = lat1 * Math.PI / 180.0;//转换成弧度
		double radious = RadiansDic.queryradians(lat1);
		double alfa = Math.sin(dis / (radious * 2));
		double sa = Math.abs(Math.asin(alfa) * 2);
		double dislat = sa * arc2degree;
		return dislat;
	}
	
	
	public static float CalRollAngle(double dis, double altitude){
		float rollangle;
		double value = dis / altitude;
		//System.out.println(value);
		rollangle = (float) (Math.atan(value) * arc2degree);
		return rollangle;
	}
	public static float CalDisbyangle(double rollangle, double altitude){
		double value = Math.abs(rollangle) / arc2degree;
		//System.out.println(value);
		float dis = (float) (Math.tan(value) * altitude);
		return dis;
	}
	
	public static void main(String[] args) {

		//System.out.println(CalRollAngle(1.732, 1));
		//System.out.println(CalDisbyangle(60,1));
		/*double l1 = -70.001363;
		double l2 = -20.760372;
		LatLon latLon = LatLon.fromDegrees(l1,l2);//褰撳墠鐐规槦涓嬬偣鏁版嵁
		double dislon = CalDistance.calDisLon(l1,1000);
		double dislat = CalDistance.calDisLat(l1,1000);
		System.out.println(dislon);
		System.out.println(dislat);
		double lon = l2+dislon;
		double lat = l1+dislat;
		double lon2 = l2-dislon;
		double lat2 = l1-dislat;
		LatLon latLon2 = LatLon.fromDegrees(l1,lon);
		double y = LatLon.ellipsoidalDistance(latLon, latLon2, 6378137.0, 6356752.0);
		System.out.println("y = " + y);
		LatLon latLon22 = LatLon.fromDegrees(l1,lon2);
		double y2 = LatLon.ellipsoidalDistance(latLon, latLon22, 6378137.0, 6356752.0);
		System.out.println("y2 = " + y2);
		LatLon latLon3 = LatLon.fromDegrees(lat,l2);
		double z = LatLon.ellipsoidalDistance(latLon, latLon3, 6378137.0, 6356752.0);
		System.out.println("z = " + z);
		LatLon latLon33 = LatLon.fromDegrees(lat2,l2);
		double z2 = LatLon.ellipsoidalDistance(latLon, latLon33, 6378137.0, 6356752.0);
		System.out.println("z2 = " + z2);*/
		//85.37660253788319
		//695696.5791184338
		//double dislon = Distance(55.666668,89.833336,59.666668,80.833336);
		double lat1 = 26.313307;
		double lon1 = 122.33887;
		double lat2 = 25.95609;
		double lon2 = 124.13177;
		LatLon p1 = LatLon.fromDegrees(lat1,lon1);
		LatLon p2 = LatLon.fromDegrees(lat2,lon2); 
		double y = LatLon.ellipsoidalDistance(p1, p2, 6378137.0, 6356752.0);
		//double y2 = LatLon.greatCircleDistance(p1, p2).radians;
		//System.out.println(dislon);
		System.out.println(y);
		//System.out.println(y2);

		
	}
}
