本文主要是介绍计算两点间距离,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
页面
python更简单
#导入math包
import math
#定义点的函数
class Point:def __init__(self,x=0,y=0):self.x=xself.y=ydef getx(self):return self.xdef 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" )@ResponseBodypublic 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);}
这篇关于计算两点间距离的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!