API  0.9.6
 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 #define _function(inline) function inline { return _##inline; }
26 
27 _function(CGAffineTransformMake(a, b, c, d, tx, ty))
28 _function(CGAffineTransformMakeIdentity())
29 _function(CGAffineTransformMakeCopy(anAffineTransform))
30 
31 _function(CGAffineTransformMakeScale(sx, sy))
32 _function(CGAffineTransformMakeTranslation(tx, ty))
33 _function(CGAffineTransformTranslate(aTransform, tx, ty))
34 _function(CGAffineTransformScale(aTransform, sx, sy))
35 
36 _function(CGAffineTransformConcat(lhs, rhs))
37 _function(CGPointApplyAffineTransform(aPoint, aTransform))
38 _function(CGSizeApplyAffineTransform(aSize, aTransform))
39 
40 _function(CGAffineTransformIsIdentity(aTransform))
41 _function(CGAffineTransformEqualToTransform(lhs, rhs))
42 
43 _function(CGStringCreateWithCGAffineTransform(aTransform))
44 
45 /*
46  FIXME: !!!!
47  @return void
48  @group CGAffineTransform
49 */
50 function CGAffineTransformCreateCopy(aTransform)
51 {
52  return _CGAffineTransformMakeCopy(aTransform);
53 }
54 
64 {
65  var sin = SIN(anAngle),
66  cos = COS(anAngle);
67 
68  return _CGAffineTransformMake(cos, sin, -sin, cos, 0.0, 0.0);
69 }
70 
78 function CGAffineTransformRotate(aTransform, anAngle)
79 {
80  var sin = SIN(anAngle),
81  cos = COS(anAngle);
82 
83  return {
84  a:aTransform.a * cos + aTransform.c * sin,
85  b:aTransform.b * cos + aTransform.d * sin,
86  c:aTransform.c * cos - aTransform.a * sin,
87  d:aTransform.d * cos - aTransform.b * sin,
88  tx:aTransform.tx,
89  ty:aTransform.ty
90  };
91 }
92 
99 function CGAffineTransformInvert(aTransform)
100 {
101  var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
102 
103  return {
104  a:determinant * aTransform.d,
105  b:-determinant * aTransform.b,
106  c:-determinant * aTransform.c,
107  d:determinant * aTransform.a,
108  tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
109  ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
110  };
111 }
112 
121 function CGRectApplyAffineTransform(aRect, anAffineTransform)
122 {
123  var top = _CGRectGetMinY(aRect),
124  left = _CGRectGetMinX(aRect),
125  right = _CGRectGetMaxX(aRect),
126  bottom = _CGRectGetMaxY(aRect),
127  topLeft = CGPointApplyAffineTransform(_CGPointMake(left, top), anAffineTransform),
128  topRight = CGPointApplyAffineTransform(_CGPointMake(right, top), anAffineTransform),
129  bottomLeft = CGPointApplyAffineTransform(_CGPointMake(left, bottom), anAffineTransform),
130  bottomRight = CGPointApplyAffineTransform(_CGPointMake(right, bottom), anAffineTransform),
131  minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
132  maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
133  minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
134  maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
135 
136  return _CGRectMake(minX, minY, (maxX - minX), (maxY - minY));
137 }
138 
145 function CPStringFromCGAffineTransform(anAffineTransform)
146 {
147  return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
148 }