/ CoreGraphics / CGAffineTransform.m
CGAffineTransform.m
  1  /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
  2  
  3  Permission is hereby granted, free of charge, to any person obtaining a copy of
  4  this software and associated documentation files (the "Software"), to deal in
  5  the Software without restriction, including without limitation the rights to
  6  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7  the Software, and to permit persons to whom the Software is furnished to do so,
  8  subject to the following conditions:
  9  
 10  The above copyright notice and this permission notice shall be included in all
 11  copies or substantial portions of the Software.
 12  
 13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 15  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 16  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 17  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 18  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
 19  
 20  #import <CoreGraphics/CGAffineTransform.h>
 21  
 22  #include <stdio.h>
 23  
 24  const CGAffineTransform CGAffineTransformIdentity = {1, 0, 0, 1, 0, 0};
 25  
 26  bool CGAffineTransformIsIdentity(CGAffineTransform xform) {
 27      return xform.a == 1 && xform.b == 0 && xform.c == 0 && xform.d == 1 &&
 28             xform.tx == 0 && xform.ty == 0;
 29  }
 30  
 31  CGAffineTransform CGAffineTransformMakeRotation(CGFloat radians) {
 32      CGAffineTransform xform = {
 33              cos(radians), sin(radians), -sin(radians), cos(radians), 0, 0};
 34      return xform;
 35  }
 36  
 37  CGAffineTransform CGAffineTransformMakeScale(CGFloat scalex, CGFloat scaley) {
 38      CGAffineTransform xform = {scalex, 0, 0, scaley, 0, 0};
 39      return xform;
 40  }
 41  
 42  CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty) {
 43      CGAffineTransform xform = {1, 0, 0, 1, tx, ty};
 44      return xform;
 45  }
 46  
 47  CGAffineTransform CGAffineTransformConcat(CGAffineTransform xform,
 48                                            CGAffineTransform append)
 49  {
 50      CGAffineTransform result;
 51  
 52      result.a = xform.a * append.a + xform.b * append.c;
 53      result.b = xform.a * append.b + xform.b * append.d;
 54      result.c = xform.c * append.a + xform.d * append.c;
 55      result.d = xform.c * append.b + xform.d * append.d;
 56      result.tx = xform.tx * append.a + xform.ty * append.c + append.tx;
 57      result.ty = xform.tx * append.b + xform.ty * append.d + append.ty;
 58  
 59      return result;
 60  }
 61  
 62  CGAffineTransform CGAffineTransformInvert(CGAffineTransform xform) {
 63      CGAffineTransform result;
 64      CGFloat determinant;
 65  
 66      determinant = xform.a * xform.d - xform.c * xform.b;
 67      if (determinant == 0) {
 68          return xform;
 69      }
 70  
 71      result.a = xform.d / determinant;
 72      result.b = -xform.b / determinant;
 73      result.c = -xform.c / determinant;
 74      result.d = xform.a / determinant;
 75      result.tx = (-xform.d * xform.tx + xform.c * xform.ty) / determinant;
 76      result.ty = (xform.b * xform.tx - xform.a * xform.ty) / determinant;
 77  
 78      return result;
 79  }
 80  
 81  CGAffineTransform CGAffineTransformRotate(CGAffineTransform xform,
 82                                            CGFloat radians)
 83  {
 84      CGAffineTransform rotate = CGAffineTransformMakeRotation(radians);
 85      return CGAffineTransformConcat(rotate, xform);
 86  }
 87  
 88  CGAffineTransform CGAffineTransformScale(CGAffineTransform xform,
 89                                           CGFloat scalex, CGFloat scaley)
 90  {
 91      CGAffineTransform scale = CGAffineTransformMakeScale(scalex, scaley);
 92      return CGAffineTransformConcat(scale, xform);
 93  }
 94  
 95  CGAffineTransform CGAffineTransformTranslate(CGAffineTransform xform,
 96                                               CGFloat tx, CGFloat ty)
 97  {
 98      CGAffineTransform translate = CGAffineTransformMakeTranslation(tx, ty);
 99      return CGAffineTransformConcat(translate, xform);
100  }
101  
102  CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t) {
103      printf("CGRectApplyAffineTransform STUB\n");
104      return rect;
105  }