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>

Collaboration diagram for Aleph::LineEq:
[legend]

Public Member Functions

constexpr LineEq () noexcept=default
 Default constructor creates line y = x.
 
 LineEq (const Geom_Number &__y0, const Geom_Number &__m) noexcept
 Construct line from y-intercept and slope.
 
 LineEq (const Geom_Number &x1, const Geom_Number &y1, const Geom_Number &__m) noexcept
 Construct line through a point with given slope.
 
 LineEq (const Geom_Number &x1, const Geom_Number &y1, const Geom_Number &x2, const Geom_Number &y2)
 Construct a line through two points.
 
 LineEq (const Point &p, const Geom_Number &__m) noexcept
 Construct a line through a Point with a given slope.
 
 LineEq (const Point &p1, const Point &p2)
 Construct line through two Points.
 
Geom_Number operator() (const Geom_Number &x) const noexcept
 Evaluate line at given x-coordinate.
 
const Geom_Numberslope () const noexcept
 Get the slope of the line.
 
const Geom_Numbery_intercept () const noexcept
 Get the y-intercept of the line.
 
bool is_horizontal (const Geom_Number &epsilon=LINE_EPSILON) const noexcept
 Check if the line is horizontal (slope = 0).
 
bool is_parallel_to (const LineEq &l, const Geom_Number &epsilon=LINE_EPSILON) const noexcept
 Check if this line is parallel to another.
 
bool is_perpendicular_to (const LineEq &l, const Geom_Number &epsilon=LINE_EPSILON) const noexcept
 Check if this line is perpendicular to another.
 
Geom_Number x_at (const Geom_Number &y) const
 Compute x-coordinate for a given y-coordinate.
 
std::pair< Geom_Number, Geom_Numberintersection (const LineEq &l, const Geom_Number &epsilon=LINE_EPSILON) const
 Compute the intersection point with another line.
 
LineEq perpendicular_through (const Geom_Number &px, const Geom_Number &py) const
 Compute perpendicular line through a point.
 
Geom_Number distance_to (const Geom_Number &px, const Geom_Number &py) const noexcept
 Compute distance from a point to this line.
 
bool contains_point (const Geom_Number &px, const Geom_Number &py, const Geom_Number &epsilon=LINE_EPSILON) const noexcept
 Check if a point lies on this line.
 
Geom_Number distance_to (const Point &p) const noexcept
 Compute distance from a Point to this line.
 
LineEq perpendicular_through (const Point &p) const
 Compute a perpendicular line through a Point.
 
bool contains_point (const Point &p, const Geom_Number &epsilon=LINE_EPSILON) const noexcept
 Check if a Point lies on this line.
 
Point intersection_point (const LineEq &l, const Geom_Number &epsilon=LINE_EPSILON) const
 Compute the intersection Point with another 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.
 
bool approx_equal (const LineEq &l, const Geom_Number &epsilon) const noexcept
 Test approximate equality of two lines.
 
std::string to_string () const
 Convert line to a string representation.
 

Public Attributes

Geom_Number y0 {0}
 Y-intercept (where line crosses y-axis)
 
Geom_Number 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 125 of file line.H.

Constructor & Destructor Documentation

◆ LineEq() [1/6]

constexpr Aleph::LineEq::LineEq ( )
constexprdefaultnoexcept

Default constructor creates line y = x.

The default line passes through origin with slope 1.

◆ LineEq() [2/6]

Aleph::LineEq::LineEq ( const Geom_Number __y0,
const Geom_Number __m 
)
inlinenoexcept

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(Geom_Number(5), Geom_Number(2)); // y = 5 + 2x
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:115
2D infinite line in slope-intercept form.
Definition line.H:126

Definition at line 150 of file line.H.

◆ LineEq() [3/6]

Aleph::LineEq::LineEq ( const Geom_Number x1,
const Geom_Number y1,
const Geom_Number __m 
)
inlinenoexcept

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(Geom_Number(2), Geom_Number(4), Geom_Number(3)); // y = -2 + 3x

Definition at line 168 of file line.H.

◆ LineEq() [4/6]

