1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::mem;
use range::Range;
use vec::VecCont;
use vec::Vec2;
use vec::XAXIS;
use vec::YAXIS;
use vec::Axis;
use vec::AxisIter;
use std::fmt::Debug;
use XAXIS_S;
use YAXIS_S;
#[derive(Copy,Clone,Debug)]
#[must_use]
pub struct Rect<T:Copy+Debug>{
a:VecCont<Range<T>>
}
use AxisTrait;
impl<T:Copy+Debug> Rect<T>{
#[inline(always)]
pub fn new(a:T,b:T,c:T,d:T)->Rect<T>{
Rect{a:VecCont::new(Range{start:a,end:b},Range{start:c,end:d})}
}
#[inline(always)]
pub fn get_range2<X:AxisTrait>(&self)->&Range<T>{
self.a.get_axis(X::get())
}
#[inline(always)]
pub fn get_range2_mut<X:AxisTrait>(&mut self)->&mut Range<T>{
self.a.get_axis_mut(X::get())
}
#[inline(always)]
pub fn get_range(&self,axis:Axis)->&Range<T>{
self.a.get_axis(axis)
}
#[inline(always)]
pub fn get_range_mut(&mut self,axis:Axis)->&mut Range<T>{
self.a.get_axis_mut(axis)
}
}
impl Rect<f32>{
#[inline(always)]
pub fn from_pos_and_radius(pos:&Vec2,radius:f32)->Rect<f32>{
let rel=pos.get_axis(XAXIS);
let a=Range{start:rel-radius,end:rel+radius};
let rel=pos.get_axis(YAXIS);
let b=Range{start:rel-radius,end:rel+radius};
Rect{a:VecCont::new(a,b)}
}
#[inline(always)]
pub fn contains_vec(&self,pos:&Vec2)->bool{
let x=*(pos.get().0);
let y=*(pos.get().1);
let a=self.get_range2::<XAXIS_S>();
let b=self.get_range2::<YAXIS_S>();
x>=a.start&&x<=a.end && y>=b.start&&y<=b.end
}
#[inline(always)]
pub fn midpoint(&self)->Vec2{
let a=self.get_range2::<XAXIS_S>().midpoint();
let b=self.get_range2::<YAXIS_S>().midpoint();
Vec2::new(a,b)
}
#[inline(always)]
pub fn grow(&mut self,val:f32)->&mut Rect<f32>{
self.get_range2_mut::<XAXIS_S>().grow(val);
self.get_range2_mut::<YAXIS_S>().grow(val);
self
}
}
impl<T:PartialOrd+PartialEq+Copy+Debug> Rect<T>{
#[inline(always)]
pub fn subdivide(&self, mut divider: T, axis: Axis) -> (Rect<T>,Rect<T>) {
let ca=axis;
let na=axis.next();
let rel=self.a.get_axis(ca);
let carry_thru=self.a.get_axis(na);
if divider<rel.start{
divider=rel.start;
}else if divider>rel.end{
divider=rel.end;
}
let l=Range{start:rel.start,end:divider};
let r=Range{start:divider,end:rel.end};
let mut left:Rect<T>=unsafe{mem::uninitialized()};
*left.a.get_axis_mut(ca)=l;
*left.a.get_axis_mut(na)=*carry_thru;
let mut right:Rect<T>=unsafe{mem::uninitialized()};
*right.a.get_axis_mut(ca)=r;
*right.a.get_axis_mut(na)=*carry_thru;
(left,right)
}
}
impl<T:Ord+Copy+Debug> Rect<T>{
#[inline(always)]
pub fn contains_pos(&self,a:T,b:T)->bool{
self.get_range2::<XAXIS_S>().contains(a) &&
self.get_range2::<YAXIS_S>().contains(b)
}
#[inline(always)]
pub fn contains_rect(&self,rect:&Rect<T>)->bool{
if !self.get_range2::<XAXIS_S>().contains_range(&rect.get_range2::<XAXIS_S>()) {
return false;
}
if !self.get_range2::<YAXIS_S>().contains_range(&rect.get_range2::<YAXIS_S>()) {
return false;
}
true
}
#[inline(always)]
pub fn grow_to_fit(&mut self,rect:&Rect<T>){
for axis in AxisIter::new() {
let a=self.get_range_mut(axis);
let b=rect.get_range(axis);
if b.start<a.start{
a.start=b.start;
}
if b.end>a.end{
a.end=b.end;
}
}
}
#[inline(always)]
pub fn get_intersect_rect(&self,rect:&Rect<T>)->Option<Rect<T>>{
let mut rr:Rect<T>=unsafe{mem::uninitialized()};
for axis in AxisIter::new() {
let a=self.get_range(axis);
let b=rect.get_range(axis);
let left=a.start.max(b.start);
let right=a.end.min(b.end);
rr.get_range_mut(axis).start=left;
rr.get_range_mut(axis).end=right;
if right<=left{
return None;
}
}
Some(rr)
}
#[inline(always)]
pub fn intersects_rect(&self, rect: &Rect<T>)->bool{
for axis in AxisIter::new() {
if !self.get_range(axis).intersects(&rect.get_range(axis)){
return false;
}
}
return true;
}
}