写段python代码,根据三点坐标计算角度
时间: 2023-06-03 08:07:40 浏览: 160
Sure, here's the Python code to calculate the angle between three points:
```
import math
def calculate_angle(x1, y1, x2, y2, x3, y3):
# calculate distances between the points
a = math.sqrt((x2-x1)**2 + (y2-y1)**2)
b = math.sqrt((x3-x2)**2 + (y3-y2)**2)
c = math.sqrt((x3-x1)**2 + (y3-y1)**2)
# calculate angle using law of cosines
angle_in_radians = math.acos((a**2 + b**2 - c**2) / (2 * a * b))
# convert angle from radians to degrees
angle_in_degrees = math.degrees(angle_in_radians)
return angle_in_degrees
# example usage
x1, y1 = 0, 0
x2, y2 = 1, 1
x3, y3 = 2, 0
angle = calculate_angle(x1, y1, x2, y2, x3, y3)
print(f"The angle between ({x1}, {y1}), ({x2}, {y2}), and ({x3}, {y3}) is {angle} degrees.")
```
Hope this helps! Let me know if you have any more questions.
阅读全文