Aleph::LineEq::LineEq ( const Geom_Number x1,
const Geom_Number y1,
const Geom_Number x2,
const Geom_Number y2 
)
inline

Construct a 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
domain_errorif x1 == x2 (vertical line, undefined slope)
Note
Horizontal lines (y1 == y2) are valid and result in slope = 0.
Example
LineEq line(Geom_Number(0), Geom_Number(0), Geom_Number(2), Geom_Number(6)); // y = 3x
LineEq horizontal(Geom_Number(0), Geom_Number(5), Geom_Number(10), Geom_Number(5)); // y = 5 (horizontal)

Definition at line 192 of file line.H.

References ah_domain_error_if, m, y0, and y1().

◆ LineEq() [5/6]

Aleph::LineEq::LineEq ( const Point p,
const Geom_Number __m 
)
inlinenoexcept

Construct a line through a Point with a given slope.

Parameters
pPoint on the line
__mSlope of the line
Example
LineEq line(p, Geom_Number(3)); // Line through (2, 4) with slope 3
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229

Definition at line 215 of file line.H.

◆ LineEq() [6/6]

Aleph::LineEq::LineEq ( const Point p1,
const Point p2 
)
inline

Construct line through two Points.

Parameters
p1First point
p2Second point
Exceptions
domain_errorif points have the same x-coordinate (vertical line)
Example
LineEq line(a, b); // y = 3x

Definition at line 232 of file line.H.

Member Function Documentation

◆ approx_equal()

bool Aleph::LineEq::approx_equal ( const LineEq l,
const Geom_Number epsilon 
) const
inlinenoexcept

Test approximate equality of two lines.

Lines are approximately equal if their slope and y-intercept are within an epsilon tolerance.

Parameters
lLine to compare with
epsilonTolerance for comparison
Returns
True if lines are approximately equal

Definition at line 497 of file line.H.

References abs(), Aleph::and, l, m, and y0.

◆ contains_point() [1/2]

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

Check if a point lies on this line.

Parameters
pxX-coordinate of point
pyY-coordinate of point
epsilonTolerance for comparison (default: exact equality)
Returns
True if point is on the line

Definition at line 399 of file line.H.

References abs(), and Aleph::divide_and_conquer_partition_dp().

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

◆ contains_point() [2/2]

bool Aleph::LineEq::contains_point ( const Point p,
const Geom_Number epsilon = LINE_EPSILON 
) const
inlinenoexcept

Check if a Point lies on this line.

Parameters
pThe point to test
epsilonTolerance for comparison (default: exact equality)
Returns
True if the point is on the line

Definition at line 435 of file line.H.

References contains_point().

◆ distance_to() [1/2]

