java 代码 点到线段的最短距离

本文介绍了一种计算平面上一点到线段最短距离的方法。通过判断点与线段的位置关系,采用不同的数学公式来精确计算距离。适用于计算机图形学、游戏开发等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 点到直线的最短距离的判断 点(x0,y0) 到由两点组成的线段(x1,y1) ,( x2,y2 )  
	public static double pointToLine(double x1, double y1, double x2, double y2, double x0, double y0) {  
         double space = 0;  
         double a, b, c;  
         a = lineSpace(x1, y1, x2, y2);// 线段的长度  
         b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离  
         c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离  
         if (c+b == a) {//点在线段上  
             space = 0;  
             return space;  
         }  
         if (a <= 0.000001) {//不是线段,是一个点  
             space = b;  
             return space;  
         }  
         if (c * c >= a * a + b * b) { //组成直角三角形或钝角三角形,(x1,y1)为直角或钝角  
        	 System.out.println("组成直角三角形或钝角三角形,(x1,y1)为直角或钝角  ");
             space = b;  
             return space;  
         }  
         if (b * b >= a * a + c * c) {//组成直角三角形或钝角三角形,(x2,y2)为直角或钝角  
        	 System.out.println("组成直角三角形或钝角三角形,(x2,y2)为直角或钝角 ");
             space = c;  
             return space;  
         }  
         //组成锐角三角形,则求三角形的高  
         System.out.println("组成锐角三角形,则求三角形的高  ");
         double p = (a + b + c) / 2;// 半周长  
         double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积  
         space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)  
         return space;  
     }  

     //计算两点之间的距离  
	 public static double lineSpace(double x1, double y1, double x2, double y2) {  
         double lineLength = 0;  
         lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));  
         return lineLength;  
     }  


在 Android 高德地图中,计算点到连线线段的最短距离可以通过以下步骤实现: 1. 获取地图上的点和线段。 ```java LatLng point = new LatLng(39.915168, 116.403875); LatLng start = new LatLng(39.993015, 116.474172); LatLng end = new LatLng(39.902981, 116.447901); Polyline polyline = new PolylineOptions().add(start, end).color(Color.RED).width(10f); aMap.addPolyline(polyline); ``` 2. 计算点到线段的距离。 ```java double distance = AMapUtils.calculateLineDistance(point, AMapUtils.nearestPointOnLine(point, start, end)); ``` 其中,nearestPointOnLine 方法可以计算点到线段的垂足,然后使用 calculateLineDistance 方法计算点到垂足的距离。 3. 显示距离信息。 ```java Toast.makeText(this, "点到线段最短距离为:" + distance + "米", Toast.LENGTH_SHORT).show(); ``` 完整代码如下: ```java public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { private AMap aMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapView mapView = findViewById(R.id.map_view); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); } @Override public void onMapReady(AMap aMap) { this.aMap = aMap; LatLng point = new LatLng(39.915168, 116.403875); LatLng start = new LatLng(39.993015, 116.474172); LatLng end = new LatLng(39.902981, 116.447901); Polyline polyline = new PolylineOptions().add(start, end).color(Color.RED).width(10f); aMap.addPolyline(polyline); double distance = AMapUtils.calculateLineDistance(point, AMapUtils.nearestPointOnLine(point, start, end)); Toast.makeText(this, "点到线段最短距离为:" + distance + "米", Toast.LENGTH_SHORT).show(); } @Override protected void onResume() { super.onResume(); MapView mapView = findViewById(R.id.map_view); mapView.onResume(); } @Override protected void onPause() { super.onPause(); MapView mapView = findViewById(R.id.map_view); mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); MapView mapView = findViewById(R.id.map_view); mapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); MapView mapView = findViewById(R.id.map_view); mapView.onSaveInstanceState(outState); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值