1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: xform.cpp,v 1.5 2001/01/02 16:02:13 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
21 #include "ctsupport.h"
25 * rotate2d Rotates an array of 2 dimensional vectors
28 * rotate2d (x, y, n, angle)
29 * double x[], y[] Array of points
30 * int n Number of points in array
31 * double angle Rotation angle (counter-clockwise)
35 rotate2d (double x[], double y[], int n, double angle)
37 double cos_theta = cos (angle);
38 double sin_theta = sin (angle);
40 for (int i = 0; i < n; i++) {
41 double xrot = x[i] * cos_theta - y[i] * sin_theta;
42 double yrot = x[i] * sin_theta + y[i] * cos_theta;
50 * xlat2d Translates an array of 2 dimensional vectors
53 * xlat2d (x, y, n, xoffset, yoffset)
54 * double x[], y[] Array of points
55 * int n Number of points in array
56 * double xoffset, yoffset Offset to translate points by
60 xlat2d (double x[], double y[], int n, double xoffset, double yoffset)
62 for (int i = 0; i < n; i++) {
70 * scale2d Scale an array of 2 dimensional vectors
73 * scale2d (x, y, n, xoffset, yoffset)
74 * double x[], y[] Array of points
75 * int n Number of points in array
76 * double xfact, yfact x & y scaling factors
80 scale2d (double x[], double y[], int n, double xfact, double yfact)
82 for (int i = 0; i < n; i++) {
90 indent_mtx2 (GRFMTX_2D m)
92 m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0;
93 m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0;
94 m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0;
98 xlat_mtx2 (GRFMTX_2D m, const double x, const double y)
106 scale_mtx2 (GRFMTX_2D m, const double sx, const double sy)
114 rot_mtx2 (GRFMTX_2D m, const double theta)
116 double c = cos(theta);
117 double s = sin(theta);
120 m[0][0] = c; m[0][1] = s;
121 m[1][0] = -s; m[1][1] = c;
125 mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result)
129 for (int row = 0; row < 3; row++) {
130 for (int col = 0; col < 3; col++) {
132 for (int calc = 0; calc < 3; calc++)
133 temp[row][col] += m1[row][calc] * m2[calc][col];
137 for (int r = 0; r < 3; r++)
138 for (int col = 0; col < 3; col++)
139 result[r][col] = temp[r][col];
143 xform_mtx2 (const GRFMTX_2D m, double& x, double& y)
145 double xt = x * m[0][0] + y * m[1][0] + m[2][0];
146 double yt = x * m[0][1] + y * m[1][1] + m[2][1];