API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CGAffineTransform.j
Go to the documentation of this file.
1 /*
2  * CGAffineTransform.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 function CGAffineTransformMake(a, b, c, d, tx, ty)
26 {
27  return { a:a, b:b, c:c, d:d, tx:tx, ty:ty };
28 }
29 
31 {
32  return { a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0 };
33 }
34 
35 function CGAffineTransformMakeCopy(anAffineTransform)
36 {
37  return { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
38 }
39 
41 {
42  return { a:sx, b:0.0, c:0.0, d:sy, tx:0.0, ty:0.0 };
43 }
44 
46 {
47  return { a:1.0, b:0.0, c:0.0, d:1.0, tx:tx, ty:ty };
48 }
49 
50 function CGAffineTransformTranslate(aTransform, tx, ty)
51 {
52  return CGAffineTransformMake(aTransform.a, aTransform.b, aTransform.c, aTransform.d, aTransform.tx + aTransform.a * tx + aTransform.c * ty, aTransform.ty + aTransform.b * tx + aTransform.d * ty);
53 }
54 
55 function CGAffineTransformScale(aTransform, sx, sy)
56 {
57  return CGAffineTransformMake(aTransform.a * sx, aTransform.b * sx, aTransform.c * sy, aTransform.d * sy, aTransform.tx, aTransform.ty);
58 }
59 
60 
61 function CGAffineTransformConcat(lhs, rhs)
62 {
63  return CGAffineTransformMake(lhs.a * rhs.a + lhs.b * rhs.c, lhs.a * rhs.b + lhs.b * rhs.d, lhs.c * rhs.a + lhs.d * rhs.c, lhs.c * rhs.b + lhs.d * rhs.d, lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx, lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty);
64 }
65 
66 function CGAffineTransformConcatTo(lhs, rhs, to)
67 {
68  var tx = lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx;
69 
70  to.ty = lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty;
71  to.tx = tx;
72 
73  var a = lhs.a * rhs.a + lhs.b * rhs.c,
74  b = lhs.a * rhs.b + lhs.b * rhs.d,
75  c = lhs.c * rhs.a + lhs.d * rhs.c;
76 
77  to.d = lhs.c * rhs.b + lhs.d * rhs.d;
78  to.a = a;
79  to.b = b;
80  to.c = c;
81 }
82 
83 function CGPointApplyAffineTransform(aPoint, aTransform)
84 {
85  return { x:aPoint.x * aTransform.a + aPoint.y * aTransform.c + aTransform.tx,
86  y:aPoint.x * aTransform.b + aPoint.y * aTransform.d + aTransform.ty };
87 }
88 
89 function CGSizeApplyAffineTransform(aSize, aTransform)
90 {
91  return { width:aSize.width * aTransform.a + aSize.height * aTransform.c,
92  height:aSize.width * aTransform.b + aSize.height * aTransform.d };
93 }
94 
95 
96 function CGAffineTransformIsIdentity(aTransform)
97 {
98  return (aTransform.a === 1.0 &&
99  aTransform.b === 0.0 &&
100  aTransform.c === 0.0 &&
101  aTransform.d === 1.0 &&
102  aTransform.tx === 0.0 &&
103  aTransform.ty === 0.0);
104 }
105 
107 {
108  return (lhs.a === rhs.a &&
109  lhs.b === rhs.b &&
110  lhs.c === rhs.c &&
111  lhs.d === rhs.d &&
112  lhs.tx === rhs.tx &&
113  lhs.ty === rhs.ty);
114 }
115 
116 
118 {
119  return (" [[ " + aTransform.a + ", " + aTransform.b + ", 0 ], [ " + aTransform.c + ", " + aTransform.d + ", 0 ], [ " + aTransform.tx + ", " + aTransform.ty + ", 1]]");
120 }
121 
122 
123 /*
124  FIXME: !!!!
125  @return void
126  @group CGAffineTransform
127 */
129 
139 {
140  var sin = SIN(anAngle),
141  cos = COS(anAngle);
142 
143  return CGAffineTransformMake(cos, sin, -sin, cos, 0.0, 0.0);
144 }
145 
153 function CGAffineTransformRotate(aTransform, anAngle)
154 {
155  var sin = SIN(anAngle),
156  cos = COS(anAngle);
157 
158  return {
159  a:aTransform.a * cos + aTransform.c * sin,
160  b:aTransform.b * cos + aTransform.d * sin,
161  c:aTransform.c * cos - aTransform.a * sin,
162  d:aTransform.d * cos - aTransform.b * sin,
163  tx:aTransform.tx,
164  ty:aTransform.ty
165  };
166 }
167 
174 function CGAffineTransformInvert(aTransform)
175 {
176  var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
177 
178  return {
179  a:determinant * aTransform.d,
180  b:-determinant * aTransform.b,
181  c:-determinant * aTransform.c,
182  d:determinant * aTransform.a,
183  tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
184  ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
185  };
186 }
187 
196 function CGRectApplyAffineTransform(aRect, anAffineTransform)
197 {
198  var top = CGRectGetMinY(aRect),
199  left = CGRectGetMinX(aRect),
200  right = CGRectGetMaxX(aRect),
201  bottom = CGRectGetMaxY(aRect),
202  topLeft = CGPointApplyAffineTransform(CGPointMake(left, top), anAffineTransform),
203  topRight = CGPointApplyAffineTransform(CGPointMake(right, top), anAffineTransform),
204  bottomLeft = CGPointApplyAffineTransform(CGPointMake(left, bottom), anAffineTransform),
205  bottomRight = CGPointApplyAffineTransform(CGPointMake(right, bottom), anAffineTransform),
206  minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
207  maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
208  minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
209  maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
210 
211  return CGRectMake(minX, minY, (maxX - minX), (maxY - minY));
212 }
213 
220 function CPStringFromCGAffineTransform(anAffineTransform)
221 {
222  return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
223 }