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;

/// Returns a random number between the min and maximum.
/// # Example
/// ```
/// use pathfinder::tools;
/// let nr = tools::roll(50,100);
/// assert_eq!(nr >= 50 && nr <= 100, true);
/// ```
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
}

/// Returns a random item from a given list.
pub fn get_random_item(list: &[String]) -> &String {
    let roll = roll(0, list.len() as u32);
    &list[roll as usize]
}

/// Checks so that the applied adjustments stay within a u8.
/// # Example
/// ```
/// use pathfinder::tools;
/// let a = 50;
/// let b = 250;
/// let max = tools::border(a, b);
/// let zero = tools::border(a, -b);
/// assert_eq!(max, 255);
/// assert_eq!(zero, 0);
/// ```
pub fn border(a: u8, b: i32) -> u8 {
    let a = a as i32;

    // If it's too big.
    if a+b > 255 {
        255 as u8
        // If it's too small.
    } else if a+b < 0 {
        0 as u8
        // If it's alright.
    } else {
        (a+b) as u8
    }
}

/// Returns a random Rgb color. the opacity is always 255.
pub fn gen_rgba() -> Rgba<u8> {

    // Node
    let mut primary: Rgba<u8> = Rgba {data: [0,0,0,255]};

    // Color of the node.
    for i in 0..4 {
        let v = primary.data[i] as u32 + roll(0,255);

        // If v goes above what a u8 can take. Set it to max.
        let v2 = if v > 255 {
            255
        } else {
            v
        };

        primary.data[i] = v2 as u8;
    }

    primary
}

/// Returns a Rgb color based on a seed value. the opacity is always 255.
pub fn seed_rgba(seed: u64) -> Rgba<u8> {

    // Node
    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;
    }

    // Color of the node.
    for i in 0..4 {
        let v = primary.data[i] as u32 + roll(min, max);

        // If v goes above what a u8 can take. Set it to max.
        let v2 = if v > 255 {
            255
        } else {
            v
        };

        primary.data[i] = v2 as u8;
    }

    primary
}

/// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
/// Plots the pixels between two coordinate points.
/// Checks so that the applied adjustments stay within a u8.
/// # Example
/// ```
/// use pathfinder::tools;
/// use pathfinder::Coordinate;
/// let a = Coordinate::new(0,0);
/// let b = Coordinate::new(1,1);
/// let path = tools::plot(&a, &b);
/// let correct_path = vec!(Coordinate::new(0,0), Coordinate::new(1,0), Coordinate::new(1,1));
/// assert_eq!(path, correct_path);
/// ```
pub fn plot(coordinates1: &Coordinate, coordinates2: &Coordinate) -> Vec<Coordinate> {

    // If any of the coordinates are negative, interally add to make them positive.
    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 it's a vertical line
    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
        // If it's not a vertical line
    } else {
        plot_bresenham(x0, y0, x1, y1)
    }
}

/// Draws a line between two coordinate points. 
/// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
/// # Panics
///
/// delta_x == 0.00
fn plot_bresenham(mut x0: usize, mut y0: usize, mut x1: usize, mut y1: usize) -> Vec<Coordinate> {

    // This case is handles reversed plotting, meaning going from a larger node to a smaller one.
    if (y0 < y1 && x0 > x1) || (y0 > y1 && x0 > x1) {
        swap(&mut x0, &mut x1);
        swap(&mut y0, &mut y1);
    }

    // Convert the numbers to be isize to enable the usize values to be less than 0.
    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
}