~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/2geom/point.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Kees Cook, Ted Gould
  • Date: 2008-02-10 14:20:16 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080210142016-vcnb2zqyhszu0xvb
Tags: 0.46~pre1-0ubuntu1
[ Kees Cook ]
* debian/control:
  - add libgtkspell-dev build-dep to gain GtkSpell features (LP: #183547).
  - update Standards version (no changes needed).
  - add Vcs and Homepage fields.
  - switch to new python-lxml dep.
* debian/{control,rules}: switch from dpatch to quilt for more sanity.
* debian/patches/20_fix_glib_and_gxx43_ftbfs.patch:
  - merged against upstream fixes.
  - added additional fixes for newly written code.
* debian/rules: enable parallel building.

[ Ted Gould ]
* Updating POTFILES.in to make it so things build correctly.
* debian/control:
  - add ImageMagick++ and libboost-dev to build-deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef SEEN_Geom_POINT_H
 
2
#define SEEN_Geom_POINT_H
 
3
 
 
4
/** \file
 
5
 * Cartesian point class.
 
6
 */
 
7
 
 
8
#include <iostream>
 
9
 
 
10
#include "coord.h"
 
11
#include "utils.h"
 
12
 
 
13
namespace Geom {
 
14
 
 
15
enum Dim2 { X=0, Y=1 };
 
16
 
 
17
class Matrix;
 
18
 
 
19
/// Cartesian point.
 
20
class Point {
 
21
    Coord _pt[2];
 
22
 
 
23
  public:
 
24
    inline Point()
 
25
    { _pt[X] = _pt[Y] = 0; }
 
26
 
 
27
    inline Point(Coord x, Coord y) {
 
28
        _pt[X] = x; _pt[Y] = y;
 
29
    }
 
30
 
 
31
    inline Point(Point const &p) {
 
32
        for (unsigned i = 0; i < 2; ++i)
 
33
            _pt[i] = p._pt[i];
 
34
    }
 
35
 
 
36
    inline Point &operator=(Point const &p) {
 
37
        for (unsigned i = 0; i < 2; ++i)
 
38
            _pt[i] = p._pt[i];
 
39
        return *this;
 
40
    }
 
41
 
 
42
    inline Coord operator[](unsigned i) const { return _pt[i]; }
 
43
    inline Coord &operator[](unsigned i) { return _pt[i]; }
 
44
 
 
45
    Coord operator[](Dim2 d) const throw() { return _pt[d]; }
 
46
    Coord &operator[](Dim2 d) throw() { return _pt[d]; }
 
47
 
 
48
    static inline Point polar(Coord angle, Coord radius) {
 
49
        return Point(radius * std::cos(angle), radius * std::sin(angle));
 
50
    }
 
51
 
 
52
    inline Coord length() const { return hypot(_pt[0], _pt[1]); }
 
53
 
 
54
    /** Return a point like this point but rotated -90 degrees.
 
55
        (If the y axis grows downwards and the x axis grows to the
 
56
        right, then this is 90 degrees counter-clockwise.)
 
57
    **/
 
58
    Point ccw() const {
 
59
        return Point(_pt[Y], -_pt[X]);
 
60
    }
 
61
 
 
62
    /** Return a point like this point but rotated +90 degrees.
 
63
        (If the y axis grows downwards and the x axis grows to the
 
64
        right, then this is 90 degrees clockwise.)
 
65
    **/
 
66
    Point cw() const {
 
67
        return Point(-_pt[Y], _pt[X]);
 
68
    }
 
69
 
 
70
    /**
 
71
        \brief A function to lower the precision of the point
 
72
        \param  places  The number of decimal places that should be in
 
73
                        the final number.
 
74
    */
 
75
    inline void round (int places = 0) {
 
76
        _pt[X] = (Coord)(decimal_round((double)_pt[X], places));
 
77
        _pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
 
78
        return;
 
79
    }
 
80
 
 
81
    void normalize();
 
82
 
 
83
    inline Point operator+(Point const &o) const {
 
84
        return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
 
85
    }
 
86
    inline Point operator-(Point const &o) const {
 
87
        return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
 
88
    }
 
89
    inline Point &operator+=(Point const &o) {
 
90
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
 
91
            _pt[i] += o._pt[i];
 
92
        }
 
93
        return *this;
 
94
    }  
 
95
    inline Point &operator-=(Point const &o) {
 
96
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
 
97
            _pt[i] -= o._pt[i];
 
98
        }
 
99
        return *this;
 
100
    }
 
101
 
 
102
    inline Point operator-() const {
 
103
        return Point(-_pt[X], -_pt[Y]);
 
104
    }
 
105
    inline Point operator*(double const s) const {
 
106
        return Point(_pt[X] * s, _pt[Y] * s);
 
107
    }
 
108
    inline Point operator/(double const s) const {
 
109
        //TODO: s == 0?
 
110
        return Point(_pt[X] / s, _pt[Y] / s);
 
111
    }
 
112
    inline Point &operator*=(double const s) {
 
113
        for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
 
114
        return *this;
 
115
    }
 
116
    inline Point &operator/=(double const s) {
 
117
        //TODO: s == 0?
 
118
        for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
 
119
        return *this;
 
120
    }
 
121
 
 
122
    Point &operator*=(Matrix const &m);
 
123
 
 
124
    inline int operator == (const Point &in_pnt) {
 
125
        return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
 
126
    }
 
127
 
 
128
    friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
 
129
};
 
