mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
point2D.hpp
Go to the documentation of this file.
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
30namespace mx
31{
32namespace 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 */
40template <typename _angleT>
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
96template <typename angleT>
97point2D<angleT>::point2D() : x( 0 ), y( 0 )
98{
99}
100
101template <typename angleT>
102point2D<angleT>::point2D( realT nx, realT ny ) : x( nx ), y( ny )
103{
104}
105
106template <typename angleT>
107point2D<angleT>::point2D( realT nx, realT ny, realT ang ) : x( nx ), y( ny )
108{
110}
111
112template <typename angleT>
113point2D<angleT>::point2D( realT nx, realT ny, realT c, realT s ) : x( nx ), y( ny )
114{
115 x = nx * c - ny * s;
116 y = nx * s + ny * c;
117
118}
119
120template <typename angleT>
122{
123 realT c = cos( ang * angleT::radians );
124 realT s = sin( ang * angleT::radians );
125
126 return rotate( c, s );
127}
128
129template <typename angleT>
131{
132 realT c = cos( ang * angleT::radians );
133 realT s = sin( ang * angleT::radians );
134
135 return rotateInPlace( c, s );
136}
137
138template <typename angleT>
140{
142
143 p.x = x * c - y * s;
144 p.y = x * s + y * c;
145
146 return p;
147}
148
149template <typename angleT>
150void point2D<angleT>::rotateInPlace( realT c, realT s )
151{
152 realT tx = x * c - y * s;
153 realT ty = x * s + y * c;
154
155 x = tx;
156 y = ty;
157}
158
159} // namespace math
160} // namespace mx
161
162#endif // math_point2D_hpp
A point in 2-dimensional space.
Definition point2D.hpp:42
point2D()
Default c'tor.
Definition point2D.hpp:97
void rotateInPlace(realT ang)
Rotate the point about the origin by a given angle.
Definition point2D.hpp:130
realT y
The y-coordinate.
Definition point2D.hpp:49
realT x
The x-coordinate.
Definition point2D.hpp:48
point2D rotate(realT ang)
Rotate the point about the origin by a given angle.
Definition point2D.hpp:121
constexpr floatT six_fifths()
Return 6/5 in the specified precision.
The mxlib c++ namespace.
Definition mxError.hpp:106