00001
00002
00003
00004
00005
00006
00007
00038
00039
00040
00041
00042 #include <math.h>
00043 #include "t2d.h"
00044
00045
00046
00047
00048
00049 #ifndef rad
00050 #define rad(a) ((a)*(double)0.01745329252)
00051 #endif
00052
00055 typedef double Matrix2d[2][3];
00056
00059 static Matrix2d mt =
00060 {
00061 { 1.0, 0.0, 0.0 },
00062 { 0.0, 1.0, 0.0 }
00063 };
00064
00065
00066
00067
00068
00069
00070
00077 static void t2dAccumulate( Matrix2d nmt )
00078 {
00079 double t;
00080
00081 t = mt[0][0]*nmt[0][0] + mt[1][0]*nmt[0][1];
00082 mt[1][0] = mt[0][0]*nmt[1][0] + mt[1][0]*nmt[1][1];
00083 mt[0][0] = t;
00084
00085 t = mt[0][1]*nmt[0][0] + mt[1][1]*nmt[0][1];
00086 mt[1][1] = mt[0][1]*nmt[1][0] + mt[1][1]*nmt[1][1];
00087 mt[0][1] = t;
00088
00089 t = mt[0][2]*nmt[0][0] + mt[1][2]*nmt[0][1] + nmt[0][2];
00090 mt[1][2] = mt[0][2]*nmt[1][0] + mt[1][2]*nmt[1][1] + nmt[1][2];
00091 mt[0][2] = t;
00092 }
00093
00094
00095
00096
00097
00098
00099
00107 void t2dTransform( double *x, double *y )
00108 {
00109 double t;
00110 t = (*x)*mt[0][0] + (*y)*mt[0][1] + mt[0][2];
00111 *y = (*x)*mt[1][0] + (*y)*mt[1][1] + mt[1][2];
00112 *x = t;
00113 }
00114
00115
00120 void t2dIdentity( void )
00121 {
00122 mt[0][0] = 1.0; mt[0][1] = 0.0; mt[0][2] = 0.0;
00123 mt[1][0] = 0.0; mt[1][1] = 1.0; mt[1][2] = 0.0;
00124 }
00125
00126
00134 void t2dTranslate( double dx, double dy )
00135 {
00136 Matrix2d trl;
00137
00138
00139
00140 t2dAccumulate( trl );
00141 }
00142
00143
00152 void t2dScale( double sx, double sy )
00153 {
00154 Matrix2d scl;
00155
00156
00157
00158 t2dAccumulate( scl );
00159 }
00160
00161
00172 void t2dScaleAbout( double cx, double cy, double sx, double sy )
00173 {
00174 t2dTranslate( -cx, -cy );
00175 t2dScale( sx, sy );
00176 t2dTranslate( cx, cy );
00177 }
00178
00179
00188 void t2dRotate( double angle )
00189 {
00190 double cost = cos(rad(angle));
00191 double sint = sin(rad(angle));
00192 Matrix2d rot;
00193
00194
00195
00196 t2dAccumulate( rot );
00197 }
00198
00199
00210 void t2dRotateAbout( double cx, double cy, double angle )
00211 {
00212 t2dTranslate( -cx, -cy );
00213 t2dRotate( angle );
00214 t2dTranslate( cx, cy );
00215 }
00216