这是我提出的确定最接近指南针方向的方法,该指南针方向由线段 [A, B] 所指向的方向由其端点 point_a 和 point_b 定义:
- 所有计算均在标准笛卡尔坐标中完成,
最后完成对屏幕坐标的更改。这简化了
方法,并使代码可在其他地方重用。
- 先把原点改成
point_a
- 第二次计算线段与x_axis的夹角
- 确定最近的方位(在标准笛卡尔坐标中)
- 将标准方位转换为屏幕坐标方位(水平翻转)
在屏幕坐标中定义点(Y 轴向下),调用get_bearings(point_a, point_b)
如果标准中定义的点
笛卡尔坐标(Y轴向上),调用
assign_bearing_to_compass(point_a, point_b)
(代码下方的测试显示了使用标准坐标和屏幕坐标中的点的结果。)
import math
def _change_origin_of_point_b_to_point_a(point_a, point_b):
# uses standard Y axis orientation, not screen orientation
return (point_b[0] - point_a[0], point_b[1] - point_a[1])
def _calc_angle_segment_a_b_with_x_axis(point_a, point_b):
# uses standard Y axis orientation, not screen orientation
xa, ya = point_a
xb, yb = _change_origin_of_point_b_to_point_a(point_a, point_b)
return math.atan2(yb, xb)
def determine_bearing_in_degrees(point_a, point_b):
"""returns the angle in degrees that line segment [point_a, point_b)]
makes with the horizontal X axis
"""
# uses standard Y axis orientation, not screen orientation
return _calc_angle_segment_a_b_with_x_axis(point_a, point_b) * 180 / math.pi
def assign_bearing_to_compass(point_a, point_b):
"""returns the standard bearing of line segment [point_a, point_b)
"""
# uses standard Y axis orientation, not screen orientation
compass = {'W' : [157.5, -157.5],
'SW': [-157.5, -112.5],
'S' : [-112.5, -67.5],
'SE': [-67.5, -22.5],
'E' : [-22.5, 22.5],
"NE": [22.5, 67.5],
'N' : [67.5, 112.5],
'NW': [112.5, 157.5]}
bear = determine_bearing_in_degrees(point_a, point_b)
for direction, interval in compass.items():
low, high = interval
if bear >= low and bear < high:
return direction
return 'W'
def _convert_to_negative_Y_axis(compass_direction):
"""flips the compass_direction horizontally
"""
compass_conversion = {'E' : 'E',
'SE': 'NE',
'S' : 'N',
'SW': 'NW',
'W' : 'W',
"NW": 'SW',
'N' : 'S',
'NE': 'SE'}
return compass_conversion[compass_direction]
def get_bearings(point_a, point_b):
return _convert_to_negative_Y_axis(assign_bearing_to_compass(point_a, point_b))
测试:
(使用标准三角圆象限)
第一象限:
point_a = (0, 0)
points_b = [(1, 0), (1, 3), (1, 2), (1, 1), (2, 1), (3, 1), (0, 1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
结果:
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (1, 0) E E
(0, 0) (1, 3) N S
(0, 0) (1, 2) NE SE
(0, 0) (1, 1) NE SE
(0, 0) (2, 1) NE SE
(0, 0) (3, 1) E E
(0, 0) (0, 1) N S
象限二:
point_a = (0, 0)
points_b = [(-1, 0), (-1, 3), (-1, 2), (-1, 1), (-2, 1), (-3, 1), (0, 1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
结果:
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (-1, 0) W W
(0, 0) (-1, 3) N S
(0, 0) (-1, 2) NW SW
(0, 0) (-1, 1) NW SW
(0, 0) (-2, 1) NW SW
(0, 0) (-3, 1) W W
(0, 0) (0, 1) N S
象限 III:
point_a = (0, 0)
points_b = [(-1, 0), (-1, -3), (-1, -2), (-1, -1), (-2, -1), (-3, -1), (0, -1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
结果:
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (-1, 0) W W
(0, 0) (-1, -3) S N
(0, 0) (-1, -2) SW NW
(0, 0) (-1, -1) SW NW
(0, 0) (-2, -1) SW NW
(0, 0) (-3, -1) W W
(0, 0) (0, -1) S N
第四象限:
point_a = (0, 0)
points_b = [(1, 0), (1, -3), (1, -2), (1, -1), (2, -1), (3, -1), (0, -1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
结果:
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (1, 0) E E
(0, 0) (1, -3) S N
(0, 0) (1, -2) SE NE
(0, 0) (1, -1) SE NE
(0, 0) (2, -1) SE NE
(0, 0) (3, -1) E E
(0, 0) (0, -1) S N