Beispiel #1
0
def aabb_with_result(a: Rectangle, b: Rectangle, result: Rectangle) -> bool:
    """
    Does Axis Aligned Bounding Box collision detection between two rectangles a and b.
    Also calculates the intersection rectangle.
    :param a:
    :param b:
    :param result:
    :return:
    """
    # Horizontal
    amin = a.x
    amax = a.x + a.width
    bmin = b.x
    bmax = b.x + b.width
    if bmin > amin:
        amin = bmin
    result.x = amin
    if bmax < amax:
        amax = bmax
    result.width = amax - amin

    # Vertical
    amin = a.y
    amax = a.y + a.height
    bmin = b.y
    bmax = b.y + b.height
    if bmin > amin:
        amin = bmin
    result.y = amin
    if bmax < amax:
        amax = bmax
    result.height = amax - amin

    return not utils.rect_empty(result)
Beispiel #2
0
def set_rect(rect: Rectangle, x, y, w, h):
    """
    Sets the rectangle dimensions to different ones.
    """
    rect.x = x
    rect.y = y
    rect.width = w
    rect.height = h
Beispiel #3
0
def center_rect_in_rect(center_rect: Rectangle, outer_rect: Rectangle):
    """
    Aligns a rectangle in center of another rectangle.
    """
    center_rect.x = (outer_rect.x +
                     outer_rect.width // 2) - center_rect.width // 2
    center_rect.y = (outer_rect.y +
                     outer_rect.height // 2) - center_rect.height // 2
Beispiel #4
0
def test_rect_functions():
    rect = Rectangle(20, -10, 20, 5)
    utils.add_vector_to_rect(rect, (6, 8))
    assert rect.x == 26 and rect.y == -2 and rect.width == 20 and rect.height == 5
    rect = Rectangle(20, -10, 20, 5)
    utils.add_vector_to_rect(rect, (-5, -13))
    assert rect.x == 15 and rect.y == -23 and rect.width == 20 and rect.height == 5
    rect = Rectangle(20, -10, 20, 5)
    utils.add_vector_to_rect(rect, (45, 100))
    assert rect.x == 65 and rect.y == 90 and rect.width == 20 and rect.height == 5

    rect = Rectangle(10, 20, 50, 50)
    rect2 = Rectangle(0, 0, 30, 30)
    utils.center_rect_in_rect(rect2, rect)
    assert rect2.x == 20 and rect2.y == 30 and rect2.width == 30 and rect2.height == 30
    assert rect.x == 10 and rect.y == 20 and rect.width == 50 and rect.height == 50

    rect = Rectangle(10, 20, 50, 50)
    rect2 = Rectangle(0, 0, 30, 30)
    utils.center_rect_in_rect(rect, rect2)
    assert rect.x == -10 and rect.y == -10 and rect.width == 50 and rect.height == 50
    assert rect2.x == 0 and rect2.y == 0 and rect2.width == 30 and rect2.height == 30

    rect = Rectangle(10, 20, 50, 50)
    rect2 = Rectangle(0, 0, 30, 30)
    assert not utils.rect_empty(rect) and not utils.rect_empty(rect2)
    assert utils.rect_empty(Rectangle(10, 10, 1, 0))
    assert utils.rect_empty(Rectangle(10, 10, -0, 5))
    assert utils.rect_empty(Rectangle(10, 10, 43, -2))
    assert utils.rect_empty(Rectangle(10, 10, 0, 0))
    assert not utils.rect_empty(Rectangle(10, 10, 1, 1))

    assert not utils.rect_equal(rect, rect2)
    rect2.x = rect.x
    rect2.y = rect.y
    assert not utils.rect_equal(rect, rect2)
    rect2.width = rect.width
    rect2.height = rect.height
    assert utils.rect_equal(rect, rect2)

    assert utils.rect_in_rect(rect, rect2)
    rect2.x += 1
    assert not utils.rect_in_rect(rect, rect2)
    rect2.height = 10
    rect2.width = 10
    assert utils.rect_in_rect(rect2, rect)

    assert utils.point_in_rect((25, 25), rect)
    assert utils.point_in_rect((rect.x, 25), rect)
    assert utils.point_in_rect((rect.x + rect.width, rect.y), rect)
    assert not utils.point_in_rect((rect.x + rect.width + 1, 25), rect)

    rect = Rectangle(0, 0, 10, 10)
    utils.set_rect(rect, 4, -2, 22, 6)
    assert rect.x == 4 and rect.y == -2 and rect.width == 22 and rect.height == 6

    assert utils.get_center_of_rect(rect) == (15, 1)