|
| 1 | +// |
| 2 | +// ERCGUtilities.h |
| 3 | +// EngineRoom-iOS |
| 4 | +// |
| 5 | +// Created by Bjoern Kriews on 20.11.11. |
| 6 | +// Copyright (c) 2011 __MyCompanyName__. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#ifndef EngineRoom_iOS_ERCGUtilities_h |
| 10 | +#define EngineRoom_iOS_ERCGUtilities_h |
| 11 | + |
| 12 | +// these CGAffine Utilities were kindly put into the public domain by Jeff LaMarche |
| 13 | +// I like to recommend his blog at http://iphonedevelopment.blogspot.com/ |
| 14 | + |
| 15 | +#import <CoreGraphics/CoreGraphics.h> |
| 16 | + |
| 17 | +#ifdef __cplusplus |
| 18 | +extern "C" { |
| 19 | +#endif |
| 20 | + |
| 21 | +#define ERDegreesToRadian(x) (M_PI * x / 180.0) |
| 22 | +#define ERRadiansToDegrees(x) (180.0 * x / M_PI) |
| 23 | + |
| 24 | + static inline ERCGAffineTransform CGAffineTransformMakeShear(CGFloat shearX, CGFloat shearY) |
| 25 | + { |
| 26 | + return CGAffineTransformMake(1.f, shearY, shearX, 1.f, 0.f, 0.f); |
| 27 | + } |
| 28 | + static inline ERCGAffineTransform CGAffineTransformShear(CGAffineTransform transform, CGFloat shearX, CGFloat shearY) |
| 29 | + { |
| 30 | + CGAffineTransform sheared = ERCGAffineTransformMakeShear(shearX, shearY); |
| 31 | + return CGAffineTransformConcat(transform, sheared); |
| 32 | + } |
| 33 | + static inline CGFloat ERCGAffineTransformGetDeltaX(CGAffineTransform transform) {return transform.tx;}; |
| 34 | + static inline CGFloat ERCGAffineTransformGetDeltaY(CGAffineTransform transform) {return transform.ty;}; |
| 35 | + static inline CGFloat ERCGAffineTransformGetScaleX(CGAffineTransform transform) {return sqrtf( (transform.a * transform.a) + (transform.c * transform.c) );}; |
| 36 | + static inline CGFloat ERCGAffineTransformGetScaleY(CGAffineTransform transform) {return sqrtf( (transform.b * transform.b) + (transform.d * transform.d) );}; |
| 37 | + static inline CGFloat ERCGAffineTransformGetShearX(CGAffineTransform transform) {return transform.b;}; |
| 38 | + static inline CGFloat ERCGAffineTransformGetShearY(CGAffineTransform transform) {return transform.c;}; |
| 39 | + static inline CGFloat ERCGPointAngleBetweenPoints(CGPoint first, CGPoint second) |
| 40 | + { |
| 41 | + CGFloat dy = second.y - first.y; |
| 42 | + CGFloat dx = second.x - first.x; |
| 43 | + return atan2f(dy, dx); |
| 44 | + } |
| 45 | + static inline CGFloat ERCGAffineTransformGetRotation(CGAffineTransform transform) |
| 46 | + { |
| 47 | + // No exact way to get rotation out without knowing order of all previous operations |
| 48 | + // So, we'll cheat. We'll apply the transformation to two points and then determine the |
| 49 | + // angle betwen those two points |
| 50 | + |
| 51 | + CGPoint testPoint1 = CGPointMake(-100.f, 0.f); |
| 52 | + CGPoint testPoint2 = CGPointMake(100.f, 0.f); |
| 53 | + CGPoint transformed1 = CGPointApplyAffineTransform(testPoint1, transform); |
| 54 | + CGPoint transformed2 = CGPointApplyAffineTransform(testPoint2, transform); |
| 55 | + return ERCGPointAngleBetweenPoints(transformed1, transformed2); |
| 56 | + } |
| 57 | + |
| 58 | +#ifdef __cplusplus |
| 59 | +} |
| 60 | +#endif |
| 61 | + |
| 62 | +#endif |
0 commit comments