计算两点间距离的公式是欧几里得距离公式,即:
\[ \text{距离} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \]
Python
```python
import math
def calculate_distance(point1, point2):
计算两点的欧几里得距离
return math.sqrt((point2 - point1)2 + (point2 - point1)2)
示例使用
point1 = (1, 2)
point2 = (4, 6)
distance = calculate_distance(point1, point2)
print(f"Distance between points: {distance}")
```
C++
```cpp
include
include
struct Point {
double x;
double y;
};
double distance(const Point& p1, const Point& p2) {
return std::sqrt(std::pow(p2.x - p1.x, 2) + std::pow(p2.y - p1.y, 2));
}
int main() {
Point p1 = {1.0, 2.0};
Point p2 = {4.0, 6.0};
double dist = distance(p1, p2);
std::cout << "Distance between points: " << std::fixed << std::setprecision(2) << dist << std::endl;
return 0;
}
```
Java
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
System.out.printf("%.2f\n", distance);
}
}
}
```
C
```csharp
using System;
class Program {
static void Main() {
Console.WriteLine("输入A的坐标(只能输入整数)");
int x1 = Convert.ToInt32(Console.ReadLine());
int y1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入B的坐标");
int x2 = Convert.ToInt32(Console.ReadLine());
int y2 = Convert.ToInt32(Console.ReadLine());
int distance = (int)Math.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
Console.WriteLine(distance);
}
}
```
这些示例展示了如何在不同编程语言中计算两点间的距离。你可以根据具体需求选择合适的编程语言和实现方式。