#ifndef LINE_CLASS #define LINE_CLASS /*****************************************************************************\ * * * Name : line * * Author : Chris Koeritz * * * ******************************************************************************* * Copyright (c) 1992-$now By Author. This program is free software; you can * * redistribute it and/or modify it under the terms of the GNU General Public * * License as published by the Free Software Foundation; either version 2 of * * the License or (at your option) any later version. This is online at: * * http://www.fsf.org/copyleft/gpl.html * * Please send any updates to: fred@gruntose.com * \*****************************************************************************/ #include "point.h" namespace geometric { //! Represents a geometric line segment. template class line { public: line(const point &endpoint1, const point &endpoint2); line(numeric_type end1_x = 0, numeric_type end1_y = 0, numeric_type end2_x = 0, numeric_type end2_y = 0); point center() const; //!< Returns the point at the center of the line segment. line operator + (const point &to_add) const; line operator - (const point &to_subtract) const; //!< Returns this line with "to_add" added to it. /*!< This returns a line that is the result of adding or subtracting a point to the endpoints of this line. */ line &operator += (const point &to_add); line &operator -= (const point &to_subtract); //!< Adds or subtracts a point from `this' line. point endpoint_1() const; point endpoint_2() const; void endpoint_1(const point &to_set); void endpoint_2(const point &to_set); basis::astring text_form() const; //!< returns a string form of the points defining the line. protected: point _endpoint_1; point _endpoint_2; }; ////////////// // implementations below... template line::line(const point &p1, const point &p2) : _endpoint_1(p1), _endpoint_2(p2) {} template line::line(numeric_type x1, numeric_type y1, numeric_type x2, numeric_type y2) : _endpoint_1(point(x1, y1)), _endpoint_2(point(x2, y2)) {} template point line::center() const { return point(_endpoint_1.x() / 2.0 + _endpoint_2.x() / 2.0, _endpoint_1.y() / 2.0 + _endpoint_2.y() / 2.0); } template basis::astring line::text_form() const { return basis::astring("<") + _endpoint_1.text_form() + basis::astring(" ") + _endpoint_2.text_form() + basis::astring(">"); } template line &line::operator += (const point &to_add) { _endpoint_1 += to_add; _endpoint_2 += to_add; return *this; } template line &line::operator -= (const point &to_subtract) { _endpoint_1 -= to_subtract; _endpoint_2 -= to_subtract; return *this; } template line line::operator + (const point &to_add) const { line to_return(*this); to_return += to_add; return to_return; } template line line::operator - (const point &to_subtract) const { line to_return(*this); to_return -= to_subtract; return to_return; } template point line::endpoint_1() const { return _endpoint_1; } template point line::endpoint_2() const { return _endpoint_2; } template void line::endpoint_1(const point &to_set) { _endpoint_1 = to_set; } template void line::endpoint_2(const point &to_set) { _endpoint_2 = to_set; } } // namespace. #endif