Update copyright date; remove old CVS keyword
[ctsim.git] / libctsupport / xform.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2009 Kevin Rosenberg
4 **
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.
8 **
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.
13 **
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 ******************************************************************************/
18
19 #include "ctsupport.h"
20
21
22 /* NAME
23  *   rotate2d           Rotates an array of 2 dimensional vectors
24  *
25  * SYNOPSIS
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)
30  */
31
32 void
33 rotate2d (double x[], double y[], int n, double angle)
34 {
35   double cos_theta = cos (angle);
36   double sin_theta = sin (angle);
37
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;
41     x[i] = xrot;
42     y[i] = yrot;
43   }
44 }
45
46
47 /* NAME
48  *   xlat2d                     Translates an array of 2 dimensional vectors
49  *
50  * SYNOPSIS
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
55  */
56
57 void
58 xlat2d (double x[], double y[], int n, double xoffset, double yoffset)
59 {
60   for (int i = 0; i < n; i++) {
61     x[i] += xoffset;
62     y[i] += yoffset;
63   }
64 }
65
66
67 /* NAME
68  *   scale2d                    Scale an array of 2 dimensional vectors
69  *
70  * SYNOPSIS
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
75  */
76
77 void
78 scale2d (double x[], double y[], int n, double xfact, double yfact)
79 {
80   for (int i = 0; i < n; i++) {
81     x[i] *= xfact;
82     y[i] *= yfact;
83   }
84 }
85
86
87 void
88 indent_mtx2 (GRFMTX_2D m)
89 {
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;
93 }
94
95 void
96 xlat_mtx2 (GRFMTX_2D m, const double x, const double y)
97 {
98   indent_mtx2 (m);
99   m[2][0] = x;
100   m[2][1] = y;
101 }
102
103 void
104 scale_mtx2 (GRFMTX_2D m, const double sx, const double sy)
105 {
106   indent_mtx2 (m);
107   m[0][0] = sx;
108   m[1][1] = sy;
109 }
110
111 void
112 rot_mtx2 (GRFMTX_2D m, const double theta)
113 {
114   double c = cos(theta);
115   double s = sin(theta);
116
117   indent_mtx2 (m);
118   m[0][0] =  c;  m[0][1] = s;
119   m[1][0] = -s;  m[1][1] = c;
120 }
121
122 void
123 mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result)
124 {
125   GRFMTX_2D temp;
126
127   for (int row = 0; row < 3; row++) {
128     for (int col = 0; col < 3; col++) {
129       temp[row][col] = 0;
130       for (int calc = 0; calc < 3; calc++)
131             temp[row][col] += m1[row][calc] * m2[calc][col];
132     }
133   }
134
135   for (int r = 0; r < 3; r++)
136     for (int col = 0; col < 3; col++)
137       result[r][col] = temp[r][col];
138 }
139
140 void
141 xform_mtx2 (const GRFMTX_2D m, double& x, double& y)
142 {
143   double xt = x * m[0][0] + y * m[1][0] + m[2][0];
144   double yt = x * m[0][1] + y * m[1][1] + m[2][1];
145
146   x = xt;
147   y = yt;
148 }