Path: blob/main/crates/bevy_gizmos/src/primitives/helpers.rs
6596 views
use core::f32::consts::TAU;12use bevy_math::{ops, Vec2};34/// Calculates the `nth` coordinate of a circle.5///6/// Given a circle's radius and its resolution, this function computes the position7/// of the `nth` point along the circumference of the circle. The rotation starts at `(0.0, radius)`8/// and proceeds counter-clockwise.9pub(crate) fn single_circle_coordinate(radius: f32, resolution: u32, nth_point: u32) -> Vec2 {10let angle = nth_point as f32 * TAU / resolution as f32;11let (x, y) = ops::sin_cos(angle);12Vec2::new(x, y) * radius13}1415/// Generates an iterator over the coordinates of a circle.16///17/// The coordinates form an open circle, meaning the first and last points aren't the same.18///19/// This function creates an iterator that yields the positions of points approximating a20/// circle with the given radius, divided into linear segments. The iterator produces `resolution`21/// number of points.22pub(crate) fn circle_coordinates(radius: f32, resolution: u32) -> impl Iterator<Item = Vec2> {23(0..)24.map(move |p| single_circle_coordinate(radius, resolution, p))25.take(resolution as usize)26}2728/// Generates an iterator over the coordinates of a circle.29///30/// The coordinates form a closed circle, meaning the first and last points are the same.31///32/// This function creates an iterator that yields the positions of points approximating a33/// circle with the given radius, divided into linear segments. The iterator produces `resolution`34/// number of points.35pub(crate) fn circle_coordinates_closed(36radius: f32,37resolution: u32,38) -> impl Iterator<Item = Vec2> {39circle_coordinates(radius, resolution).chain(core::iter::once(single_circle_coordinate(40radius, resolution, resolution,41)))42}434445