Claw 1.7.3
line_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 line_2d.tpp
27 * \brief Implementation of claw::math::line_2d class.
28 * \author Julien Jorge
29 */
30
31/*----------------------------------------------------------------------------*/
32/**
33 * \brief Constructor.
34 */
35template<class T>
36claw::math::line_2d<T>::line_2d()
37{
38
39} // line_2d::line_2d() [constructor]
40
41/*----------------------------------------------------------------------------*/
42/**
43 * \brief Constructor.
44 * \param that Line to copy from.
45 */
46template<class T>
47template<class U>
48claw::math::line_2d<T>::line_2d( const line_2d<U>& that )
49 : origin(that.origin), direction(that.direction)
50{
51
52} // line_2d::line_2d() [copy constructor]
53
54/*----------------------------------------------------------------------------*/
55/**
56 * \brief Constructor with initializations.
57 * \param _origin A point on the line.
58 * \param _direction The direction of the line.
59 */
60template<class T>
61claw::math::line_2d<T>::line_2d
62( const point_type& _origin, const direction_type& _direction )
63 : origin(_origin), direction(_direction)
64{
65
66} // line_2d::line_2d() [constructor with values]
67
68/*----------------------------------------------------------------------------*/
69/**
70 * \brief Constructor with initializations.
71 * \param ox X-coordinate of the origin.
72 * \param oy Y-coordinate of the origin.
73 * \param dx X direction of the line.
74 * \param dy Y direction of the line.
75 */
76template<class T>
77claw::math::line_2d<T>::line_2d( const value_type& ox, const value_type& oy,
78 const value_type& dx, const value_type& dy )
79 : origin(ox, oy), direction(dx, dy)
80{
81
82} // line_2d::line_2d() [constructor with detailed origin & direction]
83
84/*----------------------------------------------------------------------------*/
85/**
86 * \brief Tell if two lines are parallels.
87 * \param that The other line.
88 */
89template<class T>
90bool claw::math::line_2d<T>::parallel( const self_type& that ) const
91{
92 return !( (direction.x * that.direction.y)
93 - (that.direction.x * direction.y) );
94} // line_2d::parallel()
95
96/*----------------------------------------------------------------------------*/
97/**
98 * \brief Tell if two lines are orthogonal.
99 * \param that The other line.
100 */
101template<class T>
102bool claw::math::line_2d<T>::orthogonal( const self_type& that ) const
103{
104 return !( direction.dot_product( that.direction ) );
105} // line_2d::orthogonal()
106
107/*----------------------------------------------------------------------------*/
108/**
109 * \brief Get the point at the intersection of two lines.
110 * \param that The other line.
111 * \remark The result if unknow if the two lines are parallel.
112 */
113template<class T>
114typename claw::math::line_2d<T>::point_type
115claw::math::line_2d<T>::intersection( const self_type& that ) const
116{
117 point_type result;
118
119 if ( ! parallel( that ) )
120 {
121 point_type delta( that.origin - origin );
122 value_type n, m;
123
124 n = direction.x * delta.y - direction.y * delta.x;
125 m = that.direction.x * direction.y - direction.x * that.direction.y;
126
127 result.x = that.origin.x + (n * that.direction.x) / m;
128 result.y = that.origin.y + (n * that.direction.y) / m;
129 }
130
131 return result;
132} // line_2d::intersection()
133
134/*----------------------------------------------------------------------------*/
135/**
136 * \brief Get the y value of the point of the line at position \a x.
137 * \param x The X-coordinate for which we want the Y-coordinate.
138 */
139template<class T>
140typename claw::math::line_2d<T>::value_type
141claw::math::line_2d<T>::y_value( const value_type& x ) const
142{
143 return (direction.y * (x - origin.x) + direction.x * origin.y) / direction.x;
144} // line_2d::y_value()