Module bevy_math::sampling::shape_sampling

source ·
Available on crate feature rand only.
Expand description

The ShapeSample trait, allowing random sampling from geometric shapes.

At the most basic level, this allows sampling random points from the interior and boundary of geometric primitives. For example:

// Get some `Rng`:
let rng = &mut StdRng::from_entropy();
// Make a circle of radius 2:
let circle = Circle::new(2.0);
// Get a point inside this circle uniformly at random:
let interior_pt = circle.sample_interior(rng);
// Get a point on the circle's boundary uniformly at random:
let boundary_pt = circle.sample_boundary(rng);

For repeated sampling, ShapeSample also includes methods for accessing a Distribution:

// Use a rectangle this time:
let rectangle = Rectangle::new(1.0, 2.0);
// Get an iterator that spits out random interior points:
let interior_iter = rectangle.interior_dist().sample_iter(rng1);
// Collect random interior points from the iterator:
let interior_pts: Vec<Vec2> = interior_iter.take(1000).collect();
// Similarly, get an iterator over many random boundary points and collect them:
let boundary_pts: Vec<Vec2> = rectangle.boundary_dist().sample_iter(rng2).take(1000).collect();

In any case, the Rng used as the source of randomness must be provided explicitly.

Structs§

Traits§

  • Exposes methods to uniformly sample a variety of primitive shapes.