Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::LineEq Struct Reference

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, doubleintersection (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.
 

Detailed Description

2D infinite line in slope-intercept form.

Represents an infinite line using the equation y = y0 + m*x where:

  • y0 is the y-intercept (value of y when x = 0)
  • m is the slope (dy/dx)
Note
This representation cannot handle vertical lines (infinite slope). For vertical lines, use parametric or implicit form.

Definition at line 120 of file line.H.

Constructor & Destructor Documentation

◆ LineEq() [1/4]

constexpr Aleph::LineEq::LineEq ( )
constexprdefaultnoexcept

Default constructor creates line y = x.

The default line passes through origin with slope 1.

Referenced by perpendicular_through().

◆ LineEq() [2/4]

constexpr Aleph::LineEq::LineEq ( double  __y0,
double  __m 
)
inlineconstexprnoexcept

Construct line from y-intercept and slope.

Creates line: y = y0 + m*x

Parameters
__y0Y-intercept (value of y when x = 0)
__mSlope of the line
Example
LineEq line(5.0, 2.0); // y = 5 + 2x
2D infinite line in slope-intercept form.
Definition line.H:121

Definition at line 145 of file line.H.

◆ LineEq() [3/4]

constexpr Aleph::LineEq::LineEq ( double  x1,
double  y1,
double  __m 
)
inlineconstexprnoexcept

Construct line through a point with given slope.

Computes y-intercept from point and slope using: y0 = y1 - m*x1

Parameters
x1X-coordinate of point on the line
y1Y-coordinate of point on the line
__mSlope of the line
Example
// Line through (2, 4) with slope 3
LineEq line(2.0, 4.0, 3.0); // y = -2 + 3x

Definition at line 163 of file line.H.

◆ LineEq() [4/4]

Aleph::LineEq::LineEq ( double  x1,
double  y1,
double  x2,
double  y2 
)
inline

Construct line through two points.

Computes slope and y-intercept from the two points. The slope is: m = (y2 - y1) / (x2 - x1)

Parameters
x1X-coordinate of first point
y1Y-coordinate of first point
x2X-coordinate of second point
y2Y-coordinate of second point
Exceptions
std::domain_errorif x1 == x2 (vertical line, undefined slope)
Note
Horizontal lines (y1 == y2) are valid and result in slope = 0.
Example
LineEq line(0.0, 0.0, 2.0, 6.0); // y = 3x
LineEq horizontal(0.0, 5.0, 10.0, 5.0); // y = 5 (horizontal)

Definition at line 187 of file line.H.

References ah_domain_error_if, Aleph::LINE_EPSILON, m, y0, and y1().

Member Function Documentation

◆ contains_point()

bool Aleph::LineEq::contains_point ( double  px,
double  py,
double  epsilon = LINE_EPSILON 
) const
inlinenoexcept

Check if a point lies on this line.

Parameters
pxX-coordinate of point
pyY-coordinate of point
epsilonTolerance for floating-point comparison
Returns
True if point is on the line

Definition at line 359 of file line.H.

References Aleph::maps().

Referenced by TEST_F(), TEST_F(), and TEST_F().

◆ distance_to()

double Aleph::LineEq::distance_to ( double  px,
double  py 
) const
inlinenoexcept

Compute distance from a point to this line.

Uses the formula: d = |y_point - (y0 + m*x_point)| / sqrt(1 + m^2)

Parameters
pxX-coordinate of point
pyY-coordinate of point
Returns
Perpendicular distance from point to line

Definition at line 345 of file line.H.

References Aleph::diff(), m, Aleph::maps(), and y0.

Referenced by TEST_F(), TEST_F(), and TEST_F().

◆ intersection()

std::pair< double, double > Aleph::LineEq::intersection ( const LineEq l,
double  epsilon = LINE_EPSILON 
) const
inline

Compute intersection point with another line.

Solves the system of equations to find where lines cross:

  • y = y0_1 + m_1 * x
  • y = y0_2 + m_2 * x

Solution: x = (y0_1 - y0_2) / (m_2 - m_1)

Parameters
lThe other line
epsilonTolerance for parallel line detection
Returns
Pair (x, y) of intersection coordinates
Exceptions
std::domain_errorif lines are parallel (no unique intersection)
Example
LineEq l1(0.0, 1.0); // y = x
LineEq l2(2.0, -1.0); // y = 2 - x
auto [x, y] = l1.intersection(l2); // (1, 1)
static mpfr_t y
Definition mpfr_mul_d.c:3

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().

◆ is_horizontal()

constexpr bool Aleph::LineEq::is_horizontal ( double  epsilon = LINE_EPSILON) const
inlineconstexprnoexcept

Check if the line is horizontal (slope = 0).

Parameters
epsilonTolerance for floating-point comparison
Returns
True if the line is horizontal

Definition at line 233 of file line.H.

References m.

Referenced by perpendicular_through(), TEST(), TEST_F(), TEST_F(), TEST_F(), and x_at().

◆ is_parallel_to()

constexpr bool Aleph::LineEq::is_parallel_to ( const LineEq l,
double  epsilon = LINE_EPSILON 
) const
inlineconstexprnoexcept

Check if this line is parallel to another.

Two lines are parallel if they have the same slope.

Parameters
lThe other line
epsilonTolerance for floating-point comparison
Returns
True if lines are parallel

Definition at line 247 of file line.H.

References l, and m.

Referenced by intersection(), and TEST_F().

◆ is_perpendicular_to()

constexpr bool Aleph::LineEq::is_perpendicular_to ( const LineEq l,
double  epsilon = LINE_EPSILON 
) const
inlineconstexprnoexcept

Check if this line is perpendicular to another.

Two lines are perpendicular if m1 * m2 = -1

Parameters
lThe other line
epsilonTolerance for floating-point comparison
Returns
True if lines are perpendicular

Definition at line 262 of file line.H.

References l, and m.

Referenced by TEST_F(), and TEST_F().

◆ operator!=()

bool Aleph::LineEq::operator!= ( const LineEq l) const
inlinenoexcept

Test inequality of two lines.

Parameters
lLine to compare with
Returns
True if lines differ

Definition at line 386 of file line.H.

References l, and Aleph::maps().

◆ operator()()

constexpr double Aleph::LineEq::operator() ( double  x) const
inlineconstexprnoexcept

Evaluate line at given x-coordinate.

Computes y = y0 + m*x

Parameters
xX-coordinate to evaluate
Returns
Y-coordinate on the line
Example
LineEq line(1.0, 2.0); // y = 1 + 2x
double y = line(3.0); // y = 7

Definition at line 211 of file line.H.

References m, and y0.

◆ operator==()

bool Aleph::LineEq::operator== ( const LineEq l) const
inlinenoexcept

Test equality of two lines.

Lines are equal if they have the same slope and y-intercept (within epsilon tolerance).

Parameters
lLine to compare with
Returns
True if lines are identical

Definition at line 374 of file line.H.

References l, Aleph::LINE_EPSILON, m, Aleph::maps(), and y0.

◆ perpendicular_through()

LineEq Aleph::LineEq::perpendicular_through ( double  px,
double  py 
) const
inline

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.

Parameters
pxX-coordinate of point
pyY-coordinate of point
Returns
Perpendicular line through (px, py)
Exceptions
std::domain_errorif 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().

Referenced by TEST_F(), TEST_F(), and TEST_F().

◆ slope()

constexpr double Aleph::LineEq::slope ( ) const
inlineconstexprnoexcept

Get the slope of the line.

Returns
The slope (m)

Definition at line 220 of file line.H.

References m.

Referenced by TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ to_string()

std::string Aleph::LineEq::to_string ( ) const
inline

Convert line to string representation.

Returns
String in format "y = y0 + m * x"

Definition at line 396 of file line.H.

References m, and y0.

Referenced by TEST(), and TEST_F().

◆ x_at()

double Aleph::LineEq::x_at ( double  y) const
inline

Compute x-coordinate for a given y-coordinate.

Solves x = (y - y0) / m

Parameters
yY-coordinate
Returns
X-coordinate on the line
Exceptions
std::domain_errorif line is horizontal (undefined x)

Definition at line 277 of file line.H.

References ah_domain_error_if, is_horizontal(), m, y, and y0.

Referenced by TEST_F(), and TEST_F().

◆ y_intercept()

constexpr double Aleph::LineEq::y_intercept ( ) const
inlineconstexprnoexcept

Get the y-intercept of the line.

Returns
The y-intercept (y0)

Definition at line 226 of file line.H.

References y0.

Referenced by TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
const LineEq l 
)
friend

Output stream operator.

Parameters
outOutput stream
lLine to output
Returns
Reference to output stream

Definition at line 410 of file line.H.

Member Data Documentation

◆ m

◆ y0

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().


The documentation for this struct was generated from the following file: