pub struct Layout { /* private fields */ }
Expand description
Coefficients for converting between hexagonal grid coordinates and x/y pixels. Used as parameter for different conversion functions.
Implementations§
source§impl Layout
impl Layout
sourcepub fn new(orientation: &Orientation, size: Point, origin: Point) -> Self
pub fn new(orientation: &Orientation, size: Point, origin: Point) -> Self
Creates a new grid layout, consisting of hexagon orientation parameters, hexagon size in pixels, and the grid origin point.
sourcepub fn orientation(&self) -> &Orientation
pub fn orientation(&self) -> &Orientation
Returns a reference to the grid orientation parameters.
sourcepub fn is_pointy(&self) -> bool
pub fn is_pointy(&self) -> bool
Returns whether the hexagon orientation is “pointy top” or not.
sourcepub fn with_orientation(&self, orientation: &Orientation) -> Self
pub fn with_orientation(&self, orientation: &Orientation) -> Self
Creates a copy of this layout with new grid orientation parameters.
sourcepub fn with_size(&self, size: Point) -> Self
pub fn with_size(&self, size: Point) -> Self
Creates a copy of this layout with new hexagon size parameters.
sourcepub fn with_origin(&self, origin: Point) -> Self
pub fn with_origin(&self, origin: Point) -> Self
Creates a copy of this layout with new grid origin point.
sourcepub fn hexagon_corners(&self, hex: Coordinate) -> [Point; 6]
pub fn hexagon_corners(&self, hex: Coordinate) -> [Point; 6]
Calculates the corners of a hexagon with the given coordinate.
sourcepub fn hexagon_rect(&self, hex: Coordinate) -> Rect
pub fn hexagon_rect(&self, hex: Coordinate) -> Rect
Calculates the rectangular box of a hexagon with the given coordinate.
sourcepub fn direction_to_angle<D>(&self, dir: D) -> f32where
D: Into<FloatDirection>,
pub fn direction_to_angle<D>(&self, dir: D) -> f32where
D: Into<FloatDirection>,
Convert a hexagon grid direction into an angle in degrees.
Applies the layout start_angle
to the given direction. The returned
value is wrapped within a range of 0° to 360°.
§Examples
let layout = Layout::new(Orientation::pointy(), Point(1.0, 1.0), Point(0.0, 0.0));
let dir = Direction::A;
assert_eq!(layout.direction_to_angle(dir), 90.0);
let dir = Direction::B;
assert_eq!(layout.direction_to_angle(dir), 150.0);
let dir = FloatDirection(1.5);
assert_eq!(layout.direction_to_angle(dir), 180.0);
sourcepub fn direction_from_angle(&self, value: f32) -> FloatDirection
pub fn direction_from_angle(&self, value: f32) -> FloatDirection
Convert an angle in degrees into a hexagon grid direction.
Subtracts the layout start_angle
from the given angle.
§Examples
let layout = Layout::new(Orientation::pointy(), Point(1.0, 1.0), Point(0.0, 0.0));
let angle = 90.0;
assert_eq!(layout.direction_from_angle(angle), Direction::A.into());
let angle = 150.0;
assert_eq!(layout.direction_from_angle(angle), Direction::B.into());
let angle = 180.0;
assert_eq!(layout.direction_from_angle(angle), FloatDirection(1.5));