17 #ifndef _IGNITION_LINE2_HH_
18 #define _IGNITION_LINE2_HH_
39 this->
Set(_ptA, _ptB);
47 public:
Line2(
double _x1,
double _y1,
double _x2,
double _y2)
49 this->
Set(_x1, _y1, _x2, _y2);
67 public:
void Set(
double _x1,
double _y1,
double _x2,
double _y2)
69 this->pts[0].Set(_x1, _y1);
70 this->pts[1].Set(_x2, _y2);
81 return (this->pts[0].X() - this->pts[1].X()) *
82 (_line[0].Y() -_line[1].Y()) -
83 (this->pts[0].Y() - this->pts[1].Y()) *
84 (_line[0].X() - _line[1].X());
94 return (_pt.
Y() - this->pts[0].Y()) *
95 (this->pts[1].X() - this->pts[0].X()) -
96 (_pt.
X() - this->pts[0].X()) *
97 (this->pts[1].Y() - this->pts[0].Y());
107 double _epsilon = 1e-6)
const
110 static_cast<T>(0), _epsilon);
121 double _epsilon = 1e-6)
const
124 static_cast<T>(0), _epsilon);
135 double _epsilon = 1e-6)
const
137 return this->
Parallel(_line, _epsilon) &&
147 double _epsilon = 1e-6)
const
160 double _epsilon = 1e-6)
const
162 return _pt.
X() <=
std::max(this->pts[0].X(),
163 this->pts[1].X()) + _epsilon &&
165 this->pts[1].X()) - _epsilon &&
167 this->pts[1].Y()) + _epsilon &&
169 this->pts[1].Y()) - _epsilon;
178 double _epsilon = 1e-6)
const
181 return this->
Intersect(_line, ignore, _epsilon);
193 double _epsilon = 1e-6)
const
202 if (this->
Within(_line[0], _epsilon))
208 else if (this->
Within(_line[1], _epsilon))
218 _pt.
X((_line[0].X() - _line[1].X()) *
219 (this->pts[0].X() * this->pts[1].Y() -
220 this->pts[0].Y() * this->pts[1].X()) -
221 (this->pts[0].X() - this->pts[1].X()) *
222 (_line[0].X() * _line[1].Y() - _line[0].Y() * _line[1].X()));
224 _pt.
Y((_line[0].Y() - _line[1].Y()) *
225 (this->pts[0].X() * this->pts[1].Y() -
226 this->pts[0].Y() * this->pts[1].X()) -
227 (this->pts[0].Y() - this->pts[1].Y()) *
228 (_line[0].X() * _line[1].Y() - _line[0].Y() * _line[1].X()));
232 if (_pt.
X() <
std::min(this->pts[0].X(), this->pts[1].X()) ||
233 _pt.
X() >
std::max(this->pts[0].X(), this->pts[1].X()) ||
234 _pt.
X() <
std::min(_line[0].X(), _line[1].X()) ||
235 _pt.
X() >
std::max(_line[0].X(), _line[1].X()))
240 if (_pt.
Y() <
std::min(this->pts[0].Y(), this->pts[1].Y()) ||
241 _pt.
Y() >
std::max(this->pts[0].Y(), this->pts[1].Y()) ||
242 _pt.
Y() <
std::min(_line[0].Y(), _line[1].Y()) ||
243 _pt.
Y() >
std::max(_line[0].Y(), _line[1].Y()))
255 return sqrt((this->pts[0].X() - this->pts[1].X()) *
256 (this->pts[0].X() - this->pts[1].X()) +
257 (this->pts[0].Y() - this->pts[1].Y()) *
258 (this->pts[0].Y() - this->pts[1].Y()));
265 if (
math::equal(this->pts[1].X(), this->pts[0].X()))
268 return (this->pts[1].Y() - this->pts[0].Y()) /
269 static_cast<double>(this->pts[1].X() - this->pts[0].X());
277 return this->pts[0] == _line[0] && this->pts[1] == _line[1];
285 return !(*
this == _line);
295 return this->pts[_index];
304 std::ostream &_out,
const Line2<T> &_line)
306 _out << _line[0] <<
" " << _line[1];
bool operator!=(const Line2< T > &_line) const
Inequality operator.
Definition: Line2.hh:283
Line2< float > Line2f
Definition: Line2.hh:316
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:70
void Set(double _x1, double _y1, double _x2, double _y2)
Set the start and end point of the line segment.
Definition: Line2.hh:67
double CrossProduct(const Vector2< T > &_pt) const
Return the cross product of this line and the given point.
Definition: Line2.hh:92
T Y() const
Return the y value.
Definition: Vector2.hh:282
T X() const
Return the x value.
Definition: Vector2.hh:274
bool Collinear(const math::Line2< T > &_line, double _epsilon=1e-6) const
Check if the given line is collinear with this line.
Definition: Line2.hh:134
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:150
T Length() const
Get the length of the line.
Definition: Line2.hh:253
A two dimensional line segment.
Definition: Line2.hh:32
bool OnSegment(const math::Vector2< T > &_pt, double _epsilon=1e-6) const
Return whether the given point is on this line segment.
Definition: Line2.hh:146
Line2(const math::Vector2< T > &_ptA, const math::Vector2< T > &_ptB)
Constructor.
Definition: Line2.hh:37
Line2< double > Line2d
Definition: Line2.hh:315
double CrossProduct(const Line2< T > &_line) const
Return the cross product of this line and the given line.
Definition: Line2.hh:79
bool Within(const math::Vector2< T > &_pt, double _epsilon=1e-6) const
Check if the given point is between the start and end points of the line segment. ...
Definition: Line2.hh:159
friend std::ostream & operator<<(std::ostream &_out, const Line2< T > &_line)
Stream extraction operator.
Definition: Line2.hh:303
double Slope() const
Get the slope of the line.
Definition: Line2.hh:263
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:30
bool Collinear(const math::Vector2< T > &_pt, double _epsilon=1e-6) const
Check if the given point is collinear with this line.
Definition: Line2.hh:106
Line2(double _x1, double _y1, double _x2, double _y2)
Constructor.
Definition: Line2.hh:47
bool Parallel(const math::Line2< T > &_line, double _epsilon=1e-6) const
Check if the given line is parallel with this line.
Definition: Line2.hh:120
bool Intersect(const Line2< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line2.hh:177
bool operator==(const Line2< T > &_line) const
Equality operator.
Definition: Line2.hh:275
bool Intersect(const Line2< T > &_line, math::Vector2< T > &_pt, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line2.hh:192
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:177
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:163
Line2< int > Line2i
Definition: Line2.hh:314
math::Vector2< T > operator[](size_t _index) const
Get the start or end point.
Definition: Line2.hh:291
void Set(const math::Vector2< T > &_ptA, const math::Vector2< T > &_ptB)
Set the start and end point of the line segment.
Definition: Line2.hh:55