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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use ::std::convert::Into;
use ::std::ops::Add;
use ::std::ops::AddAssign;
use ::std::ops::Div;
use ::std::ops::DivAssign;
use ::std::ops::Mul;
use ::std::ops::MulAssign;
use ::std::ops::Neg;
use ::std::ops::Rem;
use ::std::ops::Shl;
use ::std::ops::ShlAssign;
use ::std::ops::Shr;
use ::std::ops::ShrAssign;
use ::std::ops::Sub;
use ::std::ops::SubAssign;

use ::num_traits::sign::abs;
use ::num_traits::sign::signum;
use ::num_traits::sign::Signed;

use crate::num::FromRounded;
use crate::num::INum;
use crate::num::Num;
use crate::num::NumIdentity;
use crate::num::NumTuple;
use crate::num::NumberExtensions;
use crate::num::ToRounded;
use crate::num::ToSignedClamped;

use crate::geom::Line;
use crate::geom::Rect;
use crate::geom::Size;
use crate::geom::Transform;

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Point<N: Num = f32>(pub N, pub N);

// Point Speicifc functions.
impl<N: Num> Point<N> {
    pub fn new_zero_value() -> Self {
        Point(N::zero(), N::zero())
    }

    pub fn new_from_angle(angle: f32, hypot: f32) -> Self {
        let cos = angle.cos();
        let sin = angle.sin();

        Point(hypot * cos, hypot * sin).from_f32()
    }

    pub fn move_from_angle(self, angle: f32, hypot: f32) -> Self {
        let cos = angle.cos();
        let sin = angle.sin();
        let move_xy = Point(hypot * cos, hypot * sin);

        let new_position = self.to_f32() + move_xy;
        new_position.from_f32()
    }

    pub fn x(&self) -> N {
        self.first()
    }

    pub fn y(&self) -> N {
        self.second()
    }

    pub fn move_x(&mut self, x: N) {
        self.0 += x;
    }

    pub fn move_y(&mut self, y: N) {
        self.1 += y;
    }

    pub fn set_x(&mut self, x: N) {
        self.set_first(x);
    }

    pub fn set_y(&mut self, y: N) {
        self.set_second(y);
    }

    pub fn line_to(self, other: Self) -> Line<N> {
        Line(self, other)
    }

    pub fn rect_to(self, other: Self) -> Rect<N> {
        let xy = self.min(other);
        let other_xy = self.max(other);
        let size = xy.distance_to(other_xy);

        Rect(xy, size)
    }

    pub fn distance_to(self, other: Self) -> Size<N> {
        Size::new(other.first() - self.first(), other.second() - self.second())
    }

    pub fn direction_to(self, other: Self) -> Size<f32> {
        Line(self, other).direction()
    }

    /// This is to allow creating a new Point, which wraps a different number type.
    ///
    /// For example `Point(1u8, 2u8)::to::<u32>()` creates a `Point(1u32, 2u32)`.
    pub fn to<T: Num + From<N>>(&self) -> Point<T> {
        Point(T::from(self.first()), T::from(self.second()))
    }

    pub fn abs(&self) -> Self {
        Self(self.x().abs(), self.y().abs())
    }

    pub fn min(self, other: Self) -> Self {
        Self(self.x().min(other.x()), self.y().min(other.y()))
    }

    pub fn max(self, other: Self) -> Self {
        Self(self.x().max(other.x()), self.y().max(other.y()))
    }

    pub fn hypot_to(self, other: Point<N>) -> N {
        self.distance_to(other).hypot()
    }

    pub fn rotate_around_point(self, angle: f32, other: Self) -> Self {
        (self - other).rotate_around_zero(angle) + other
    }

    pub fn rotate_around_zero(self, rotation: f32) -> Self {
        let Point(x, y): Point<f32> = self.to_f32();

        let hypot = x.hypot(y);
        let angle = self.angle_to_zero() - rotation;
        let cos = angle.cos();
        let sin = angle.sin();

        Point(hypot * cos, hypot * sin).from_f32()
    }

    pub fn angle_to(self, other: Self) -> f32 {
        (self - other).angle_to_zero()
    }

    fn angle_to_zero(self) -> f32 {
        let self_f32 = self.to_f32();
        self_f32.y().atan2(self_f32.x())
    }

    pub fn interpolate_to(self, other: Point<N>, n: N) -> Point<N> {
        let start_f32 = self.to_f32();
        let other_f32 = other.to_f32();
        let n_f32: f32 = n.to_rounded();

        let new_size_f32 = (start_f32 * n_f32.inverse()) + (other_f32 * n_f32);
        new_size_f32.from_f32()
    }

