feisty meow concerns codebase 2.140
line.h
Go to the documentation of this file.
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
20namespace geometric {
21
23
24template <class numeric_type>
25class line
26{
27public:
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
35
36 line operator + (const point<numeric_type> &to_add) const;
37 line operator - (const point<numeric_type> &to_subtract) const;
39
45
48
49 void endpoint_1(const point<numeric_type> &to_set);
50 void endpoint_2(const point<numeric_type> &to_set);
51
54
55protected:
58};
59
61
62// implementations below...
63
64template <class numeric_type>
66 const point<numeric_type> &p2)
67: _endpoint_1(p1), _endpoint_2(p2) {}
68
69template <class numeric_type>
70line<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
76template <class numeric_type>
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
83template <class numeric_type>
85{
86 return basis::astring("<") + _endpoint_1.text_form() + basis::astring(" ")
87 + _endpoint_2.text_form() + basis::astring(">");
88}
89
90template <class numeric_type>
92 (const point<numeric_type> &to_add)
93{ _endpoint_1 += to_add; _endpoint_2 += to_add; return *this; }
94
95template <class numeric_type>
97 (const point<numeric_type> &to_subtract)
98{ _endpoint_1 -= to_subtract; _endpoint_2 -= to_subtract; return *this; }
99
100template <class numeric_type>
102 (const point<numeric_type> &to_add) const
103{ line<numeric_type> to_return(*this); to_return += to_add; return to_return; }
104
105template <class numeric_type>
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
114template <class numeric_type>
116{ return _endpoint_1; }
117
118template <class numeric_type>
120{ return _endpoint_2; }
121
122template <class numeric_type>
124{ _endpoint_1 = to_set; }
125
126template <class numeric_type>
128{ _endpoint_2 = to_set; }
129
130} // namespace.
131
132#endif
133
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition astring.cpp:130
Represents a geometric line segment.
Definition line.h:26
line & operator+=(const point< numeric_type > &to_add)
Definition line.h:92
void endpoint_1(const point< numeric_type > &to_set)
Definition line.h:123
point< numeric_type > center() const
Returns the point at the center of the line segment.
Definition line.h:77
void endpoint_2(const point< numeric_type > &to_set)
Definition line.h:127
line operator+(const point< numeric_type > &to_add) const
Definition line.h:102
line(numeric_type end1_x=0, numeric_type end1_y=0, numeric_type end2_x=0, numeric_type end2_y=0)
Definition line.h:70
point< numeric_type > _endpoint_1
Definition line.h:56
point< numeric_type > endpoint_2() const
Definition line.h:119
line & operator-=(const point< numeric_type > &to_subtract)
Adds or subtracts a point from ‘this’ line.
Definition line.h:97
point< numeric_type > _endpoint_2
Definition line.h:57
line operator-(const point< numeric_type > &to_subtract) const
Returns this line with "to_add" added to it.
Definition line.h:107
basis::astring text_form() const
returns a string form of the points defining the line.
Definition line.h:84
line(const point< numeric_type > &endpoint1, const point< numeric_type > &endpoint2)
Definition line.h:65
point< numeric_type > endpoint_1() const
Definition line.h:115
Represents a geometric point.
Definition point.h:37
Contains all of our objects for geometry and avoids name clashes.
Definition angle.h:25