From 14136d712f2e5063a7e638ffd70c8f3b13c919b9 Mon Sep 17 00:00:00 2001 From: Rapen765 Date: Sat, 19 Oct 2024 22:20:59 +0200 Subject: [PATCH 1/6] Fixed rand_in_circle to ensure uniform distribution of points --- arcade/math.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arcade/math.py b/arcade/math.py index f8494d480f..a14b85c33c 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -224,10 +224,7 @@ def rand_in_circle(center: Point2, radius: float) -> Point2: Generate a point in a circle, or can think of it as a vector pointing a random direction with a random magnitude <= radius. - Reference: https://stackoverflow.com/a/30564123 - - .. note:: This algorithm returns a higher concentration of points - around the center of the circle + Reference: https://stackoverflow.com/a/50746409 Args: center (Point2): The center of the circle @@ -236,7 +233,7 @@ def rand_in_circle(center: Point2, radius: float) -> Point2: # random angle angle = 2 * math.pi * random.random() # random radius - r = radius * random.random() + r = radius * math.sqrt(random.random()) # calculating coordinates return (r * math.cos(angle) + center[0], r * math.sin(angle) + center[1]) From e544f4ad2d816811d9c806812a2f8f6bf2dfcd4d Mon Sep 17 00:00:00 2001 From: Rapen765 Date: Mon, 21 Oct 2024 11:16:21 +0200 Subject: [PATCH 2/6] fixed math point types --- arcade/math.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/arcade/math.py b/arcade/math.py index a14b85c33c..00762243de 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -235,22 +235,19 @@ def rand_in_circle(center: Point2, radius: float) -> Point2: # random radius r = radius * math.sqrt(random.random()) # calculating coordinates - return (r * math.cos(angle) + center[0], r * math.sin(angle) + center[1]) + return r * math.cos(angle) + center[0], r * math.sin(angle) + center[1] def rand_on_circle(center: Point2, radius: float) -> Point2: """ Generate a point on a circle. - .. note: by passing a random value in for float, - you can achieve what rand_in_circle() does - Args: center (Point2): The center of the circle radius (float): The radius of the circle """ angle = 2 * math.pi * random.random() - return (radius * math.cos(angle) + center[0], radius * math.sin(angle) + center[1]) + return radius * math.cos(angle) + center[0], radius * math.sin(angle) + center[1] def rand_on_line(pos1: Point2, pos2: Point2) -> Point: @@ -286,7 +283,7 @@ def rand_angle_spread_deg(angle: float, half_angle_spread: float) -> float: def rand_vec_spread_deg( angle: float, half_angle_spread: float, length: float -) -> tuple[float, float]: +) -> Point2: """ Returns a random vector, within a spread of the given angle. @@ -304,7 +301,7 @@ def rand_vec_magnitude( angle: float, lo_magnitude: float, hi_magnitude: float, -) -> tuple[float, float]: +) -> Point2: """ Returns a random vector, within a spread of the given angle. From 2f6656b8d8f564796044f0d8cf874c735a5c8545 Mon Sep 17 00:00:00 2001 From: Rapen765 Date: Mon, 21 Oct 2024 11:21:45 +0200 Subject: [PATCH 3/6] fixed rand_vec_magnitude documentation --- arcade/math.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/math.py b/arcade/math.py index 00762243de..ef4adeab41 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -303,10 +303,10 @@ def rand_vec_magnitude( hi_magnitude: float, ) -> Point2: """ - Returns a random vector, within a spread of the given angle. + Returns a random vector, with random magnitude. Args: - angle (float): The angle to spread from + angle (float): The vector angle lo_magnitude (float): The lower magnitude hi_magnitude (float): The higher magnitude """ From a5240cf8e8d36bf9733a6e6dff7491e84b7884cf Mon Sep 17 00:00:00 2001 From: Raxeli1 <83366936+Rapen765@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:05:03 +0200 Subject: [PATCH 4/6] Update arcade/math.py Co-authored-by: Paul <36696816+pushfoo@users.noreply.github.com> --- arcade/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/math.py b/arcade/math.py index ef4adeab41..3d0ed22f7b 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -306,7 +306,7 @@ def rand_vec_magnitude( Returns a random vector, with random magnitude. Args: - angle (float): The vector angle + angle (float): The vector angle in radians lo_magnitude (float): The lower magnitude hi_magnitude (float): The higher magnitude """ From 0cacf38a7a72dd246ef956ce31e922a7a19f04bb Mon Sep 17 00:00:00 2001 From: Raxeli1 <83366936+Rapen765@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:05:13 +0200 Subject: [PATCH 5/6] Update arcade/math.py Co-authored-by: Paul <36696816+pushfoo@users.noreply.github.com> --- arcade/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/math.py b/arcade/math.py index 3d0ed22f7b..d0c8a39443 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -303,7 +303,7 @@ def rand_vec_magnitude( hi_magnitude: float, ) -> Point2: """ - Returns a random vector, with random magnitude. + Return a vector of randomized magnitude pointing in the given direction. Args: angle (float): The vector angle in radians From 066357975a8e85c7df8bf3cfbaf294b2c637acaa Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 1 Jan 2025 16:38:25 -0600 Subject: [PATCH 6/6] Run formatting --- arcade/math.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arcade/math.py b/arcade/math.py index d0c8a39443..2198e5c815 100644 --- a/arcade/math.py +++ b/arcade/math.py @@ -281,9 +281,7 @@ def rand_angle_spread_deg(angle: float, half_angle_spread: float) -> float: return angle + s -def rand_vec_spread_deg( - angle: float, half_angle_spread: float, length: float -) -> Point2: +def rand_vec_spread_deg(angle: float, half_angle_spread: float, length: float) -> Point2: """ Returns a random vector, within a spread of the given angle.