1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::f32;

/**
 * Width/Height of a pointy top hexagon
 */
#[derive(Copy, Clone, Debug)]
pub struct HexSize {
    width: f32,
    height: f32,
}

impl HexSize {
    /**
     * Calculate the size of a regular hexagon from the width
     */
    pub fn from_regular_width(width: f32) -> HexSize {
        HexSize {
            width,
            height: 2.0 * width / 3.0_f32.sqrt(),
        }
    }
    /**
     * Calculate the size of a regular hexagon from the height
     */
    pub fn from_regular_height(height: f32) -> HexSize {
        HexSize {
            width: (height / 2.0) * 3.0_f32.sqrt(),
            height,
        }
    }

    pub fn new_irregular(width: f32, height: f32) -> HexSize {
        HexSize { width, height }
    }

    pub fn width(&self) -> f32 {
        self.width
    }
    pub fn height(&self) -> f32 {
        self.height
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    pub fn test_from_width() {
        let hex_size = HexSize::from_regular_width(1.0);
        assert_eq!(hex_size.width(), 1.0);
        assert_eq!(hex_size.height(), 1.1547005);
    }

    #[test]
    pub fn test_from_height() {
        let hex_size = HexSize::from_regular_height(1.0);
        assert_eq!(hex_size.width(), 0.8660254);
        assert_eq!(hex_size.height(), 1.0);
    }
}