pub struct Coordinate<I: Integer = i32> {
pub x: I,
pub y: I,
}Expand description
Coordinate on 2d hexagonal grid
Fields§
§x: Ix coordinate
y: Iy coordinate
Implementations§
Source§impl<I: Integer> Coordinate<I>
impl<I: Integer> Coordinate<I>
Sourcepub fn new(x: I, y: I) -> Coordinate<I>
pub fn new(x: I, y: I) -> Coordinate<I>
Create new Coord from x and y
Sourcepub fn nearest<F: Float>(x: F, y: F) -> Coordinate<I>
pub fn nearest<F: Float>(x: F, y: F) -> Coordinate<I>
Round x, y float to nearest hex coordinates
Sourcepub fn nearest_lossy<F: Float>(x: F, y: F) -> Option<Coordinate<I>>
pub fn nearest_lossy<F: Float>(x: F, y: F) -> Option<Coordinate<I>>
Round x, y float to nearest hex coordinates
Return None, if exactly on the border of two hex coordinates
Sourcepub fn from_pixel<F: Float>(x: F, y: F, spacing: Spacing<F>) -> Coordinate<I>
pub fn from_pixel<F: Float>(x: F, y: F, spacing: Spacing<F>) -> Coordinate<I>
Find the hex containing a pixel. The origin of the pixel coordinates is the center of the hex at (0,0) in hex coordinates.
Sourcepub fn nearest_with_offset(
spacing: IntegerSpacing<I>,
v: (I, I),
) -> (Coordinate<I>, (I, I))
pub fn nearest_with_offset( spacing: IntegerSpacing<I>, v: (I, I), ) -> (Coordinate<I>, (I, I))
Convert integer pixel coordinates v using spacing to nearest coordinate that has both
integer pixel coordinates lower or equal to v. Also return offset (in integer pixels)
from that coordinate.
Useful for ASCII visualization.
Sourcepub fn to_pixel<F: Float>(&self, spacing: Spacing<F>) -> (F, F)
pub fn to_pixel<F: Float>(&self, spacing: Spacing<F>) -> (F, F)
Convert to pixel coordinates using spacing, where the
parameter means the edge length of a hexagon.
This function is meant for graphical user interfaces where resolution is big enough that floating point calculation make sense.
Sourcepub fn to_pixel_integer(&self, spacing: IntegerSpacing<I>) -> (I, I)
pub fn to_pixel_integer(&self, spacing: IntegerSpacing<I>) -> (I, I)
Convert to integer pixel coordinates using spacing, where the
parameters mean the width and height multiplications
Sourcepub fn scale(&self, s: I) -> Coordinate<I>
pub fn scale(&self, s: I) -> Coordinate<I>
Scale coordinate by a factor s
Sourcepub fn neighbors(&self) -> [Coordinate<I>; 6]
pub fn neighbors(&self) -> [Coordinate<I>; 6]
Array with all the neighbors of a coordinate
Sourcepub fn rotate_around_zero(&self, a: Angle) -> Coordinate<I>
pub fn rotate_around_zero(&self, a: Angle) -> Coordinate<I>
Rotate self around a point (0, 0, 0) using angle of rotation a
Sourcepub fn rotate_around(&self, center: Coordinate<I>, a: Angle) -> Coordinate<I>
pub fn rotate_around(&self, center: Coordinate<I>, a: Angle) -> Coordinate<I>
Rotate self around a center using angle of rotation a
Sourcepub fn line_to_iter(&self, dest: Coordinate<I>) -> LineTo<I> ⓘ
pub fn line_to_iter(&self, dest: Coordinate<I>) -> LineTo<I> ⓘ
Iterator over each coordinate in straight line from self to dest
Sourcepub fn line_to_lossy_iter(&self, dest: Coordinate<I>) -> LineToLossy<I> ⓘ
pub fn line_to_lossy_iter(&self, dest: Coordinate<I>) -> LineToLossy<I> ⓘ
Iterator over each coordinate in straight line from self to dest
Skip points on the border of two tiles
Sourcepub fn line_to_with_edge_detection_iter(
&self,
dest: Coordinate<I>,
) -> LineToWithEdgeDetection<I> ⓘ
pub fn line_to_with_edge_detection_iter( &self, dest: Coordinate<I>, ) -> LineToWithEdgeDetection<I> ⓘ
Iterator over each coordinate in straight line from self to dest
On edge condition the pair contains different members, otherwise it’s the same.
Sourcepub fn direction_from_center_cw(&self) -> Option<Direction>
pub fn direction_from_center_cw(&self) -> Option<Direction>
Direction from center (0, 0) to coordinate
In case of diagonals (edge of two major directions), prefers direction that is clockwise from the diagonal
Returns: None if is center
use hex2d::{Direction, Coordinate};
use hex2d::{Left, Right};
let center = Coordinate::new(0, 0);
assert_eq!(center.direction_from_center_cw(), None);
for &d in Direction::all() {
assert_eq!((center + d).direction_from_center_cw(), Some(d));
assert_eq!((center + d + (d + Left)).direction_from_center_cw(), Some(d));
assert_eq!((center + d + (d + Right)).direction_from_center_cw(), Some(d + Right));
}Sourcepub fn directions_from_center(&self) -> Vec<Direction>
pub fn directions_from_center(&self) -> Vec<Direction>
Directions that lead from center to a given point.
Returns an array of one or two directions.
Sourcepub fn direction_from_center_ccw(&self) -> Option<Direction>
pub fn direction_from_center_ccw(&self) -> Option<Direction>
Direction from center (0, 0) to coordinate
In case of diagonals (edge of two major directions), prefers direction that is counter-clockwise from the diagonal.
Returns: None if is center
use hex2d::{Direction, Coordinate};
use hex2d::{Left, Right};
let center = Coordinate::new(0, 0);
assert_eq!(center.direction_from_center_ccw(), None);
for &d in Direction::all() {
assert_eq!((center + d).direction_from_center_ccw(), Some(d));
assert_eq!((center + d + (d + Left)).direction_from_center_ccw(), Some(d + Left));
assert_eq!((center + d + (d + Right)).direction_from_center_ccw(), Some(d));
}Sourcepub fn directions_to(&self, coord: Coordinate<I>) -> Vec<Direction>
pub fn directions_to(&self, coord: Coordinate<I>) -> Vec<Direction>
Directions from self to coord
Sourcepub fn direction_to_cw(&self, coor: Coordinate<I>) -> Option<Direction>
pub fn direction_to_cw(&self, coor: Coordinate<I>) -> Option<Direction>
Direction from self to coord
In case of diagonals (edge of two major directions), prefers direction that is clockwise from the diagonal.
Returns: None if is center
Sourcepub fn direction_to_ccw(&self, coor: Coordinate<I>) -> Option<Direction>
pub fn direction_to_ccw(&self, coor: Coordinate<I>) -> Option<Direction>
Direction from self to coor
In case of diagonals (edge of two major directions), prefers direction that is counter-clockwise from the diagonal.
Returns: None if is center
Sourcepub fn distance(&self, c: Coordinate<I>) -> I
pub fn distance(&self, c: Coordinate<I>) -> I
Distance between two Coordinates
Sourcepub fn range_iter(&self, r: I) -> Range<I> ⓘ
pub fn range_iter(&self, r: I) -> Range<I> ⓘ
An iterator over all coordinates in radius r
Sourcepub fn ring_iter(&self, r: i32, s: Spin) -> Ring<I> ⓘ
pub fn ring_iter(&self, r: i32, s: Spin) -> Ring<I> ⓘ
Iterator over each coordinate in a ring
Example: Elements in order for Ring of radius 2, Direction ZX, CCW
8
9 7
10 . 6
. .
11 x 5
. .
0 . 4
1 3
2
use hex2d::{Coordinate, Spin, XY};
let center = Coordinate::new(5, -1);
for &c in ¢er.neighbors() {
for ring_c in c.ring_iter(5, Spin::CCW(XY)) {
assert_eq!(c.distance(ring_c), 5);
}
}Trait Implementations§
Source§impl<I: Integer, T: Into<Coordinate<I>>> Add<T> for Coordinate<I>
impl<I: Integer, T: Into<Coordinate<I>>> Add<T> for Coordinate<I>
Source§type Output = Coordinate<I>
type Output = Coordinate<I>
+ operator.Source§fn add(self, c: T) -> Coordinate<I>
fn add(self, c: T) -> Coordinate<I>
+ operation. Read moreSource§impl<I: Clone + Integer> Clone for Coordinate<I>
impl<I: Clone + Integer> Clone for Coordinate<I>
Source§fn clone(&self) -> Coordinate<I>
fn clone(&self) -> Coordinate<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'de, I> Deserialize<'de> for Coordinate<I>where
I: Deserialize<'de> + Integer,
impl<'de, I> Deserialize<'de> for Coordinate<I>where
I: Deserialize<'de> + Integer,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<I: Integer> Neg for Coordinate<I>
impl<I: Integer> Neg for Coordinate<I>
Source§type Output = Coordinate<I>
type Output = Coordinate<I>
- operator.Source§fn neg(self) -> Coordinate<I>
fn neg(self) -> Coordinate<I>
- operation. Read moreSource§impl<I: Ord + Integer> Ord for Coordinate<I>
impl<I: Ord + Integer> Ord for Coordinate<I>
Source§fn cmp(&self, other: &Coordinate<I>) -> Ordering
fn cmp(&self, other: &Coordinate<I>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<I: PartialOrd + Integer> PartialOrd for Coordinate<I>
impl<I: PartialOrd + Integer> PartialOrd for Coordinate<I>
Source§impl<I> Serialize for Coordinate<I>
impl<I> Serialize for Coordinate<I>
Source§impl<I: Integer, T: Into<Coordinate<I>>> Sub<T> for Coordinate<I>
impl<I: Integer, T: Into<Coordinate<I>>> Sub<T> for Coordinate<I>
Source§type Output = Coordinate<I>
type Output = Coordinate<I>
- operator.Source§fn sub(self, c: T) -> Coordinate<I>
fn sub(self, c: T) -> Coordinate<I>
- operation. Read more