当前位置:网站首页>Calculate the distance between two points (2D, 3D)
Calculate the distance between two points (2D, 3D)
2022-06-26 13:32:00 【Eat bones without spitting out femoral head skin】
Project scenario :
Reference resources : Distance between two points
Calculate the distance between two points
The distance between two points in space
Euclidean distance ( Euclidean distance) Also known as Euclidean distance , It is a commonly used definition of distance , It's in m The real distance between two points in dimensional space .

Solution :
Coordinate entity class
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;
}
}
Calculation method
2D
/** * Calculate all 2d The distance between the points . * * @param coordinates All coordinate points . * @return Returns the values of all coordinate point distances */
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
/** * Calculate all 3d The distance between the points . * * @param coordinates All coordinate points . * @return Returns the values of all coordinate point distances */
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;
}
边栏推荐
- MongoDB系列之Window环境部署配置
- 组合模式(Composite )
- Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
- Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
- Input text to automatically generate images. It's fun!
- Detailed sorting of HW blue team traceability process
- Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module
- To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
- 【Spark】. Explanation of several icons of scala file in idea
- MediaPipe手势(Hands)
猜你喜欢

Mediapipe gestures (hands)

Es sauvegarde et restauration des données par instantané

Beifu twincat3 can read and write CSV and txt files

三维向量的夹角

Decorator

Enjoy element mode (flyweight)

装饰器(Decorator)
![8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)](/img/e7/2fd8ec8d5e5473c7f84f3e3bcedca8.png)
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)

MongoDB系列之Window环境部署配置

Use of wangeditor rich text editor
随机推荐
Mysql database explanation (6)
7-3 minimum toll
Typescript
Stack, LIFO
Mediapipe gestures (hands)
HDU 3709 Balanced Number
7-2 a Fu the thief
Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
H5 video automatic playback and circular playback
Nexys A7开发板资源使用技巧
There are many contents in the widget, so it is a good scheme to support scrolling
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
12 SQL optimization schemes summarized by old drivers (very practical)
Mysql database explanation (V)
Oplg: new generation cloud native observable best practices
C - Common Subsequence
B - Bridging signals
Lamp compilation and installation
Aesthetic experience (episode 238) Luo Guozheng
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