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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use {
    std::{fmt, ops},
    crate::math_f32::*,
    //    makepad_microserde::*,
    //    crate::colorhex::*
};


pub struct PrettyPrintedF64(pub f64);

impl fmt::Display for PrettyPrintedF64 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.0.abs().fract() < 0.00000001 {
            write!(f, "{}.0", self.0)
        } else {
            write!(f, "{}", self.0)
        }
    }
}


#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Rect {
    pub pos: DVec2,
    pub size: DVec2,
}

impl Rect {
    
    pub fn translate(self, pos: DVec2) -> Rect {
        Rect {pos: self.pos + pos, size: self.size}
    }
    
    pub fn contains(&self, pos: DVec2) -> bool {
        pos.x >= self.pos.x && pos.x <= self.pos.x + self.size.x &&
        pos.y >= self.pos.y && pos.y <= self.pos.y + self.size.y
    }
    
    pub fn center(&self) -> DVec2 {
        DVec2 {
            x: self.pos.x + self.size.x * 0.5,
            y: self.pos.y + self.size.y * 0.5,
        }
    }
    
    pub fn scale_and_shift(&self, center: DVec2, scale: f64, shift: DVec2) -> Rect {
        Rect {
            pos: (self.pos - center) * scale + center + shift,
            size: self.size * scale
        }
    }
    
    pub fn intersects(&self, r: Rect) -> bool {
        !(
            r.pos.x > self.pos.x + self.size.x ||
            r.pos.x + r.size.x < self.pos.x ||
            r.pos.y > self.pos.y + self.size.y ||
            r.pos.y + r.size.y < self.pos.y
        )
    }
    
    pub fn add_margin(self, size: DVec2) -> Rect {
        Rect {pos: self.pos - size, size: self.size + 2.0 * size}
    }
    
    pub fn contain(&self, other: Rect) -> Rect {
        let mut pos = other.pos;
        if pos.x < self.pos.x{ pos.x = self.pos.x };
        if pos.y < self.pos.y{ pos.y = self.pos.y };
        if pos.x + other.size.x > self.pos.x + self.size.x{
            pos.x = self.pos.x + self.size.x - other.size.x
        }
        if pos.y + other.size.y > self.pos.y+ self.size.y{
            pos.y = self.pos.y + self.size.y - other.size.y
        }
        Rect{
            pos,
            size:other.size
        }
    }
    
    pub fn clip(&self, clip: (DVec2, DVec2)) -> Rect {
        let mut x1 = self.pos.x;
        let mut y1 = self.pos.y;
        let mut x2 = x1 + self.size.x;
        let mut y2 = y1 + self.size.y;
        x1 = x1.max(clip.0.x).min(clip.1.x);
        y1 = y1.max(clip.0.y).min(clip.1.y);
        x2 = x2.max(clip.0.x).min(clip.1.x);
        y2 = y2.max(clip.0.y).min(clip.1.y);
        Rect {pos: dvec2(x1, y1), size: dvec2(x2 - x1, y2 - y1)}
    }
    
    pub fn from_lerp(a: Rect, b: Rect, f: f64) -> Rect {
        Rect {
            pos: (b.pos - a.pos) * f + a.pos,
            size: (b.size - a.size) * f + a.size
        }
    }

    pub fn dpi_snap(&self, f:f64)->Rect{
        Rect{
            pos: dvec2((self.pos.x / f).floor() * f,(self.pos.y / f).floor() * f),
            size:  dvec2((self.size.x / f).floor() * f,(self.size.y / f).floor() * f),
        }
    }
    
    pub fn is_nan(&self)->bool{
        self.pos.is_nan() || self.size.is_nan()
    }

}


#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct DVec2 {
    pub x: f64,
    pub y: f64,
}

impl std::convert::From<Vec2> for DVec2 {
    fn from(other: Vec2) -> DVec2 {DVec2 {x: other.x as f64, y: other.y as f64}}
}

impl std::convert::From<DVec2> for Vec2 {
    fn from(other: DVec2) -> Vec2 {Vec2 {x: other.x as f32, y: other.y as f32}}
}

impl std::convert::From<(DVec2, DVec2)> for Rect {
    fn from(o: (DVec2, DVec2)) -> Rect {Rect {pos: dvec2(o.0.x, o.0.y), size: dvec2(o.1.x - o.0.x, o.1.y - o.0.y)}}
}

impl DVec2 {
    pub fn new() -> DVec2 {
        DVec2::default()
    }


    pub fn dpi_snap(&self, f:f64)->DVec2{
        DVec2{
            x:(self.x * f).round() / f,
            y:(self.y * f).round() / f
        }
    }
    
    pub fn all(x: f64) -> DVec2 {
        DVec2 {x, y: x}
    }
    
    pub fn index(&self, index:Vec2Index)->f64{
        match index{
            Vec2Index::X=>self.x,
            Vec2Index::Y=>self.y
        }
    }

    pub fn set_index(&mut self, index:Vec2Index, v: f64){
        match index{
            Vec2Index::X=>{self.x = v},
            Vec2Index::Y=>{self.y = v}
        }
    }
    
    pub fn from_index_pair(index:Vec2Index, a: f64, b:f64)->Self{
        match index{
            Vec2Index::X=>{Self{x:a,y:b}},
            Vec2Index::Y=>{Self{x:b,y:a}}
        }
    }
    
