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
extern crate rand;
extern crate pythagoras;
use std::cmp::Ordering;
use std::f64;
use super::super::tools::roll;
use super::super::Coordinate;
use std::ops::Add;
pub fn from_list<T>(list: &[(i16, i16)], get: &Fn(Coordinate, usize) -> T) -> Vec<T> {
let mut result: Vec<T> = Vec::new();
for (i, &(x,y)) in list.iter().enumerate() {
result.push(get(Coordinate::new(x, y), i));
}
result
}
pub fn gen() -> Coordinate {
Coordinate {
x: rand::random::<i16>(),
y: rand::random::<i16>(),
}
}
pub fn diff(c1: Coordinate, c2: Coordinate) -> (i16, i16) {
((c1.x - c2.x).abs(), (c1.y - c2.y).abs())
}
pub fn distance(a: Coordinate, b: Coordinate) -> u32 {
let diff = diff(a, b);
pythagoras::theorem(diff.0, diff.1) as u32
}
pub fn gen_within_radius(coord: Coordinate, radius: u32) -> Coordinate {
gen_radius(coord, 0, radius)
}
pub fn gen_radius(coord: Coordinate, min: u32, max: u32) -> Coordinate {
let r = f64::from(roll(min, max));
let circle = |a: f64, b: f64| a + r * b;
let angle = roll(0, 3600);
let a: f64 = f64::consts::PI * 0.001 * f64::from(angle);
let x = circle(f64::from(coord.x), a.cos()) as i16;
let y = circle(f64::from(coord.y), a.sin()) as i16;
Coordinate {
x,
y
}
}
impl Ord for Coordinate {
fn cmp(&self, other: &Coordinate) -> Ordering {
(self.x + self.y).cmp(&(other.x + other.y))
}
}
impl PartialOrd for Coordinate {
fn partial_cmp(&self, other: &Coordinate) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Coordinate {
fn eq(&self, other: &Coordinate) -> bool {
(self.x == other.x) && (self.y == other.y)
}
}
impl Add for Coordinate {
type Output = Coordinate;
fn add(self, other: Coordinate) -> Coordinate {
Coordinate::new(self.x + other.x, self.y + other.y)
}
}