lat1 = lat1 || 0
lng1 = lng1 || 0
lat2 = lat2 || 0
lng2 = lng2 || 0
var rad1 = lat1 * Math.PI / 180.0
var rad2 = lat2 * Math.PI / 180.0
var a = rad1 - rad2
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0
var r = 6378137
return r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
}
import java.util.Scannerpublic class Distance {
public static void main(String[] args) {
Line L = new Line(2,3,5)
Scanner sc = new Scanner(System.in)
System.out.println("输入点的x坐标:")
double x = sc.nextDouble()
System.out.println("输入点的y坐标:")
double y = sc.nextDouble()
System.out.println(distence(x,y,L))
}
public static double distence(double x,double y,Line L){
return Math.abs(L.A*x+L.B*y+L.C)/Math.sqrt(L.A*L.A+L.B*L.B)
}
}
class Line{//直线类,方程为:Ax+By+C=0
double A
double B
double C
public Line(double A,double B,double C){
this.A = A
this.B = B
this.C = C
}
}
let PI = 3.14159265358979323//圆周率let R = 6371229//地球半径
function getDistance(lon1,lat1,lon2,lat2){
let x,y,distance
let lonres = lon1 >lon2?lon1-lon2:lon2-lon1
let latres = lat1 >lat2?lat1-lat2:lat2-lat1
x = (lonres) * PI * R * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180
y = (lat2 - lat1) * PI * R / 180
return distance = Math.hypot(x, y)
}