Code

Update to 2geom rev. 1538
[inkscape.git] / src / 2geom / transforms.cpp
1 #include <2geom/transforms.h>
3 namespace Geom {
5 Matrix operator*(Translate const &t, Scale const &s) {
6     Matrix ret(s);
7     ret[4] = t[X] * s[X];
8     ret[5] = t[Y] * s[Y];
9     return ret;
10 }
12 Matrix operator*(Translate const &t, Rotate const &r) {
13     Matrix ret(r);
14     ret.setTranslation(t.vec * ret);
15     return ret;
16 }
18 Matrix operator*(Scale const &s, Translate const &t) {
19     return Matrix(s[0], 0,
20                   0   , s[1],
21                   t[0], t[1]);
22 }
24 Matrix operator*(Scale const &s, Matrix const &m) {
25     Matrix ret(m);
26     ret[0] *= s[X];
27     ret[1] *= s[X];
28     ret[2] *= s[Y];
29     ret[3] *= s[Y];
30     return ret;
31 }
33 Matrix operator*(Matrix const &m, Translate const &t) {
34     Matrix ret(m);
35     ret[4] += t[X];
36     ret[5] += t[Y];
37     return ret;
38 }
40 Matrix operator*(Matrix const &m, Scale const &s) {
41     Matrix ret(m);
42     ret[0] *= s[X]; ret[1] *= s[Y];
43     ret[2] *= s[X]; ret[3] *= s[Y];
44     ret[4] *= s[X]; ret[5] *= s[Y];
45     return ret;
46 }
48 Matrix operator*(Matrix const &m, Rotate const &r) {
49     // TODO: we just convert the Rotate to a matrix and use the existing operator*(); is there a better way?
50     Matrix ret(m);
51     ret *= (Matrix) r;
52     return ret;
53 }
55 Translate pow(Translate const &t, int n) {
56     return Translate(t[0]*n, t[1]*n);
57 }
59 Coord pow(Coord x, long n) // shamelessly lifted from WP
60 {
61     Coord result = 1;
62     while ( n ) {
63         if ( n & 1 ) {
64             result = result * x;
65             n = n-1;
66         }
67         x = x*x;
68         n = n/2;
69     }
70     return result;
71 }
72 Scale pow(Scale const &s, int n) {
73     return Scale(pow(s[0],n), pow(s[1],n));
75 }
77 Rotate pow(Rotate x, long n)
78 {
79     Rotate result(0,1); // identity
80     while ( n ) {
81         if ( n & 1 ) {
82             result = result * x;
83             n = n-1;
84         }
85         x = x*x;
86         n = n/2;
87     }
88     return result;
89 }
91 Matrix pow(Matrix x, long n)
92 {
93     Matrix result;
94     result.setIdentity();
95     while ( n ) {
96         if ( n & 1 ) {
97             result = result * x;
98             n = n-1;
99         }
100         x = x*x;
101         n = n/2;
102     }
103     return result;
108 /*
109   Local Variables:
110   mode:c++
111   c-file-style:"stroustrup"
112   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
113   indent-tabs-mode:nil
114   fill-column:99
115   End:
116 */
117 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :