darling-cocotron/Foundation/NSAffineTransform.m
Christopher Lloyd 53acf47923 - mass change of unsigned/int to NSUInteger/NSInteger in Foundation and assorted type changes for 64-bit
- Added Darwin x86_64 Foundation/objc target for 64-bit compile testing
2009-05-19 17:14:03 +00:00

150 lines
4.8 KiB
Objective-C
Executable File

/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <Foundation/NSAffineTransform.h>
#import <Foundation/NSRaise.h>
#import <math.h>
@implementation NSAffineTransform
static inline NSAffineTransformStruct multiplyStruct(NSAffineTransformStruct start, NSAffineTransformStruct append){
NSAffineTransformStruct result;
result.m11=append.m11*start.m11+append.m12*start.m21;
result.m12=append.m11*start.m12+append.m12*start.m22;
result.m21=append.m21*start.m11+append.m22*start.m21;
result.m22=append.m21*start.m12+append.m22*start.m22;
result.tX=append.tX*start.m11+append.tY*start.m21+start.tX;
result.tY=append.tX*start.m12+append.tY*start.m22+start.tY;
return result;
}
static inline NSAffineTransformStruct invertStruct(NSAffineTransformStruct matrix){
NSAffineTransformStruct result;
CGFloat determinant;
determinant=matrix.m11*matrix.m22-matrix.m21*matrix.m12;
if(determinant == 0.)
[NSException raise:NSGenericException format:@"NSAffineTransform: Transform has no inverse"];
result.m11=matrix.m22/determinant;
result.m12=-matrix.m12/determinant;
result.m21=-matrix.m21/determinant;
result.m22=matrix.m11/determinant;
result.tX=(-matrix.m22*matrix.tX+matrix.m21*matrix.tY)/determinant;
result.tY=(matrix.m12*matrix.tX-matrix.m11*matrix.tY)/determinant;
return result;
}
-init {
_matrix.m11=1;
_matrix.m12=0;
_matrix.m21=0;
_matrix.m22=1;
_matrix.tX=0;
_matrix.tY=0;
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder {
NSUnimplementedMethod();
}
-initWithCoder:(NSCoder *)coder {
NSUnimplementedMethod();
return self;
}
-initWithTransform:(NSAffineTransform *)other {
// Cocoa doesn't raise when 'other' is nil
_matrix=[other transformStruct];
return self;
}
-copyWithZone:(NSZone *)zone {
return [[[self class] allocWithZone:zone] initWithTransform:self];
}
+(NSAffineTransform *)transform {
return [[self new] autorelease];
}
-(NSAffineTransformStruct)transformStruct {
return _matrix;
}
-(void)setTransformStruct:(NSAffineTransformStruct)matrix {
_matrix=matrix;
}
-(void)invert {
_matrix=invertStruct(_matrix);
}
-(void)appendTransform:(NSAffineTransform *)other {
// Cocoa doesn't raise when 'other' is nil
_matrix=multiplyStruct(_matrix,[other transformStruct]);
}
-(void)prependTransform:(NSAffineTransform *)other {
// Cocoa doesn't raise when 'other' is nil
_matrix=multiplyStruct([other transformStruct],_matrix);
}
-(void)translateXBy:(CGFloat)xby yBy:(CGFloat)yby {
NSAffineTransformStruct translate={1,0,0,1,xby,yby};
_matrix=multiplyStruct(_matrix,translate);
}
-(NSPoint)transformPoint:(NSPoint)point {
NSPoint result;
result.x=_matrix.m11*point.x+_matrix.m21*point.y+_matrix.tX;
result.y=_matrix.m12*point.x+_matrix.m22*point.y+_matrix.tY;
return result;
}
-(NSSize)transformSize:(NSSize)value {
NSSize result;
result.width = _matrix.m11 * value.width + _matrix.m21 * value.height;
result.height = _matrix.m12 * value.width + _matrix.m22 * value.height;
return result;
}
-(void)rotateByDegrees:(CGFloat)angle
{
[self rotateByRadians:M_PI*angle/180.0];
}
-(void)rotateByRadians:(CGFloat)radians
{
NSAffineTransformStruct rotate={cos(radians),sin(radians),-sin(radians),cos(radians),0,0};
_matrix=multiplyStruct(_matrix,rotate);
}
-(void)scaleBy:(CGFloat)value {
NSAffineTransformStruct rotate={value,0,0,value,0,0};
_matrix=multiplyStruct(_matrix,rotate);
}
-(void)scaleXBy:(CGFloat)xvalue yBy:(CGFloat)yvalue {
NSAffineTransformStruct rotate={xvalue,0,0,yvalue,0,0};
_matrix=multiplyStruct(_matrix,rotate);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ 0x%p> {%g, %g, %g, %g, %g, %g}", NSStringFromClass([self class]), self, _matrix.m11, _matrix.m12, _matrix.m21, _matrix.m22, _matrix.tX, _matrix.tY];
}
@end