Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Geom::Line Class Reference

Infinite line on a plane. More...

#include <line.h>

Inheritance diagram for Geom::Line:

Public Member Functions

std::vector< ShapeIntersectionintersect (Line const &other) const
 
std::vector< ShapeIntersectionintersect (Ray const &r) const
 
std::vector< ShapeIntersectionintersect (LineSegment const &ls) const
 
template<typename T >
Lineoperator*= (T const &tr)
 
bool operator== (Line const &other) const
 
Retrieve and set the line's parameters.
Point origin () const
 Get the line's origin point.
 
Point vector () const
 Get the line's raw direction vector.
 
Point versor () const
 Get the line's normalized direction vector.
 
Coord angle () const
 Angle the line makes with the X axis, in mathematical convention.
 
void setOrigin (Point const &p)
 Set the point at zero time.
 
void setVector (Point const &v)
 Set the speed of the line.
 
void setAngle (Coord angle)
 Set the angle the line makes with the X axis.
 
void setPoints (Point const &a, Point const &b)
 Set a line based on two points it should pass through.
 
void setCoefficients (double a, double b, double c)
 Set the coefficients of the line equation.
 
std::vector< double > coefficients () const
 Get the coefficients of the line equation as a vector.
 
void coefficients (Coord &a, Coord &b, Coord &c) const
 Get the coefficients of the line equation by reference.
 
bool isDegenerate () const
 Check if the line has more than one point.
 
bool isHorizontal () const
 Check if the line is horizontal (y is constant).
 
bool isVertical () const
 Check if the line is vertical (x is constant).
 
void normalize ()
 Reparametrize the line so that it has unit speed.
 
Line normalized () const
 Return a new line reparametrized for unit speed.
 
Evaluate the line as a function.
Point initialPoint () const
 
Point finalPoint () const
 
Point pointAt (Coord t) const
 
Coord valueAt (Coord t, Dim2 d) const
 
Coord timeAt (Point const &p) const
 Get a time value corresponding to a point.
 
Coord timeAtProjection (Point const &p) const
 Get a time value corresponding to a projection of a point on the line.
 
Coord nearestTime (Point const &p) const
 Find a point on the line closest to the query point.
 
std::vector< Coordroots (Coord v, Dim2 d) const
 Find intersection with an axis-aligned line.
 
Coord root (Coord v, Dim2 d) const
 
Create other objects based on this line.
void reverse ()
 
Line reversed () const
 Create a line containing the same points, but in opposite direction.
 
Curveportion (Coord f, Coord t) const
 Same as segment(), but allocate the line segment dynamically.
 
LineSegment segment (Coord f, Coord t) const
 Create a segment of this line.
 
std::optional< LineSegmentclip (Rect const &r) const
 Return the portion of the line that is inside the given rectangle.
 
Ray ray (Coord t)
 Create a ray starting at the specified time value.
 
Line derivative () const
 Create a derivative of the line.
 
Line transformed (Affine const &m) const
 Create a line transformed by an affine transformation.
 
Point normal () const
 Get a unit vector normal to the line.
 
Point normalAndDist (double &dist) const
 
Affine reflection () const
 Compute an affine matrix representing a reflection about the line.
 
Affine rotationToZero (Dim2 d) const
 Compute an affine which transforms all points on the line to zero X or Y coordinate.
 
Affine rotationToAxis (Dim2 d) const
 Compute a rotation affine which transforms the line to one of the axes.
 
Affine transformTo (Line const &other) const
 Create a transformation that maps one line to another.
 

Private Attributes

Point _initial
 
Point _final
 

Friends

template<typename T >
Line operator* (Line const &l, T const &tr)
 

Related Symbols

(Note that these are not member symbols.)

double distance (Point const &p, Line const &line)
 Compute distance from point to line.
 
bool are_same (Line const &l1, Line const &l2, double eps=EPSILON)
 Test whether two lines are approximately the same.
 
bool are_orthogonal (Line const &l1, Line const &l2, double eps=EPSILON)
 Test whether two lines are perpendicular.
 

Creating lines.

 Line ()
 Create a default horizontal line.
 
 Line (Point const &origin, Coord angle)
 Create a line with the specified inclination.
 
 Line (Point const &a, Point const &b)
 Create a line going through two points.
 
 Line (double a, double b, double c)
 Create a line based on the coefficients of its equation.
 
 Line (LineSegment const &seg)
 Create a line by extending a line segment.
 
 Line (Ray const &r)
 Create a line by extending a ray.
 
