LibreOffice Module vcl (master) 1
Matrix3.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11#include <pdf/Matrix3.hxx>
12#include <cmath>
13
14namespace vcl::pdf
15{
17{
18 // initialize to unity
19 f[0] = 1.0;
20 f[1] = 0.0;
21 f[2] = 0.0;
22 f[3] = 1.0;
23 f[4] = 0.0;
24 f[5] = 0.0;
25}
26
27Point Matrix3::transform(const Point& rOrig) const
28{
29 double x = static_cast<double>(rOrig.X()), y = static_cast<double>(rOrig.Y());
30 return Point(x * f[0] + y * f[2] + f[4], x * f[1] + y * f[3] + f[5]);
31}
32
33void Matrix3::skew(double alpha, double beta)
34{
35 double fn[6];
36 double tb = tan(beta);
37 fn[0] = f[0] + f[2] * tb;
38 fn[1] = f[1];
39 fn[2] = f[2] + f[3] * tb;
40 fn[3] = f[3];
41 fn[4] = f[4] + f[5] * tb;
42 fn[5] = f[5];
43 if (alpha != 0.0)
44 {
45 double ta = tan(alpha);
46 fn[1] += f[0] * ta;
47 fn[3] += f[2] * ta;
48 fn[5] += f[4] * ta;
49 }
50 set(fn);
51}
52
53void Matrix3::scale(double sx, double sy)
54{
55 double fn[6];
56 fn[0] = sx * f[0];
57 fn[1] = sy * f[1];
58 fn[2] = sx * f[2];
59 fn[3] = sy * f[3];
60 fn[4] = sx * f[4];
61 fn[5] = sy * f[5];
62 set(fn);
63}
64
65void Matrix3::rotate(double angle)
66{
67 double fn[6];
68 double fSin = sin(angle);
69 double fCos = cos(angle);
70 fn[0] = f[0] * fCos - f[1] * fSin;
71 fn[1] = f[0] * fSin + f[1] * fCos;
72 fn[2] = f[2] * fCos - f[3] * fSin;
73 fn[3] = f[2] * fSin + f[3] * fCos;
74 fn[4] = f[4] * fCos - f[5] * fSin;
75 fn[5] = f[4] * fSin + f[5] * fCos;
76 set(fn);
77}
78
79void Matrix3::translate(double tx, double ty)
80{
81 f[4] += tx;
82 f[5] += ty;
83}
84
86{
87 // short circuit trivial cases
88 if (f[1] == f[2] && f[1] == 0.0 && f[0] == f[3] && f[0] == 1.0)
89 {
90 f[4] = -f[4];
91 f[5] = -f[5];
92 return;
93 }
94
95 // check determinant
96 const double fDet = f[0] * f[3] - f[1] * f[2];
97 if (fDet == 0.0)
98 return;
99
100 // invert the matrix
101 double fn[6];
102 fn[0] = +f[3] / fDet;
103 fn[1] = -f[1] / fDet;
104 fn[2] = -f[2] / fDet;
105 fn[3] = +f[0] / fDet;
106
107 // apply inversion to translation
108 fn[4] = -(f[4] * fn[0] + f[5] * fn[2]);
109 fn[5] = -(f[4] * fn[1] + f[5] * fn[3]);
110
111 set(fn);
112}
113
114} // end vcl::pdf
115
116/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void translate(double tx, double ty)
Definition: Matrix3.cxx:79
Point transform(const Point &rPoint) const
Definition: Matrix3.cxx:27
double f[6]
Definition: Matrix3.hxx:28
void rotate(double angle)
Definition: Matrix3.cxx:65
void set(const double *pn)
Definition: Matrix3.hxx:30
void scale(double sx, double sy)
Definition: Matrix3.cxx:53
void skew(double alpha, double beta)
Definition: Matrix3.cxx:33
float y
float x