java
public class PointToLineDistance {
public static double distanceToLine(double x1, double y1, double x2, double y2, double x, double y) {
double APx = x - x1;
double APy = y - y1;
double ABx = x2 - x1;
double ABy = y2 - y1;
double dotProduct = APx * ABx + APy * ABy;
double magnitudeAB = Math.sqrt(ABx * ABx + ABy * ABy);
double distance = Math.abs(dotProduct) / magnitudeAB;
return distance;
}
public static void main(String[] args) {
double distance = distanceToLine(0, 0, 5, 5, 2, 3);
System.out.println("点到线段的距离是:" + distance);
}
}
顶一下
(0)
0%
踩一下
(0)
0%
- 相关评论
- 我要评论
-
上一篇:返回栏目