From 545e6b0ad4e35298b7479a78b59daba66556c384 Mon Sep 17 00:00:00 2001 From: Shuvo <38564994+0shuvo0@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:18:25 +0600 Subject: [PATCH 1/2] Create collision_between_rectangles.py --- maths/collision_between_rectangles.py | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 maths/collision_between_rectangles.py diff --git a/maths/collision_between_rectangles.py b/maths/collision_between_rectangles.py new file mode 100644 index 000000000000..2f95db936b17 --- /dev/null +++ b/maths/collision_between_rectangles.py @@ -0,0 +1,29 @@ +rect1 = { + "x": 10, + "y": 10, + "height": 30, + "width": 50 +} + +rect2 = { + "x": 20, + "y": 30, + "height": 40, + "width": 30 +} + +def checkCollision(rect1, rect2): + """ + Check if two rectangle are colliding/overlaping + + >>> checkCollision(rect1, rect2) + True + """ + x_bound = rect1["x"] < rect2["x"] + rect1["width"] and rect1["x"] + rect2["width"] > rect2["x"] + y_bound = rect1["y"] < rect2["y"] + rect1["height"] and rect1["height"] + rect1["y"] > rect2["y"] + return x_bound and y_bound + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3f81a537622246e99ce7043284a0e1cc25310e42 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Oct 2022 07:20:08 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/collision_between_rectangles.py | 28 +++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/maths/collision_between_rectangles.py b/maths/collision_between_rectangles.py index 2f95db936b17..8ee8ba12d0fc 100644 --- a/maths/collision_between_rectangles.py +++ b/maths/collision_between_rectangles.py @@ -1,28 +1,26 @@ -rect1 = { - "x": 10, - "y": 10, - "height": 30, - "width": 50 -} +rect1 = {"x": 10, "y": 10, "height": 30, "width": 50} + +rect2 = {"x": 20, "y": 30, "height": 40, "width": 30} -rect2 = { - "x": 20, - "y": 30, - "height": 40, - "width": 30 -} def checkCollision(rect1, rect2): """ Check if two rectangle are colliding/overlaping - + >>> checkCollision(rect1, rect2) True """ - x_bound = rect1["x"] < rect2["x"] + rect1["width"] and rect1["x"] + rect2["width"] > rect2["x"] - y_bound = rect1["y"] < rect2["y"] + rect1["height"] and rect1["height"] + rect1["y"] > rect2["y"] + x_bound = ( + rect1["x"] < rect2["x"] + rect1["width"] + and rect1["x"] + rect2["width"] > rect2["x"] + ) + y_bound = ( + rect1["y"] < rect2["y"] + rect1["height"] + and rect1["height"] + rect1["y"] > rect2["y"] + ) return x_bound and y_bound + if __name__ == "__main__": import doctest