r109: reorganized header files
[ctsim.git] / libctsupport / xform.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: xform.cpp,v 1.2 2000/06/19 19:04:05 kevin Exp $
6 **
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.
10 **
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.
15 **
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 ******************************************************************************/
20
21 #include "ctsupport.h"
22
23
24 /* NAME
25  *   rotate2d           Rotates an array of 2 dimensional vectors
26  *
27  * SYNOPSIS
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)
32  */
33
34 void 
35 rotate2d (double x[], double y[], int n, double angle)
36 {
37   double cos_theta = cos (angle);
38   double sin_theta = sin (angle);
39
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;
43     x[i] = xrot;
44     y[i] = yrot;
45   }
46 }
47
48
49 /* NAME
50  *   xlat2d                     Translates an array of 2 dimensional vectors
51  *
52  * SYNOPSIS
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
57  */
58
59 void 
60 xlat2d (double x[], double y[], int n, double xoffset, double yoffset)
61 {
62   for (int i = 0; i < n; i++) {
63     x[i] += xoffset;
64     y[i] += yoffset;
65   }
66 }
67
68
69 /* NAME
70  *   scale2d                    Scale an array of 2 dimensional vectors
71  *
72  * SYNOPSIS
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
77  */
78
79 void 
80 scale2d (double x[], double y[], int n, double xfact, double yfact)
81 {
82   for (int i = 0; i < n; i++) {
83     x[i] *= xfact;
84     y[i] *= yfact;
85   }
86 }
87
88
89 void 
90 indent_mtx2 (GRFMTX_2D m)
91 {
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;
95 }
96
97 void 
98 xlat_mtx2 (GRFMTX_2D m, const double x, const double y)
99 {
100   indent_mtx2 (m);
101   m[2][0] = x;
102   m[2][1] = y;
103 }
104
105 void 
106 scale_mtx2 (GRFMTX_2D m, const double sx, const double sy)
107 {
108   indent_mtx2 (m);
109   m[0][0] = sx;
110   m[1][1] = sy;
111 }
112
113 void 
114 rot_mtx2 (GRFMTX_2D m, const double theta)
115 {
116   double c = cos(theta);
117   double s = sin(theta);
118
119   indent_mtx2 (m);
120   m[0][0] =  c;  m[0][1] = s;
121   m[1][0] = -s;  m[1][1] = c;
122 }
123
124 void 
125 mult_mtx2 (GRFMTX_2D m1, GRFMTX_2D m2, GRFMTX_2D result)
126 {
127   GRFMTX_2D temp;
128
129   for (int row = 0; row < 3; row++)
130     for (int col = 0; col < 3; col++) {
131       temp[row][col] = 0;
132       for (int calc = 0; calc < 3; calc++)
133         temp[row][col] += m1[row][calc] * m2[calc][col];
134     }
135
136   for (int row = 0; row < 3; row++)
137     for (int col = 0; col < 3; col++)
138       result[row][col] = temp[row][col];
139 }
140
141 void 
142 xform_mtx2 (GRFMTX_2D m, double& x, double& y)
143 {
144   double xt = x * m[0][0] + y * m[1][0] + m[2][0];
145   double yt = x * m[0][1] + y * m[1][1] + m[2][1];
146
147   x = xt;
148   y = yt;
149 }