130
 
 
131
inline Point operator*(double const s, Point const &p) { return p * s; }
 
132
 
 
133
/** A function to print out the Point.  It just prints out the coords
 
134
    on the given output stream */
 
135
inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
 
136
    out_file << "X: " << in_pnt[X] << "  Y: " << in_pnt[Y];
 
137
    return out_file;
 
138
}
 
139
 
 
140
/** This is a rotation (sort of). */
 
141
inline Point operator^(Point const &a, Point const &b) {
 
142
    Point const ret(a[0] * b[0] - a[1] * b[1],
 
143
                    a[1] * b[0] + a[0] * b[1]);
 
144
    return ret;
 
145
}
 
146
 
 
147
//IMPL: boost::EqualityComparableConcept
 
148
inline bool operator==(Point const &a, Point const &b) {
 
149
    return (a[X] == b[X]) && (a[Y] == b[Y]);
 
150
}
 
151
inline bool operator!=(Point const &a, Point const &b) {
 
152
    return (a[X] != b[X]) || (a[Y] != b[Y]);
 
153
}
 
154
 
 
155
/** This is a lexicographical ordering for points.  It is remarkably useful for sweepline algorithms*/
 
156
inline bool operator<=(Point const &a, Point const &b) {
 
157
    return ( ( a[Y] < b[Y] ) ||
 
158
             (( a[Y] == b[Y] ) && ( a[X] < b[X] )));
 
159
}
 
160
 
 
161
Coord L1(Point const &p);
 
162
 
 
163
/** Compute the L2, or euclidean, norm of \a p. */
 
164
inline Coord L2(Point const &p) { return p.length(); }
 
165
 
 
166
/** Compute the square of L2 norm of \a p. Warning: this can overflow where L2 won't.*/
 
167
inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }
 
168
 
 
169
double LInfty(Point const &p);
 
170
bool is_zero(Point const &p);
 
171
bool is_unit_vector(Point const &p);
 
172
 
 
173
extern double atan2(Point const p);
 
174
/** compute the angle turning from a to b (signed). */
 
175
extern double angle_between(Point const a, Point const b);
 
176
 
 
177
//IMPL: NearConcept
 
178
inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
 
179
    return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
 
180
}
 
181
 
 
182
/** Returns p * Geom::rotate_degrees(90), but more efficient.
 
183
 *
 
184
 * Angle direction in Inkscape code: If you use the traditional mathematics convention that y
 
185
 * increases upwards, then positive angles are anticlockwise as per the mathematics convention.  If
 
186
 * you take the common non-mathematical convention that y increases downwards, then positive angles
 
187
 * are clockwise, as is common outside of mathematics.
 
188
 *
 
189
 * There is no rot_neg90 function: use -rot90(p) instead.
 
190
 */
 
191
inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }
 
192
 
 
193
/** Given two points and a parameter t \in [0, 1], return a point
 
194
 * proportionally from a to b by t.  Akin to 1 degree bezier.*/
 
195
inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }
 
196
 
 
197
Point unit_vector(Point const &a);
 
198
 
 
199
/** compute the dot product (inner product) between the vectors a and b. */
 
200
inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
 
201
/** Defined as dot(a, b.cw()). */
 
202
inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }
 
203
 
 
204
/** compute the euclidean distance between points a and b.  TODO: hypot safer/faster? */
 
205
inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }
 
206
 
 
207
/** compute the square of the distance between points a and b. */
 
208
inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }
 
209
 
 
210
Point abs(Point const &b);
 
211
 
 
212
Point operator*(Point const &v, Matrix const &m);
 
213
 
 
214
Point operator/(Point const &p, Matrix const &m);
 
215
 
 
216
} /* namespace Geom */
 
217
 
 
218
#endif /* !SEEN_Geom_POINT_H */
 
219
 
 
220
/*
 
221
  Local Variables:
 
222
  mode:c++
 
223
  c-file-style:"stroustrup"
 
224
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
225
  indent-tabs-mode:nil
 
226
  fill-column:99
 
227
  End:
 
228
*/
 
229
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :