From 585f6314c5418acb8c516d69b5dafd548ec18928 Mon Sep 17 00:00:00 2001 From: Shuvo <38564994+0shuvo0@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:33:35 +0600 Subject: [PATCH 1/5] 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..df3b00ee5229 --- /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 check_collision(rect1, rect2) -> bool: + """ + Check if two rectangle are colliding/overlaping + + >>> check_collision(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 fd14a82ca3bc5d293aeee8621ed2900b4b0bd8b6 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:35:02 +0000 Subject: [PATCH 2/5] [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 df3b00ee5229..f279cb158507 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 check_collision(rect1, rect2) -> bool: """ Check if two rectangle are colliding/overlaping - + >>> check_collision(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 From ad9f14b8c9d519b3f5eb07fb7418fed6f182e321 Mon Sep 17 00:00:00 2001 From: Shuvo <38564994+0shuvo0@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:37:39 +0600 Subject: [PATCH 3/5] Added parameter type hint --- maths/collision_between_rectangles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/collision_between_rectangles.py b/maths/collision_between_rectangles.py index f279cb158507..71868a60f602 100644 --- a/maths/collision_between_rectangles.py +++ b/maths/collision_between_rectangles.py @@ -3,7 +3,7 @@ rect2 = {"x": 20, "y": 30, "height": 40, "width": 30} -def check_collision(rect1, rect2) -> bool: +def check_collision(rect1 -> dict, rect2 -> dict) -> bool: """ Check if two rectangle are colliding/overlaping From c03713be64cb22411c9229f0222c71f1b6fa4375 Mon Sep 17 00:00:00 2001 From: Shuvo <38564994+0shuvo0@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:42:50 +0600 Subject: [PATCH 4/5] Fixed parameter type error --- maths/collision_between_rectangles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/collision_between_rectangles.py b/maths/collision_between_rectangles.py index 71868a60f602..cf8377521f82 100644 --- a/maths/collision_between_rectangles.py +++ b/maths/collision_between_rectangles.py @@ -3,7 +3,7 @@ rect2 = {"x": 20, "y": 30, "height": 40, "width": 30} -def check_collision(rect1 -> dict, rect2 -> dict) -> bool: +def check_collision(rect1: dict, rect2: dict) -> bool: """ Check if two rectangle are colliding/overlaping From 7acd1ba724c6c8b695f1699ea87f7998b6c5f0db Mon Sep 17 00:00:00 2001 From: Shuvo <38564994+0shuvo0@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:47:16 +0600 Subject: [PATCH 5/5] fixed a trype --- maths/collision_between_rectangles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/collision_between_rectangles.py b/maths/collision_between_rectangles.py index cf8377521f82..5f9fdc482b85 100644 --- a/maths/collision_between_rectangles.py +++ b/maths/collision_between_rectangles.py @@ -5,7 +5,7 @@ def check_collision(rect1: dict, rect2: dict) -> bool: """ - Check if two rectangle are colliding/overlaping + Check if two rectangle are colliding/overlapping >>> check_collision(rect1, rect2) True