1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (c) 1983-2009 Kevin Rosenberg
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License (version 2) as
7 ** published by the Free Software Foundation.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 ******************************************************************************/
19 #include "ctsupport.h"
23 * rotate2d Rotates an array of 2 dimensional vectors
26 * rotate2d (x, y, n, angle)
27 * double x[], y[] Array of points
28 * int n Number of points in array
29 * double angle Rotation angle (counter-clockwise)
33 rotate2d (double x[], double y[], int n, double angle)
35 double cos_theta = cos (angle);
36 double sin_theta = sin (angle);
38 for (int i = 0; i < n; i++) {
39 double xrot = x[i] * cos_theta - y[i] * sin_theta;
40 double yrot = x[i] * sin_theta + y[i] * cos_theta;
48 * xlat2d Translates an array of 2 dimensional vectors
51 * xlat2d (x, y, n, xoffset, yoffset)
52 * double x[], y[] Array of points
53 * int n Number of points in array
54 * double xoffset, yoffset Offset to translate points by
58 xlat2d (double x[], double y[], int n, double xoffset, double yoffset)
60 for (int i = 0; i < n; i++) {
68 * scale2d Scale an array of 2 dimensional vectors
71 * scale2d (x, y, n, xoffset, yoffset)
72 * double x[], y[] Array of points
73 * int n Number of points in array
74 * double xfact, yfact x & y scaling factors
78 scale2d (double x[], double y[], int n, double xfact, double yfact)
80 for (int i = 0; i < n; i++) {
88 indent_mtx2 (GRFMTX_2D m)
90 m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0;
91 m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0;
92 m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0;
96 xlat_mtx2 (GRFMTX_2D m, const double x, const double y)
104 scale_mtx2 (GRFMTX_2D m, const double sx, const double sy)
112 rot_mtx2 (GRFMTX_2D m, const double theta)
114 double c = cos(theta);
115 double s = sin(theta);
118 m[0][0] = c; m[0][1] = s;
119 m[1][0] = -s; m[1][1] = c;
123 copy_mtx2 (GRFMTX_2D to, const GRFMTX_2D from) {
124 for (int r = 0; r < 3; r++)
125 for (int c = 0; c < 3; c++)
126 to[r][c] = from[r][c];
130 mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result)
134 for (int row = 0; row < 3; row++) {
135 for (int col = 0; col < 3; col++) {
137 for (int calc = 0; calc < 3; calc++)
138 temp[row][col] += m1[row][calc] * m2[calc][col];
141 copy_mtx2 (result, temp);
145 xform_mtx2 (const GRFMTX_2D m, double& x, double& y)
147 double xt = x * m[0][0] + y * m[1][0] + m[2][0];
148 double yt = x * m[0][1] + y * m[1][1] + m[2][1];