当前位置:网站首页>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;
}
边栏推荐
- Electron official docs series: Processes in Electron
- sed编辑器
- 5+api, clear application cache
- 【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
- Awk tools
- CloudCompare——泊松重建
- 8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
- Electron official docs series: Development
- LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
- Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
猜你喜欢

MySQL讲解(二)

Es snapshot based data backup and restore

Use of wangeditor rich text editor

三维向量的夹角
![[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect](/img/e3/a666ba2f48e8edcc7db80503a6156d.png)
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect

Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units

Create your own cross domain proxy server

MySQL explanation (I)
随机推荐
IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
Aesthetic experience (episode 238) Luo Guozheng
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
Nexys A7开发板资源使用技巧
33、使用RGBD相机进行目标检测和深度信息输出
Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
Mediapipe gestures (hands)
嵌入式virlog代码运行流程
Detailed sorting of HW blue team traceability process
A collection of common tools for making we media videos
Create your own cross domain proxy server
Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
Input text to automatically generate images. It's fun!
Reflect the technical depth (unable to speed up)
Design of simple digital circuit traffic light
Electron official docs series: Best Practices
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
[how to connect the network] Chapter 2 (middle): sending a network packet
7-3 minimum toll