1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::concepts::*;
use crate::isotropy::*;
use crate::prelude as types;
use crate::prelude::{IntoEdges, Translate, Scale, CoordinateType};
pub struct I32Coordinates;
impl CoordinateBase for I32Coordinates {
type Coord = i32;
}
impl CoordinateConcept for I32Coordinates {
type Area = f64;
type ManhattanArea = i64;
type UnsignedArea = f64;
type CoordinateDifference = i32;
type CoordinateDistance = f64;
}
impl<C> PointBase<C> for types::Point<C::Coord>
where C: CoordinateBase {
fn new(x: C::Coord, y: C::Coord) -> Self {
types::Point::new(x, y)
}
fn get(&self, orient: Orientation2D) -> C::Coord {
match orient {
Orientation2D::Horizontal => self.x,
Orientation2D::Vertical => self.y
}
}
fn set(&mut self, orient: Orientation2D, value: C::Coord) {
let c = match orient {
Orientation2D::Horizontal => &mut self.x,
Orientation2D::Vertical => &mut self.y,
};
*c = value
}
}
impl<C> PointConcept<C> for types::Point<C::Coord>
where C: CoordinateConcept {}
impl<C> Segment<C> for types::REdge<C::Coord>
where C: CoordinateConcept {
type Point = types::Point<C::Coord>;
fn get_point(&self, dir: Direction1D) -> Self::Point {
match dir {
Direction1D::Low => self.start(),
Direction1D::High => self.end()
}
}
}
impl<C> Interval<C> for types::Interval<C>
where C: CoordinateType {
fn get(&self, d: Direction1D) -> C {
match d {
Direction1D::Low => self.start(),
Direction1D::High => self.end()
}
}
}
impl<C> Rectangle<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
type Interval = types::Interval<C::Coord>;
fn get(&self, orientation: Orientation2D) -> Self::Interval {
let (start, end) = match orientation {
Orientation2D::Horizontal => (self.lower_left().x, self.upper_right().x),
Orientation2D::Vertical => (self.lower_left().y, self.upper_right().y)
};
types::Interval::new(start, end)
}
}
impl<C> Polygon90<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
type CompactIterator = std::vec::IntoIter<C::Coord>;
fn compact_iter(&self) -> Self::CompactIterator {
vec![self.lower_left.x, self.lower_left.y, self.upper_right.x, self.upper_right.y]
.into_iter()
}
}
impl<C> Polygon<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
fn set(&mut self, iter: ()) {
unimplemented!()
}
}
impl<C> PolygonWithHoles<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
fn num_holes(&self) -> usize {
0
}
}
impl<C> Polygon90WithHoles<C> for types::Rect<C::Coord>
where C: CoordinateConcept {}
impl<C> Polygon90Set<C> for types::Rect<C::Coord>
where C: CoordinateConcept {}
impl<C> IntoSegments<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
type Segment = types::REdge<C::Coord>;
type SegmentIter = types::RectEdgeIterator<C::Coord>;
fn into_segments(self) -> Self::SegmentIter {
self.into_edges()
}
}
impl<C> IntoPoints<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
type Point = types::Point<C::Coord>;
type PointIter = <Self as IntoIterator>::IntoIter;
fn into_points(self) -> Self::PointIter {
self.into_iter()
}
}
impl<C> PolygonSet<C> for types::Rect<C::Coord>
where C: CoordinateConcept {
type Point = types::Point<C::Coord>;
type Segment = types::REdge<C::Coord>;
type AllPoints = <Self as IntoIterator>::IntoIter;
fn num_polygons(&self) -> usize {
1
}
fn convolved(mut self, p: &Self::Point) -> Self {
todo!()
}
fn convolve(&mut self, p: &Self::Point) {
todo!()
}
fn scaled(mut self, scale: C::Coord) -> Self {
self.scale(scale);
self
}
fn scale(&mut self, scale: C::Coord) {
todo!()
}
fn all_points(&self) -> Self::AllPoints {
self.into_iter()
}
}
#[test]
fn test_point() {
fn some_point_function<P: PointBase<C>, C: CoordinateBase>(p: P) -> C::Coord {
p.x() + p.y()
}
let p = types::Point::new(1, 2);
assert_eq!(some_point_function::<_, i32>(p), 3);
}