Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsTransform2D_h___
7 : #define nsTransform2D_h___
8 :
9 : #include "nsCoord.h"
10 :
11 : class nsTransform2D
12 : {
13 : private:
14 : /**
15 : * This represents the following matrix (note that the order of row/column
16 : * indices is opposite to usual notation)
17 : *
18 : * / m00 0 m20 \
19 : * M = | 0 m11 m21 |
20 : * \ 0 0 1 /
21 : *
22 : * Transformation of a coordinate (x, y) is obtained by setting
23 : * v = (x, y, 1)^T and evaluating M . v
24 : **/
25 :
26 : float m00, m11, m20, m21;
27 :
28 : public:
29 0 : nsTransform2D(void) { m20 = m21 = 0.0f; m00 = m11 = 1.0f; }
30 :
31 0 : ~nsTransform2D(void) { }
32 :
33 : /**
34 : * set this transform to a translation
35 : *
36 : * @param tx, x translation
37 : * @param ty, y translation
38 : **/
39 :
40 0 : void SetToTranslate(float tx, float ty) { m00 = m11 = 1.0f; m20 = tx; m21 = ty; }
41 :
42 : /**
43 : * get the translation portion of this transform
44 : *
45 : * @param pt, Point to return translation values in
46 : **/
47 :
48 : void GetTranslationCoord(nscoord *ptX, nscoord *ptY) const { *ptX = NSToCoordRound(m20); *ptY = NSToCoordRound(m21); }
49 :
50 : /**
51 : * apply matrix to vector
52 : *
53 : * @param pt Point to transform
54 : **/
55 :
56 : void TransformCoord(nscoord *ptX, nscoord *ptY) const;
57 :
58 : /**
59 : * apply matrix to rect
60 : *
61 : * @param rect Rect to transform
62 : **/
63 :
64 : void TransformCoord(nscoord *aX, nscoord *aY, nscoord *aWidth, nscoord *aHeight) const;
65 :
66 : /**
67 : * add a scale to a Transform via x, y pair
68 : *
69 : * @param ptX x value to add as x scale
70 : * @param ptY y value to add as y scale
71 : **/
72 :
73 : void AddScale(float ptX, float ptY) { m00 *= ptX; m11 *= ptY; }
74 :
75 : /**
76 : * Set the scale (overriding any previous calls to AddScale, but leaving
77 : * any existing translation).
78 : *
79 : * @param ptX x value to add as x scale
80 : * @param ptY y value to add as y scale
81 : **/
82 :
83 0 : void SetScale(float ptX, float ptY) { m00 = ptX; m11 = ptY; }
84 : };
85 :
86 : #endif
|