計算兩點間距離
阿新 • • 發佈:2021-02-05
頁面
python更簡單
#匯入math包 import math #定義點的函式 class Point: def __init__(self,x=0,y=0): self.x=x self.y=y def getx(self): return self.x def gety(self): return self.y #定義直線函式 class Getlen: def __init__(self,p1,p2): self.x=p1.getx()-p2.getx() self.y=p1.gety()-p2.gety() #用math.sqrt()求平方根 self.len= math.sqrt((self.x**2)+(self.y**2)) #定義得到直線長度的函式 def getlen(self): return self.len #設定點p1的座標為(0,0) p1=Point(0,0) #設定點p2的座標為(3,4) p2=Point(3,4) l=Getlen(p1,p2) #獲取兩點之間直線的長度 l.getlen()
Java的具體實現
<form name="PointDistance_form"> <br/> ------------------------------------------------------- <br/> (計算兩座標點間距離(米) :引數是標準的工程座標,或者wgs84座標) <p> 起始點x座標: <input type="text" value="" name="start_dx" size="30" id="start_dx"/> 終止點x座標: <input type="text" value="" name="end_dx" size="30" id="end_dx"/> </p> <p> 起始點y座標: <input type="text" value="" name="start_dy" size="30" id="start_dy"/> 終止點y座標: <input type="text" value="" name="end_dy" size="30" id="end_dy"/> </p> <p> 兩座標點間距離(米): <input type="text" value="" name="result_PointDistance" id="result_PointDistance" /> </p> <p><input id="go_calPointDistance" type="button" value="go" /></p> </form>
$(document).ready(function(){ $("#go_calPointDistance").click(function () { //獲取值 var start_x = $("#start_dx").val(); var start_y = $("#start_dy").val(); var end_x = $("#end_dx").val(); var end_y = $("#end_dy").val(); if (start_x == "" || start_y == "" || end_x == "" || end_y == "") { alert("請輸入完整的座標資訊!!!"); return; } $.ajax({ url:"/gj_tools/PointDistance_action", type:"POST", data:{"start_x":start_x,"start_y":start_y,"end_x":end_x,"end_y":end_y}, dataType:"json", success:function (result) { // alert(result.result); $("#result_PointDistance").val(result.result); }, error:function(data){ alert("計算點間距離失敗!!"); } }) }); });
服務程式碼:
/*
* 計算兩點間距離
*/
@RequestMapping(value = "/gj_tools/PointDistance_action" )
@ResponseBody
public Map<String,Object> PointDistance(double start_x ,double start_y ,double end_x ,double end_y){
Map<String,Object> resultMap = new HashMap<String, Object>();
// double result = TrackPointUtil.AngleToNorth(Double.parseDouble(start_x), Double.parseDouble(start_y), Double.parseDouble(end_x), Double.parseDouble(end_y));
double result = TrackPointUtil.calPoint2PointDistance(start_x,end_x, start_y, end_y);
resultMap.put("result", result);
return resultMap;
}
/**
* 將度轉化為弧度
*
* @param {degree} Number 度
* @returns {Number} 弧度
*/
static double degreeToRad(double degree) {
return Math.PI * degree / 180;
}
/**
* 計算兩點之間的距離,兩點座標必須為經緯度
*
* @param {point1} Point 點物件
* @param {point2} Point 點物件
* @param point1
* @param point2
* @return
* @returns 兩點之間距離,單位為米
*/
public static double calPoint2PointDistance(double X1,double X2 ,double Y1 ,double Y2) {
/**
* 地球半徑
*/
double EARTHRADIUS = 6370996.81;
double x1, x2, y1, y2;
x1 = degreeToRad(X1);
y1 = degreeToRad(Y1);
x2 = degreeToRad(X2);
y2 = degreeToRad(Y2);
double result = Math.sin(y1) * Math.sin(y2) + Math.cos(y1) * Math.cos(y2) * Math.cos(x2 - x1);
if (result > 1.0) { result = 1.0; }
return EARTHRADIUS * Math.acos(result);
}