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
15 #include <2geom/point.h>
17 namespace Inkscape {
19 namespace Util {
21 /**
22 * Returns area in triangle given by points; may be negative.
23 */
24 inline double
25 triangle_area (Geom::Point p1, Geom::Point p2, Geom::Point p3)
26 {
27 using Geom::X;
28 using Geom::Y;
29 return (p1[X]*p2[Y] + p1[Y]*p3[X] + p2[X]*p3[Y] - p2[Y]*p3[X] - p1[Y]*p2[X] - p1[X]*p3[Y]);
30 }
32 /**
33 * \return x rounded to the nearest multiple of c1 plus c0.
34 *
35 * \note
36 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
37 * mean "ignore the grid in this dimension".
38 */
39 inline double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
40 {
41 return floor((x - c0) / c1 + .5) * c1 + c0;
42 }
44 /**
45 * \return x rounded to the lower multiple of c1 plus c0.
46 *
47 * \note
48 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
49 * mean "ignore the grid in this dimension".
50 */
51 inline double round_to_lower_multiple_plus(double x, double const c1, double const c0)
52 {
53 return floor((x - c0) / c1) * c1 + c0;
54 }
56 /**
57 * \return x rounded to the upper multiple of c1 plus c0.
58 *
59 * \note
60 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
61 * mean "ignore the grid in this dimension".
62 */
63 inline double round_to_upper_multiple_plus(double x, double const c1, double const c0)
64 {
65 return ceil((x - c0) / c1) * c1 + c0;
66 }
69 }
71 }
73 #endif
74 /*
75 Local Variables:
76 mode:c++
77 c-file-style:"stroustrup"
78 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
79 indent-tabs-mode:nil
80 fill-column:99
81 End:
82 */
83 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :