4 /*****************************************************************************\
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
22 //! Represents a geometric line segment.
24 template <class numeric_type>
28 line(const point<numeric_type> &endpoint1,
29 const point<numeric_type> &endpoint2);
30 line(numeric_type end1_x = 0, numeric_type end1_y = 0,
31 numeric_type end2_x = 0, numeric_type end2_y = 0);
33 point<numeric_type> center() const;
34 //!< Returns the point at the center of the line segment.
36 line operator + (const point<numeric_type> &to_add) const;
37 line operator - (const point<numeric_type> &to_subtract) const;
38 //!< Returns this line with "to_add" added to it.
39 /*!< This returns a line that is the result of adding or subtracting a
40 point to the endpoints of this line. */
42 line &operator += (const point<numeric_type> &to_add);
43 line &operator -= (const point<numeric_type> &to_subtract);
44 //!< Adds or subtracts a point from `this' line.
46 point<numeric_type> endpoint_1() const;
47 point<numeric_type> endpoint_2() const;
49 void endpoint_1(const point<numeric_type> &to_set);
50 void endpoint_2(const point<numeric_type> &to_set);
52 basis::astring text_form() const;
53 //!< returns a string form of the points defining the line.
56 point<numeric_type> _endpoint_1;
57 point<numeric_type> _endpoint_2;
62 // implementations below...
64 template <class numeric_type>
65 line<numeric_type>::line(const point<numeric_type> &p1,
66 const point<numeric_type> &p2)
67 : _endpoint_1(p1), _endpoint_2(p2) {}
69 template <class numeric_type>
70 line<numeric_type>::line(numeric_type x1, numeric_type y1, numeric_type x2,
72 : _endpoint_1(point<numeric_type>(x1, y1)),
73 _endpoint_2(point<numeric_type>(x2, y2))
76 template <class numeric_type>
77 point<numeric_type> line<numeric_type>::center() const
79 return point<numeric_type>(_endpoint_1.x() / 2.0 + _endpoint_2.x() / 2.0,
80 _endpoint_1.y() / 2.0 + _endpoint_2.y() / 2.0);
83 template <class numeric_type>
84 basis::astring line<numeric_type>::text_form() const
86 return basis::astring("<") + _endpoint_1.text_form() + basis::astring(" ")
87 + _endpoint_2.text_form() + basis::astring(">");
90 template <class numeric_type>
91 line<numeric_type> &line<numeric_type>::operator +=
92 (const point<numeric_type> &to_add)
93 { _endpoint_1 += to_add; _endpoint_2 += to_add; return *this; }
95 template <class numeric_type>
96 line<numeric_type> &line<numeric_type>::operator -=
97 (const point<numeric_type> &to_subtract)
98 { _endpoint_1 -= to_subtract; _endpoint_2 -= to_subtract; return *this; }
100 template <class numeric_type>
101 line<numeric_type> line<numeric_type>::operator +
102 (const point<numeric_type> &to_add) const
103 { line<numeric_type> to_return(*this); to_return += to_add; return to_return; }
105 template <class numeric_type>
106 line<numeric_type> line<numeric_type>::operator -
107 (const point<numeric_type> &to_subtract) const
109 line<numeric_type> to_return(*this);
110 to_return -= to_subtract;
114 template <class numeric_type>
115 point<numeric_type> line<numeric_type>::endpoint_1() const
116 { return _endpoint_1; }
118 template <class numeric_type>
119 point<numeric_type> line<numeric_type>::endpoint_2() const
120 { return _endpoint_2; }
122 template <class numeric_type>
123 void line<numeric_type>::endpoint_1(const point<numeric_type> &to_set)
124 { _endpoint_1 = to_set; }
126 template <class numeric_type>
127 void line<numeric_type>::endpoint_2(const point<numeric_type> &to_set)
128 { _endpoint_2 = to_set; }