66 lines
1.3 KiB
C++
66 lines
1.3 KiB
C++
#ifndef LINE_H
|
|
#define LINE_H
|
|
|
|
#include <cmath> // For abs, sqrt
|
|
|
|
#include "Point.h"
|
|
|
|
class Line
|
|
{
|
|
private:
|
|
Point m_from, m_to, m_to_from_origin;
|
|
|
|
public:
|
|
Line(Point from, Point to) : m_from(from), m_to(to), m_to_from_origin(to - from) {}
|
|
|
|
Line() = default;
|
|
|
|
Point from() const
|
|
{
|
|
return m_from;
|
|
}
|
|
|
|
Point to() const
|
|
{
|
|
return m_to;
|
|
}
|
|
|
|
void set_from(Point from)
|
|
{
|
|
m_from = from;
|
|
}
|
|
|
|
void set_to(Point to)
|
|
{
|
|
m_to = to;
|
|
}
|
|
|
|
// Return true if the given point is to the right of this line.
|
|
// False is also returned if the point is directly on the line.
|
|
bool is_point_right(Point other) const
|
|
{
|
|
other -= from();
|
|
|
|
// Cross product greater than zero?
|
|
if (m_to_from_origin.x() * other.y() - m_to_from_origin.y() * other.x() > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
float distance_squared_to(Point other) const
|
|
{
|
|
float a = from().y() - to().y();
|
|
float b = to().x() - from().x();
|
|
float c = from().x() * to().y() - to().x() * from().y();
|
|
|
|
float d = a * other.x() + b * other.y() + c;
|
|
|
|
return (d * d) / (a * a + b * b);
|
|
}
|
|
};
|
|
#endif // LINE_H
|