【发布时间】:2018-12-18 20:37:46
【问题描述】:
对于涉及对象在笛卡尔坐标系中的坐标(例如一维笛卡尔坐标系中的有界区间的坐标)的算法,用坐标表示它们有什么优点或缺点独立 * 来自轴方向(例如 x_min 和 x_max),相对在坐标中依赖 ** 轴方向(例如 x_left和x_right,或者x_left 和width)?
例如,对于一个测试两个有界区间是否重叠的算法,你会写
def test_overlap(x_min_1, x_max_1, x_min_2, x_max_2):
"""Test if two bounded intervals overlap.
Coordinates are independent from axis orientation.
"""
assert x_min_1 <= x_max_1
assert x_min_2 <= x_max_2
return x_min_1 <= x_max_2 and x_min_2 <= x_max_1
或两者之一
def test_overlap(x_left_1, x_right_1, x_left_2, x_right_2):
"""Test if two bounded intervals overlap.
Coordinates are dependent on axis orientation.
"""
assert (x_left_1 <= x_right_1 and x_left_2 <= x_right_2
or x_left_1 >= x_right_1 and x_left_2 >= x_right_2)
if x_left_1 <= x_right_1: # x-axis oriented to the right
return x_left_1 <= x_right_2 and x_left_2 <= x_right_1
else: # x-axis oriented to the left
return x_left_1 >= x_right_2 and x_left_2 >= x_right_1
或
# Here the orientation of the x-axis cannot be deduced from the arguments,
# so you need to pass it explicitly to the algorithm.
def test_overlap(x_left_1, width_1, x_left_2, width_2, x_orientation):
"""Test if two bounded intervals overlap.
Coordinates are dependent on axis orientation.
"""
assert width_1 >= 0
assert width_2 >= 0
assert x_orientation in ["right", "left"]
if x_orientation == "right": # x-axis oriented to the right
x_right_1 = x_left_1 + width_1
x_right_2 = x_left_2 + width_2
return x_left_1 <= x_right_2 and x_left_2 <= x_right_1
else: # x-axis oriented to the left
x_right_1 = x_left_1 - width_1
x_right_2 = x_left_2 - width_2
return x_left_1 >= x_right_2 and x_left_2 >= x_right_1
* 坐标指定一个特定的点并随坐标系而变化。当我说“坐标独立于轴方向”时,它意味着所考虑的点会发生变化,以使坐标在轴方向反转下保持不变。
** 当我说“坐标取决于轴方向”时,这意味着 坐标 会发生变化,以使所考虑的点在轴方向反转下保持不变。这是标准用法。
【问题讨论】:
-
在不假设坐标轴的情况下很难想到坐标,因此您的第一个选项不仅不那么冗长而且更容易推理。
-
“更好”在什么意义上?
-
@Will 第一个选项是不假设任何关于 x 轴的选项:它可以是左向或右向,公式仍然是一样。
-
@martineau 好问题。你会在实践中使用上述函数的哪个版本?
标签: python coordinates orientation axis coordinate-systems