Lineduplicate () const
 
static Line from_normal_distance (Point const &n, Coord c)
 Create a line normal to a vector at a specified distance from origin.
 
static Line from_origin_and_vector (Point const &o, Point const &v)
 Create a line from origin and unit vector.
 

Detailed Description

Infinite line on a plane.

A line is specified as two points through which it passes. Lines can be interpreted as functions \( f: (-\infty, \infty) \to \mathbb{R}^2\). Zero corresponds to the first (origin) point, one corresponds to the second (final) point. All other points are computed as a linear interpolation between those two: \(p = (1-t) a + t b\). Many such functions have the same image and therefore represent the same lines; for example, adding \(b-a\) to both points yields the same line.

2Geom can represent the same line in many ways by design: using a different representation would lead to precision loss. For example, a line from (1e30, 1e30) to (10,0) would actually evaluate to (0,0) at time 1 if it was stored as origin and normalized versor, or origin and angle.

Definition at line 51 of file line.h.

Constructor & Destructor Documentation

◆ Line() [1/6]

Geom::Line::Line ( )
inline

Create a default horizontal line.

Creates a line with unit speed going in +X direction.

Definition at line 62 of file line.h.

Referenced by duplicate().

◆ Line() [2/6]

Geom::Line::Line ( Point const &  origin,
Coord  angle 
)
inline

Create a line with the specified inclination.

Parameters
originOne of the points on the line
angleAngle of the line in mathematical convention

Definition at line 68 of file line.h.

References _final, _initial, angle(), Geom::sincos(), Geom::X, and Geom::Y.

◆ Line() [3/6]

Geom::Line::Line ( Point const &  a,
Point const &  b 
)
inline

Create a line going through two points.

The first point will be at time 0, while the second one will be at time 1.

Parameters
aInitial point
bFirst point

Definition at line 81 of file line.h.

◆ Line() [4/6]

Geom::Line::Line ( double  a,
double  b,
double  c 
)
inline

Create a line based on the coefficients of its equation.

See also
Line::setCoefficients()

Definition at line 88 of file line.h.

References c, and setCoefficients().

◆ Line() [5/6]

Geom::Line::Line ( LineSegment const &  seg)
inlineexplicit

Create a line by extending a line segment.

Definition at line 93 of file line.h.

◆ Line() [6/6]

Geom::Line::Line ( Ray const &  r)
inlineexplicit

Create a line by extending a ray.

Definition at line 99 of file line.h.

Member Function Documentation

◆ angle()

Coord Geom::Line::angle ( ) const
inline

◆ clip()

◆ coefficients() [1/2]

std::vector< Coord > Geom::Line::coefficients ( ) const

Get the coefficients of the line equation as a vector.

Get the implicit line equation coefficients.

Returns
STL vector v such that v[0] contains \(a\), v[1] contains \(b\), and v[2] contains \(c\).

Note that conversion to implicit form always causes loss of precision when dealing with lines that start far from the origin and end very close to it. It is recommended to normalize the line before converting it to implicit form.

Returns
Vector with three values corresponding to the A, B and C coefficients of the line equation for this line.

Definition at line 120 of file line.cpp.

References c, and coefficients().

Referenced by coefficients(), draw_line(), Geom::Ellipse::intersect(), Geom::xAx::set(), Geom::xAx::set(), Geom::detail::bezier_clipping::signed_distance(), and TEST().

◆ coefficients() [2/2]

void Geom::Line::coefficients ( Coord a,
Coord b,
Coord c 
) const

Get the coefficients of the line equation by reference.

Definition at line 105 of file line.cpp.

References _final, _initial, c, Geom::cross(), Geom::Point::cw(), vector(), Geom::X, and Geom::Y.

◆ derivative()

Line Geom::Line::derivative ( ) const
inline

Create a derivative of the line.

The new line will always be degenerate. Its origin will be equal to this line's vector.

Definition at line 305 of file line.h.

References result, and vector().

Referenced by wrap_line().

◆ duplicate()

Line * Geom::Line::duplicate ( ) const
inline

Definition at line 119 of file line.h.

References Line().

◆ finalPoint()

◆ from_normal_distance()

static Line Geom::Line::from_normal_distance ( Point const &  n,
Coord  c 
)
inlinestatic

