当前位置:网站首页>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;
}
边栏推荐
- Enjoy element mode (flyweight)
- Lamp compilation and installation
- Mediapipe gestures (hands)
- F - Charm Bracelet
- KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- MySQL数据库常见故障——遗忘数据库密码
- MediaPipe手势(Hands)
- 计算两点之间的距离(二维、三维)
- 7-2 a Fu the thief
猜你喜欢

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

Es6: iterator

May product upgrade observation station

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

【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示

8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)

防火墙介绍

Ring queue PHP

Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module

Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
随机推荐
Electron official docs series: Testing And Debugging
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
Detailed introduction to shell script (4)
输入文本自动生成图像,太好玩了!
Stack, LIFO
7-2 picking peanuts
Uva10341 solve it
C - Common Subsequence
12 SQL optimization schemes summarized by old drivers (very practical)
DataGrip配置的连接迁移
CloudCompare——泊松重建
创建一个自己的跨域代理服务器
Create your own cross domain proxy server
Here document interaction free and expect automatic interaction
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
[how to connect the network] Chapter 2 (next): receiving a network packet
MySQL数据库讲解(六)
LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module
code force Party Lemonade