    pub fn into_vec2(self) -> Vec2 {
        Vec2 {x: self.x as f32, y: self.y as f32}
    }
    
    pub fn from_lerp(a: DVec2, b: DVec2, f: f64) -> DVec2 {
        let nf = 1.0 - f;
        DVec2 {
            x: nf * a.x + f * b.x,
            y: nf * a.y + f * b.y,
        }
    }
    
    pub fn floor(self) -> DVec2 {
        DVec2 {
            x: self.x.floor(), 
            y: self.y.floor(),
        }
    }
        
    pub fn ceil(self) -> DVec2 {
        DVec2 {
            x: self.x.ceil(), 
            y: self.y.ceil(),
        }
    }
    
    pub fn distance(&self, other: &DVec2) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        (dx * dx + dy * dy).sqrt()
    }
    
    pub fn length(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
    }
    
    pub fn is_nan(&self)->bool{
        self.x.is_nan() || self.y.is_nan()
    }
}

impl fmt::Display for DVec2 {
    // This trait requires `fmt` with this exact signature.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "vec2f64({},{})", self.x, self.y)
    }
}

pub fn dvec2(x: f64, y: f64) -> DVec2 {DVec2 {x, y}}

//------ Vec2 operators

impl ops::Add<DVec2> for DVec2 {
    type Output = DVec2;
    fn add(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self.x + rhs.x, y: self.y + rhs.y}
    }
}

impl ops::Sub<DVec2> for DVec2 {
    type Output = DVec2;
    fn sub(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self.x - rhs.x, y: self.y - rhs.y}
    }
}

impl ops::Mul<DVec2> for DVec2 {
    type Output = DVec2;
    fn mul(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self.x * rhs.x, y: self.y * rhs.y}
    }
}

impl ops::Div<DVec2> for DVec2 {
    type Output = DVec2;
    fn div(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self.x / rhs.x, y: self.y / rhs.y}
    }
}


impl ops::Add<DVec2> for f64 {
    type Output = DVec2;
    fn add(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self + rhs.x, y: self + rhs.y}
    }
}

impl ops::Sub<DVec2> for f64 {
    type Output = DVec2;
    fn sub(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self -rhs.x, y: self -rhs.y}
    }
}

impl ops::Mul<DVec2> for f64 {
    type Output = DVec2;
    fn mul(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self *rhs.x, y: self *rhs.y}
    }
}

impl ops::Div<DVec2> for f64 {
    type Output = DVec2;
    fn div(self, rhs: DVec2) -> DVec2 {
        DVec2 {x: self / rhs.x, y: self / rhs.y}
    }
}


impl ops::Add<f64> for DVec2 {
    type Output = DVec2;
    fn add(self, rhs: f64) -> DVec2 {
        DVec2 {x: self.x + rhs, y: self.y + rhs}
    }
}

impl ops::Sub<f64> for DVec2 {
    type Output = DVec2;
    fn sub(self, rhs: f64) -> DVec2 {
        DVec2 {x: self.x - rhs, y: self.y - rhs}
    }
}

impl ops::Mul<f64> for DVec2 {
    type Output = DVec2;
    fn mul(self, rhs: f64) -> DVec2 {
        DVec2 {x: self.x * rhs, y: self.y * rhs}
    }
}

impl ops::Div<f64> for DVec2 {
    type Output = DVec2;
    fn div(self, rhs: f64) -> DVec2 {
        DVec2 {x: self.x / rhs, y: self.y / rhs}
    }
}

impl ops::AddAssign<DVec2> for DVec2 {
    fn add_assign(&mut self, rhs: DVec2) {
        self.x = self.x + rhs.x;
        self.y = self.y + rhs.y;
    }
}

impl ops::SubAssign<DVec2> for DVec2 {
    fn sub_assign(&mut self, rhs: DVec2) {
        self.x = self.x - rhs.x;
        self.y = self.y - rhs.y;
    }
}

impl ops::MulAssign<DVec2> for DVec2 {
    fn mul_assign(&mut self, rhs: DVec2) {
        self.x = self.x * rhs.x;
        self.y = self.y * rhs.y;
    }
}

impl ops::DivAssign<DVec2> for DVec2 {
    fn div_assign(&mut self, rhs: DVec2) {
        self.x = self.x / rhs.x;
        self.y = self.y / rhs.y;
    }
}


impl ops::AddAssign<f64> for DVec2 {
    fn add_assign(&mut self, rhs: f64) {
        self.x = self.x + rhs;
        self.y = self.y + rhs;
    }
}

impl ops::SubAssign<f64> for DVec2 {
    fn sub_assign(&mut self, rhs: f64) {
        self.x = self.x - rhs;
        self.y = self.y - rhs;
    }
}

impl ops::MulAssign<f64> for DVec2 {
    fn mul_assign(&mut self, rhs: f64) {
        self.x = self.x * rhs;
        self.y = self.y * rhs;
    }
}

impl ops::DivAssign<f64> for DVec2 {
    fn div_assign(&mut self, rhs: f64) {
        self.x = self.x / rhs;
        self.y = self.y / rhs;
    }
}

impl ops::Neg for DVec2 {
    type Output = DVec2;
    fn neg(self) -> Self {DVec2 {x: -self.x, y: -self.y}}
}