rust/integral-geometry/src/lib.rs
author unc0rr
Sat, 20 Oct 2018 13:22:30 +0200
changeset 13950 48796bef9e69
parent 13947 85645992bc8a
child 14031 c47283feafac
child 14051 8a0d69c16cad
permissions -rw-r--r--
Add --protocol option to engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
     1
use std::cmp;
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
     2
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     3
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     4
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     5
pub struct Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     6
    pub x: i32,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     7
    pub y: i32,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     8
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
     9
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    10
impl Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    11
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    12
    pub fn new(x: i32, y: i32) -> Self {
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
    13
        Self { x, y }
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    14
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    15
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    16
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    17
    pub fn zero() -> Self {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    18
        Self::new(0, 0)
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    19
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    20
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    21
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    22
    pub fn signum(self) -> Self {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    23
        Self::new(self.x.signum(), self.y.signum())
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    24
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    25
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    26
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    27
    pub fn abs(self) -> Self {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    28
        Self::new(self.x.abs(), self.y.abs())
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    29
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    30
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    31
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    32
    pub fn dot(self, other: Point) -> i32 {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    33
        self.x * other.x + self.y * other.y
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    34
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    35
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    36
    #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    37
    pub fn max_norm(self) -> i32 {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    38
        std::cmp::max(self.x.abs(), self.y.abs())
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    39
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    40
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    41
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    42
macro_rules! bin_op_impl {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    43
    ($op: ty, $name: tt) => {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    44
        impl $op for Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    45
            type Output = Self;
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    46
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    47
            #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    48
            fn $name(self, rhs: Self) -> Self::Output {
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
    49
                Self::new(self.x.$name(rhs.x), self.y.$name(rhs.y))
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    50
            }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    51
        }
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
    52
    };
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    53
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    54
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    55
macro_rules! bin_assign_op_impl {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    56
    ($op: ty, $name: tt) => {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    57
        impl $op for Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    58
            #[inline]
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
    59
            fn $name(&mut self, rhs: Self) {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    60
                self.x.$name(rhs.x);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    61
                self.y.$name(rhs.y);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    62
            }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    63
        }
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
    64
    };
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    65
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    66
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    67
bin_op_impl!(Add, add);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    68
bin_op_impl!(Sub, sub);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    69
bin_op_impl!(Mul, mul);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    70
bin_op_impl!(Div, div);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    71
bin_assign_op_impl!(AddAssign, add_assign);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    72
bin_assign_op_impl!(SubAssign, sub_assign);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    73
bin_assign_op_impl!(MulAssign, mul_assign);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    74
bin_assign_op_impl!(DivAssign, div_assign);
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    75
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    76
pub struct LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    77
    accumulator: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    78
    direction: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    79
    sign: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    80
    current: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    81
    total_steps: i32,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    82
    step: i32,
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    83
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    84
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    85
impl LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    86
    pub fn new(from: Point, to: Point) -> Self {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    87
        let dir = to - from;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    88
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    89
        Self {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    90
            accumulator: Point::zero(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    91
            direction: dir.abs(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    92
            sign: dir.signum(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    93
            current: from,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    94
            total_steps: dir.max_norm(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
    95
            step: 0,
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    96
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    97
    }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    98
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
    99
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   100
impl Iterator for LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   101
    type Item = Point;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   102
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   103
    fn next(&mut self) -> Option<Self::Item> {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   104
        if self.step <= self.total_steps {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   105
            self.accumulator += self.direction;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   106
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   107
            if self.accumulator.x > self.total_steps {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   108
                self.accumulator.x -= self.total_steps;
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   109
                self.current.x += self.sign.x;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   110
            }
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   111
            if self.accumulator.y > self.total_steps {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   112
                self.accumulator.y -= self.total_steps;
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   113
                self.current.y += self.sign.y;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   114
            }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   115
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   116
            self.step += 1;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   117
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   118
            Some(self.current)
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   119
        } else {
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   120
            None
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   121
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   122
    }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   123
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   124
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   125
pub struct ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   126
    point: Point,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   127
    step: i32,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   128
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   129
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   130
impl ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   131
    pub fn new(radius: i32) -> Self {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   132
        Self {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   133
            point: Point::new(0, radius),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   134
            step: 3 - 2 * radius,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   135
        }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   136
    }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   137
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   138
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   139
impl Iterator for ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   140
    type Item = Point;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   141
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   142
    fn next(&mut self) -> Option<Self::Item> {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   143
        if self.point.x < self.point.y {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   144
            let result = self.point;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   145
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   146
            if self.step < 0 {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   147
                self.step += self.point.x * 4 + 6;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   148
            } else {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   149
                self.step += (self.point.x - self.point.y) * 4 + 10;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   150
                self.point.y -= 1;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   151
            }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   152
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   153
            self.point.x += 1;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   154
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   155
            Some(result)
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   156
        } else if self.point.x == self.point.y {
13947
85645992bc8a Fix ArcPoints never finishing
unc0rr
parents: 13943
diff changeset
   157
            self.point.x += 1;
13950
48796bef9e69 Add --protocol option to engine
unc0rr
parents: 13947
diff changeset
   158
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   159
            Some(self.point)
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   160
        } else {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   161
            None
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   162
        }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   163
    }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   164
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   165
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   166
pub struct EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   167
    vector: Point,
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   168
    iteration: u8,
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   169
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   170
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   171
impl EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   172
    pub fn new(vector: Point) -> Self {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   173
        Self {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   174
            vector,
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   175
            iteration: if vector.x == vector.y { 4 } else { 8 },
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   176
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   177
    }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   178
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   179
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   180
impl Iterator for EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   181
    type Item = Point;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   182
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   183
    fn next(&mut self) -> Option<Self::Item> {
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   184
        if self.iteration > 0 {
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   185
            self.vector.x = -self.vector.x;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   186
            if self.iteration & 1 == 0 {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   187
                self.vector.y = -self.vector.y;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   188
            }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   189
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   190
            if self.iteration == 4 {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   191
                std::mem::swap(&mut self.vector.x, &mut self.vector.y);
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   192
            }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   193
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   194
            self.iteration -= 1;
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   195
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   196
            Some(self.vector)
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   197
        } else {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   198
            None
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   199
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   200
    }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   201
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   202
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   203
#[cfg(test)]
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   204
mod tests {
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   205
    use super::*;
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   206
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   207
    fn get_points(coords: &[(i32, i32)]) -> Vec<Point> {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   208
        coords.iter().map(|(x, y)| Point::new(*x, *y)).collect()
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   209
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   210
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   211
    #[test]
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   212
    fn line_basic() {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   213
        let line = LinePoints::new(Point::new(0, 0), Point::new(3, 3));
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   214
        let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3), (123, 456)]);
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   215
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   216
        for (&a, b) in v.iter().zip(line) {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   217
            assert_eq!(a, b);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   218
        }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   219
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   220
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   221
    #[test]
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   222
    fn line_skewed() {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   223
        let line = LinePoints::new(Point::new(0, 0), Point::new(5, -7));
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   224
        let v = get_points(&[
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   225
            (0, 0),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   226
            (1, -1),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   227
            (2, -2),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   228
            (2, -3),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   229
            (3, -4),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   230
            (4, -5),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   231
            (4, -6),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   232
            (5, -7),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   233
        ]);
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   234
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   235
        for (&a, b) in v.iter().zip(line) {
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   236
            assert_eq!(a, b);
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   237
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   238
    }
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   239
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   240
    #[test]
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   241
    fn equidistant_full() {
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   242
        let n = EquidistantPoints::new(Point::new(1, 3));
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   243
        let v = get_points(&[
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   244
            (-1, -3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   245
            (1, -3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   246
            (-1, 3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   247
            (1, 3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   248
            (-3, -1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   249
            (3, -1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   250
            (-3, 1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   251
            (3, 1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   252
            (123, 456),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   253
        ]);
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   254
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   255
        for (&a, b) in v.iter().zip(n) {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   256
            assert_eq!(a, b);
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   257
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   258
    }
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   259
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   260
    #[test]
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   261
    fn equidistant_half() {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   262
        let n = EquidistantPoints::new(Point::new(2, 2));
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   263
        let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2), (123, 456)]);
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   264
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   265
        for (&a, b) in v.iter().zip(n) {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   266
            assert_eq!(a, b);
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   267
        }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   268
    }
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   269
}