Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Random2D.point_in_region accept a Rect2 over endpoints #68

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/math/2d/random_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ real_t Random2D::get_rotation() {
return randf_range(0.0, Math_TAU);
}

Vector2 Random2D::point_in_region(const Vector2 &p_pos, const Vector2 &p_end) {
Vector2 Random2D::point_in_region(const Rect2 &p_region) {
Point2 point;
point.x = randf_range(p_pos.x, p_end.x);
point.y = randf_range(p_pos.y, p_end.y);
const Vector2& end = p_region.position + p_region.size;
point.x = randf_range(p_region.position.x, end.x);
point.y = randf_range(p_region.position.y, end.y);
return point;
}

Expand Down Expand Up @@ -120,7 +121,7 @@ void Random2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_direction"), &Random2D::get_direction);
ClassDB::bind_method(D_METHOD("get_rotation"), &Random2D::get_rotation);

ClassDB::bind_method(D_METHOD("point_in_region", "position_start", "position_end"), &Random2D::point_in_region);
ClassDB::bind_method(D_METHOD("point_in_region", "region"), &Random2D::point_in_region);
ClassDB::bind_method(D_METHOD("point_in_circle", "radius_min", "radius_max"), &Random2D::point_in_circle, DEFVAL(0.0), DEFVAL(1.0));
ClassDB::bind_method(D_METHOD("point_in_triangle", "triangle"), &Random2D::point_in_triangle);
ClassDB::bind_method(D_METHOD("point_in_polygon", "polygon", "point_count"), &Random2D::point_in_polygon, DEFVAL(1));
Expand Down
2 changes: 1 addition & 1 deletion core/math/2d/random_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Random2D : public Random {
real_t get_rotation();
Vector2 get_direction(); // Unit vector.

Vector2 point_in_region(const Vector2 &p_pos, const Vector2 &p_end);
Vector2 point_in_region(const Rect2 &p_region);
Vector2 point_in_circle(real_t p_min_radius = 0.0, real_t p_max_radius = 1.0);
Vector2 point_in_triangle(const Vector<Point2> &p_triangle);
Variant point_in_polygon(const Variant &p_polygon, int p_point_count = 1);
Expand Down
8 changes: 3 additions & 5 deletions doc/Random2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@
<method name="point_in_region">
<return type="Vector2">
</return>
<argument index="0" name="position_start" type="Vector2">
</argument>
<argument index="1" name="position_end" type="Vector2">
<argument index="0" name="region" type="Rect2">
</argument>
<description>
Generates a random point in the area specified by top-left and bottom-right corners of a rectangle.
Generates a random point in the area specified by top-left and bottom-right corners of a [Rect2].
[codeblock]
var rect := Rect2(-100, -100, 100, 100) # Position and size.
var point = Random2D.point_in_region(rect.position, rect.end)
var point = Random2D.point_in_region(rect)
[/codeblock]
</description>
</method>
Expand Down
2 changes: 1 addition & 1 deletion tests/project/goost/core/math/test_random_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func test_point_within_circle_range():
func test_point_in_region():
var rect = Rect2(Vector2(-100, -100), Vector2(200, 200))
for i in 100:
var point = Random2D.point_in_region(rect.position, rect.end)
var point = Random2D.point_in_region(rect)
assert_true(rect.has_point(point))


Expand Down