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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#[cfg(feature = "img")]
pub mod ulamspiral_img;
#[cfg(feature = "prime")]
pub mod prime;

pub mod calc_coord;


use serde::{Deserialize, Serialize};

/// A quadrant that is useful in knowing where the x,y coordinate exist in a cartisan plan.
/// Any 2 directional value like (NorthWest) will be on a perfect diagonal (ex: x: -8, y: 8).
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum Quad {
    North,
    NorthEast,
    East,
    SouthEast,
    South,
    SouthWest,
    West,
    NorthWest,
    Center,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct UlamPoint {
    pub value: u32,
    pub quad: Quad,
    pub is_prime: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Coord {
    pub x: i32,
    pub y: i32,
}

impl Coord {
    pub fn new(x: i32, y: i32) -> Coord {
        Coord { x, y }
    }
}

#[allow(clippy::comparison_chain)]
fn quad_of_coord(c: &Coord) -> Quad {
    if c.x == 0 && c.y == 0 {
        Quad::Center
    // Checking for Cartesian Q1
    } else if c.x >= 0 && c.y >= 0 {
        // check for north shard
        if c.y > c.x {
            Quad::North
        // check for east shard
        } else if c.x > c.y {
            Quad::East
        // check for diagonal
        } else {
            Quad::NorthEast
        }
    // Checking for Cartesian Q2
    } else if c.x <= 0 && c.y >= 0 {
        // check for north shard
        if c.y > c.x.abs() {
            Quad::North
        // check for west shard
        } else if c.x.abs() > c.y {
            Quad::West
        // check for diagonal
        } else {
            Quad::NorthWest
        }
    }
    // Checking for Cartesian Q3
    else if c.x <= 0 && c.y <= 0 {
        // check for south shard
        if c.y.abs() > c.x.abs() {
            Quad::South
        // check for west shard
        } else if c.x.abs() > c.y.abs() {
            Quad::West
        // check for diagonal
        } else {
            Quad::SouthWest
        }
    }
    // Checking for Cartesian Q4
    else if c.x >= 0 && c.y <= 0 {
        // check for south shard
        if c.y.abs() > c.x {
            Quad::South
        // check for west shard
        } else if c.x > c.y.abs() {
            Quad::East
        // check for diagonal
        } else {
            Quad::SouthEast
        }
    } else {
        Quad::Center
    }
}

/// Get the value from the ulam spiral given a Quad and a Coord.
/// # Examples
/// ```
/// use ulam::{Coord, value_of_coord};
/// let c1 = Coord::new(0, 1);
/// let result = ulam::value_of_coord(&c1);    
///
/// ```
pub fn value_of_coord(c: &Coord) -> u32 {
    let q = quad_of_coord(c);
    match q {
        // n = y c = -x
        // 4n^2 - 1n + c
        Quad::North => (4 * (c.y * c.y) - c.y + (-c.x)).try_into().unwrap(),
        // n = x c = y
        // 4n^2 - 3n + c
        Quad::East => (4 * (c.x * c.x) - (3 * c.x) + c.y).try_into().unwrap(),
        // n = -y c = x
        // 4n^2 + 3n + c
        Quad::South => (4 * (-c.y * -c.y) + (3 * -c.y) + c.x) as u32,
        // n = -x c = -y
        // 4n^2 + 1n + c
        Quad::West => (4 * (-c.x * -c.x) + (-c.x) + (-c.y)) as u32,
        // 4n^2
        Quad::NorthWest => (4 * (c.x * c.x)) as u32,
        // 4n*2 - 2n
        Quad::NorthEast => (4 * (c.x * c.x) - 2 * c.x) as u32,
        // 4n^2 + 2n // putting in abs to make this work
        Quad::SouthWest => (4 * (c.x * c.x) + 2 * c.x.abs()) as u32,
        // 4n^2 + 4n
        Quad::SouthEast => (4 * (c.x * c.x) + 4 * c.x) as u32,
        // middle
        Quad::Center => 0,
    }
}

/// Get the value from the ulam spiral given a Quad and a Coord.
/// # Examples
/// ```
/// use ulam::value_of_xy;
/// let result = ulam::value_of_xy(3, 4);    
///
/// ```
pub fn value_of_xy(x: i32, y: i32) -> u32 {
    let c = Coord { x, y };
    let q = quad_of_coord(&c);
    match q {
        // n = y c = -x
        // 4n^2 - 1n + c
        Quad::North => (4 * (c.y * c.y) - c.y + (-c.x)).try_into().unwrap(),
        // n = x c = y
        // 4n^2 - 3n + c
        Quad::East => (4 * (c.x * c.x) - (3 * c.x) + c.y).try_into().unwrap(),
        // n = -y c = x
        // 4n^2 + 3n + c
        Quad::South => (4 * (-c.y * -c.y) + (3 * -c.y) + c.x) as u32,
        // n = -x c = -y
        // 4n^2 + 1n + c
        Quad::West => (4 * (-c.x * -c.x) + (-c.x) + (-c.y)) as u32,
        // 4n^2
        Quad::NorthWest => (4 * (c.x * c.x)) as u32,
        // 4n*2 - 2n
        Quad::NorthEast => (4 * (c.x * c.x) - 2 * c.x) as u32,
        // 4n^2 + 2n // putting in abs to make this work
        Quad::SouthWest => (4 * (c.x * c.x) + 2 * c.x.abs()) as u32,
        // 4n^2 + 4n
        Quad::SouthEast => (4 * (c.x * c.x) + 4 * c.x) as u32,
        // middle
        Quad::Center => 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    ///
    // value_of_coord tests
    #[test]
    fn check_n_val() {
        let c1 = Coord::new(-9, 10);

        let result = value_of_coord(&c1);
        assert_eq!(result, 399);
    }
    #[test]
    fn check_w_val() {
        let c1 = Coord::new(-6, -3);

        let result = value_of_coord(&c1);
        assert_eq!(result, 153);
    }
    #[test]
    fn check_e_val() {
        let c1 = Coord::new(8, -2);
        let result = value_of_coord(&c1);
        assert_eq!(result, 230);
    }
    #[test]
    fn check_s_val() {
        let c1 = Coord::new(0, -2);

        let result = value_of_coord(&c1);
        assert_eq!(result, 22);
    }
    #[test]
    fn check_se_val() {
        let c1 = Coord::new(9, -9);

        let result = value_of_coord(&c1);
        dbg!(result);
        assert_eq!(result, 360);
    }
    #[test]
    fn check_ne_val() {
        let c1 = Coord::new(2, 2);

        let result = value_of_coord(&c1);
        assert_eq!(result, 12);
    }
    #[test]
    fn check_nw_val() {
        let c1 = Coord::new(-3, 3);

        let result = value_of_coord(&c1);
        assert_eq!(result, 36);
    }
    #[test]
    fn check_sw_val() {
        let c1 = Coord::new(-9, -9);

        let result = value_of_coord(&c1);
        assert_eq!(result, 342);
    }
    #[test]
    fn check_e_val_big() {
        let c1 = Coord::new(400, -221);

        let result = value_of_coord(&c1);
        dbg!(result);
        assert_eq!(result, 638579);
    }
    #[test]
    fn check_w_val_big() {
        let c1 = Coord::new(-398, -129);

        let result = value_of_coord(&c1);
        assert_eq!(result, 634143);
    }
    #[test]
    fn check_s_val_big() {
        let c1 = Coord::new(-397, -996);

        let result = value_of_coord(&c1);
        assert_eq!(result, 3970655);
    }
    #[test]
    fn check_n_val_big() {
        let c1 = Coord::new(250, 999);

        let result = value_of_coord(&c1);
        assert_eq!(result, 3990755);
    }
}