summaryrefslogtreecommitdiffstats
path: root/src/2geom/point.h
blob: 91df159cad90f5364b3cdda88e8cbdaac5c5ee95 (plain)
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
#ifndef SEEN_Geom_POINT_H
#define SEEN_Geom_POINT_H

/** \file
 * Cartesian point class.
 */

#include <iostream>

#include "coord.h"
#include "utils.h"

namespace Geom {

enum Dim2 { X=0, Y=1 };

class Matrix;

/// Cartesian point.
class Point {
    Coord _pt[2];

  public:
    inline Point()
    { _pt[X] = _pt[Y] = 0; }

    inline Point(Coord x, Coord y) {
        _pt[X] = x; _pt[Y] = y;
    }

    inline Point(Point const &p) {
        for (unsigned i = 0; i < 2; ++i)
            _pt[i] = p._pt[i];
    }

    inline Point &operator=(Point const &p) {
        for (unsigned i = 0; i < 2; ++i)
            _pt[i] = p._pt[i];
        return *this;
    }

    inline Coord operator[](unsigned i) const { return _pt[i]; }
    inline Coord &operator[](unsigned i) { return _pt[i]; }

    Coord operator[](Dim2 d) const throw() { return _pt[d]; }
    Coord &operator[](Dim2 d) throw() { return _pt[d]; }

    static inline Point polar(Coord angle, Coord radius) {
        return Point(radius * std::cos(angle), radius * std::sin(angle));
    }

    inline Coord length() const { return hypot(_pt[0], _pt[1]); }

    /** Return a point like this point but rotated -90 degrees.
        (If the y axis grows downwards and the x axis grows to the
        right, then this is 90 degrees counter-clockwise.)
    **/
    Point ccw() const {
        return Point(_pt[Y], -_pt[X]);
    }

    /** Return a point like this point but rotated +90 degrees.
        (If the y axis grows downwards and the x axis grows to the
        right, then this is 90 degrees clockwise.)
    **/
    Point cw() const {
        return Point(-_pt[Y], _pt[X]);
    }

    /**
        \brief A function to lower the precision of the point
        \param  places  The number of decimal places that should be in
                        the final number.
    */
    inline void round (int places = 0) {
        _pt[X] = (Coord)(decimal_round((double)_pt[X], places));
        _pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
        return;
    }

    void normalize();

    inline Point operator+(Point const &o) const {
        return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
    }
    inline Point operator-(Point const &o) const {
        return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
    }
    inline Point &operator+=(Point const &o) {
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
            _pt[i] += o._pt[i];
        }
        return *this;
    }  
    inline Point &operator-=(Point const &o) {
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
            _pt[i] -= o._pt[i];
        }
        return *this;
    }

    inline Point operator-() const {
        return Point(-_pt[X], -_pt[Y]);
    }
    inline Point operator*(double const s) const {
        return Point(_pt[X] * s, _pt[Y] * s);
    }
    inline Point operator/(double const s) const {
        //TODO: s == 0?
        return Point(_pt[X] / s, _pt[Y] / s);
    }
    inline Point &operator*=(double const s) {
        for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
        return *this;
    }
    inline Point &operator/=(double const s) {
        //TODO: s == 0?
        for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
        return *this;
    }

    Point &operator*=(Matrix const &m);

    inline int operator == (const Point &in_pnt) {
        return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
    }

    friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
};

inline Point operator*(double const s, Point const &p) { return p * s; }

/** A function to print out the Point.  It just prints out the coords
    on the given output stream */
inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
    out_file << "X: " << in_pnt[X] << "  Y: " << in_pnt[Y];
    return out_file;
}

/** This is a rotation (sort of). */
inline Point operator^(Point const &a, Point const &b) {
    Point const ret(a[0] * b[0] - a[1] * b[1],
                    a[1] * b[0] + a[0] * b[1]);
    return ret;
}

//IMPL: boost::EqualityComparableConcept
inline bool operator==(Point const &a, Point const &b) {
    return (a[X] == b[X]) && (a[Y] == b[Y]);
}
inline bool operator!=(Point const &a, Point const &b) {
    return (a[X] != b[X]) || (a[Y] != b[Y]);
}

/** This is a lexicographical ordering for points.  It is remarkably useful for sweepline algorithms*/
inline bool operator<=(Point const &a, Point const &b) {
    return ( ( a[Y] < b[Y] ) ||
             (( a[Y] == b[Y] ) && ( a[X] < b[X] )));
}

Coord L1(Point const &p);

/** Compute the L2, or euclidean, norm of \a p. */
inline Coord L2(Point const &p) { return p.length(); }

/** Compute the square of L2 norm of \a p. Warning: this can overflow where L2 won't.*/
inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }

double LInfty(Point const &p);
bool is_zero(Point const &p);
bool is_unit_vector(Point const &p);

extern double atan2(Point const p);
/** compute the angle turning from a to b (signed). */
extern double angle_between(Point const a, Point const b);

//IMPL: NearConcept
inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
    return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
}

/** Returns p * Geom::rotate_degrees(90), but more efficient.
 *
 * Angle direction in Inkscape code: If you use the traditional mathematics convention that y
 * increases upwards, then positive angles are anticlockwise as per the mathematics convention.  If
 * you take the common non-mathematical convention that y increases downwards, then positive angles
 * are clockwise, as is common outside of mathematics.
 *
 * There is no rot_neg90 function: use -rot90(p) instead.
 */
inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }

/** Given two points and a parameter t \in [0, 1], return a point
 * proportionally from a to b by t.  Akin to 1 degree bezier.*/
inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }

Point unit_vector(Point const &a);

/** compute the dot product (inner product) between the vectors a and b. */
inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
/** Defined as dot(a, b.cw()). */
inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }

/** compute the euclidean distance between points a and b.  TODO: hypot safer/faster? */
inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }

/** compute the square of the distance between points a and b. */
inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }

Point abs(Point const &b);

Point operator*(Point const &v, Matrix const &m);

Point operator/(Point const &p, Matrix const &m);

} /* namespace Geom */

#endif /* !SEEN_Geom_POINT_H */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :