【问题标题】:Invariant vs variant coordinates under axis orientation reversal轴方向反转下的不变坐标与变坐标
【发布时间】:2018-12-18 20:37:46
【问题描述】:

对于涉及对象在笛卡尔坐标系中的坐标(例如一维笛卡尔坐标系中的有界区间的坐标)的算法,用坐标表示它们有什么优点或缺点独立 * 来自轴方向(例如 x_minx_max),相对在坐标中依赖 ** 轴方向(例如 x_leftx_right,或者x_leftwidth)?

例如,对于一个测试两个有界区间是否重叠的算法,你会写

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


【解决方案1】:

我不知道这是否是“更好”,但我的方式做了一个项目,它的表现还算不错,是定义class类似如下: P>

class Interval(object):
    """ Representation of a closed interval.

    "a" & "b" can be any numeric type, or any other type than can be
    compared. If the type can also be incremented by 1, then it
    will be possible to iterate between the two values in
    ascending order.
    """
    def __init__(self, a, b):
        self.lowerbound, self.upperbound = (a, b) if a < b else (b, a)

    def __contains__(self, val):
        return self.lowerbound <= val <= self.upperbound

    # Implemented as a generator (so no 'next' method is needed)
    def __iter__(self):
        nextvalue = self.lowerbound  # Initialize iteration state.
        while nextvalue <= self.upperbound:
            yield nextvalue
            nextvalue += 1

__contains__()方法控制表达式等if x in interval:如何检查x靠在@ 987654326值@“的上部以及下部边界。 (在我自己的应用程序中,我使用Date S作为间隔的边界。) P>

这样做,这样使您能够控制一切到底是怎样的作品近,再加上你也可以将专用的方法,它如果需要的话。例如:一个test_overlap()方法可以做你决定它是什么你想要的任何方式。事实上,这样的方法可以被写入到返回结果作为Interval P>

【讨论】:

  • 谢谢这个漂亮容器和可迭代数据结构。所以,你会用我的第一种方法,而不是我的第二个的(lowerboundupperbound代替leftboundrightbound的)。但是,为什么,是什么优势? SPAN>
  • 在 I>类不胜枚举。你应该取决于效率,可读性,可维护性什么,再加上你可能也就像一个这样或那样的原因,喜欢它的更容易或更小打字写&lt;代替&lt;=。亲自我喜欢后者因为我更喜欢使用封闭的,而不是在我设计它们中可以使用的应用程序开放时间间隔,因此我也他们的方式。跨度>
  • 还有一点要注意一下我的实现是没有assert语句,因为它有效地排序两个参数并存储一个枝条较小的值在self.lowerbound和其他在其他属性,所以有没有必要使用断言。如果您愿意,当然,但类型检查通常被认为没有“Python的”你可能想要把其它种类。 SPAN>
  • 我认为有误解。当我说你选择了lowerboundupperbound代替leftboundrightbound,我不是指封闭VS开放间隔。我的意思是,leftboundrightbound分别指区间和区间的右端点的左端点,而lowerboundupperbound是指具有最小协调端点和端点已经最大分别坐标。跨度>
  • 您可以为它们命名任何你想要的。您还可以定义他们的意思是什么在我的反向什么答案,你甚至可以使它这样一个Interval有可能在创建时指定了特征,以及打开或关闭。一类使得所有可能以相对容易的方式。跨度>
猜你喜欢
  • 2020-02-29
  • 1970-01-01
  • 2017-08-22
  • 2021-02-16
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
相关资源
最近更新 更多