Line data Source code
1 : /** \file point2D.hpp
2 : * \author Jared R. Males
3 : * \brief A 2D point in space
4 : * \ingroup gen_math_files
5 : *
6 : */
7 :
8 : //***********************************************************************//
9 : // Copyright 2025 Jared R. Males (jaredmales@gmail.com)
10 : //
11 : // This file is part of mxlib.
12 : //
13 : // mxlib is free software: you can redistribute it and/or modify
14 : // it under the terms of the GNU General Public License as published by
15 : // the Free Software Foundation, either version 3 of the License, or
16 : // (at your option) any later version.
17 : //
18 : // mxlib is distributed in the hope that it will be useful,
19 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 : // GNU General Public License for more details.
22 : //
23 : // You should have received a copy of the GNU General Public License
24 : // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 : //***********************************************************************//
26 :
27 : #ifndef math_point2D_hpp
28 : #define math_point2D_hpp
29 :
30 : namespace mx
31 : {
32 : namespace math
33 : {
34 :
35 : /// A point in 2-dimensional space
36 : /**
37 : *
38 : * \tparam _angleT specifies the angle units, either radiansT<realT> or degreesT<realT>.
39 : */
40 : template <typename _angleT>
41 : class point2D
42 : {
43 :
44 : public:
45 : typedef _angleT angleT;
46 : typedef typename angleT::realT realT;
47 :
48 : realT x; ///< The x-coordinate
49 : realT y; ///< The y-coordinate
50 :
51 : /// Default c'tor
52 : point2D();
53 :
54 : /// Construct with values for x and y
55 : point2D( realT nx, /**< [in] the x-coordinate*/
56 : realT ny /**< [in] the y-coordinate*/ );
57 :
58 : /// Construct with values for x and y and rotate them by an angle
59 : point2D( realT nx, /**< [in] the x-coordinate*/
60 : realT ny, /**< [in] the y-coordinate*/
61 : realT ang /**< [in] the angle by which to rotate. Degrees or radians specified by angleT*/ );
62 :
63 : /// Construct with values for x and y and rotate them by an angle specified by cosine and sine
64 : point2D( realT nx, /**< [in] the x-coordinate*/
65 : realT ny, /**< [in] the y-coordinate*/
66 : realT c, /**< [in] cosine of the angle by which to rotate.*/
67 : realT s /**< [in] sine of the angle by which to rotate.*/ );
68 :
69 : /// Rotate the point about the origin by a given angle
70 : /**
71 : * \returns a point2D at the rotated coordinates
72 : */
73 : point2D rotate( realT ang /**< [in] the angle by which to rotate. Degrees or radians specified by angleT*/ );
74 :
75 : /// Rotate the point about the origin by a given angle
76 : /** In-place: operates on the current point, changing its coordinates
77 : *
78 : */
79 : void rotateInPlace( realT ang /**< [in] the angle by which to rotate. Degrees or radians specified by angleT*/ );
80 :
81 : /// Rotate the point about the origin by an angle specified by its cosine and sine
82 : /**
83 : * \returns a point2D at the rotated coordinates
84 : */
85 : point2D rotate( realT c, /**< [in] cosine of the angle by which to rotate.*/
86 : realT s /**< [in] sine of the angle by which to rotate.*/ );
87 :
88 : /// Rotate the point about the origin by an angle specified by its cosine and sine
89 : /** In-place: operates on the current point, changing its coordinates
90 : *
91 : */
92 : void rotateInPlace( realT c, /**< [in] cosine of the angle by which to rotate.*/
93 : realT s /**< [in] sine of the angle by which to rotate.*/ );
94 : };
95 :
96 : template <typename angleT>
97 0 : point2D<angleT>::point2D() : x( 0 ), y( 0 )
98 : {
99 0 : }
100 :
101 : template <typename angleT>
102 0 : point2D<angleT>::point2D( realT nx, realT ny ) : x( nx ), y( ny )
103 : {
104 0 : }
105 :
106 : template <typename angleT>
107 0 : point2D<angleT>::point2D( realT nx, realT ny, realT ang ) : x( nx ), y( ny )
108 : {
109 0 : rotateInPlace( ang );
110 0 : }
111 :
112 : template <typename angleT>
113 0 : point2D<angleT>::point2D( realT nx, realT ny, realT c, realT s ) : x( nx ), y( ny )
114 : {
115 0 : x = nx * c - ny * s;
116 0 : y = nx * s + ny * c;
117 :
118 0 : }
119 :
120 : template <typename angleT>
121 0 : point2D<angleT> point2D<angleT>::rotate( realT ang )
122 : {
123 0 : realT c = cos( ang * angleT::radians );
124 0 : realT s = sin( ang * angleT::radians );
125 :
126 0 : return rotate( c, s );
127 : }
128 :
129 : template <typename angleT>
130 0 : void point2D<angleT>::rotateInPlace( realT ang )
131 : {
132 0 : realT c = cos( ang * angleT::radians );
133 0 : realT s = sin( ang * angleT::radians );
134 :
135 0 : return rotateInPlace( c, s );
136 : }
137 :
138 : template <typename angleT>
139 0 : point2D<angleT> point2D<angleT>::rotate( realT c, realT s )
140 : {
141 0 : point2D<angleT> p;
142 :
143 0 : p.x = x * c - y * s;
144 0 : p.y = x * s + y * c;
145 :
146 0 : return p;
147 : }
148 :
149 : template <typename angleT>
150 0 : void point2D<angleT>::rotateInPlace( realT c, realT s )
151 : {
152 0 : realT tx = x * c - y * s;
153 0 : realT ty = x * s + y * c;
154 :
155 0 : x = tx;
156 0 : y = ty;
157 0 : }
158 :
159 : } // namespace math
160 : } // namespace mx
161 :
162 : #endif // math_point2D_hpp
|