当前位置:网站首页>计算两点之间的距离(二维、三维)
计算两点之间的距离(二维、三维)
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;
}
边栏推荐
- Decorator
- Beifu twincat3 can read and write CSV and txt files
- Nlp-d60-nlp competition D29
- POJ 3070 Fibonacci
- KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- I - Dollar Dayz
- Ubuntu installation and configuration PostgreSQL (18.04)
- Some conclusions about Nan
- Beifu cx5130 card replacement and transfer of existing authorization files
- Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
猜你喜欢

2. Introduction to parallel interface, protocol and related chips (8080, 8060)

12 SQL optimization schemes summarized by old drivers (very practical)

Explain C language 11 in detail (C language series)

利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)

ES6:Map

Decorator
What should the software test report include? Interview must ask

Script - crawl the customized storage path of the cartoon and download it to the local

What features are added to Photoshop 2022 23.4.1? Do you know anything

Prototype
随机推荐
H5视频自动播放和循环播放
享元模式(Flyweight)
Do you know the limitations of automated testing?
Electron official docs series: Best Practices
[how to connect the network] Chapter 2 (next): receiving a network packet
Ubuntu installation and configuration PostgreSQL (18.04)
Electron official docs series: Get Started
What should the software test report include? Interview must ask
Here Document免交互及Expect自动化交互
shell脚本详细介绍(四)
Beifu twincat3 can read and write CSV and txt files
Custom encapsulation drop-down component
Summary of wechat applet test points
Processing function translate (mousex, mousey) learning
外观模式(Facade)
POJ 3070 Fibonacci
C language: Exercise 2
Mysql database explanation (V)
code force Party Lemonade
E - Apple Catching