    pub(crate) fn to_f32(self) -> Point<f32> {
        self.to_rounded()
    }
}

impl<N: Num> NumTuple<N> for Point<N> {
    fn new(x: N, y: N) -> Self {
        Point(x, y)
    }

    fn first(&self) -> N {
        self.0
    }

    fn second(&self) -> N {
        self.1
    }

    fn set_first(&mut self, n: N) {
        self.0 = n;
    }

    fn set_second(&mut self, n: N) {
        self.1 = n;
    }

    fn set(&mut self, first: N, second: N) {
        self.0 = first;
        self.1 = second;
    }

    fn get(&mut self) -> (N, N) {
        (self.0, self.1)
    }
}

impl<O: Num, N: Num + ToRounded<O>> ToRounded<Point<O>> for Point<N> {
    fn to_rounded(self) -> Point<O> {
        Point(self.x().to_rounded(), self.y().to_rounded())
    }
}

impl<N: Num + ToSignedClamped> Point<N>
where
    <N as ToSignedClamped>::Output: Signed,
{
    pub fn to_signed_clamped(self) -> Point<<N as ToSignedClamped>::Output> {
        Point(self.x().to_signed_clamped(), self.y().to_signed_clamped())
    }

    pub fn step_direction_to(self, other: Point<N>) -> Point<<N as ToSignedClamped>::Output> {
        Line(self, other).step_direction()
    }
}

impl<N: Num + Signed> Point<N> {
    pub fn sign(&self) -> Self {
        Self(signum(self.0), signum(self.1))
    }

    pub fn step(&self) -> Self {
        let x = if self.0 == <N as NumIdentity>::zero() {
            <N as NumIdentity>::zero()
        } else {
            signum(self.0)
        };

        let y = if self.1 == <N as NumIdentity>::zero() {
            <N as NumIdentity>::zero()
        } else {
            signum(self.1)
        };

        Self(x, y)
    }

    pub fn sign_exclusive_dir(&self) -> Self {
        let w_abs = abs(self.first());
        let h_abs = abs(self.second());

        if w_abs > h_abs {
            Self(signum(self.first()), <N as NumIdentity>::zero())
        } else {
            Self(<N as NumIdentity>::zero(), signum(self.second()))
        }
    }

    pub fn flip_x(self) -> Self {
        Self(-self.x(), self.y())
    }

    pub fn flip_y(self) -> Self {
        Self(self.x(), -self.y())
    }
}

impl Point<f32> {
    pub fn overlaps(self, Point(w, h): Self, xy1: Self, xy2: Self) -> bool {
        let Point(x_min, y_min) = xy1.min(xy2);
        let Point(x_max, y_max) = xy1.max(xy2);

        let half_w = w / 2.0;
        let x = self.0 + half_w;
        let other_half_w = (x_max - x_min) / 2.0;
        let other_x = x_min + other_half_w;
        let dist_x = (x - other_x).abs();

        if dist_x > (half_w + other_half_w) {
            return false;
        }

        let half_h = h / 2.0;
        let y = self.1 + half_h;
        let other_half_h = (y_max - y_min) / 2.0;
        let other_y = y_min + other_half_h;
        let dist_y = (y - other_y).abs();

        if dist_y > (half_h + other_half_h) {
            return false;
        }

        true
    }

    pub fn floor(self) -> Self {
        Self::new(self.first().floor(), self.second().floor())
    }

    pub fn ceil(self) -> Self {
        Self::new(self.first().ceil(), self.second().ceil())
    }

    pub fn round(self) -> Self {
        Self::new(self.first().round(), self.second().round())
    }

    pub(crate) fn from_f32<N: Num>(self) -> Point<N> {
        Point(
            FromRounded::from_rounded(self.x()),
            FromRounded::from_rounded(self.y()),
        )
    }
}

impl<N: Num> Add<Self> for Point<N> {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        NumTuple::new(self.first() + other.first(), self.second() + other.second())
    }
}

impl<N: Num> AddAssign<Self> for Point<N> {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other;
    }
}

impl<N: Num> Sub<Self> for Point<N> {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Point::new(self.0 - other.0, self.1 - other.1)
    }
}

impl<N: Num> SubAssign<Self> for Point<N> {
    fn sub_assign(&mut self, other: Self) {
        *self = *self - other;
    }
}

impl<N: Num> Mul<Self> for Point<N> {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        Point::new(self.0 * other.0, self.1 * other.1)
    }
}

impl<N: Num> MulAssign<Self> for Point<N> {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other;
    }
}

impl<N: Num> Div<Self> for Point<N> {
    type Output = Self;

    fn div(self, other: Self) -> Self {
        Point::new(self.0 / other.0, self.1 / other.1)
    }
}

impl<N: Num> DivAssign<Self> for Point<N> {
    fn div_assign(&mut self, other: Self) {
        *self = *self / other;
    }
}

impl<N: Num> Rem<Self> for Point<N> {
    type Output = Self;

    fn rem(self, other: Self) -> Self {
        Point::new(self.0 % other.0, self.1 % other.1)
    }
}

impl<N: Num> Add<Size<N>> for Point<N> {
    type Output = Self;

    fn add(self, other: Size<N>) -> Self {
        NumTuple::new(self.x() + other.width(), self.y() + other.height())
    }
}

impl<N: Num> AddAssign<Size<N>> for Point<N> {
    fn add_assign(&mut self, other: Size<N>) {
        *self = *self + other;
    }
}

impl<N: Num> Sub<Size<N>> for Point<N> {
    type Output = Self;

    fn sub(self, other: Size<N>) -> Self {
        Point::new(self.x() - other.width(), self.y() - other.height())
    }
}

impl<N: Num> SubAssign<Size<N>> for Point<N> {
    fn sub_assign(&mut self, other: Size<N>) {
        *self = *self - other;
    }
}

impl<N: Num> Mul<Size<N>> for Point<N> {
    type Output = Self;

    fn mul(self, other: Size<N>) -> Self {
        Point::new(self.x() * other.width(), self.y() * other.height())
    }
}

impl<N: Num> MulAssign<Size<N>> for Point<N> {
    fn mul_assign(&mut self, other: Size<N>) {
        *self = *self * other;
    }
}

impl<N: Num> Div<Size<N>> for Point<N> {
    type Output = Self;

    fn div(self, other: Size<N>) -> Self {
        Point::new(self.x() / other.width(), self.y() / other.height())
    }
}

impl<N: Num> DivAssign<Size<N>> for Point<N> {
    fn div_assign(&mut self, other: Size<N>) {
        *self = *self / other;
    }
}

impl<N: Num> Mul<N> for Point<N> {
    type Output = Self;

    fn mul(self, other: N) -> Self {
        Point::new(self.0 * other, self.1 * other)
    }
}

impl<N: Num> MulAssign<N> for Point<N> {
    fn mul_assign(&mut self, other: N) {
        *self = *self * other;
    }
}

impl<N: Num> Div<N> for Point<N> {
    type Output = Self;

    fn div(self, other: N) -> Self {
        Point::new(self.0 / other, self.1 / other)
    }
}

impl<N: Num> DivAssign<N> for Point<N> {
    fn div_assign(&mut self, other: N) {
        *self = *self / other;
    }
}

impl<N: INum> Shl<N> for Point<N> {
    type Output = Self;

    fn shl(self, other: N) -> Self {
        Self(self.0 << other, self.1 << other)
    }
}

impl<N: INum> ShlAssign<N> for Point<N> {
    fn shl_assign(&mut self, other: N) {
        *self = *self << other;
    }
}

impl<N: INum> Shr<N> for Point<N> {
    type Output = Self;

    fn shr(self, other: N) -> Self {
        Self(self.0 >> other, self.1 >> other)
    }
}

impl<N: INum> ShrAssign<N> for Point<N> {
    fn shr_assign(&mut self, other: N) {
        *self = *self >> other;
    }
}

impl<N: Num + Signed> Neg for Point<N> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self(-self.x(), -self.y())
    }
}

impl<N> Add<Transform<N>> for Point<N>
where
    N: Num,
{
    type Output = Point<N>;

    #[inline(always)]
    fn add(self, transform: Transform<N>) -> Self::Output {
        transform + self
    }
}

impl<N: Num> From<(N, N)> for Point<N> {
    /// (N, N) -> Point(N, N)
    fn from((x, y): (N, N)) -> Self {
        Self::new(x, y)
    }
}

impl<N: Num> Into<(N, N)> for Point<N> {
    /// Point(N, N) -> (N, N)
    fn into(self) -> (N, N) {
        (self.0, self.1)
    }
}

#[cfg(test)]
mod flip_x {
    use super::*;

    #[test]
    fn it_should_flip_x() {
        let point: Point<i32> = Point(15, 23);
        assert_eq!(point.flip_x(), Point(-15, 23));
    }
}

#[cfg(test)]
mod flip_y {
    use super::*;

