r99: *** empty log message ***
[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.1 2000/06/19 02:58:08 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 "kmath.h"
22
23 /* NAME
24  *   rotate2d           Rotates an array of 2 dimensional vectors
25  *
26  * SYNOPSIS
27  *   rotate2d (x, y, n, angle)
28  *   double x[], y[]    Array of points
29  *   int n              Number of points in array
30  *   double angle       Rotation angle (counter-clockwise)
31  */
32
33 void 
34 rotate2d (double x[], double y[], int n, double angle)
35 {
36   double cos_theta = cos (angle);
37   double sin_theta = sin (angle);
38
39   for (int i = 0; i < n; i++) {
40     double xrot = x[i] * cos_theta - y[i] * sin_theta;
41     double yrot = x[i] * sin_theta + y[i] * cos_theta;
42     x[i] = xrot;
43     y[i] = yrot;
44   }
45 }
46
47
48 /* NAME
49  *   xlat2d                     Translates an array of 2 dimensional vectors
50  *
51  * SYNOPSIS
52  *   xlat2d (x, y, n, xoffset, yoffset)
53  *   double x[], y[]            Array of points
54  *   int n                      Number of points in array
55  *   double xoffset, yoffset    Offset to translate points by
56  */
57
58 void 
59 xlat2d (double x[], double y[], int n, double xoffset, double yoffset)
60 {
61   for (int i = 0; i < n; i++) {
62     x[i] += xoffset;
63     y[i] += yoffset;
64   }
65 }
66
67
68 /* NAME
69  *   scale2d                    Scale an array of 2 dimensional vectors
70  *
71  * SYNOPSIS
72  *   scale2d (x, y, n, xoffset, yoffset)
73  *   double x[], y[]            Array of points
74  *   int n                      Number of points in array
75  *   double xfact, yfact        x & y scaling factors
76  */
77
78 void 
79 scale2d (double x[], double y[], int n, double xfact, double yfact)
80 {
81   for (int i = 0; i < n; i++) {
82     x[i] *= xfact;
83     y[i] *= yfact;
84   }
85 }
86
87
88 void 
89 indent_mtx2 (GRFMTX_2D m)
90 {
91   m[0][0] = 1.0;  m[0][1] = 0.0;  m[0][2] = 0.0;
92   m[1][0] = 0.0;  m[1][1] = 1.0;  m[1][2] = 0.0;
93   m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] = 1.0;
94 }
95
96 void 
97 xlat_mtx2 (GRFMTX_2D m, const double x, const double y)
98 {
99   indent_mtx2 (m);
100   m[2][0] = x;
101   m[2][1] = y;
102 }
103
104 void 
105 scale_mtx2 (GRFMTX_2D m, const double sx, const double sy)
106 {
107   indent_mtx2 (m);
108   m[0][0] = sx;
109   m[1][1] = sy;
110 }
111
112 void 
113 rot_mtx2 (GRFMTX_2D m, const double theta)
114 {
115   double c = cos(theta);
116   double s = sin(theta);
117
118   indent_mtx2 (m);
119   m[0][0] =  c;  m[0][1] = s;
120   m[1][0] = -s;  m[1][1] = c;
121 }
122
123 void 
124 mult_mtx2 (GRFMTX_2D m1, GRFMTX_2D m2, GRFMTX_2D result)
125 {
126   GRFMTX_2D temp;
127
128   for (int row = 0; row < 3; row++)
129     for (int col = 0; col < 3; col++) {
130       temp[row][col] = 0;
131       for (int calc = 0; calc < 3; calc++)
132         temp[row][col] += m1[row][calc] * m2[calc][col];
133     }
134
135   for (int row = 0; row < 3; row++)
136     for (int col = 0; col < 3; col++)
137       result[row][col] = temp[row][col];
138 }
139
140 void 
141 xform_mtx2 (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 }