|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
2D infinite line in slope-intercept form. More...
#include <line.H>
Public Member Functions | |
| constexpr | LineEq () noexcept=default |
| Default constructor creates line y = x. | |
| constexpr | LineEq (double __y0, double __m) noexcept |
| Construct line from y-intercept and slope. | |
| constexpr | LineEq (double x1, double y1, double __m) noexcept |
| Construct line through a point with given slope. | |
| LineEq (double x1, double y1, double x2, double y2) | |
| Construct line through two points. | |
| constexpr double | operator() (double x) const noexcept |
| Evaluate line at given x-coordinate. | |
| constexpr double | slope () const noexcept |
| Get the slope of the line. | |
| constexpr double | y_intercept () const noexcept |
| Get the y-intercept of the line. | |
| constexpr bool | is_horizontal (double epsilon=LINE_EPSILON) const noexcept |
| Check if the line is horizontal (slope = 0). | |
| constexpr bool | is_parallel_to (const LineEq &l, double epsilon=LINE_EPSILON) const noexcept |
| Check if this line is parallel to another. | |
| constexpr bool | is_perpendicular_to (const LineEq &l, double epsilon=LINE_EPSILON) const noexcept |
| Check if this line is perpendicular to another. | |
| double | x_at (double y) const |
| Compute x-coordinate for a given y-coordinate. | |
| std::pair< double, double > | intersection (const LineEq &l, double epsilon=LINE_EPSILON) const |
| Compute intersection point with another line. | |
| LineEq | perpendicular_through (double px, double py) const |
| Compute perpendicular line through a point. | |
| double | distance_to (double px, double py) const noexcept |
| Compute distance from a point to this line. | |
| bool | contains_point (double px, double py, double epsilon=LINE_EPSILON) const noexcept |
| Check if a point lies on this line. | |
| bool | operator== (const LineEq &l) const noexcept |
| Test equality of two lines. | |
| bool | operator!= (const LineEq &l) const noexcept |
| Test inequality of two lines. | |
| std::string | to_string () const |
| Convert line to string representation. | |
Public Attributes | |
| double | y0 = 0 |
| Y-intercept (where line crosses y-axis) | |
| double | m = 1 |
| Slope (dy/dx) | |
Friends | |
| std::ostream & | operator<< (std::ostream &out, const LineEq &l) |
| Output stream operator. | |
2D infinite line in slope-intercept form.
Represents an infinite line using the equation y = y0 + m*x where:
|
constexprdefaultnoexcept |
Default constructor creates line y = x.
The default line passes through origin with slope 1.
Referenced by perpendicular_through().
Construct line from y-intercept and slope.
Creates line: y = y0 + m*x
| __y0 | Y-intercept (value of y when x = 0) |
| __m | Slope of the line |
Construct line through a point with given slope.
Computes y-intercept from point and slope using: y0 = y1 - m*x1
| x1 | X-coordinate of point on the line |
| y1 | Y-coordinate of point on the line |
| __m | Slope of the line |
Construct line through two points.
Computes slope and y-intercept from the two points. The slope is: m = (y2 - y1) / (x2 - x1)
| x1 | X-coordinate of first point |
| y1 | Y-coordinate of first point |
| x2 | X-coordinate of second point |
| y2 | Y-coordinate of second point |
| std::domain_error | if x1 == x2 (vertical line, undefined slope) |
Definition at line 187 of file line.H.
References ah_domain_error_if, Aleph::LINE_EPSILON, m, y0, and y1().
|
inlinenoexcept |
Compute distance from a point to this line.
Uses the formula: d = |y_point - (y0 + m*x_point)| / sqrt(1 + m^2)
| px | X-coordinate of point |
| py | Y-coordinate of point |
Definition at line 345 of file line.H.
References Aleph::diff(), m, Aleph::maps(), and y0.
|
inline |
Compute intersection point with another line.
Solves the system of equations to find where lines cross:
Solution: x = (y0_1 - y0_2) / (m_2 - m_1)
| l | The other line |
| epsilon | Tolerance for parallel line detection |
| std::domain_error | if lines are parallel (no unique intersection) |
Definition at line 305 of file line.H.
References ah_domain_error_if, is_parallel_to(), l, m, and y0.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
Check if this line is parallel to another.
Two lines are parallel if they have the same slope.
| l | The other line |
| epsilon | Tolerance for floating-point comparison |
Definition at line 247 of file line.H.
Referenced by intersection(), and TEST_F().
|
inlineconstexprnoexcept |
Test inequality of two lines.
| l | Line to compare with |
Definition at line 386 of file line.H.
References l, and Aleph::maps().
Test equality of two lines.
Lines are equal if they have the same slope and y-intercept (within epsilon tolerance).
| l | Line to compare with |
Definition at line 374 of file line.H.
References l, Aleph::LINE_EPSILON, m, Aleph::maps(), and y0.
Compute perpendicular line through a point.
Returns the line perpendicular to this one that passes through the given point. The perpendicular slope is -1/m.
| px | X-coordinate of point |
| py | Y-coordinate of point |
| std::domain_error | if this line is horizontal (perpendicular would be vertical) |
Definition at line 327 of file line.H.
References LineEq(), ah_domain_error_if, is_horizontal(), m, and Aleph::maps().
|
inline |
Compute x-coordinate for a given y-coordinate.
Solves x = (y - y0) / m
| y | Y-coordinate |
| std::domain_error | if line is horizontal (undefined x) |
Definition at line 277 of file line.H.
References ah_domain_error_if, is_horizontal(), m, y, and y0.
| double Aleph::LineEq::m = 1 |
Slope (dy/dx)
Definition at line 123 of file line.H.
Referenced by LineEq(), distance_to(), intersection(), is_horizontal(), is_parallel_to(), is_perpendicular_to(), operator()(), operator==(), perpendicular_through(), slope(), TEST_F(), TEST_F(), TEST_F(), to_string(), and x_at().
| double Aleph::LineEq::y0 = 0 |
Y-intercept (where line crosses y-axis)
Definition at line 122 of file line.H.
Referenced by LineEq(), distance_to(), intersection(), operator()(), operator==(), TEST_F(), TEST_F(), TEST_F(), to_string(), x_at(), and y_intercept().