4
#include "isnan.h" //temporary fix for isnan()
11
/** Scales this vector to make it a unit vector (within rounding error).
13
* The current version tries to handle infinite coordinates gracefully,
14
* but it's not clear that any callers need that.
16
* \pre \f$this \neq (0, 0)\f$
17
* \pre Neither component is NaN.
18
* \post \f$-\epsilon<\left|this\right|-1<\epsilon\f$
20
void Point::normalize() {
21
double len = hypot(_pt[0], _pt[1]);
23
if(is_nan(len)) return;
24
static double const inf = std::numeric_limits<double>::infinity();
28
unsigned n_inf_coords = 0;
29
/* Delay updating pt in case neither coord is infinite. */
31
for ( unsigned i = 0 ; i < 2 ; ++i ) {
32
if ( _pt[i] == inf ) {
35
} else if ( _pt[i] == -inf ) {
42
switch (n_inf_coords) {
44
/* Can happen if both coords are near +/-DBL_MAX. */
46
len = hypot(_pt[0], _pt[1]);
56
*this = tmp * sqrt(0.5);
63
/** Compute the L1 norm, or manhattan distance, of \a p. */
64
Coord L1(Point const &p) {
66
for ( int i = 0 ; i < 2 ; i++ ) {
72
/** Compute the L infinity, or maximum, norm of \a p. */
73
Coord LInfty(Point const &p) {
74
Coord const a(fabs(p[0]));
75
Coord const b(fabs(p[1]));
76
return ( a < b || is_nan(b)
81
/** Returns true iff p is a zero vector, i.e.\ Point(0, 0).
83
* (NaN is considered non-zero.)
86
is_zero(Point const &p)
93
is_unit_vector(Point const &p)
95
return fabs(1.0 - L2(p)) <= 1e-4;
96
/* The tolerance of 1e-4 is somewhat arbitrary. Point::normalize is believed to return
97
points well within this tolerance. I'm not aware of any callers that want a small
98
tolerance; most callers would be ok with a tolerance of 0.25. */
101
Coord atan2(Point const p) {
102
return std::atan2(p[Y], p[X]);
105
/** compute the angle turning from a to b. This should give \f$\pi/2\f$ for angle_between(a, rot90(a));
106
* This works by projecting b onto the basis defined by a, rot90(a)
108
Coord angle_between(Point const a, Point const b) {
109
return std::atan2(cross(b,a), dot(b,a));
114
/** Returns a version of \a a scaled to be a unit vector (within rounding error).
116
* The current version tries to handle infinite coordinates gracefully,
117
* but it's not clear that any callers need that.
119
* \pre a != Point(0, 0).
120
* \pre Neither coordinate is NaN.
121
* \post L2(ret) very near 1.0.
123
Point unit_vector(Point const &a)
130
Point abs(Point const &b)
133
for ( int i = 0 ; i < 2 ; i++ ) {
139
Point operator*(Point const &v, Matrix const &m) {
141
for(int i = 0; i < 2; i++) {
142
ret[i] = v[X] * m[i] + v[Y] * m[i + 2] + m[i + 4];
147
Point operator/(Point const &p, Matrix const &m) { return p * m.inverse(); }
149
Point &Point::operator*=(Matrix const &m)
160
c-file-style:"stroustrup"
161
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :