first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / graphiq / library / geometric / line.h
1 #ifndef LINE_CLASS
2 #define LINE_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : line                                                              *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include "point.h"
19
20 namespace geometric {
21
22 //! Represents a geometric line segment.
23
24 template <class numeric_type>
25 class line
26 {
27 public:
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);
32
33   point<numeric_type> center() const;
34     //!< Returns the point at the center of the line segment.
35
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. */
41
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.
45
46   point<numeric_type> endpoint_1() const;
47   point<numeric_type> endpoint_2() const;
48
49   void endpoint_1(const point<numeric_type> &to_set);
50   void endpoint_2(const point<numeric_type> &to_set);
51
52   basis::astring text_form() const;
53     //!< returns a string form of the points defining the line.
54
55 protected:
56   point<numeric_type> _endpoint_1;
57   point<numeric_type> _endpoint_2;
58 };
59
60 //////////////
61
62 // implementations below...
63
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) {}
68
69 template <class numeric_type>
70 line<numeric_type>::line(numeric_type x1, numeric_type y1, numeric_type x2,
71     numeric_type y2)
72 : _endpoint_1(point<numeric_type>(x1, y1)),
73   _endpoint_2(point<numeric_type>(x2, y2))
74 {}
75
76 template <class numeric_type>
77 point<numeric_type> line<numeric_type>::center() const
78 {
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);
81 }
82
83 template <class numeric_type>
84 basis::astring line<numeric_type>::text_form() const
85 {
86   return basis::astring("<") + _endpoint_1.text_form() + basis::astring(" ")
87       + _endpoint_2.text_form() + basis::astring(">");
88 }
89
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; }
94
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; }
99
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; }
104
105 template <class numeric_type>
106 line<numeric_type> line<numeric_type>::operator -
107     (const point<numeric_type> &to_subtract) const
108 {
109   line<numeric_type> to_return(*this);
110   to_return -= to_subtract;
111   return to_return;
112 }
113
114 template <class numeric_type>
115 point<numeric_type> line<numeric_type>::endpoint_1() const
116 { return _endpoint_1; }
117
118 template <class numeric_type>
119 point<numeric_type> line<numeric_type>::endpoint_2() const
120 { return _endpoint_2; }
121
122 template <class numeric_type>
123 void line<numeric_type>::endpoint_1(const point<numeric_type> &to_set)
124 { _endpoint_1 = to_set; }
125
126 template <class numeric_type>
127 void line<numeric_type>::endpoint_2(const point<numeric_type> &to_set)
128 { _endpoint_2 = to_set; }
129
130 } // namespace.
131
132 #endif
133