Claw  1.7.3
vector_2d.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file vector_2d.tpp
27  * \brief Implementation of claw::math::vector_2d class.
28  * \author Julien Jorge
29  */
30 #include <cmath>
31 
32 /*----------------------------------------------------------------------------*/
33 /**
34  * \brief Constructor.
35  */
36 template<class T>
37 claw::math::vector_2d<T>::vector_2d()
38 {
39 
40 } // vector_2d::vector_2d() [constructor]
41 
42 /*----------------------------------------------------------------------------*/
43 /**
44  * \brief Copy constructor.
45  */
46 template<class T>
47 template<class U>
48 claw::math::vector_2d<T>::vector_2d(const coordinate_2d<U>& that)
49  : super(that)
50 {
51 
52 } // vector_2d::vector_2d() [copy constructor]
53 
54 /*----------------------------------------------------------------------------*/
55 /**
56  * \brief Construct a vector from two points.
57  */
58 template<class T>
59 template<class U>
60 claw::math::vector_2d<T>::vector_2d
61 ( const coordinate_2d<U>& p1, const coordinate_2d<U>& p2 )
62  : super( p2.x - p1.x, p2.y - p1.y )
63 {
64 
65 } // vector_2d::vector_2d() [constructor from two points]
66 
67 /*----------------------------------------------------------------------------*/
68 /**
69  * \brief Constructor with initialization.
70  * \param _x x value.
71  * \param _y y Value.
72  */
73 template<class T>
74 claw::math::vector_2d<T>::vector_2d(const value_type& _x, const value_type& _y)
75  : super(_x, _y)
76 {
77 
78 } // vector_2d::vector_2d() [constructor whit values]
79 
80 /*----------------------------------------------------------------------------*/
81 /**
82  * \brief Gets vector length.
83  */
84 template<class T>
85 typename claw::math::vector_2d<T>::value_type
86 claw::math::vector_2d<T>::length() const
87 {
88  return std::sqrt( (this->x * this->x) + (this->y * this->y) );
89 } // vector_2d::length()
90 
91 /*----------------------------------------------------------------------------*/
92 /**
93  * \brief Normalize the vector.
94  */
95 template<class T>
96 void claw::math::vector_2d<T>::normalize()
97 {
98  value_type l = length();
99 
100  if (l != 0)
101  {
102  this->x /= l;
103  this->y /= l;
104  }
105 } // vector_2d::normalize()
106 
107 /*----------------------------------------------------------------------------*/
108 /**
109  * \brief Get a vector orthonormal to this vector.
110  */
111 template<class T>
112 claw::math::vector_2d<T>
113 claw::math::vector_2d<T>::get_orthonormal_clockwise() const
114 {
115  return self_type(this->y, -this->x);
116 } // vector_2d::get_orthonormal_clockwise()
117 
118 /*----------------------------------------------------------------------------*/
119 /**
120  * \brief Get a vector orthonormal to this vector.
121  */
122 template<class T>
123 claw::math::vector_2d<T>
124 claw::math::vector_2d<T>::get_orthonormal_anticlockwise() const
125 {
126  return self_type(-this->y, this->x);
127 } // vector_2d::get_orthonormal_anticlockwise()
128 
129 /*----------------------------------------------------------------------------*/
130 /**
131  * \brief Dot product.
132  * \param that The other operand.
133  */
134 template<class T>
135 typename claw::math::vector_2d<T>::value_type
136 claw::math::vector_2d<T>::dot_product(const self_type& that) const
137 {
138  return this->x * that.x + this->y * that.y;
139 } // vector_2d::dot_product()