Geom_Number Aleph::LineEq::distance_to ( const Geom_Number px,
const Geom_Number 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 383 of file line.H.

References abs(), Aleph::diff(), Aleph::divide_and_conquer_partition_dp(), m, sqrt(), and y0.

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

◆ distance_to() [2/2]

Geom_Number Aleph::LineEq::distance_to ( const Point p) const
inlinenoexcept

Compute distance from a Point to this line.

Parameters
pThe point
Returns
Perpendicular distance from point to line

Definition at line 411 of file line.H.

References distance_to().

◆ intersection()

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

Compute the 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
domain_errorif lines are parallel (no unique intersection)
Example
LineEq l1(Geom_Number(0), Geom_Number(1)); // y = x
LineEq l2(Geom_Number(2), Geom_Number(-1)); // y = 2 - x
auto [x, y] = l1.intersection(l2); // (1, 1)
static mpfr_t y
Definition mpfr_mul_d.c:3
DynList< int > l1
DynList< int > l2

Definition at line 343 of file line.H.

References ah_domain_error_if, is_parallel_to(), l, m, and y0.

Referenced by intersection_point(), and TEST_F().

◆ intersection_point()

Point Aleph::LineEq::intersection_point ( const LineEq l,
const Geom_Number epsilon = LINE_EPSILON 
) const
inline

Compute the intersection Point with another line.

Parameters
lThe other line
epsilonTolerance for parallel line detection
Returns
Intersection point
Exceptions
domain_errorif lines are parallel (no unique intersection)
Example
LineEq l1(Geom_Number(0), Geom_Number(1)); // y = x
LineEq l2(Geom_Number(2), Geom_Number(-1)); // y = 2 - x
Point p = l1.intersection_point(l2); // Point(1, 1)

Definition at line 456 of file line.H.

References intersection(), l, and y.

◆ is_horizontal()

bool Aleph::LineEq::is_horizontal ( const Geom_Number epsilon = LINE_EPSILON) const
inlinenoexcept

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

Parameters
epsilonTolerance for comparison (default: exact equality)
Returns
True if the line is horizontal

Definition at line 271 of file line.H.

References abs(), and m.

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

◆ is_parallel_to()

bool Aleph::LineEq::is_parallel_to ( const LineEq l,
const Geom_Number epsilon = LINE_EPSILON 
) const
inlinenoexcept

Check if this line is parallel to another.

Two lines are parallel if they have the same slope.

Parameters
lThe other line
epsilonTolerance for comparison (default: exact equality)
Returns
True if lines are parallel

Definition at line 285 of file line.H.

References abs(), l, and m.

Referenced by intersection().

◆ is_perpendicular_to()

bool Aleph::LineEq::is_perpendicular_to ( const LineEq l,
const Geom_Number epsilon = LINE_EPSILON 
) const
inlinenoexcept

Check if this line is perpendicular to another.

Two lines are perpendicular if m1 * m2 = -1

Parameters
lThe other line
epsilonTolerance for comparison (default: exact equality)
Returns
True if lines are perpendicular

Definition at line 300 of file line.H.

References abs(), l, and m.

◆ 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 482 of file line.H.

References Aleph::divide_and_conquer_partition_dp(), and l.

◆ operator()()

Geom_Number Aleph::LineEq::operator() ( const Geom_Number x) const
inlinenoexcept

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(Geom_Number(1), Geom_Number(2)); // y = 1 + 2x
Geom_Number y = line(Geom_Number(3)); // y = 7

Definition at line 249 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.

Parameters
lLine to compare with
Returns
True if lines are identical

Definition at line 471 of file line.H.

References Aleph::and, l, m, and y0.

◆ perpendicular_through() [1/2]

LineEq Aleph::LineEq::perpendicular_through ( const Geom_Number px,
const Geom_Number 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
domain_errorif this line is horizontal (perpendicular would be vertical)

Definition at line 365 of file line.H.

References ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), is_horizontal(), and m.

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

◆ perpendicular_through() [2/2]

LineEq Aleph::LineEq::perpendicular_through ( const Point p) const
inline

Compute a perpendicular line through a Point.

Parameters
pPoint on the perpendicular line
Returns
Perpendicular line through p
Exceptions
domain_errorif this line is horizontal (perpendicular would be vertical)

Definition at line 423 of file line.H.

References Aleph::Point::get_x(), Aleph::Point::get_y(), and perpendicular_through().

◆ slope()

const Geom_Number & Aleph::LineEq::slope ( ) const
inlinenoexcept

Get the slope of the line.

Returns
The slope (m)

Definition at line 258 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 a string representation.

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

Definition at line 507 of file line.H.

References m, and y0.

Referenced by TEST(), and TEST_F().

◆ x_at()

Geom_Number Aleph::LineEq::x_at ( const Geom_Number 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
domain_errorif line is horizontal (undefined x)

Definition at line 315 of file line.H.

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

Referenced by TEST_F(), and TEST_F().

◆ y_intercept()

const Geom_Number & Aleph::LineEq::y_intercept ( ) const
inlinenoexcept

Get the y-intercept of the line.

Returns
The y-intercept (y0)

Definition at line 264 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
outThe output stream.
lThe line to output.
Returns
A reference to the output stream.

Definition at line 521 of file line.H.

Member Data Documentation

◆ m

◆ y0

Geom_Number Aleph::LineEq::y0 {0}

Y-intercept (where line crosses y-axis)

Definition at line 127 of file line.H.

Referenced by LineEq(), approx_equal(), 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: