Code

When snapping, consider all four grid lines around the current point instead of only...
[inkscape.git] / src / util / mathfns.h
1 /*
2  * Inkscape::Util::... some mathmatical functions 
3  *
4  * Authors:
5  *   Johan Engelen <goejendaagh@zonnet.nl>
6  *
7  * Copyright (C) 2007 Johan Engelen
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #ifndef SEEN_INKSCAPE_UTIL_MATHFNS_H
13 #define SEEN_INKSCAPE_UTIL_MATHFNS_H
16 namespace Inkscape {
18 namespace Util {
20 /**
21  * Returns area in triangle given by points; may be negative.
22  */
23 inline double
24 triangle_area (NR::Point p1, NR::Point p2, NR::Point p3)
25 {
26     return (p1[NR::X]*p2[NR::Y] + p1[NR::Y]*p3[NR::X] + p2[NR::X]*p3[NR::Y] - p2[NR::Y]*p3[NR::X] - p1[NR::Y]*p2[NR::X] - p1[NR::X]*p3[NR::Y]);
27 }
29 /**
30  * \return x rounded to the nearest multiple of c1 plus c0.
31  *
32  * \note
33  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
34  * mean "ignore the grid in this dimension".
35  */
36 inline double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
37 {
38     return floor((x - c0) / c1 + .5) * c1 + c0;
39 }
41 /**
42  * \return x rounded to the lower multiple of c1 plus c0.
43  *
44  * \note
45  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
46  * mean "ignore the grid in this dimension".
47  */
48 inline double round_to_lower_multiple_plus(double x, double const c1, double const c0)
49 {
50     return floor((x - c0) / c1) * c1 + c0;
51 }
53 /**
54  * \return x rounded to the upper multiple of c1 plus c0.
55  *
56  * \note
57  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
58  * mean "ignore the grid in this dimension".
59  */
60 inline double round_to_upper_multiple_plus(double x, double const c1, double const c0)
61 {
62     return ceil((x - c0) / c1) * c1 + c0;
63 }
66 }
68 }
70 #endif
71 /*
72   Local Variables:
73   mode:c++
74   c-file-style:"stroustrup"
75   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
76   indent-tabs-mode:nil
77   fill-column:99
78   End:
79 */
80 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :