17 #ifndef IGNITION_MATH_LINE2_HH_ 18 #define IGNITION_MATH_LINE2_HH_ 22 #include <ignition/math/config.hh> 28 inline namespace IGNITION_MATH_VERSION_NAMESPACE
39 public:
Line2(
const math::Vector2<T> &_ptA,
const math::Vector2<T> &_ptB)
41 this->
Set(_ptA, _ptB);
49 public:
Line2(
double _x1,
double _y1,
double _x2,
double _y2)
51 this->
Set(_x1, _y1, _x2, _y2);
57 public:
void Set(
const math::Vector2<T> &_ptA,
58 const math::Vector2<T> &_ptB)
69 public:
void Set(
double _x1,
double _y1,
double _x2,
double _y2)
71 this->pts[0].Set(_x1, _y1);
72 this->pts[1].Set(_x2, _y2);
83 return (this->pts[0].X() - this->pts[1].X()) *
84 (_line[0].Y() -_line[1].Y()) -
85 (this->pts[0].Y() - this->pts[1].Y()) *
86 (_line[0].X() - _line[1].X());
96 return (_pt.
Y() - this->pts[0].Y()) *
97 (this->pts[1].X() - this->pts[0].X()) -
98 (_pt.
X() - this->pts[0].X()) *
99 (this->pts[1].Y() - this->pts[0].Y());
109 double _epsilon = 1e-6)
const 112 static_cast<T>(0), _epsilon);
123 double _epsilon = 1e-6)
const 126 static_cast<T>(0), _epsilon);
137 double _epsilon = 1e-6)
const 139 return this->
Parallel(_line, _epsilon) &&
149 double _epsilon = 1e-6)
const 161 public:
bool Within(
const math::Vector2<T> &_pt,
162 double _epsilon = 1e-6)
const 164 return _pt.X() <=
std::max(this->pts[0].X(),
165 this->pts[1].X()) + _epsilon &&
166 _pt.X() >=
std::min(this->pts[0].X(),
167 this->pts[1].X()) - _epsilon &&
168 _pt.Y() <=
std::max(this->pts[0].Y(),
169 this->pts[1].Y()) + _epsilon &&
170 _pt.Y() >=
std::min(this->pts[0].Y(),
171 this->pts[1].Y()) - _epsilon;
180 double _epsilon = 1e-6)
const 182 static math::Vector2<T> ignore;
183 return this->
Intersect(_line, ignore, _epsilon);
195 double _epsilon = 1e-6)
const 204 if (this->
Within(_line[0], _epsilon))
210 else if (this->
Within(_line[1], _epsilon))
220 _pt.X((_line[0].X() - _line[1].X()) *
221 (this->pts[0].X() * this->pts[1].Y() -
222 this->pts[0].Y() * this->pts[1].X()) -
223 (this->pts[0].X() - this->pts[1].X()) *
224 (_line[0].X() * _line[1].Y() - _line[0].Y() * _line[1].X()));
226 _pt.Y((_line[0].Y() - _line[1].Y()) *
227 (this->pts[0].X() * this->pts[1].Y() -
228 this->pts[0].Y() * this->pts[1].X()) -
229 (this->pts[0].Y() - this->pts[1].Y()) *
230 (_line[0].X() * _line[1].Y() - _line[0].Y() * _line[1].X()));
234 if (_pt.X() <
std::min(this->pts[0].X(), this->pts[1].X()) ||
235 _pt.X() >
std::max(this->pts[0].X(), this->pts[1].X()) ||
236 _pt.X() <
std::min(_line[0].X(), _line[1].X()) ||
237 _pt.X() >
std::max(_line[0].X(), _line[1].X()))
242 if (_pt.Y() <
std::min(this->pts[0].Y(), this->pts[1].Y()) ||
243 _pt.Y() >
std::max(this->pts[0].Y(), this->pts[1].Y()) ||
244 _pt.Y() <
std::min(_line[0].Y(), _line[1].Y()) ||
245 _pt.Y() >
std::max(_line[0].Y(), _line[1].Y()))
257 return sqrt((this->pts[0].X() - this->pts[1].X()) *
258 (this->pts[0].X() - this->pts[1].X()) +
259 (this->pts[0].Y() - this->pts[1].Y()) *
260 (this->pts[0].Y() - this->pts[1].Y()));
267 if (
math::equal(this->pts[1].X(), this->pts[0].X()))
270 return (this->pts[1].Y() - this->pts[0].Y()) /
271 static_cast<double>(this->pts[1].X() - this->pts[0].X());
279 return this->pts[0] == _line[0] && this->pts[1] == _line[1];
287 return !(*
this == _line);
303 std::ostream &_out,
const Line2<T> &_line)
305 _out << _line[0] <<
" " << _line[1];
309 private: math::Vector2<T> pts[2];
Line2(double _x1, double _y1, double _x2, double _y2)
Constructor.
Definition: Line2.hh:49
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:108
math::Vector2< T > operator[](size_t _index) const
Get the start or end point.
Definition: Line2.hh:293
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:148
T Y() const
Return the y value.
Definition: Vector2.hh:398
double Slope() const
Get the slope of the line.
Definition: Line2.hh:265
Line2< float > Line2f
Definition: Line2.hh:315
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:219
A two dimensional line segment.
Definition: Line2.hh:34
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:531
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition: Helpers.hh:545
void Set(double _x1, double _y1, double _x2, double _y2)
Set the start and end point of the line segment.
Definition: Line2.hh:69
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:216
bool operator!=(const Line2< T > &_line) const
Inequality operator.
Definition: Line2.hh:285
Two dimensional (x, y) vector.
Definition: Vector2.hh:32
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:161
bool operator==(const Line2< T > &_line) const
Equality operator.
Definition: Line2.hh:277
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:136
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:518
friend std::ostream & operator<<(std::ostream &_out, const Line2< T > &_line)
Stream extraction operator.
Definition: Line2.hh:302
T Length() const
Get the length of the line.
Definition: Line2.hh:255
Line2(const math::Vector2< T > &_ptA, const math::Vector2< T > &_ptB)
Constructor.
Definition: Line2.hh:39
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:57
Line2< double > Line2d
Definition: Line2.hh:314
Line2< int > Line2i
Definition: Line2.hh:313
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:122
bool Intersect(const Line2< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line2.hh:179
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:194
T X() const
Return the x value.
Definition: Vector2.hh:391
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:258
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:395
double CrossProduct(const Vector2< T > &_pt) const
Return the cross product of this line and the given point.
Definition: Line2.hh:94
double CrossProduct(const Line2< T > &_line) const
Return the cross product of this line and the given line.
Definition: Line2.hh:81