当前位置:网站首页>计算两点之间的距离(二维、三维)
计算两点之间的距离(二维、三维)
2022-06-26 12:41:00 【吃骨头不吐股骨头皮】
项目场景:
参考:两点之间距离
计算两点之间的距离
空间两点间距离
欧氏距离( Euclidean distance)也称欧几里得距离,它是一个通常采用的距离定义,它是在m维空间中两个点之间的真实距离。

解决方案:
坐标实体类
public class Point {
public float x;
public float y;
public float z;
public Point (float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
}
计算方式
2D
/** * 计算所有2d点之间的距离. * * @param coordinates 所有坐标点. * @return 返回所有坐标点距离的值 */
public static double calculatePointsDistance(List<Point> coordinates) {
if (coordinates.size() < 2) {
return 0.0;
}
if (coordinates.size() == 2) {
double powX = Math.pow((coordinates.get(0).x - coordinates.get(1).x), 2);
double powY = Math.pow((coordinates.get(0).y - coordinates.get(1).y), 2);
return Math.sqrt(powX + powY);
}
double distance = Math.sqrt(Math.pow((coordinates.get(0).x - coordinates.get(coordinates.size() - 1).x), 2)
+ Math.pow((coordinates.get(0).y - coordinates.get(coordinates.size() - 1).y), 2));
for (int i = 0; i < coordinates.size() - 1; i++) {
double powX = Math.pow((coordinates.get(i).x - coordinates.get(i + 1).x), 2);
double powY = Math.pow((coordinates.get(i).y - coordinates.get(i + 1).y), 2);
distance += Math.sqrt(powX + powY);
}
return distance;
}
3D
/** * 计算所有3d点之间的距离. * * @param coordinates 所有坐标点. * @return 返回所有坐标点距离的值 */
public static double calculateTdPointDistance(List<Point> coordinates) {
if (coordinates.size() < 2) {
return 0.0;
}
if (coordinates.size() == 2) {
double powX = Math.pow((coordinates.get(0).x - coordinates.get(1).x), 2);
double powY = Math.pow((coordinates.get(0).y - coordinates.get(1).y), 2);
double powZ = Math.pow((coordinates.get(0).z - coordinates.get(1).z), 2);
return Math.sqrt(powX + powY + powZ);
}
double distance = Math.sqrt(Math.pow((coordinates.get(0).x - coordinates.get(coordinates.size() - 1).x), 2)
+ Math.pow((coordinates.get(0).y - coordinates.get(coordinates.size() - 1).y), 2)
+ Math.pow((coordinates.get(0).z - coordinates.get(coordinates.size() - 1).z), 2));
for (int i = 0; i < coordinates.size() - 1; i++) {
double powX = Math.pow((coordinates.get(i).x - coordinates.get(i + 1).x), 2);
double powY = Math.pow((coordinates.get(i).y - coordinates.get(i + 1).y), 2);
double powZ = Math.pow((coordinates.get(i).z - coordinates.get(i + 1).z), 2);
distance += Math.sqrt(powX + powY + powZ);
}
return distance;
}
边栏推荐
- Nlp-d60-nlp competition D29
- Vivado error code [drc pdcn-2721] resolution
- Beifu cx5130 card replacement and transfer of existing authorization files
- MySQL数据库讲解(五)
- Some conclusions about Nan
- Luogu p3426 [poi2005]sza-template solution
- IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
- 10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
- [how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
- Script - crawl the customized storage path of the cartoon and download it to the local
猜你喜欢

Decorator

MySQL explanation (I)

Processsing mouse interactive learning

Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder

Chapter 10 setting up structured logging (2)

解中小企业之困,百度智能云打个样

Learning Processing Zoog

Here Document免交互及Expect自动化交互

Es6: iterator

May product upgrade observation station
随机推荐
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
HDU 5860
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
Common creation and usage of singletons
QT . Establishment and use of pri
偶言佳句,孤芳自赏
shell脚本详细介绍(四)
C - Common Subsequence
中国剩余定理模板题 互质与非互质
Beifu realizes the control of time slice size and quantity through CTU and ton
Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
Opencv high speed download
原型模式(prototype)
Reflect the technical depth (unable to speed up)
H - Sumsets POJ 2229
Go structure method
Word document export (using fixed template)
What features are added to Photoshop 2022 23.4.1? Do you know anything
MySQL explanation (II)
Adapter mode