This is cgt.c in view mode; [Download] [Up]
/*
cgt - Functions to deal with CGT Matrices
Copyright (C) 1993, Adam Fedor.
*/
#include <math.h>
#include "cgt.h"
#ifndef SQR
#define SQR(a) (a)*(a)
#endif
cgt_t
concatCGT(cgt_t acgt, cgt_t bcgt)
{
cgt_t cgt;
cgt.a = bcgt.a*acgt.a + bcgt.b*acgt.c;
cgt.c = bcgt.c*acgt.a + bcgt.d*acgt.c;
cgt.b = bcgt.a*acgt.b + bcgt.b*acgt.d;
cgt.d = bcgt.c*acgt.b + bcgt.d*acgt.d;
cgt.tx = bcgt.tx*acgt.a + bcgt.ty*acgt.c + acgt.tx;
cgt.ty = bcgt.tx*acgt.b + bcgt.ty*acgt.d + acgt.ty;
return cgt;
}
cgt_t
composeCGT(const NSSize *scale, double rotation, const NSPoint *trans)
{
cgt_t cgt;
rotation *= M_PI / 180.0; /* Convert to radians */
cgt.a = cos(rotation);
cgt.c = -sin(rotation);
cgt.b = -cgt.c;
cgt.d = cgt.a;
if (scale) {
cgt.a *= scale->width;
cgt.b *= scale->width;
cgt.c *= scale->height;
cgt.d *= scale->height;
}
if (trans) {
cgt.tx = trans->x;
cgt.ty = trans->y;
} else {
cgt.tx = 0;
cgt.ty = 0;
}
return cgt;
}
void
decomposeCGT(cgt_t cgt, NSSize *scale, double *rotation, NSPoint *trans)
{
scale->width = sqrt(SQR(cgt.a) + SQR(cgt.b));
scale->height = sqrt(SQR(cgt.c) + SQR(cgt.d));
*rotation = atan2(cgt.b, cgt.a);
*rotation *= 180.0 / M_PI;
trans->x = cgt.tx;
trans->y = cgt.ty;
}
cgt_t
invertCGT(cgt_t cgt)
{
cgt_t invert;
double denom = -cgt.b*cgt.c + cgt.a*cgt.d;
invert.a = cgt.d / denom;
invert.b = -cgt.b / denom;
invert.c = -cgt.c / denom;
invert.d = -cgt.a / denom;
invert.tx = (-cgt.d*cgt.tx + cgt.c*cgt.ty) / denom;
invert.ty = (cgt.b*cgt.tx - cgt.a*cgt.ty) / denom;
return invert;
}
double
rotationFromCGT(cgt_t cgt)
{
NSSize scale;
NSPoint trans;
double rot;
decomposeCGT(cgt, &scale, &rot, &trans);
return rot;
}
NSSize
scaleFromCGT(cgt_t cgt)
{
NSSize scale;
NSPoint trans;
double rot;
decomposeCGT(cgt, &scale, &rot, &trans);
return scale;
}
NSPoint
offsetFromCGT(cgt_t cgt)
{
NSSize scale;
NSPoint trans;
double rot;
decomposeCGT(cgt, &scale, &rot, &trans);
return trans;
}
NSPoint
applyCGT(NSPoint point, cgt_t cgt)
{
double temp;
temp = point.x * cgt.a + point.y * cgt.c + cgt.tx;
point.y = point.x * cgt.b + point.y * cgt.d + cgt.ty;
point.x = temp;
return point;
}
static float m[6];
const float *
cgtToMatrix(cgt_t acgt)
{
m[0] = acgt.a; m[1] = acgt.b; m[2] = acgt.c; m[3] = acgt.d;
m[4] = acgt.tx; m[5] = acgt.ty;
return m;
}
cgt_t
matrixToCgt(const float *m)
{
cgt_t acgt;
acgt.a = m[0]; acgt.b = m[1]; acgt.c = m[2]; acgt.d = m[3];
acgt.tx = m[4]; acgt.ty = m[5];
return acgt;
}These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.