Logo Search packages:      
Sourcecode: inkscape version File versions

void Geom::Point::normalize (  ) 

Scales this vector to make it a unit vector (within rounding error).

The current version tries to handle infinite coordinates gracefully, but it's not clear that any callers need that.

Precondition:
$this \neq (0, 0)$

Neither component is NaN.

Postcondition:
$-\epsilon<\left|this\right|-1<\epsilon$

Definition at line 18 of file point.cpp.

Referenced by Geom::darray_center_tangent(), Inkscape::Text::Layout::fitToPathAlign(), sp_spiral_get_tangent(), and Geom::unit_vector().

                      {
    double len = hypot(_pt[0], _pt[1]);
    if(len == 0) return;
    if(IS_NAN(len)) return;
    static double const inf = 1e400;
    if(len != inf) {
        *this /= len;
    } else {
        unsigned n_inf_coords = 0;
        /* Delay updating pt in case neither coord is infinite. */
        Point tmp;
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
            if ( _pt[i] == inf ) {
                ++n_inf_coords;
                tmp[i] = 1.0;
            } else if ( _pt[i] == -inf ) {
                ++n_inf_coords;
                tmp[i] = -1.0;
            } else {
                tmp[i] = 0.0;
            }
        }
        switch (n_inf_coords) {
            case 0: {
                /* Can happen if both coords are near +/-DBL_MAX. */
                *this /= 4.0;
                len = hypot(_pt[0], _pt[1]);
                assert(len != inf);
                *this /= len;
                break;
            }
            case 1: {
                *this = tmp;
                break;
            }
            case 2: {
                *this = tmp * sqrt(0.5);
                break;
            }
        }
    }
}


Generated by  Doxygen 1.6.0   Back to index