    #[test]
    fn it_should_flip_y() {
        let point: Point<i32> = Point(15, 23);
        assert_eq!(point.flip_y(), Point(15, -23));
    }
}

#[cfg(test)]
mod rotate_around_point {
    use super::*;
    use crate::geom::testing_utils::assert_approx_point_eq;
    use ::std::f32::consts::TAU;

    #[test]
    fn it_should_rotate_90_degrees() {
        let point = Point(0.0, 10.0);
        let rotate = point.rotate_around_point(TAU * 0.25, Point(0.0, 0.0));

        assert_approx_point_eq(rotate, Point(10.0, 0.0));
    }

    #[test]
    fn it_should_rotate_90_degrees_around_point() {
        let point = Point(25.0, 35.0);
        let rotate = point.rotate_around_point(TAU * 0.25, Point(25.0, 25.0));

        assert_approx_point_eq(rotate, Point(35.0, 25.0));
    }
}

#[cfg(test)]
mod angle_to {
    use super::*;
    use ::assert_approx_eq::assert_approx_eq;
    use ::std::f32::consts::TAU;
    use ::testcat::*;

    describe!("angle to zero", {
        it!(
            "should angle to zero from right",
            test_angle_to_zero_from_right
        );
        it!(
            "should angle to zero from above",
            test_angle_to_zero_from_above
        );
        it!(
            "should angle to zero from left",
            test_angle_to_zero_from_left
        );
        it!(
            "should angle to zero from below",
            test_angle_to_zero_from_below
        );
    });

    describe!("angle to point", {
        it!(
            "should angle to point from right",
            test_angle_to_point_from_right
        );
        it!(
            "should angle to point from above",
            test_angle_to_point_from_above
        );
        it!(
            "should angle to point from left",
            test_angle_to_point_from_left
        );
        it!(
            "should angle to point from below",
            test_angle_to_point_from_below
        );
    });

    fn test_angle_to_zero_from_right() {
        let point = Point(10.0, 0.0);
        assert_approx_eq!(point.angle_to(Point(0.0, 0.0)), 0.0);
    }

    fn test_angle_to_zero_from_above() {
        let point = Point(0.0, 10.0);
        assert_approx_eq!(point.angle_to(Point(0.0, 0.0)), TAU * 0.25);
    }

    fn test_angle_to_zero_from_left() {
        let point = Point(-10.0, 0.0);
        assert_approx_eq!(point.angle_to(Point(0.0, 0.0)), TAU * 0.5);
    }

    fn test_angle_to_zero_from_below() {
        let point = Point(0.0, -10.0);
        assert_approx_eq!(point.angle_to(Point(0.0, 0.0)), -TAU * 0.25);
    }

    fn test_angle_to_point_from_right() {
        let origin = Point(5.0, 8.0);
        let point = Point(10.0, 0.0) + origin;
        assert_approx_eq!(point.angle_to(origin), 0.0);
    }

    fn test_angle_to_point_from_above() {
        let origin = Point(5.0, 8.0);
        let point = Point(0.0, 10.0) + origin;
        assert_approx_eq!(point.angle_to(origin), TAU * 0.25);
    }

    fn test_angle_to_point_from_left() {
        let origin = Point(5.0, 8.0);
        let point = Point(-10.0, 0.0) + origin;
        assert_approx_eq!(point.angle_to(origin), TAU * 0.5);
    }

    fn test_angle_to_point_from_below() {
        let origin = Point(5.0, 8.0);
        let point = Point(0.0, -10.0) + origin;
        assert_approx_eq!(point.angle_to(origin), -TAU * 0.25);
    }
}

#[cfg(test)]
mod step_direction_to {
    use super::*;
    use ::testcat::*;

    it!(
        "should return positive steps when stepping positively",
        test_positive_step
    );
    it!(
        "should return a step of 0 when both points on top of each other",
        test_zero_step
    );
    it!(
        "should return negative steps when stepping negatively",
        test_negative_step
    );

    fn test_positive_step() {
        let from: Point<i32> = Point(100, 200);
        let to: Point<i32> = Point(333, 666);
        let step = from.step_direction_to(to);

        assert_eq!(step, Point(1, 1));
    }

    fn test_zero_step() {
        let from: Point<i32> = Point(100, 200);
        let to: Point<i32> = Point(100, 200);
        let step = from.step_direction_to(to);

        assert_eq!(step, Point(0, 0));
    }

    fn test_negative_step() {
        let from: Point<i32> = Point(333, 666);
        let to: Point<i32> = Point(-100, -200);
        let step = from.step_direction_to(to);

        assert_eq!(step, Point(-1, -1));
    }
}