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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
extern crate image;
extern crate rand;
extern crate gif;
pub mod node;
pub mod map;
pub mod tools;
pub mod group;
pub mod data;
mod tests;
#[derive(Debug, Eq, Copy, Clone)]
pub struct Coordinate {
pub x: i16,
pub y: i16,
}
#[derive(Clone)]
pub struct Node<T: Shape> {
pub hash: u64,
pub geo: Coordinate,
pub color: image::Rgba<u8>,
pub radius: Option<u32>,
shape: T
}
#[derive(Clone)]
pub struct Group<T: Shape> {
pub settings: Node<T>,
pub nodes: Vec<Node<T>>,
}
pub struct Map {
pub image: Option<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>>,
pub add: (i16, i16),
pub size: u32,
}
pub struct Link<'a> {
pub from: &'a Coordinate,
pub to: &'a Coordinate,
pub color: image::Rgba<u8>,
}
pub trait Shape {
fn new() -> Self;
fn area(&self, size: u32) -> Vec<Coordinate>;
}
#[derive(Debug)]
pub struct Square {}
#[derive(Debug)]
pub struct Circle {}
#[derive(Debug)]
pub struct Triangle {}
pub trait Draw {
fn draw(&self, mut image: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, x_offset: i16, y_offset: i16, size: u32) ->
image::ImageBuffer<image::Rgba<u8>, Vec<u8>>;
fn get_size(&self) -> u32;
fn get_coordinate(&self) -> Vec<&Coordinate>;
}
impl<T: Shape> Draw for Node<T> {
fn draw(&self, mut image: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, x_offset: i16, y_offset: i16, size: u32) ->
image::ImageBuffer<image::Rgba<u8>, Vec<u8>> {
let x = self.geo.x +x_offset as i16;
let y = self.geo.y +y_offset as i16;
let size = match self.radius {
Some(_) => self.radius.unwrap(),
None => size
};
for offset in self.shape.area(size) {
image.put_pixel((x +offset.x) as u32, (y +offset.y) as u32, self.color);
}
image
}
fn get_size(&self) -> u32 {
match self.radius.is_none() {
true => 4,
false => self.radius.unwrap(),
}
}
fn get_coordinate(&self) -> Vec<&Coordinate> {
vec!(&self.geo)
}
}
impl<T: Shape> Draw for Group<T> {
fn draw(&self, mut image: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, x_offset: i16, y_offset: i16, size: u32) ->
image::ImageBuffer<image::Rgba<u8>, Vec<u8>> {
for node in self.nodes.iter() {
image = node.draw(image, x_offset, y_offset, size);
}
image
}
fn get_size(&self) -> u32 {
let mut max = 0;
for node in self.nodes.iter() {
let tmp = node.get_size();
if tmp > max {
max = tmp;
}
}
match self.settings.radius {
Some(e) => max + e,
None => max,
}
}
fn get_coordinate(&self) -> Vec<&Coordinate> {
let mut vec = Vec::new();
for item in self.nodes.iter() {
vec.append(&mut item.get_coordinate())
}
vec
}
}
impl<'a> Draw for Link<'a> {
fn draw(&self, mut image: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, x_offset: i16, y_offset: i16, size: u32) ->
image::ImageBuffer<image::Rgba<u8>, Vec<u8>> {
let x_offset = x_offset + (size/2) as i16;
let y_offset = y_offset + (size/2) as i16;
let a = Coordinate::new(
self.from.x +x_offset,
self.from.y +y_offset
);
let b = Coordinate::new(
self.to.x +x_offset,
self.to.y +y_offset
);
tools::plot(&a, &b).iter().map(|c|
image.put_pixel( c.x as u32, c.y as u32, self.color)
).collect::<Vec<_>>();
image
}
fn get_size(&self) -> u32 {
1
}
fn get_coordinate(&self) -> Vec<&Coordinate> {
vec!(&self.from, &self.from)
}
}
impl Shape for Square {
fn new() -> Square {
Square {}
}
fn area(&self, size: u32) -> Vec<Coordinate> {
let mut vec = Vec::new();
for i in 0..size {
for j in 0..size {
vec.push(Coordinate::new(i as i16, j as i16));
}
}
vec
}
}
impl Shape for Circle {
fn new() -> Circle {
Circle {}
}
fn area(&self, size: u32) -> Vec<Coordinate> {
let mut vec = Vec::new();
let mut x: i16 = (size-1) as i16;
let mut y: i16 = 0;
let mut dx: i16 = 1;
let mut dy: i16 = 1;
let x0: i16 = 0;
let y0: i16 = 0;
let mut err: i16 = dx - (size << 1) as i16;
while x >= y {
vec.append(&mut tools::plot(&Coordinate::new(x0 + x, y0 + y), &Coordinate::new(x0 - x, y0 + y)));
vec.append(&mut tools::plot(&Coordinate::new(x0 + x, y0 - y), &Coordinate::new(x0 - x, y0 - y)));
vec.append(&mut tools::plot(&Coordinate::new(x0 - y, y0 - x), &Coordinate::new(x0 - y, y0 + x)));
vec.append(&mut tools::plot(&Coordinate::new(x0 + y, y0 - x), &Coordinate::new(x0 + y, y0 + x)));
if err <= 0 {
y += 1;
err += dy;
dy += 2;
} else {
x -= 1;
dx += 2;
err += dx - (size << 1) as i16;
}
}
vec
}
}
impl Shape for Triangle {
fn new() -> Triangle {
Triangle {}
}
fn area(&self, size: u32) -> Vec<Coordinate> {
let mut vec = Vec::new();
let size = size as i16;
let start_x = size/2;
for i in 0..size {
vec.append(&mut tools::plot(&Coordinate::new(start_x,0), &Coordinate::new(i, size)));
}
vec
}
}
impl Coordinate {
pub fn new(x: i16, y: i16) -> Coordinate {
Coordinate {
x,
y
}
}
}
impl<T: Shape> Node<T> {
pub fn new(name: &str, geo: Coordinate) -> Node<T> {
Node {
hash: data::calculate_hash(&name),
geo,
color: image::Rgba {data: [0,0,0,255]},
radius: None,
shape: T::new(),
}
}
}
impl<T: Shape> Group<T> {
pub fn new(name: &str, coordinates: Coordinate) -> Group<T> {
Group {
settings: Node::new(name, coordinates),
nodes: Vec::new(),
}
}
}
impl<'a> Link<'a> {
pub fn new(from: &'a Coordinate, to: &'a Coordinate) -> Link<'a> {
Link {
from,
to,
color: image::Rgba {data: [0,0,0,255]},
}
}
}
impl Coordinate {
pub fn diff(&self, other: &Coordinate) -> (i16, i16) {
node::coordinates::diff(&self, other)
}
}
impl<T: Shape> Group<T> {
pub fn get_nodes(&self) -> &Vec<Node<T>> {
&self.nodes
}
pub fn new_node(&mut self, name: &str) {
let geo = node::coordinates::gen_radius(&self.settings.geo, 0, self.get_dynamic_radius());
self.new_node_inner(geo, name);
}
pub fn new_node_min_auto(&mut self, name: &str, min: u32) -> &Node<T> {
let geo = node::coordinates::gen_radius(&self.settings.geo, 0, min+5);
self.new_node_inner(geo, name)
}
pub fn new_node_min_max(&mut self, name: &str, min: u32, max: u32) -> &Node<T> {
let geo = node::coordinates::gen_radius(&self.settings.geo, min, max);
self.new_node_inner(geo, name)
}
pub fn new_node_inner(&mut self, geo: Coordinate, name: &str) -> &Node<T> {
let mut node = Node::new(name,geo.clone());
node.color = self.gen_color(geo);
node.radius = self.settings.radius;
self.push(node);
&self.nodes.get(self.nodes.len() -1).unwrap()
}
pub fn new_simple(x: i16, y: i16) -> Group<T> {
Group::new("", Coordinate::new(x, y))
}
pub fn push(&mut self, node: Node<T>) {
self.nodes.push(node);
}
pub fn get_dynamic_radius(&self) -> u32 {
match self.settings.radius {
Some(x) => x,
None => 7 + self.nodes.len()as u32 /2,
}
}
pub fn gen_color(&self, coordinates: Coordinate) -> image::Rgba<u8> {
let radius = self.get_dynamic_radius() as i16;
let (x_dif, y_dif) = self.settings.geo.diff(&coordinates);
let x_scale: f64 = (x_dif as f64/radius as f64) as f64;
let y_scale: f64 = (y_dif as f64/radius as f64) as f64;
let c = self.settings.color.data;
let max_multi: f64 = ((c[0] as i32 + c[1] as i32 + c[2] as i32)/3) as f64;
let modify = (-max_multi*(x_scale+y_scale)/2.0) as i32;
image::Rgba {data: [
tools::border(c[0], modify),
tools::border(c[1], modify),
tools::border(c[2], modify),
tools::border(c[3], 0)
]}
}
}
impl Map {
pub fn new() -> Map {
Map {
image: None,
add: (0, 0),
size: 5,
}
}
pub fn map<T: Draw>(mut self, element: &[T]) -> Self {
if self.image.is_none() {
let min_max = map::min_max(&element);
self.add = map::gen_stuff(min_max);
let res = map::gen_map_dimensions(min_max);
self.image = Some(map::gen_canvas(res.0, res.1));
}
for e in element {
self.image = Some(e.draw(
self.image.unwrap(),
self.add.0,
self.add.1,
self.size,
));
}
self
}
}
impl<'a> PartialEq for Link<'a> {
fn eq(&self, other: &Link) -> bool {
(self.from == other.from) &&
(self.to == other.to)
}
}