1 #include "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 Translate pow(Translate const &t, int n) {
49 return Translate(t[0]*n, t[1]*n);
50 }
52 Coord pow(Coord x, long n) // shamelessly lifted from WP
53 {
54 Coord result = 1;
55 while ( n ) {
56 if ( n & 1 ) {
57 result = result * x;
58 n = n-1;
59 }
60 x = x*x;
61 n = n/2;
62 }
63 return result;
64 }
65 Scale pow(Scale const &s, int n) {
66 return Scale(pow(s[0],n), pow(s[1],n));
68 }
70 Rotate pow(Rotate x, long n)
71 {
72 Rotate result(0,1); // identity
73 while ( n ) {
74 if ( n & 1 ) {
75 result = result * x;
76 n = n-1;
77 }
78 x = x*x;
79 n = n/2;
80 }
81 return result;
82 }
84 Matrix pow(Matrix x, long n)
85 {
86 Matrix result;
87 result.setIdentity();
88 while ( n ) {
89 if ( n & 1 ) {
90 result = result * x;
91 n = n-1;
92 }
93 x = x*x;
94 n = n/2;
95 }
96 return result;
97 }
99 }
101 /*
102 Local Variables:
103 mode:c++
104 c-file-style:"stroustrup"
105 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
106 indent-tabs-mode:nil
107 fill-column:99
108 End:
109 */
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :