randomPoint method

  1. @override
Vector2 randomPoint({
  1. Random? random,
  2. bool within = true,
})
override

Returns a random point within the shape if within is true (default) and otherwise a point along the edges of the shape. Do note that within=true also includes the edges.

If isClosed is false, the within value does not make a difference.

Implementation

@override
Vector2 randomPoint({Random? random, bool within = true}) {
  final randomGenerator = random ?? randomFallback;
  if (within) {
    final result = Vector2.zero();
    final min = aabb.min;
    final max = aabb.max;

    while (true) {
      final randomX = min.x + randomGenerator.nextDouble() * (max.x - min.x);
      final randomY = min.y + randomGenerator.nextDouble() * (max.y - min.y);
      result.setValues(randomX, randomY);

      if (containsPoint(result)) {
        return result;
      }
    }
  } else {
    return Polygon.randomPointAlongEdges(_vertices, random: randomGenerator);
  }
}