Code

e927725b480973afafcb2ba6630b5cf0f099b1d2
[inkscape.git] / src / libnr / nr-point-fns.h
1 #ifndef __NR_POINT_OPS_H__
2 #define __NR_POINT_OPS_H__
4 #include <libnr/nr-point-ops.h>
5 #include <libnr/nr-dim2.h>
6 #include <libnr/nr-macros.h>
8 namespace NR {
10 /** Compute the L1 norm, or manhattan distance, of \a p. */
11 inline Coord L1(Point const &p) {
12         Coord d = 0;
13         for ( int i = 0 ; i < 2 ; i++ ) {
14                 d += fabs(p[i]);
15         }
16         return d;
17 }
19 /** Compute the L2, or euclidean, norm of \a p. */
20 inline Coord L2(Point const &p) {
21         return hypot(p[0], p[1]);
22 }
24 extern double LInfty(Point const &p);
26 bool is_zero(Point const &p);
28 bool is_unit_vector(Point const &p);
30 extern double atan2(Point const p);
32 inline bool point_equalp(Point const &a, Point const &b, double const eps)
33 {
34     return ( NR_DF_TEST_CLOSE(a[X], b[X], eps) &&
35              NR_DF_TEST_CLOSE(a[Y], b[Y], eps) );
36 }
38 /** Returns p * NR::rotate_degrees(90), but more efficient.
39  *
40  * Angle direction in Inkscape code: If you use the traditional mathematics convention that y
41  * increases upwards, then positive angles are anticlockwise as per the mathematics convention.  If
42  * you take the common non-mathematical convention that y increases downwards, then positive angles
43  * are clockwise, as is common outside of mathematics.
44  *
45  * There is no rot_neg90 function: use -rot90(p) instead.
46  */
47 inline Point rot90(Point const &p)
48 {
49     return Point(-p[Y], p[X]);
50 }
52 /** Given two points and a parameter t \in [0, 1], return a point
53  * proportionally from a to b by t. */
54 inline Point Lerp(double const t, Point const a, Point const b)
55 {
56     return ( ( 1 - t ) * a
57              + t * b );
58 }
60 Point unit_vector(Point const &a);
62 inline Coord dot(Point const &a, Point const &b)
63 {
64     Coord ret = 0;
65     for ( int i = 0 ; i < 2 ; i++ ) {
66         ret += a[i] * b[i];
67     }
68     return ret;
69 }
71 inline Coord distance (Point const &a, Point const &b)
72 {
73     Coord ret = 0;
74     for ( int i = 0 ; i < 2 ; i++ ) {
75         ret += (a[i] - b[i]) * (a[i] - b[i]);
76     }
77     return sqrt (ret);
78 }
80 /** Defined as dot(a, b.cw()). */
81 inline Coord cross(Point const &a, Point const &b)
82 {
83     Coord ret = 0;
84     ret -= a[0] * b[1];
85     ret += a[1] * b[0];
86     return ret;
87 }
89 Point abs(Point const &b);
91 } /* namespace NR */
93 NR::Point *get_snap_vector (NR::Point p, NR::Point o, double snap, double initial);
95 NR::Point snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap);
97 double get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end);
99 NR::Point project_on_linesegment(NR::Point const p, NR::Point const p1, NR::Point const p2); 
101 #endif /* !__NR_POINT_OPS_H__ */
103 /*
104   Local Variables:
105   mode:c++
106   c-file-style:"stroustrup"
107   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
108   indent-tabs-mode:nil
109   fill-column:99
110   End:
111 */
112 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :