2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
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.
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.
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
23 contact: julien.jorge@gamned.org
27 * \brief Implementation of claw::math::line_2d class.
28 * \author Julien Jorge
31 /*----------------------------------------------------------------------------*/
36 claw::math::line_2d<T>::line_2d()
39 } // line_2d::line_2d() [constructor]
41 /*----------------------------------------------------------------------------*/
44 * \param that Line to copy from.
48 claw::math::line_2d<T>::line_2d( const line_2d<U>& that )
49 : origin(that.origin), direction(that.direction)
52 } // line_2d::line_2d() [copy constructor]
54 /*----------------------------------------------------------------------------*/
56 * \brief Constructor with initializations.
57 * \param _origin A point on the line.
58 * \param _direction The direction of the line.
61 claw::math::line_2d<T>::line_2d
62 ( const point_type& _origin, const direction_type& _direction )
63 : origin(_origin), direction(_direction)
66 } // line_2d::line_2d() [constructor with values]
68 /*----------------------------------------------------------------------------*/
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.
77 claw::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)
82 } // line_2d::line_2d() [constructor with detailed origin & direction]
84 /*----------------------------------------------------------------------------*/
86 * \brief Tell if two lines are parallels.
87 * \param that The other line.
90 bool claw::math::line_2d<T>::parallel( const self_type& that ) const
92 return !( (direction.x * that.direction.y)
93 - (that.direction.x * direction.y) );
94 } // line_2d::parallel()
96 /*----------------------------------------------------------------------------*/
98 * \brief Tell if two lines are orthogonal.
99 * \param that The other line.
102 bool claw::math::line_2d<T>::orthogonal( const self_type& that ) const
104 return !( direction.dot_product( that.direction ) );
105 } // line_2d::orthogonal()
107 /*----------------------------------------------------------------------------*/
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.
114 typename claw::math::line_2d<T>::point_type
115 claw::math::line_2d<T>::intersection( const self_type& that ) const
119 if ( ! parallel( that ) )
121 point_type delta( that.origin - origin );
124 n = direction.x * delta.y - direction.y * delta.x;
125 m = that.direction.x * direction.y - direction.x * that.direction.y;
127 result.x = that.origin.x + (n * that.direction.x) / m;
128 result.y = that.origin.y + (n * that.direction.y) / m;
132 } // line_2d::intersection()
134 /*----------------------------------------------------------------------------*/
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.
140 typename claw::math::line_2d<T>::value_type
141 claw::math::line_2d<T>::y_value( const value_type& x ) const
143 return (direction.y * (x - origin.x) + direction.x * origin.y) / direction.x;
144 } // line_2d::y_value()