Create a line normal to a vector at a specified distance from origin.

Definition at line 105 of file line.h.

References c, Geom::rot90(), and start.

◆ from_origin_and_vector()

static Line Geom::Line::from_origin_and_vector ( Point const &  o,
Point const &  v 
)
inlinestatic

Create a line from origin and unit vector.

Note that each line direction has two possible unit vectors.

Parameters
oPoint through which the line will pass
vUnit vector of the line's direction

Definition at line 114 of file line.h.

Referenced by Inkscape::CanvasItemGridXY::_render(), Inkscape::CanvasItemGuideLine::_render(), Inkscape::CanvasItemGuideLine::closest_distance_to(), Geom::decompose_degenerate(), Geom::PlanarGraph< EdgeLabel >::deviatesLeft(), Geom::RatQuad::fromPointsTangents(), Inkscape::Extension::Internal::PdfBuilder::Document::get_radial_pattern(), and Geom::xAx::toCurve().

◆ initialPoint()

◆ intersect() [1/3]

std::vector< ShapeIntersection > Geom::Line::intersect ( Line const &  other) const

Definition at line 253 of file line.cpp.

References Geom::cross(), initialPoint(), result, and vector().

Referenced by Geom::Ellipse::intersect(), intersect(), intersect(), and TEST().

◆ intersect() [2/3]

std::vector< ShapeIntersection > Geom::Line::intersect ( LineSegment const &  ls) const

Definition at line 277 of file line.cpp.

References Geom::filter_line_segment_intersections(), intersect(), and result.

◆ intersect() [3/3]

std::vector< ShapeIntersection > Geom::Line::intersect ( Ray const &  r) const

Definition at line 269 of file line.cpp.

References Geom::filter_ray_intersections(), intersect(), and result.

◆ isDegenerate()

bool Geom::Line::isDegenerate ( ) const
inline

Check if the line has more than one point.

A degenerate line can be created if the line is created from a line equation that has no solutions.

Returns
True if the line has no points or exactly one point

Definition at line 190 of file line.h.

References _final, and _initial.

Referenced by distance(), Geom::Ellipse::intersect(), timeAtProjection(), and wrap_line().

◆ isHorizontal()

bool Geom::Line::isHorizontal ( ) const
inline

Check if the line is horizontal (y is constant).

Definition at line 194 of file line.h.

References _final, _initial, and Geom::Y.

◆ isVertical()

bool Geom::Line::isVertical ( ) const
inline

Check if the line is vertical (x is constant).

Definition at line 198 of file line.h.

References _final, _initial, and Geom::X.

◆ nearestTime()

◆ normal()

Point Geom::Line::normal ( ) const
inline

Get a unit vector normal to the line.

If Y grows upwards, then this is the left normal. If Y grows downwards, then this is the right normal.

Definition at line 320 of file line.h.

References Geom::Point::normalized(), Geom::rot90(), and vector().

Referenced by normalAndDist(), and wrap_line().

◆ normalAndDist()

Point Geom::Line::normalAndDist ( double &  dist) const
inline

Definition at line 325 of file line.h.

References _initial, Geom::dot(), and normal().

Referenced by Geom::xAx::fromLine(), and wrap_line().

◆ normalize()

void Geom::Line::normalize ( )
inline

Reparametrize the line so that it has unit speed.

Note that the direction of the line may also change.

Definition at line 204 of file line.h.

References _final, and _initial.

Referenced by Geom::detail::bezier_clipping::clip< intersection_point_tag >().

◆ normalized()

Line Geom::Line::normalized ( ) const
inline

Return a new line reparametrized for unit speed.

Definition at line 215 of file line.h.

References _final, and _initial.

◆ operator*=()

template<typename T >
Line & Geom::Line::operator*= ( T const &  tr)
inline

Definition at line 375 of file line.h.

References _final, and _initial.

◆ operator==()

bool Geom::Line::operator== ( Line const &  other) const
inline

Definition at line 382 of file line.h.

References _final, _initial, distance(), nearestTime(), and pointAt().

◆ origin()

Point Geom::Line::origin ( ) const
inline

Get the line's origin point.

Definition at line 128 of file line.h.

References _initial.

Referenced by are_same(), avoid_item_poly(), Geom::intersection(), Geom::detail::intersection_impl(), Geom::detail::intersection_impl(), and Geom::xAx::roots().

