template<typename T>
werkzeugkiste::geometry::Line2d_ class

Represents a line/segment in 2d Euclidean space.

Public types

using vec_type = Vec<T, 2>
using vec3_type = Vec<T, 3>

Constructors, destructors, conversion operators

Line2d_() defaulted
Default constructor yields an invalid line/segment.
Line2d_(vec_type from, vec_type to)
Construct a line from 2 real valued points. In case of a segment, these denote the start and end points.

Public functions

auto Reversed() const -> Line2d_
Returns a line with flipped direction vector.
auto LeftToRight() const -> Line2d_
Returns a new line, where from/to are sorted by ascending x-coordinates (left-to-right). If the line was vertical, the points will be sorted by ascending y-coordinates.
auto From() const -> const vec_type&
For a segment, this returns the start point. For a line, it's simply one of the given points to construct the line in the first place.
void SetFrom(const vec_type& from)
Sets the first reference point (starting point of a line segment; or any point on a line).
auto To() const -> const vec_type&
For a segment, this returns the end point. For a line, it's simply one of the given points to construct the line in the first place.
void SetTo(const vec_type& to)
Sets the second reference point (end point of a line segment; or any point on a line).
auto Length() const -> double
Returns the length from the start to the end point. As such, it's only meaningful for a line segment.
auto Direction() const -> vec_type
Returns the non-normalized direction vector from the start point to the end point.
auto UnitDirection() const -> vec_type
Returns the unit direction vector from the start point to the end point.
auto MidPoint() const -> vec_type
Returns the point halfway between from and to.
auto IsValid() const -> bool
Returns true if the line object is valid, i.e. start and end points are not the same.
auto AngleRad(const vec_type& v) const -> double
Returns the angle between the line and the given directional vector. The angle will be between 0 and Pi.
auto AngleDeg(const vec_type& v) const -> double
Returns the angle between the line and the given directional vector. The angle will be between 0 and 180 degrees.
auto AngleRad(const Line2d_& other) const -> double
Returns the angle between the direction vectors of the lines.
auto AngleDeg(const Line2d_& other) const -> double
Returns the angle between the direction vectors of the lines.
auto HomogeneousForm() const -> vec3_type
Returns the line in homogeneous coordinates.
auto PointAtOffset(double offset_factor) const -> vec_type
Returns a point between the start and end point.
auto ClosestPointOnLine(const vec_type& point) const -> vec_type
Returns the closest point on the line, i.e. the projection of the given point onto this line.
auto ClosestPointOnSegment(const vec_type& point) const -> vec_type
Returns the point on this line segment(!) which is closest to the given point.
auto DistancePointToLine(const vec_type& point) const -> double
Returns the minimum distance between the point and this line.
auto DistancePointToSegment(const vec_type& point) const -> double
Returns the minimum distance between the point and this segment.
auto IsCollinear(const Line2d_& other) const -> bool
Returns true if the two lines are collinear.
auto IsParallel(const Line2d_& other) const -> bool
Returns true if the two lines are parallel.
auto IsPointLeftOfLine(const vec_type& point, bool* is_on_line = nullptr) const -> bool
Returns true if the point is left of this line as specified by pt_from_ --> pt_to_.
auto IntersectionLineLine(const Line2d_& other, vec_type* intersection_point = nullptr) const -> bool
Returns true if this line intersects the other line and optionally sets the intersection_point.
auto IntersectionLineLineSegment(const Line2d_& segment, vec_type* intersection_point = nullptr) const -> bool
Returns true if this line(!) intersects the other line segment(!) and optionally sets the intersection_point.
auto IntersectionLineSegmentLineSegment(const Line2d_& segment, vec_type* intersection_point = nullptr) const -> bool
Returns true if this line segment(!) intersects the other line segment(!) and optionally sets the intersection_point.
auto IntersectionLineCircle(const Circle_<T>& circle, vec_type* intersection1 = nullptr, vec_type* intersection2 = nullptr) const -> int
Returns the number of intersections (0, 1, or 2) of this line and the circle. Optionally sets the intersection points.
auto IntersectionLineSegmentCircle(const Circle_<T>& circle, vec_type* intersection1 = nullptr, vec_type* intersection2 = nullptr) const -> int
Returns the number of intersections (0, 1, or 2) of this line segment(!) and the circle. Optionally sets the intersection points.
auto ClipLineByRectangle(const vec_type& top_left, const vec_type& size) const -> Line2d_<T>
Clips this line(!) such that only the part within the rectangle remains, check via IsValid() afterwards.
auto ClipLineSegmentByRectangle(const vec_type& top_left, const vec_type& size) const -> Line2d_<T>
Clips this line segment(!) such that only the part within the rectangle remains, check via IsValid() afterwards.
auto TiltRad(double angle_rad) const -> Line2d_<T>
auto TiltDeg(double angle_deg) const -> Line2d_<T>
Tilts the line/segment, i.e. rotates the end point around the start point.

Friends

auto operator<<(std::ostream& stream, const Line2d_& line) -> std::ostream&
Overloaded output stream operator.

Function documentation

template<typename T>
double werkzeugkiste::geometry::Line2d_<T>::AngleRad(const Line2d_& other) const

Returns the angle between the direction vectors of the lines.

Parameters
other The other line.
Returns Angle in radians, between [0, pi].

template<typename T>
double werkzeugkiste::geometry::Line2d_<T>::AngleDeg(const Line2d_& other) const

Returns the angle between the direction vectors of the lines.

Parameters
other The other line.
Returns Angle in degrees, between [0, 180].

template<typename T>
vec3_type werkzeugkiste::geometry::Line2d_<T>::HomogeneousForm() const

Returns the line in homogeneous coordinates.

Returns A 3-element vector representing the line in P^2, i.e. the projective 2-space.

For more details on lines in projective space, refer to Bob Fisher's CVonline <http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/BEARDSLEY/node2.html>__, or Stan Birchfield's notes <http://robotics.stanford.edu/~birch/projective/node4.html>__.

template<typename T>
vec_type werkzeugkiste::geometry::Line2d_<T>::PointAtOffset(double offset_factor) const

Returns a point between the start and end point.

Parameters
offset_factor Factor of the distance between start and end point.
Returns The point which is offset * Direction() units away from the line's starting point, i.e. offset == 0 is the starting point, offset == 1 is the end point.

template<typename T>
bool werkzeugkiste::geometry::Line2d_<T>::IsParallel(const Line2d_& other) const

Returns true if the two lines are parallel.

Parameters
other The second line.

template<typename T>
bool werkzeugkiste::geometry::Line2d_<T>::IsPointLeftOfLine(const vec_type& point, bool* is_on_line = nullptr) const

Returns true if the point is left of this line as specified by pt_from_ --> pt_to_.

If you need to distinguish left-of vs. exactly on the line, pass a valid pointer is_on_line.

template<typename T>
Line2d_<T> werkzeugkiste::geometry::Line2d_<T>::TiltDeg(double angle_deg) const

Tilts the line/segment, i.e. rotates the end point around the start point.

Parameters
angle_deg Tilt angle in degrees.
Returns A new line which has been rotated around the start point.