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
pub mod generic;
extern crate rand;
use image::Rgba;
use super::Coordinate;
use rand::distributions::{IndependentSample, Range};
use std::cmp::{max, min};
use std::f64;
use std::mem::swap;
pub fn roll(min: u32, max: u32) -> u32 {
let mut rng = rand::thread_rng();
let between: Range<u32> = Range::new(min, max);
between.ind_sample(&mut rng) as u32
}
pub fn get_random_item(list: &[String]) -> &String {
let roll = roll(0, list.len() as u32);
&list[roll as usize]
}
pub fn border(a: u8, b: i32) -> u8 {
let a = a as i32;
if a+b > 255 {
255 as u8
} else if a+b < 0 {
0 as u8
} else {
(a+b) as u8
}
}
pub fn gen_rgba() -> Rgba<u8> {
let mut primary: Rgba<u8> = Rgba {data: [0,0,0,255]};
for i in 0..4 {
let v = primary.data[i] as u32 + roll(0,255);
let v2 = if v > 255 {
255
} else {
v
};
primary.data[i] = v2 as u8;
}
primary
}
pub fn seed_rgba(seed: u64) -> Rgba<u8> {
let mut primary: Rgba<u8> = Rgba {data: [0,0,0,255]};
let rem: u64 = 255;
let mut min: u32 = ((seed/2) % rem) as u32;
let mut max: u32 = (seed % rem) as u32;
if min > max {
swap(&mut min, &mut max);
} else if min == max {
min -=1;
}
for i in 0..4 {
let v = primary.data[i] as u32 + roll(min, max);
let v2 = if v > 255 {
255
} else {
v
};
primary.data[i] = v2 as u8;
}
primary
}
pub fn plot(coordinates1: &Coordinate, coordinates2: &Coordinate) -> Vec<Coordinate> {
if coordinates1.x < 0 || coordinates2.x < 0 || coordinates1.y < 0 || coordinates2.y < 0 {
let add_x = if coordinates1.x < coordinates2.x {
-coordinates1.x
} else {
-coordinates2.x
};
let add_y = if coordinates1.y < coordinates2.y {
-coordinates1.y
} else {
-coordinates2.y
};
let new_coordinate1 = Coordinate::new(coordinates1.x+ add_x, coordinates1.y +add_y);
let new_coordinate2 = Coordinate::new(coordinates2.x+ add_x, coordinates2.y +add_y);
let new_coordinates = plot(&new_coordinate1, &new_coordinate2);
let mut vec_final = Vec::new();
for c in new_coordinates.iter() {
vec_final.push(
Coordinate::new(c.x -add_x, c.y -add_y)
);
}
return vec_final;
}
let x0 = (coordinates1.x) as usize;
let x1 = (coordinates2.x) as usize;
let y0 = (coordinates1.y) as usize;
let y1 = (coordinates2.y) as usize;
if coordinates1.x == coordinates2.x {
let mut vec = Vec::new();
for y in min(coordinates1.y,coordinates2.y)..max(coordinates1.y,coordinates2.y) {
vec.push(Coordinate::new(coordinates1.x, y));
}
vec
} else {
plot_bresenham(x0, y0, x1, y1)
}
}
fn plot_bresenham(mut x0: usize, mut y0: usize, mut x1: usize, mut y1: usize) -> Vec<Coordinate> {
if (y0 < y1 && x0 > x1) || (y0 > y1 && x0 > x1) {
swap(&mut x0, &mut x1);
swap(&mut y0, &mut y1);
}
let delta_x: f64 = (x1 as isize - x0 as isize) as f64;
let delta_y: f64 = (y1 as isize - y0 as isize) as f64;
if delta_x == 0.00 {
panic!("Bresenham does not support straight vertical lines!");
}
let delta_err: f64 = (delta_y / delta_x).abs();
let mut error: f64 = 0.00;
let mut y: i16 = y0 as i16;
let mut plot: Vec<Coordinate> = Vec::new();
let mut last_y = y;
for x in min(x0, x1)..max(x0, x1)+1 {
for i in min(last_y, y)..max(last_y, y)+1 {
plot.push(Coordinate::new(x as i16, i));
}
last_y = y;
error += delta_err;
while error >= 0.50 {
y += f64::signum(delta_y) as i16;
error -= 1.00;
}
}
plot
}