summaryrefslogtreecommitdiff
path: root/check-if-it-is-a-straight-line/src/main.rs
blob: 711e12e12baa687e75219189bc69b5cc16701af3 (plain)
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
fn main() {
    let tests = [
        (vec![[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]], true),
        (vec![[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]], false),
        (vec![[0,0],[0,1],[0,-1]], true)
    ];

    for test in tests {
        println!("{:?} is {} should be {}", 
                 test.0, 
                 check_straight_line(test.0.iter().map(|c| c.to_vec()).collect()), 
                 test.1);
    }
}

pub fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
    let base = get_lin_fn(coordinates[0].clone(), coordinates[1].clone());
    println!("{:?} base", base);
    for c in 1..coordinates.len() {
        let lin_fn = get_lin_fn(coordinates[0].clone(), coordinates[c].clone());
        println!("{} {:?}", c, lin_fn);
        if lin_fn != base {
            return false;
        }
    }
    return true;
}

pub fn get_lin_fn(a: Vec<i32>, b: Vec<i32>) -> (f32, f32) {
    let mx = b[0] - a[0];
    let my = b[1] - a[1];
    let m = my as f32/mx as f32;
    let b = a[1] as f32 - m*a[0] as f32;

    return (m,b);
}