◆ pointAt()

◆ portion()

Curve * Geom::Line::portion ( Coord  f,
Coord  t 
) const
inline

Same as segment(), but allocate the line segment dynamically.

Definition at line 274 of file line.h.

References pointAt().

◆ ray()

Ray Geom::Line::ray ( Coord  t)
inline

Create a ray starting at the specified time value.

The created ray will go in the direction of the line's vector (in the direction of increasing time values).

Parameters
tTime value where the ray should start
Returns
Ray starting at t and going in the direction of the vector

Definition at line 295 of file line.h.

References pointAt(), result, Geom::Ray::setOrigin(), and vector().

Referenced by sp_gradient_reset_to_userspace(), and Inkscape::LivePathEffect::LPESlice::splititem().

◆ reflection()

Affine Geom::Line::reflection ( ) const
inline

Compute an affine matrix representing a reflection about the line.

Definition at line 332 of file line.h.

References _initial, versor(), Geom::X, and Geom::Y.

Referenced by TEST().

◆ reverse()

void Geom::Line::reverse ( )
inline

Definition at line 262 of file line.h.

References _final, and _initial.

Referenced by wrap_line().

◆ reversed()

Line Geom::Line::reversed ( ) const
inline

Create a line containing the same points, but in opposite direction.

Returns
Line \(g\) such that \(g(t) = f(1-t)\)

Definition at line 267 of file line.h.

References _final, _initial, and result.

◆ root()

Coord Geom::Line::root ( Coord  v,
Dim2  d 
) const

Definition at line 140 of file line.cpp.

References _initial, vector(), Geom::X, and Geom::Y.

◆ roots()

std::vector< Coord > Geom::Line::roots ( Coord  v,
Dim2  d 
) const

Find intersection with an axis-aligned line.

Parameters
vCoordinate of the axis-aligned line
dWhich axis the coordinate is on. X means a vertical line, Y means a horizontal line.
Returns
Time values at which this line intersects the query line.

Definition at line 131 of file line.cpp.

References result, and root.

Referenced by wrap_line().

◆ rotationToAxis()

Affine Geom::Line::rotationToAxis ( Dim2  d) const
inline

Compute a rotation affine which transforms the line to one of the axes.

Parameters
dWhich line should be the axis

Definition at line 362 of file line.h.

References Geom::other_dimension(), and rotationToZero().

◆ rotationToZero()

Affine Geom::Line::rotationToZero ( Dim2  d) const
inline

Compute an affine which transforms all points on the line to zero X or Y coordinate.

This operation is useful in reducing intersection problems to root-finding problems. There are many affines which do this transformation. This function returns one that preserves angles, areas and distances - a rotation combined with a translation, and additionally moves the initial point of the line to (0,0). This way it works without problems even for lines perpendicular to the target, though may in some cases have lower precision than e.g. a shear transform.

Parameters
dWhich coordinate of points on the line should be zero after the transformation

Definition at line 350 of file line.h.

References _initial, vector(), Geom::X, and Geom::Y.

Referenced by Geom::bezier_line_intersections(), rotationToAxis(), and TEST().

◆ segment()

LineSegment Geom::Line::segment ( Coord  f,
Coord  t 
) const
inline

Create a segment of this line.

Parameters
fTime value for the initial point of the segment
tTime value for the final point of the segment
Returns
Created line segment

Definition at line 283 of file line.h.

References pointAt().

Referenced by Inkscape::SnappedCurve::intersect(), Geom::projection(), and Geom::rect_line_intersect().

◆ setAngle()

void Geom::Line::setAngle ( Coord  angle)
inline

Set the angle the line makes with the X axis.

Origin remains unchanged.

Definition at line 160 of file line.h.

References _final, _initial, angle(), distance(), Geom::sincos(), Geom::X, and Geom::Y.

◆ setCoefficients()

void Geom::Line::setCoefficients ( double  a,
double  b,
double  c 
)

Set the coefficients of the line equation.

Set the line by solving the line equation.

The line equation is: \(ax + by = c\). Points that satisfy the equation are on the line.

A line is a set of points that satisfies the line equation \(Ax + By + C = 0\). This function changes the line so that its points satisfy the line equation with the given coefficients.

Definition at line 62 of file line.cpp.

References _final, _initial, c, offset, Geom::X, and Geom::Y.

