aboutsummaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/point2.cc
blob: 9d98777b64a523a1187d5eacad2c97461b3dbf0e (plain) (blame)
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
/*
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 See corresponding header file for details.
 */

#include "point2.h"

#include <math.h>

namespace mingfx {

static const Point2 s_zerop2d = Point2(0,0);
static const Point2 s_onep2d = Point2(1,1);

const Point2& Point2::Origin() { return s_zerop2d; }
const Point2& Point2::Zero() { return s_zerop2d; }
const Point2& Point2::One() { return s_onep2d; }
    
    
Point2::Point2() {
    p[0] = 0.0;
    p[1] = 0.0;
}

Point2::Point2(float x, float y) {
    p[0] = x;
    p[1] = y;
}

Point2::Point2(float *ptr) {
    p[0] = ptr[0];
    p[1] = ptr[1];
}

Point2::Point2(const Point2& other) {
    p[0] = other[0];
    p[1] = other[1];
}

Point2::~Point2() {
}

bool Point2::operator==(const Point2& other) const {
    return (fabs(other[0] - p[0]) < MINGFX_MATH_EPSILON &&
            fabs(other[1] - p[1]) < MINGFX_MATH_EPSILON);
}

bool Point2::operator!=(const Point2& other) const {
    return (fabs(other[0] - p[0]) >= MINGFX_MATH_EPSILON ||
            fabs(other[1] - p[1]) >= MINGFX_MATH_EPSILON);
}

Point2& Point2::operator=(const Point2& other) {
    p[0] = other[0];
    p[1] = other[1];
    return *this;
}

float Point2::operator[](const int i) const {
    if ((i>=0) && (i<=1)) {
        return p[i];
    }
    else {
        // w component of a point is 1 so return the constant 1.0
        return 1.0;
    }
}

float& Point2::operator[](const int i) {
    return p[i];
}


Point2 Point2::Lerp(const Point2 &b, float alpha) const {
    float x = (1.0f-alpha)*(*this)[0] + alpha*b[0];
    float y = (1.0f-alpha)*(*this)[1] + alpha*b[1];
    return Point2(x,y);
}

Point2 Point2::Lerp(const Point2 &a, const Point2 &b, float alpha) {
    float x = (1.0f-alpha)*a[0] + alpha*b[0];
    float y = (1.0f-alpha)*a[1] + alpha*b[1];
    return Point2(x,y);
}

    
const float * Point2::value_ptr() const {
    return p;
}


std::ostream & operator<< ( std::ostream &os, const Point2 &p) {
    return os << "(" << p[0] << ", " << p[1] << ")";
}

std::istream & operator>> ( std::istream &is, Point2 &p) {
    // format:  (x, y)
    char dummy;
    return is >> dummy >> p[0] >> dummy >> p[1] >> dummy;
}

    
} // end namespace