point.h revision a4030d5ca449e7e384bc699cd249ee704faaeab0
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould#ifndef SEEN_Geom_POINT_H
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould#define SEEN_Geom_POINT_H
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/**
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti * \file
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * \brief Defines a Cartesian 2D Point class.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen */
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti#include <iostream>
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti#include <2geom/coord.h>
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti#include <2geom/isnan.h>
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti#include <2geom/utils.h>
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchettinamespace Geom {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchettienum Dim2 { X=0, Y=1 };
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetticlass Matrix;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti/// Cartesian 2D point.
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetticlass Point {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti Coord _pt[2];
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti public:
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti /// The default constructor creates an Point(0,0) DO NOT RELY ON THIS, BEST NOT TO USE THIS CONSTRUCTOR
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point()
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti { _pt[X] = _pt[Y] = 0; }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point(Coord x, Coord y) {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti _pt[X] = x; _pt[Y] = y;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point(Point const &p) {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti for (unsigned i = 0; i < 2; ++i)
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti _pt[i] = p._pt[i];
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
8001ba81cb851b38d86650a2fef5817facffb763johanengelen inline Point &operator=(Point const &p) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen for (unsigned i = 0; i < 2; ++i)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[i] = p._pt[i];
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return *this;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Coord operator[](unsigned i) const { return _pt[i]; }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Coord &operator[](unsigned i) { return _pt[i]; }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Coord operator[](Dim2 d) const throw() { return _pt[d]; }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Coord &operator[](Dim2 d) throw() { return _pt[d]; }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen static inline Point polar(Coord angle, Coord radius) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return Point(radius * std::cos(angle), radius * std::sin(angle));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Coord length() const { return hypot(_pt[0], _pt[1]); }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen /** Return a point like this point but rotated -90 degrees.
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti (If the y axis grows downwards and the x axis grows to the
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen right, then this is 90 degrees counter-clockwise.)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen **/
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti Point ccw() const {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return Point(_pt[Y], -_pt[X]);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen /** Return a point like this point but rotated +90 degrees.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen (If the y axis grows downwards and the x axis grows to the
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen right, then this is 90 degrees clockwise.)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen **/
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Point cw() const {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return Point(-_pt[Y], _pt[X]);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen /**
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen \brief A function to lower the precision of the point
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen \param places The number of decimal places that should be in
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen the final number.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen */
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline void round (int places = 0) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[X] = (Coord)(decimal_round((double)_pt[X], places));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen void normalize();
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline bool isFinite() const {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen for ( unsigned i = 0 ; i < 2 ; ++i ) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen if(!IS_FINITE(_pt[i])) return false;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return true;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Point operator+(Point const &o) const {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Point operator-(Point const &o) const {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Point &operator+=(Point const &o) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen for ( unsigned i = 0 ; i < 2 ; ++i ) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[i] += o._pt[i];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen inline Point &operator-=(Point const &o) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen for ( unsigned i = 0 ; i < 2 ; ++i ) {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti _pt[i] -= o._pt[i];
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return *this;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point operator-() const {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return Point(-_pt[X], -_pt[Y]);
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point operator*(double const s) const {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return Point(_pt[X] * s, _pt[Y] * s);
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point operator/(double const s) const {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti //TODO: s == 0?
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return Point(_pt[X] / s, _pt[Y] / s);
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point &operator*=(double const s) {
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti return *this;
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti }
6e16a663ee96cd1329e48518138efb415046d9f6mcecchetti inline Point &operator/=(double const s) {
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan //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) );
}
inline
Point middle_point(Point const& P1, Point const& P2)
{
return (P1 + P2) / 2;
}
/** 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);
/** Constrains the angle (with respect to dir) of the line
* joining A and B to a multiple of pi/n.
*/
Point constrain_angle(Point const &A, Point const &B, unsigned int n = 4, Geom::Point const &dir = Geom::Point(1,0));
} /* 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:fileencoding=utf-8:textwidth=99 :