PageRenderTime 26ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 1ms

/AppKit/CoreGraphics/CGAffineTransform.j

http://github.com/cacaodev/cappuccino
Unknown | 226 lines | 192 code | 34 blank | 0 comment | 0 complexity | 48155aed13d3a48dc2489c114c45ca2f MD5 | raw 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@import "CGGeometry.j"
 24
 25@typedef CGAffineTransform
 26
 27
 28function CGAffineTransformMake(a, b, c, d, tx, ty)
 29{
 30    return { a:a, b:b, c:c, d:d, tx:tx, ty:ty };
 31}
 32
 33function CGAffineTransformMakeIdentity()
 34{
 35    return { a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0 };
 36}
 37
 38function CGAffineTransformMakeCopy(anAffineTransform)
 39{
 40    return { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
 41}
 42
 43function CGAffineTransformMakeScale(sx, sy)
 44{
 45    return { a:sx, b:0.0, c:0.0, d:sy, tx:0.0, ty:0.0 };
 46}
 47
 48function CGAffineTransformMakeTranslation(tx, ty)
 49{
 50    return { a:1.0, b:0.0, c:0.0, d:1.0, tx:tx, ty:ty };
 51}
 52
 53function CGAffineTransformTranslate(aTransform, tx, ty)
 54{
 55    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);
 56}
 57
 58function CGAffineTransformScale(aTransform, sx, sy)
 59{
 60    return CGAffineTransformMake(aTransform.a * sx, aTransform.b * sx, aTransform.c * sy, aTransform.d * sy, aTransform.tx, aTransform.ty);
 61}
 62
 63
 64function CGAffineTransformConcat(lhs, rhs)
 65{
 66    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);
 67}
 68
 69function CGAffineTransformConcatTo(lhs, rhs, to)
 70{
 71    var tx = lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx;
 72
 73    to.ty = lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty;
 74    to.tx = tx;
 75
 76    var a = lhs.a * rhs.a + lhs.b * rhs.c,
 77        b = lhs.a * rhs.b + lhs.b * rhs.d,
 78        c = lhs.c * rhs.a + lhs.d * rhs.c;
 79
 80    to.d = lhs.c * rhs.b + lhs.d * rhs.d;
 81    to.a = a;
 82    to.b = b;
 83    to.c = c;
 84}
 85
 86function CGPointApplyAffineTransform(aPoint, aTransform)
 87{
 88    return { x:aPoint.x * aTransform.a + aPoint.y * aTransform.c + aTransform.tx,
 89             y:aPoint.x * aTransform.b + aPoint.y * aTransform.d + aTransform.ty };
 90}
 91
 92function CGSizeApplyAffineTransform(aSize, aTransform)
 93{
 94    return { width:aSize.width * aTransform.a + aSize.height * aTransform.c,
 95             height:aSize.width * aTransform.b + aSize.height * aTransform.d };
 96}
 97
 98
 99function CGAffineTransformIsIdentity(aTransform)
100{
101    return (aTransform.a === 1.0 &&
102            aTransform.b === 0.0 &&
103            aTransform.c === 0.0 &&
104            aTransform.d === 1.0 &&
105            aTransform.tx === 0.0 &&
106            aTransform.ty === 0.0);
107}
108
109function CGAffineTransformEqualToTransform(lhs, rhs)
110{
111    return (lhs.a === rhs.a &&
112            lhs.b === rhs.b &&
113            lhs.c === rhs.c &&
114            lhs.d === rhs.d &&
115            lhs.tx === rhs.tx &&
116            lhs.ty === rhs.ty);
117}
118
119
120function CGStringCreateWithCGAffineTransform(aTransform)
121{
122    return (" [[ " + aTransform.a + ", " + aTransform.b + ", 0 ], [ " + aTransform.c + ", " + aTransform.d + ", 0 ], [ " + aTransform.tx + ", " + aTransform.ty + ", 1]]");
123}
124
125
126/*
127    FIXME: !!!!
128    @return void
129    @group CGAffineTransform
130*/
131CGAffineTransformCreateCopy = CGAffineTransformMakeCopy;
132
133/*!
134    Returns a transform that rotates a coordinate system.
135    @param anAngle the amount in radians for the transform
136    to rotate a coordinate system
137    @return CGAffineTransform the transform with a specified
138    rotation
139    @group CGAffineTransform
140*/
141function CGAffineTransformMakeRotation(anAngle)
142{
143    var sin = SIN(anAngle),
144        cos = COS(anAngle);
145
146    return CGAffineTransformMake(cos, sin, -sin, cos, 0.0, 0.0);
147}
148
149/*!
150    Rotates a transform.
151    @param aTransform the transform to rotate
152    @param anAngle the amount to rotate in radians
153    @return void
154    @group CGAffineTransform
155*/
156function CGAffineTransformRotate(aTransform, anAngle)
157{
158    var sin = SIN(anAngle),
159        cos = COS(anAngle);
160
161    return {
162            a:aTransform.a * cos + aTransform.c * sin,
163            b:aTransform.b * cos + aTransform.d * sin,
164            c:aTransform.c * cos - aTransform.a * sin,
165            d:aTransform.d * cos - aTransform.b * sin,
166            tx:aTransform.tx,
167            ty:aTransform.ty
168        };
169}
170
171/*!
172    Inverts a transform.
173    @param aTransform the transform to invert
174    @return CGAffineTransform an inverted transform
175    @group CGAffineTransform
176*/
177function CGAffineTransformInvert(aTransform)
178{
179    var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
180
181    return {
182        a:determinant * aTransform.d,
183        b:-determinant * aTransform.b,
184        c:-determinant * aTransform.c,
185        d:determinant * aTransform.a,
186        tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
187        ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
188    };
189}
190
191/*!
192    Applies a transform to the rectangle's points. The transformed rectangle
193    will be the smallest box that contains the transformed points.
194    @param aRect the rectangle to transform
195    @param anAffineTransform the transform to apply
196    @return CGRect the new transformed rectangle
197    @group CGAffineTransform
198*/
199function CGRectApplyAffineTransform(aRect, anAffineTransform)
200{
201    var top = CGRectGetMinY(aRect),
202        left = CGRectGetMinX(aRect),
203        right = CGRectGetMaxX(aRect),
204        bottom = CGRectGetMaxY(aRect),
205        topLeft = CGPointApplyAffineTransform(CGPointMake(left, top), anAffineTransform),
206        topRight = CGPointApplyAffineTransform(CGPointMake(right, top), anAffineTransform),
207        bottomLeft = CGPointApplyAffineTransform(CGPointMake(left, bottom), anAffineTransform),
208        bottomRight = CGPointApplyAffineTransform(CGPointMake(right, bottom), anAffineTransform),
209        minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
210        maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
211        minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
212        maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
213
214    return CGRectMake(minX, minY, (maxX - minX), (maxY - minY));
215}
216
217/*!
218    Creates and returns a string representation of an affine transform.
219    @param anAffineTransform the transform to represent as a string
220    @return CPString a string describing the transform
221    @group CGAffineTransform
222*/
223function CPStringFromCGAffineTransform(anAffineTransform)
224{
225    return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
226}