rust/integral-geometry/src/lib.rs
author alfadur
Thu, 01 Nov 2018 21:30:21 +0300
changeset 14059 c6745a1c827a
parent 14054 3185fb34f3b5
child 14076 e5904ead4864
permissions -rw-r--r--
start a physics engine to try out this data oriented thing everyone seems to be talking about
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
    }
14031
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    40
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    41
    #[inline]
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    42
    pub fn transform(self, matrix: &[i32; 4]) -> Self {
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    43
        Point::new(matrix[0] * self.x + matrix[1] * self.y,
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    44
                   matrix[2] * self.x + matrix[3] * self.y)
c47283feafac add circle filling to land2d
alfadur
parents: 13950
diff changeset
    45
    }
13938
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
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    48
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    49
pub struct Size {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    50
    pub width: usize,
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    51
    pub height: usize,
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    52
}
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    53
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    54
impl Size {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    55
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    56
    pub fn new(width: usize, height: usize) -> Self {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    57
        Size { width, height }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    58
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    59
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    60
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    61
    pub fn square(size: usize) -> Self {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    62
        Size { width: size, height: size }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    63
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    64
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    65
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    66
    pub fn area(&self) -> usize {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    67
        self.width * self.height
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    68
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    69
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    70
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    71
    pub fn linear_index(&self, x: usize, y: usize) -> usize {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    72
        y * self.width + x
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    73
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    74
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    75
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    76
    pub fn is_power_of_two(&self) -> bool {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    77
        self.width.is_power_of_two() && self.height.is_power_of_two()
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    78
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    79
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    80
    #[inline]
14052
alfadur
parents: 14051 14032
diff changeset
    81
    pub fn next_power_of_two(&self) -> Self {
alfadur
parents: 14051 14032
diff changeset
    82
        Self {
alfadur
parents: 14051 14032
diff changeset
    83
            width: self.width.next_power_of_two(),
alfadur
parents: 14051 14032
diff changeset
    84
            height: self.height.next_power_of_two()
alfadur
parents: 14051 14032
diff changeset
    85
        }
alfadur
parents: 14051 14032
diff changeset
    86
    }
alfadur
parents: 14051 14032
diff changeset
    87
alfadur
parents: 14051 14032
diff changeset
    88
    #[inline]
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    89
    pub fn to_mask(&self) -> SizeMask {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    90
        SizeMask::new(*self)
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    91
    }
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
    92
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
    93
    pub fn to_grid_index(&self) -> GridIndex {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
    94
        GridIndex::new(*self)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
    95
    }
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    96
}
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    97
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    98
pub struct SizeMask{ size: Size }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
    99
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   100
impl SizeMask {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   101
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   102
    pub fn new(size: Size) -> Self {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   103
        assert!(size.is_power_of_two());
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   104
        let size = Size {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   105
            width: !(size.width - 1),
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   106
            height: !(size.height - 1)
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   107
        };
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   108
        Self { size }
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   109
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   110
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   111
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   112
    pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   113
        (self.size.width & x.into()) == 0
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   114
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   115
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   116
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   117
    pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   118
        (self.size.height & y.into()) == 0
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   119
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   120
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   121
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   122
    pub fn contains(&self, point: Point) -> bool {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   123
        self.contains_x(point.x as usize) && self.contains_y(point.y as usize)
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14031
diff changeset
   124
    }
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   125
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   126
14059
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   127
pub struct GridIndex{ shift: Point }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   128
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   129
impl GridIndex {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   130
    pub fn new(size: Size) -> Self {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   131
        assert!(size.is_power_of_two());
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   132
        let shift = Point::new(size.width.trailing_zeros() as i32,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   133
                               size.height.trailing_zeros() as i32);
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   134
        Self { shift }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   135
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   136
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   137
    pub fn map(&self, position: Point) -> Point {
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   138
        Point::new(position.x >> self.shift.x,
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   139
                   position.y >> self.shift.y)
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   140
    }
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   141
}
c6745a1c827a start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents: 14054
diff changeset
   142
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   143
macro_rules! bin_op_impl {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   144
    ($op: ty, $name: tt) => {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   145
        impl $op for Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   146
            type Output = Self;
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   147
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   148
            #[inline]
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   149
            fn $name(self, rhs: Self) -> Self::Output {
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   150
                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
   151
            }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   152
        }
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   153
    };
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   154
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   155
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   156
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
   157
    ($op: ty, $name: tt) => {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   158
        impl $op for Point {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   159
            #[inline]
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   160
            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
   161
                self.x.$name(rhs.x);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   162
                self.y.$name(rhs.y);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   163
            }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   164
        }
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   165
    };
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   166
}
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   167
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   168
bin_op_impl!(Add, add);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   169
bin_op_impl!(Sub, sub);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   170
bin_op_impl!(Mul, mul);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   171
bin_op_impl!(Div, div);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   172
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
   173
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
   174
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
   175
bin_assign_op_impl!(DivAssign, div_assign);
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   176
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   177
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   178
pub struct Rect {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   179
    pub x: i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   180
    pub y: i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   181
    pub width: u32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   182
    pub height: u32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   183
}
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   184
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   185
impl Rect {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   186
    #[inline]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   187
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   188
        Self { x, y, width, height }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   189
    }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   190
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   191
    #[inline]
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   192
    pub fn size(&self) -> Size {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   193
        Size::new(self.width as usize, self.height as usize)
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   194
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   195
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   196
    #[inline]
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   197
    pub fn area(&self) -> usize {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   198
        self.size().area()
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   199
    }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   200
}
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   201
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 13950
diff changeset
   202
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   203
pub struct LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   204
    accumulator: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   205
    direction: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   206
    sign: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   207
    current: Point,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   208
    total_steps: i32,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   209
    step: i32,
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   210
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   211
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   212
impl LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   213
    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
   214
        let dir = to - from;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   215
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   216
        Self {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   217
            accumulator: Point::zero(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   218
            direction: dir.abs(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   219
            sign: dir.signum(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   220
            current: from,
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   221
            total_steps: dir.max_norm(),
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   222
            step: 0,
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   223
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   224
    }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   225
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   226
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   227
impl Iterator for LinePoints {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   228
    type Item = Point;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   229
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   230
    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
   231
        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
   232
            self.accumulator += self.direction;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   233
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   234
            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
   235
                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
   236
                self.current.x += self.sign.x;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   237
            }
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   238
            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
   239
                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
   240
                self.current.y += self.sign.y;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   241
            }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   242
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   243
            self.step += 1;
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   244
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   245
            Some(self.current)
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   246
        } else {
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   247
            None
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   248
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   249
    }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   250
}
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   251
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   252
pub struct ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   253
    point: Point,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   254
    step: i32,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   255
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   256
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   257
impl ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   258
    pub fn new(radius: i32) -> Self {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   259
        Self {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   260
            point: Point::new(0, radius),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   261
            step: 3 - 2 * radius,
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   262
        }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   263
    }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   264
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   265
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   266
impl Iterator for ArcPoints {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   267
    type Item = Point;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   268
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   269
    fn next(&mut self) -> Option<Self::Item> {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   270
        if self.point.x < self.point.y {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   271
            let result = self.point;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   272
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   273
            if self.step < 0 {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   274
                self.step += self.point.x * 4 + 6;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   275
            } else {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   276
                self.step += (self.point.x - self.point.y) * 4 + 10;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   277
                self.point.y -= 1;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   278
            }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   279
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   280
            self.point.x += 1;
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   281
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   282
            Some(result)
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   283
        } else if self.point.x == self.point.y {
13947
85645992bc8a Fix ArcPoints never finishing
unc0rr
parents: 13943
diff changeset
   284
            self.point.x += 1;
13950
48796bef9e69 Add --protocol option to engine
unc0rr
parents: 13947
diff changeset
   285
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   286
            Some(self.point)
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   287
        } else {
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   288
            None
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   289
        }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   290
    }
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   291
}
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   292
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   293
pub struct EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   294
    vector: Point,
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   295
    iteration: u8,
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   296
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   297
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   298
impl EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   299
    pub fn new(vector: Point) -> Self {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   300
        Self {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   301
            vector,
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   302
            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
   303
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   304
    }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   305
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   306
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   307
impl Iterator for EquidistantPoints {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   308
    type Item = Point;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   309
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   310
    fn next(&mut self) -> Option<Self::Item> {
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   311
        if self.iteration > 0 {
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   312
            self.vector.x = -self.vector.x;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   313
            if self.iteration & 1 == 0 {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   314
                self.vector.y = -self.vector.y;
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   315
            }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   316
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   317
            if self.iteration == 4 {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   318
                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
   319
            }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   320
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   321
            self.iteration -= 1;
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   322
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   323
            Some(self.vector)
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   324
        } else {
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   325
            None
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   326
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   327
    }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   328
}
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   329
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   330
#[cfg(test)]
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   331
mod tests {
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   332
    use super::*;
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   333
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   334
    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
   335
        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
   336
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   337
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   338
    #[test]
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   339
    fn line_basic() {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   340
        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
   341
        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
   342
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   343
        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
   344
            assert_eq!(a, b);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   345
        }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   346
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   347
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   348
    #[test]
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   349
    fn line_skewed() {
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   350
        let line = LinePoints::new(Point::new(0, 0), Point::new(5, -7));
13941
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   351
        let v = get_points(&[
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   352
            (0, 0),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   353
            (1, -1),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   354
            (2, -2),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   355
            (2, -3),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   356
            (3, -4),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   357
            (4, -5),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   358
            (4, -6),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   359
            (5, -7),
3f69a70063a5 Implement ArcPoints iterator for circles
unc0rr
parents: 13938
diff changeset
   360
        ]);
13938
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   361
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13937
diff changeset
   362
        for (&a, b) in v.iter().zip(line) {
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   363
            assert_eq!(a, b);
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   364
        }
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   365
    }
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   366
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   367
    #[test]
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   368
    fn equidistant_full() {
13942
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   369
        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
   370
        let v = get_points(&[
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   371
            (-1, -3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   372
            (1, -3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   373
            (-1, 3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   374
            (1, 3),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   375
            (-3, -1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   376
            (3, -1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   377
            (-3, 1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   378
            (3, 1),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   379
            (123, 456),
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   380
        ]);
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   381
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   382
        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
   383
            assert_eq!(a, b);
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   384
        }
7e7a03e85ac4 Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents: 13941
diff changeset
   385
    }
13943
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   386
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   387
    #[test]
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   388
    fn equidistant_half() {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   389
        let n = EquidistantPoints::new(Point::new(2, 2));
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   390
        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
   391
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   392
        for (&a, b) in v.iter().zip(n) {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   393
            assert_eq!(a, b);
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   394
        }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13942
diff changeset
   395
    }
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff changeset
   396
}