Algorithm 处理基本情况时的编程原理

Algorithm 处理基本情况时的编程原理,algorithm,Algorithm,我正在修补一个测试矩形和圆之间交点的函数,以及我有四个案例要测试的原始函数。顺序如下: 1.)如果圆的边界矩形与矩形不相交,则返回False 2.)如果矩形的任何点位于圆中,则返回True 3.)如果圆的中心位于矩形中,则返回True 4.)遍历矩形中的直线,并分别测试与圆的交点。如果存在任何交叉点,则中断并返回True 但是,当使用timeit对函数计时时,我意识到忽略案例1-3实际上平均速度更快,而仅仅使用4来测试碰撞(平均速度提高了100%,1000次测试的速度提高了一倍) 我最初实现了所

我正在修补一个测试矩形和圆之间交点的函数,以及我有四个案例要测试的原始函数。顺序如下:

1.)如果圆的边界矩形与矩形不相交,则返回False

2.)如果矩形的任何点位于圆中,则返回True

3.)如果圆的中心位于矩形中,则返回True

4.)遍历矩形中的直线,并分别测试与圆的交点。如果存在任何交叉点,则中断并返回True

但是,当使用
timeit
对函数计时时,我意识到忽略案例1-3实际上平均速度更快,而仅仅使用4来测试碰撞(平均速度提高了100%,1000次测试的速度提高了一倍)

我最初实现了所有四种情况,因为更常见的情况(1-3)会在返回True时快速终止函数,但我想我想错了。所以我的问题是,当涉及到不同场景的编程和会计时,你们的人的哲学是什么?您是否尝试实现一个始终有效的算法,或者三个或四个算法,每个算法的成本都比单独的算法低得多

下面是代码,以防您想查看它,它有点无用,因为它依赖于我制作的其他东西,但您可能能够了解发生了什么:

    def collide_rect(self, rect):
        """Check if the circle collides with a rect."""
##        if not rect.collide_rect(self.to_rect()):
##            # Case 1 ~ The bounding rects don't intersect.
##            return False
##        # Case 2 ~ One of the points on the rect is inside the circle.
##        points = rect.points()
##        if any(self.collide_point(v) for v in points):
##            return True
##        if rect.collide_point(self._center):
##            # Case 3 ~ The circle is entirely contained by the rect.
##            return True
        # Case 4 ~ None of the points from the rect fall inside
        # the circle. Now we must check if any of the lines from
        # the rect intersect the boundaries of the circle. To do
        # this, we know that if a line intersects a circle's boundary,
        # then there exists some value `t` such that 0 <= `t` <= 1
        # (i.e. `t` is a ratio), and the distance from the point at
        # ratio `t` on the line segment is also the radius of the circle.
        points = rect.points()
        _abs = abs
        for i in range(4):
            start = points[i - 1]
            end = points[i]
            _line = Segment(start, end)
            if _line.isHorizontal():
                if (_line._s.x < self.x < _line._e.x) or \
                   (_line._s.x > self.x > _line._e.x)    :
                    if _abs(_line._s.y - self.y) <= self.r:
                        return True
                else:
                    dist1 = (_line._s.x - self.x)**2 + (_line._s.y - self.y)**2
                    dist2 = (_line._e.x - self.x)**2 + (_line._e.y - self.y)**2
                    if dist1 <= self.r**2 or dist2 <= self.r**2:
                        return True
            elif _line.isVertical():
                if (_line._s.y < self.y < _line._e.y) or \
                   (_line._s.y > self.y > _line._e.y)    :
                    if _abs(_line._s.x - self.x) <= self.r:
                        return True
                else:
                    dist1 = (_line._s.x - self.x)**2 + (_line._s.y - self.y)**2
                    dist2 = (_line._e.x - self.x)**2 + (_line._e.y - self.y)**2
                    if dist1 <= self.r**2 or dist2 <= self.r**2:
                        return True
        return False
def collide_rect(self,rect):
“”“检查圆是否与矩形碰撞。”“”
##如果不是rect.collide_rect(self.to_rect()):
###情况1~边界矩形不相交。
##返回错误
###情况2~矩形上的一个点在圆内。
##点=矩形点()
##如果有(针对v in点的自碰撞点(v)):
##返回真值
##如果矩形碰撞点(自中心):
###情况3~圆圈完全包含在矩形中。
##返回真值
#案例4~直肠的所有点都不在里面
#圆圈。现在我们必须检查
#矩形与圆的边界相交。做
#我们知道,如果一条线与一个圆的边界相交,
#然后存在一些值't',使得0(e.x):

如果_abs(_line.s.y-self.y)问题是:最后一个算法对您来说足够快吗?如果您没有任何性能问题,就不必担心其他情况

一种情况只意味着更少的代码,因此更少的bug和更容易的维护

正如计算机科学中常说的那样,过早优化是万恶之源;)
因此,从测量性能开始,如果由于碰撞检测,性能不够好,请通过添加其他测试进行优化。然后迭代:)

您的测试用例是否涵盖了所有可能性?例如,如果您使用了错误的测试数据,那么实际上只会使用步骤4。还要注意,是否保留这些步骤可能取决于您的大多数圆是否不会与正方形相交,或者大多数圆是否会与正方形相交。最常见的情况是什么?