hexagon_map/point/
c_point.rs

1use super::*;
2
3/// A point in axial coordinates, standard form of a hexagon map
4#[derive(Copy, Clone, Ord, PartialOrd, Eq, Hash, Serialize, Deserialize)]
5pub struct CubicPoint {
6    pub p: isize,
7    pub q: isize,
8}
9
10impl CubicPoint {
11    /// Create a new point in axial coordinates
12    pub fn new(p: isize, q: isize) -> Self {
13        Self { p, q }
14    }
15    /// Create a new point in axial coordinates from pixel coordinates
16    pub fn from_pixel(x: f64, y: f64, radius: f64) -> Self {
17        let q = (x * 3.0f64.sqrt() / 3.0 - y / 3.0) / radius;
18        let r = y * 2.0 / 3.0 / radius;
19        Self::new(q.round() as isize, r.round() as isize)
20    }
21}