Coordinate

Struct Coordinate 

Source
pub struct Coordinate<I: Integer = i32> {
    pub x: I,
    pub y: I,
}
Expand description

Coordinate on 2d hexagonal grid

Fields§

§x: I

x coordinate

§y: I

y coordinate

Implementations§

Source§

impl<I: Integer> Coordinate<I>

Source

pub fn new(x: I, y: I) -> Coordinate<I>

Create new Coord from x and y

Source

pub fn nearest<F: Float>(x: F, y: F) -> Coordinate<I>

Round x, y float to nearest hex coordinates

Source

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

Source

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.

Source

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.

Source

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.

Source

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

Source

pub fn scale(&self, s: I) -> Coordinate<I>

Scale coordinate by a factor s

Source

pub fn neighbors(&self) -> [Coordinate<I>; 6]

Array with all the neighbors of a coordinate

Source

pub fn rotate_around_zero(&self, a: Angle) -> Coordinate<I>

Rotate self around a point (0, 0, 0) using angle of rotation a

Source

pub fn rotate_around(&self, center: Coordinate<I>, a: Angle) -> Coordinate<I>

Rotate self around a center using angle of rotation a

Source

pub fn line_to_iter(&self, dest: Coordinate<I>) -> LineTo<I>

Iterator over each coordinate in straight line from self to dest

Source

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

Source

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.

Source

pub fn z(&self) -> I

Z coordinate

Source

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));
}
Source

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.

Source

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));
}
Source

pub fn directions_to(&self, coord: Coordinate<I>) -> Vec<Direction>

Directions from self to coord

Source

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

Source

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

Source

pub fn distance(&self, c: Coordinate<I>) -> I

Distance between two Coordinates

Source

pub fn range_iter(&self, r: I) -> Range<I>

An iterator over all coordinates in radius r

Source

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 &center.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> Add<Coordinate<I>> for Position<I>

Source§

type Output = Position<I>

The resulting type after applying the + operator.
Source§

fn add(self, c: Coordinate<I>) -> Position<I>

Performs the + operation. Read more
Source§

impl<I: Integer, T: Into<Coordinate<I>>> Add<T> for Coordinate<I>

Source§

type Output = Coordinate<I>

The resulting type after applying the + operator.
Source§

fn add(self, c: T) -> Coordinate<I>

Performs the + operation. Read more
Source§

impl<I: Clone + Integer> Clone for Coordinate<I>

Source§

fn clone(&self) -> Coordinate<I>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Debug + Integer> Debug for Coordinate<I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<I: Integer> From<(I, I)> for Coordinate<I>

Source§

fn from(xy: (I, I)) -> Self

Converts to this type from the input type.
Source§

impl<I: Integer> From<Direction> for Coordinate<I>

Source§

fn from(dir: Direction) -> Self

Converts to this type from the input type.
Source§

impl<I: Integer> From<Position<I>> for Coordinate<I>

Source§

fn from(pos: Position<I>) -> Self

Converts to this type from the input type.
Source§

impl<I: Hash + Integer> Hash for Coordinate<I>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I: Integer> Neg for Coordinate<I>

Source§

type Output = Coordinate<I>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Coordinate<I>

Performs the unary - operation. Read more
Source§

impl<I: Ord + Integer> Ord for Coordinate<I>

Source§

fn cmp(&self, other: &Coordinate<I>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<I: PartialEq + Integer> PartialEq for Coordinate<I>

Source§

fn eq(&self, other: &Coordinate<I>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I: PartialOrd + Integer> PartialOrd for Coordinate<I>

Source§

fn partial_cmp(&self, other: &Coordinate<I>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<I> Serialize for Coordinate<I>
where I: Serialize + Integer,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<I: Integer> Sub<Coordinate<I>> for Position<I>

Source§

type Output = Position<I>

The resulting type after applying the - operator.
Source§

fn sub(self, c: Coordinate<I>) -> Position<I>

Performs the - operation. Read more
Source§

impl<I: Integer, T: Into<Coordinate<I>>> Sub<T> for Coordinate<I>

Source§

type Output = Coordinate<I>

The resulting type after applying the - operator.
Source§

fn sub(self, c: T) -> Coordinate<I>

Performs the - operation. Read more
Source§

impl<I: Copy + Integer> Copy for Coordinate<I>

Source§

impl<I: Eq + Integer> Eq for Coordinate<I>

Source§

impl<I: Integer> StructuralPartialEq for Coordinate<I>

Auto Trait Implementations§

§

impl<I> Freeze for Coordinate<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for Coordinate<I>
where I: RefUnwindSafe,

§

impl<I> Send for Coordinate<I>
where I: Send,

§

impl<I> Sync for Coordinate<I>
where I: Sync,

§

impl<I> Unpin for Coordinate<I>
where I: Unpin,

§

impl<I> UnwindSafe for Coordinate<I>
where I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,