Cappuccino  1.0.0
 All Classes Files Functions Variables Typedefs 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 @typedef CGAffineTransform
25 
26 
27 function CGAffineTransformMake(a, b, c, d, tx, ty)
28 {
29  return { a:a, b:b, c:c, d:d, tx:tx, ty:ty };
30 }
31 
33 {
34  return { a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0 };
35 }
36 
37 function CGAffineTransformMakeCopy(anAffineTransform)
38 {
39  return { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
40 }
41 
43 {
44  return { a:sx, b:0.0, c:0.0, d:sy, tx:0.0, ty:0.0 };
45 }
46 
48 {
49  return { a:1.0, b:0.0, c:0.0, d:1.0, tx:tx, ty:ty };
50 }
51 
52 function CGAffineTransformTranslate(aTransform, tx, ty)
53 {
54  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);
55 }
56 
57 function CGAffineTransformScale(aTransform, sx, sy)
58 {
59  return CGAffineTransformMake(aTransform.a * sx, aTransform.b * sx, aTransform.c * sy, aTransform.d * sy, aTransform.tx, aTransform.ty);
60 }
61 
62 
63 function CGAffineTransformConcat(lhs, rhs)
64 {
65  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);
66 }
67 
68 function CGAffineTransformConcatTo(lhs, rhs, to)
69 {
70  var tx = lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx;
71 
72  to.ty = lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty;
73  to.tx = tx;
74 
75  var a = lhs.a * rhs.a + lhs.b * rhs.c,
76  b = lhs.a * rhs.b + lhs.b * rhs.d,
77  c = lhs.c * rhs.a + lhs.d * rhs.c;
78 
79  to.d = lhs.c * rhs.b + lhs.d * rhs.d;
80  to.a = a;
81  to.b = b;
82  to.c = c;
83 }
84 
85 function CGPointApplyAffineTransform(aPoint, aTransform)
86 {
87  return { x:aPoint.x * aTransform.a + aPoint.y * aTransform.c + aTransform.tx,
88  y:aPoint.x * aTransform.b + aPoint.y * aTransform.d + aTransform.ty };
89 }
90 
91 function CGSizeApplyAffineTransform(aSize, aTransform)
92 {
93  return { width:aSize.width * aTransform.a + aSize.height * aTransform.c,
94  height:aSize.width * aTransform.b + aSize.height * aTransform.d };
95 }
96 
97 
98 function CGAffineTransformIsIdentity(aTransform)
99 {
100  return (aTransform.a === 1.0 &&
101  aTransform.b === 0.0 &&
102  aTransform.c === 0.0 &&
103  aTransform.d === 1.0 &&
104  aTransform.tx === 0.0 &&
105  aTransform.ty === 0.0);
106 }
107 
109 {
110  return (lhs.a === rhs.a &&
111  lhs.b === rhs.b &&
112  lhs.c === rhs.c &&
113  lhs.d === rhs.d &&
114  lhs.tx === rhs.tx &&
115  lhs.ty === rhs.ty);
116 }
117 
118 
120 {
121  return (" [[ " + aTransform.a + ", " + aTransform.b + ", 0 ], [ " + aTransform.c + ", " + aTransform.d + ", 0 ], [ " + aTransform.tx + ", " + aTransform.ty + ", 1]]");
122 }
123 
124 
125 /*
126  FIXME: !!!!
127  @return void
128  @group CGAffineTransform
129 */
131 
141 {
142  var sin = SIN(anAngle),
143  cos = COS(anAngle);
144 
145  return CGAffineTransformMake(cos, sin, -sin, cos, 0.0, 0.0);
146 }
147 
155 function CGAffineTransformRotate(aTransform, anAngle)
156 {
157  var sin = SIN(anAngle),
158  cos = COS(anAngle);
159 
160  return {
161  a:aTransform.a * cos + aTransform.c * sin,
162  b:aTransform.b * cos + aTransform.d * sin,
163  c:aTransform.c * cos - aTransform.a * sin,
164  d:aTransform.d * cos - aTransform.b * sin,
165  tx:aTransform.tx,
166  ty:aTransform.ty
167  };
168 }
169 
176 function CGAffineTransformInvert(aTransform)
177 {
178  var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
179 
180  return {
181  a:determinant * aTransform.d,
182  b:-determinant * aTransform.b,
183  c:-determinant * aTransform.c,
184  d:determinant * aTransform.a,
185  tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
186  ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
187  };
188 }
189 
198 function CGRectApplyAffineTransform(aRect, anAffineTransform)
199 {
200  var top = CGRectGetMinY(aRect),
201  left = CGRectGetMinX(aRect),
202  right = CGRectGetMaxX(aRect),
203  bottom = CGRectGetMaxY(aRect),
204  topLeft = CGPointApplyAffineTransform(CGPointMake(left, top), anAffineTransform),
205  topRight = CGPointApplyAffineTransform(CGPointMake(right, top), anAffineTransform),
206  bottomLeft = CGPointApplyAffineTransform(CGPointMake(left, bottom), anAffineTransform),
207  bottomRight = CGPointApplyAffineTransform(CGPointMake(right, bottom), anAffineTransform),
208  minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
209  maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
210  minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
211  maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
212 
213  return CGRectMake(minX, minY, (maxX - minX), (maxY - minY));
214 }
215 
222 function CPStringFromCGAffineTransform(anAffineTransform)
223 {
224  return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
225 }