Referenced by Geom::xAx::arc_bound(), Geom::xAx::decompose(), Line(), and wrap_line().

◆ setOrigin()

void Geom::Line::setOrigin ( Point const &  p)
inline

Set the point at zero time.

The orientation remains unchanged, modulo numeric errors during addition.

Definition at line 147 of file line.h.

References _final, and _initial.

Referenced by avoid_item_poly().

◆ setPoints()

void Geom::Line::setPoints ( Point const &  a,
Point const &  b 
)
inline

◆ setVector()

void Geom::Line::setVector ( Point const &  v)
inline

Set the speed of the line.

Origin remains unchanged.

Definition at line 154 of file line.h.

References _final, and _initial.

Referenced by avoid_item_poly().

◆ timeAt()

Coord Geom::Line::timeAt ( Point const &  p) const

Get a time value corresponding to a point.

Parameters
pPoint on the line. If the point is not on the line, the returned value will be meaningless.
Returns
Time value t such that \(f(t) = p\).
See also
timeAtProjection

Definition at line 223 of file line.cpp.

References _initial, vector(), Geom::X, and Geom::Y.

Referenced by Geom::Ellipse::intersect(), Geom::Circle::intersect(), and Geom::xAx::set().

◆ timeAtProjection()

Coord Geom::Line::timeAtProjection ( Point const &  p) const
inline

Get a time value corresponding to a projection of a point on the line.

Parameters
pArbitrary point.
Returns
Time value corresponding to a point closest to p.

Definition at line 244 of file line.h.

References _initial, Geom::dot(), isDegenerate(), and vector().

Referenced by nearestTime(), and Geom::xAx::set().

◆ transformed()

Line Geom::Line::transformed ( Affine const &  m) const
inline

Create a line transformed by an affine transformation.

Definition at line 312 of file line.h.

References _final, and _initial.

Referenced by Inkscape::SnappedCurve::intersect(), and wrap_line().

◆ transformTo()

Affine Geom::Line::transformTo ( Line const &  other) const

Create a transformation that maps one line to another.

This will return a transformation \(A\) such that \(L_1(t) \cdot A = L_2(t)\), where \(L_1\) is this line and \(L_2\) is the line passed as the parameter. The returned transformation will preserve angles.

Definition at line 244 of file line.cpp.

References _initial, Geom::angle_between(), Geom::Point::length(), Geom::length(), result, and vector().

◆ valueAt()

Coord Geom::Line::valueAt ( Coord  t,
Dim2  d 
) const
inline

Definition at line 235 of file line.h.

References _final, _initial, and Geom::lerp().

Referenced by wrap_line().

◆ vector()

◆ versor()

Point Geom::Line::versor ( ) const
inline

Get the line's normalized direction vector.

The retrieved vector is normalized to unit length.

Definition at line 135 of file line.h.

References _final, _initial, and Geom::Point::normalized().

Referenced by avoid_item_poly(), Inkscape::LineSnapper::constrainedSnap(), reflection(), Geom::xAx::roots(), Inkscape::signed_distance(), and TEST().

Friends And Related Symbol Documentation

◆ are_orthogonal()

bool are_orthogonal ( Line const &  l1,
Line const &  l2,
double  eps = EPSILON 
)
related

Test whether two lines are perpendicular.

Definition at line 447 of file line.h.

References Geom::are_near(), Geom::dot(), and vector().

◆ are_same()

bool are_same ( Line const &  l1,
Line const &  l2,
double  eps = EPSILON 
)
related

Test whether two lines are approximately the same.

This tests for being parallel and the origin of one line being close to the other, so it tests whether the images of the lines are similar, not whether the same time values correspond to similar points. For example a line from (1,1) to (2,2) and a line from (-1,-1) to (0,0) will be the same, because their images match, even though there is no time value for which the lines give similar points.

Definition at line 439 of file line.h.

References Geom::are_near(), Geom::are_parallel(), and origin().

◆ distance()

double distance ( Point const &  p,
Line const &  line 
)
related

Compute distance from point to line.

Definition at line 409 of file line.h.

References initialPoint(), isDegenerate(), nearestTime(), and pointAt().

Referenced by operator==(), and setAngle().

◆ operator*

template<typename T >
Line operator* ( Line const &  l,
T const &  tr 
)
friend

Definition at line 389 of file line.h.

Member Data Documentation

◆ _final

◆ _initial


The documentation for this class was generated from the following files: