/**
* Sets the structure which represents the matrix of the reciever. * The struct is of the form:
*{m11, m12, m21, m22, tX, tY}
*/ - (void) setTransformStruct: (NSAffineTransformStruct)val { matrix = val; } /** ** Applies the receiver's transformation matrix to each point in * the bezier path, then returns the result. The original bezier * path is not modified. *
*/ - (NSBezierPath*) transformBezierPath: (NSBezierPath*)aPath { NSBezierPath *path = [aPath copy]; [path transformUsingAffineTransform: self]; return AUTORELEASE(path); } /** * Transforms a single point based on the transformation matrix. * Returns the resulting point. */ - (NSPoint) transformPoint: (NSPoint)aPoint { NSPoint new; new.x = A * aPoint.x + C * aPoint.y + TX; new.y = B * aPoint.x + D * aPoint.y + TY; return new; } /** * Transforms the NSSize represented by aSize using the reciever's * transformation matrix. Returns the resulting NSSize. */ - (NSSize) transformSize: (NSSize)aSize { NSSize new; new.width = A * aSize.width + C * aSize.height; if (new.width < 0) new.width = - new.width; new.height = B * aSize.width + D * aSize.height; if (new.height < 0) new.height = - new.height; return new; } /** *
* Returns the NSAffineTransformStruct
structure
* which represents the matrix of the reciever.
* The struct is of the form:
{m11, m12, m21, m22, tX, tY}
*/ - (NSAffineTransformStruct) transformStruct { return matrix; } /** * Applies the translation specified by tranX and tranY to the receiver's matrix. * Points transformed by the reciever's matrix after this operation will * be shifted in position based on the specified translation. */ - (void) translateXBy: (float)tranX yBy: (float)tranY { TX += tranX; TY += tranY; } - (id) copyWithZone: (NSZone*)zone { return NSCopyObject(self, 0, zone); } - (BOOL) isEqual: (id)anObject { if ([anObject class] == isa) { NSAffineTransform *o = anObject; if (A == o->A && B == o->B && C == o->C && D == o->D && TX == o->TX && TY == o->TY) return YES; } return NO; } - (id) initWithCoder: (NSCoder*)aCoder { float replace[6]; [aCoder decodeArrayOfObjCType: @encode(float) count: 6 at: replace]; [self setMatrix: replace]; return self; } - (void) encodeWithCoder: (NSCoder*)aCoder { float replace[6]; [self getMatrix: replace]; [aCoder encodeArrayOfObjCType: @encode(float) count: 6 at: replace]; } @end /* NSAffineTransform */ @implementation NSAffineTransform (GNUstep) - (void) scaleTo: (float)sx : (float)sy { /* If it's rotated. */ if (B != 0 || C != 0) { float angle = [self rotationAngle]; A = sx; B = 0; C = 0; D = sy; [self rotateByDegrees: angle]; } else { A = sx; B = 0; C = 0; D = sy; } } - (void) translateToPoint: (NSPoint)point { float newTX, newTY; newTX = point.x * A + point.y * C + TX; newTY = point.x * B + point.y * D + TY; TX = newTX; TY = newTY; } - (void) makeIdentityMatrix { matrix = identityTransform; } - (void) setFrameOrigin: (NSPoint)point { float dx = point.x - TX; float dy = point.y - TY; [self translateToPoint: NSMakePoint(dx, dy)]; } - (void) setFrameRotation: (float)angle { [self rotateByDegrees: angle - [self rotationAngle]]; } - (float) rotationAngle { float rotationAngle = atan2(-C, A); rotationAngle *= 180.0 / pi; if (rotationAngle < 0.0) rotationAngle += 360.0; return rotationAngle; } - (void) concatenateWith: (NSAffineTransform*)anotherMatrix { [self prependTransform: anotherMatrix]; } - (void) concatenateWithMatrix: (const float[6])anotherMatrix { float newA, newB, newC, newD, newTX, newTY; newA = anotherMatrix[0] * A + anotherMatrix[1] * C; newB = anotherMatrix[0] * B + anotherMatrix[1] * D; newC = anotherMatrix[2] * A + anotherMatrix[3] * C; newD = anotherMatrix[2] * B + anotherMatrix[3] * D; newTX = anotherMatrix[4] * A + anotherMatrix[5] * C + TX; newTY = anotherMatrix[4] * B + anotherMatrix[5] * D + TY; A = newA; B = newB; C = newC; D = newD; TX = newTX; TY = newTY; } - (void)inverse { [self invert]; } - (BOOL) isRotated { if (B == 0 && C == 0) { return NO; } else { return YES; } } - (void) boundingRectFor: (NSRect)rect result: (NSRect*)new { float angle = [self rotationAngle]; float angleRad = pi * angle / 180; float angle90Rad = pi * (angle + 90) / 180; float cosWidth, cosHeight, sinWidth, sinHeight; /* Shortcuts of the usual rect values */ float x = rect.origin.x; float y = rect.origin.y; float width = rect.size.width; float height = rect.size.height; if (angle == 0) { *new = rect; return; } cosWidth = cos(angleRad); cosHeight = cos(angle90Rad); sinWidth = sin(angleRad); sinHeight = sin(angle90Rad); if (angle <= 90) { new->origin.x = x + height * cosHeight; new->origin.y = y; new->size.width = width * cosWidth - height * cosHeight; new->size.height = width * sinWidth + height * sinHeight; } else if (angle <= 180) { new->origin.x = x + width * cosWidth + height * cosHeight; new->origin.y = y + height * sinHeight; new->size.width = -width * cosWidth - height * cosHeight; new->size.height = width * sinWidth - height * sinHeight; } else if (angle <= 270) { new->origin.x = x + width * cosWidth; new->origin.y = y + width * sinWidth + height * sinHeight; new->size.width = -width * cosWidth + height * cosHeight; new->size.height = -width * sinWidth - height * sinHeight; } else { new->origin.x = x; new->origin.y = y; new->size.width = width * cosWidth + height * cosHeight; new->size.height = width * sinWidth + height * sinHeight; } } - (NSPoint) pointInMatrixSpace: (NSPoint)point { NSPoint new; new.x = A * point.x + C * point.y + TX; new.y = B * point.x + D * point.y + TY; return new; } - (NSPoint) deltaPointInMatrixSpace: (NSPoint)point { NSPoint new; new.x = A * point.x + C * point.y; new.y = B * point.x + D * point.y; return new; } - (NSSize) sizeInMatrixSpace: (NSSize)size { NSSize new; new.width = A * size.width + C * size.height; if (new.width < 0) new.width = - new.width; new.height = B * size.width + D * size.height; if (new.height < 0) new.height = - new.height; return new; } - (NSRect) rectInMatrixSpace: (NSRect)rect { NSRect new; new.origin.x = A * rect.origin.x + C * rect.origin.y + TX; new.size.width = A * rect.size.width + C * rect.size.height; if (new.size.width < 0) { new.origin.x += new.size.width; new.size.width *= -1; } new.origin.y = B * rect.origin.x + D * rect.origin.y + TY; new.size.height = B * rect.size.width + D * rect.size.height; if (new.size.height < 0) { new.origin.y += new.size.height; new.size.height *= -1; } return new; } - (NSString*) description { return [NSString stringWithFormat: @"NSAffineTransform ((%f, %f) (%f, %f) (%f, %f))", A, B, C, D, TX, TY]; } - (void) setMatrix: (const float[6])replace { matrix.m11 = replace[0]; matrix.m12 = replace[1]; matrix.m21 = replace[2]; matrix.m22 = replace[3]; matrix.tX = replace[4]; matrix.tY = replace[5]; } - (void) getMatrix: (float[6])replace { replace[0] = matrix.m11; replace[1] = matrix.m12; replace[2] = matrix.m21; replace[3] = matrix.m22; replace[4] = matrix.tX; replace[5] = matrix.tY; } - (void) takeMatrixFromTransform: (NSAffineTransform *)aTransform { matrix.m11 = aTransform->matrix.m11; matrix.m12 = aTransform->matrix.m12; matrix.m21 = aTransform->matrix.m21; matrix.m22 = aTransform->matrix.m22; matrix.tX = aTransform->matrix.tX; matrix.tY = aTransform->matrix.tY; } @end /* NSAffineTransform (GNUstep) */