KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
Line Class Reference

Represents a 2D line segment between two Point endpoints. More...

#include <line.h>

Classes

class  List
 A list of Line objects with spatial query helpers. More...
 

Public Member Functions

 Line ()
 Default constructor — creates a zero-length line at the origin.
 
 Line (const QPoint &p1, const QPoint &p2)
 Construct a line from two integer points.
 
 Line (const QPointF &p1, const QPointF &p2)
 Construct a line from two floating-point points.
 
 Line (const QPointF &origin, double bearing, double distance)
 Construct a line from an origin point, bearing, and distance.
 
 Line (const QPointF &origin, Geo::Direction direction, double distance)
 Construct a line from an origin point, cardinal direction, and distance.
 
 Line (const QLineF &other)
 Construct from a QLineF.
 
 Line (const QLine &other)
 Construct from a QLine.
 
bool operator== (const Line &other) const
 Equality comparison — endpoints must match exactly.
 
bool operator!= (const Line &other) const
 Inequality comparison.
 
Point p1 () const
 Return the start point.
 
void setP1 (const Point &value)
 Set the start point.
 
Point p2 () const
 Return the end point.
 
void setP2 (const Point &value)
 Set the end point.
 
Point midpoint () const
 Return the midpoint of this line.
 
double length () const
 Return the length (Euclidean distance between endpoints).
 
double slope () const
 Return the slope (rise/run), or infinity for vertical lines.
 
double intercept () const
 Return the Y-intercept of the line's equation.
 
bool isVertical () const
 Test whether this line is exactly vertical.
 
bool isHorizontal () const
 Test whether this line is exactly horizontal.
 
double bearing () const
 Return the compass bearing from p1 to p2, in degrees.
 
Angle angle () const
 Return the angle of this line as an Angle object.
 
double distanceTo (const QPointF &to) const
 Return the perpendicular distance from this line to a point.
 
Point closestPointTo (const QPointF &point) const
 Return the point on this line closest to the given point.
 
Point closestPointTo (const QPointF &point, double &distance) const
 Return the closest point and output the distance.
 
Point furthestPointFrom (const QPointF &point) const
 Return the endpoint furthest from the given point.
 
Point topMostPoint () const
 Return the endpoint with the smallest Y coordinate.
 
Point bottomMostPoint () const
 Return the endpoint with the largest Y coordinate.
 
Point leftMostPoint () const
 Return the endpoint with the smallest X coordinate.
 
Point rightMostPoint () const
 Return the endpoint with the largest X coordinate.
 
bool hasXBetween (double x1, double x2) const
 Test whether the line spans an X range.
 
bool crossesX (double x) const
 Test whether the line crosses a specific X coordinate.
 
bool hasYBetween (double y1, double y2) const
 Test whether the line spans a Y range.
 
bool crossesY (double y) const
 Test whether the line crosses a specific Y coordinate.
 
bool intersects (const Line &other) const
 Test whether this line intersects another.
 
bool intersects (const Line &other, Point &intersection) const
 Test whether this line intersects another and output the intersection point.
 
bool intersects (const Circle &other) const
 Test whether this line intersects a circle.
 
bool intersects (const QRectF &other) const
 Test whether this line intersects a rectangle.
 
Point intersection (const Line &other) const
 Compute the intersection point with another line.
 
bool isLeftOf (const Line &other) const
 Test whether this line is entirely to the left of other.
 
bool isRightOf (const Line &other) const
 Test whether this line is entirely to the right of other.
 
bool isAbove (const Line &other) const
 Test whether this line is entirely above other.
 
bool isBelow (const Line &other) const
 Test whether this line is entirely below other.
 
bool isPerpendicular () const
 Test whether this line is perpendicular (horizontal or vertical).
 
bool sharesAxisWith (const Line &other) const
 Test whether this line shares an axis with other (both horizontal or both vertical).
 
bool sharesEndpointWith (const Line &other, double maxDistance=0) const
 Test whether this line shares an endpoint with other within a tolerance.
 
bool sharesSameEndpoints (const Line &other) const
 Test whether this line has exactly the same two endpoints as other.
 
bool isEndpoint (const QPointF &point, int precision=0) const
 Test whether a point is an endpoint of this line.
 
bool containsPoint (const QPointF &point) const
 Test whether a point lies on this line segment.
 
Lineshorten (double howMuch)
 Shorten this line by removing length from the far end.
 
Lineextend (double howMuch)
 Extend this line by adding length at the far end.
 
Linegrow (double howMuch)
 Grow this line symmetrically by adding length at both ends.
 
Geo::Direction direction () const
 Return the cardinal direction of this line.
 
double minX () const
 Return the minimum X coordinate of the two endpoints.
 
double maxX () const
 Return the maximum X coordinate of the two endpoints.
 
double minY () const
 Return the minimum Y coordinate of the two endpoints.
 
double maxY () const
 Return the maximum Y coordinate of the two endpoints.
 
Point::List points () const
 Return a list containing both endpoints.
 
Lineround ()
 Round both endpoints to integer coordinates in place.
 
Rectangle makeRectangle (int expandedWidth) const
 Create a rectangle of the given width centred on this line.
 
Linemove (double bearing, double distance)
 Move this line by bearing and distance.
 
Linemove (Geo::Direction direction, double distance)
 Move this line in a cardinal direction by a distance.
 
LinemoveDelta (double dx, double dy)
 Translate this line by a (dx, dy) delta.
 
Linerotate (const Point &centroid, double angle)
 Rotate this line around a centroid by an angle.
 
QLine toQLine () const
 Convert to a QLine (integer endpoints).
 
QLineF toQLineF () const
 Convert to a QLineF (floating-point endpoints).
 
QString toString () const
 Format this line as a human-readable string.
 
bool isValid () const
 Test whether this line has non-zero length.
 

Static Public Member Functions

static Line fromString (const QString &value)
 Parse a Line from a string produced by toString().
 
static List verticalLines (const QRectF &rect)
 Return vertical lines along the left and right edges of a rectangle.
 
static List horizontalLines (const QRectF &rect)
 Return horizontal lines along the top and bottom edges of a rectangle.
 

Detailed Description

Represents a 2D line segment between two Point endpoints.

Provides length, slope, bearing, midpoint, intersection, containment, and spatial relationship queries as well as movement and rotation transforms.

Definition at line 27 of file line.h.

Constructor & Destructor Documentation

◆ Line() [1/7]

Line::Line ( )
inline

Default constructor — creates a zero-length line at the origin.

Definition at line 31 of file line.h.

◆ Line() [2/7]

Line::Line ( const QPoint &  p1,
const QPoint &  p2 
)
inline

Construct a line from two integer points.

Parameters
p1Start point
p2End point

Definition at line 38 of file line.h.

◆ Line() [3/7]

Line::Line ( const QPointF &  p1,
const QPointF &  p2 
)
inline

Construct a line from two floating-point points.

Parameters
p1Start point
p2End point

Definition at line 46 of file line.h.

◆ Line() [4/7]

Line::Line ( const QPointF &  origin,
double  bearing,
double  distance 
)

Construct a line from an origin point, bearing, and distance.

Parameters
originStart point
bearingCompass bearing in degrees
distanceLength of the line

◆ Line() [5/7]

Line::Line ( const QPointF &  origin,
Geo::Direction  direction,
double  distance 
)

Construct a line from an origin point, cardinal direction, and distance.

Parameters
originStart point
directionCardinal direction of travel
distanceLength of the line

◆ Line() [6/7]

Line::Line ( const QLineF &  other)
inline

Construct from a QLineF.

Parameters
otherSource QLineF to copy endpoints from

Definition at line 69 of file line.h.

◆ Line() [7/7]

Line::Line ( const QLine &  other)
inline

Construct from a QLine.

Parameters
otherSource QLine to copy endpoints from

Definition at line 76 of file line.h.

Member Function Documentation

◆ angle()

Angle Line::angle ( ) const

Return the angle of this line as an Angle object.

Returns
Angle of the line

◆ bearing()

double Line::bearing ( ) const

Return the compass bearing from p1 to p2, in degrees.

Returns
Bearing in degrees

◆ bottomMostPoint()

Point Line::bottomMostPoint ( ) const

Return the endpoint with the largest Y coordinate.

Returns
Bottom-most Point

◆ closestPointTo() [1/2]

Point Line::closestPointTo ( const QPointF &  point) const

Return the point on this line closest to the given point.

Parameters
pointReference point
Returns
Closest Point on this line segment

◆ closestPointTo() [2/2]

Point Line::closestPointTo ( const QPointF &  point,
double &  distance 
) const

Return the closest point and output the distance.

Parameters
pointReference point
distanceOutput set to the distance
Returns
Closest Point on this line segment

◆ containsPoint()

bool Line::containsPoint ( const QPointF &  point) const

Test whether a point lies on this line segment.

Parameters
pointPoint to test
Returns
true if point lies on the segment

◆ crossesX()

bool Line::crossesX ( double  x) const

Test whether the line crosses a specific X coordinate.

Parameters
xX coordinate to test
Returns
true if the line crosses x

◆ crossesY()

bool Line::crossesY ( double  y) const

Test whether the line crosses a specific Y coordinate.

Parameters
yY coordinate to test
Returns
true if the line crosses y

◆ direction()

Geo::Direction Line::direction ( ) const

Return the cardinal direction of this line.

Returns
Cardinal direction

◆ distanceTo()

double Line::distanceTo ( const QPointF &  to) const

Return the perpendicular distance from this line to a point.

Parameters
toPoint to measure distance from
Returns
Perpendicular distance

◆ extend()

Line & Line::extend ( double  howMuch)

Extend this line by adding length at the far end.

Parameters
howMuchAmount to add
Returns
Reference to this line

◆ fromString()

static Line Line::fromString ( const QString &  value)
static

Parse a Line from a string produced by toString().

Parameters
valueString representation
Returns
Parsed Line

◆ furthestPointFrom()

Point Line::furthestPointFrom ( const QPointF &  point) const

Return the endpoint furthest from the given point.

Parameters
pointReference point
Returns
The furthest of p1 and p2

◆ grow()

Line & Line::grow ( double  howMuch)

Grow this line symmetrically by adding length at both ends.

Parameters
howMuchAmount to add at each end
Returns
Reference to this line

◆ hasXBetween()

bool Line::hasXBetween ( double  x1,
double  x2 
) const

Test whether the line spans an X range.

Parameters
x1First X boundary
x2Second X boundary
Returns
true if any part of the line falls between x1 and x2

◆ hasYBetween()

bool Line::hasYBetween ( double  y1,
double  y2 
) const

Test whether the line spans a Y range.

Parameters
y1First Y boundary
y2Second Y boundary
Returns
true if any part of the line falls between y1 and y2

◆ horizontalLines()

static List Line::horizontalLines ( const QRectF &  rect)
static

Return horizontal lines along the top and bottom edges of a rectangle.

Parameters
rectRectangle to extract edges from
Returns
List of horizontal edge lines

◆ intercept()

double Line::intercept ( ) const

Return the Y-intercept of the line's equation.

Returns
Y-intercept

◆ intersection()

Point Line::intersection ( const Line other) const

Compute the intersection point with another line.

Parameters
otherLine to intersect with
Returns
Intersection point (may be invalid if lines are parallel)

◆ intersects() [1/4]

bool Line::intersects ( const Circle other) const

Test whether this line intersects a circle.

Parameters
otherCircle to test against
Returns
true if the line intersects or is tangent to the circle

◆ intersects() [2/4]

bool Line::intersects ( const Line other) const

Test whether this line intersects another.

Parameters
otherLine to test against
Returns
true if the segments intersect

◆ intersects() [3/4]

bool Line::intersects ( const Line other,
Point intersection 
) const

Test whether this line intersects another and output the intersection point.

Parameters
otherLine to test against
intersectionOutput set to the intersection point
Returns
true if the segments intersect

◆ intersects() [4/4]

bool Line::intersects ( const QRectF &  other) const

Test whether this line intersects a rectangle.

Parameters
otherRectangle to test against
Returns
true if the line intersects the rectangle boundary

◆ isAbove()

bool Line::isAbove ( const Line other) const

Test whether this line is entirely above other.

Parameters
otherLine to compare against
Returns
true if this line is entirely above

◆ isBelow()

bool Line::isBelow ( const Line other) const

Test whether this line is entirely below other.

Parameters
otherLine to compare against
Returns
true if this line is entirely below

◆ isEndpoint()

bool Line::isEndpoint ( const QPointF &  point,
int  precision = 0 
) const

Test whether a point is an endpoint of this line.

Parameters
pointPoint to test
precisionDecimal places for comparison (0 = integer)
Returns
true if point matches p1 or p2 within precision

◆ isHorizontal()

bool Line::isHorizontal ( ) const

Test whether this line is exactly horizontal.

Returns
true if horizontal

◆ isLeftOf()

bool Line::isLeftOf ( const Line other) const

Test whether this line is entirely to the left of other.

Parameters
otherLine to compare against
Returns
true if this line is entirely to the left

◆ isPerpendicular()

bool Line::isPerpendicular ( ) const

Test whether this line is perpendicular (horizontal or vertical).

Returns
true if the line is horizontal or vertical

◆ isRightOf()

bool Line::isRightOf ( const Line other) const

Test whether this line is entirely to the right of other.

Parameters
otherLine to compare against
Returns
true if this line is entirely to the right

◆ isValid()

bool Line::isValid ( ) const
inline

Test whether this line has non-zero length.

Returns
true if length() != 0

Definition at line 573 of file line.h.

◆ isVertical()

bool Line::isVertical ( ) const

Test whether this line is exactly vertical.

Returns
true if vertical

◆ leftMostPoint()

Point Line::leftMostPoint ( ) const

Return the endpoint with the smallest X coordinate.

Returns
Left-most Point

◆ length()

double Line::length ( ) const

Return the length (Euclidean distance between endpoints).

Returns
Length of the line

◆ makeRectangle()

Rectangle Line::makeRectangle ( int  expandedWidth) const

Create a rectangle of the given width centred on this line.

Parameters
expandedWidthWidth of the resulting rectangle
Returns
Rectangle enclosing the line

◆ maxX()

double Line::maxX ( ) const

Return the maximum X coordinate of the two endpoints.

Returns
Maximum X value

◆ maxY()

double Line::maxY ( ) const

Return the maximum Y coordinate of the two endpoints.

Returns
Maximum Y value

◆ midpoint()

Point Line::midpoint ( ) const

Return the midpoint of this line.

Returns
Midpoint

◆ minX()

double Line::minX ( ) const

Return the minimum X coordinate of the two endpoints.

Returns
Minimum X value

◆ minY()

double Line::minY ( ) const

Return the minimum Y coordinate of the two endpoints.

Returns
Minimum Y value

◆ move() [1/2]

Line & Line::move ( double  bearing,
double  distance 
)

Move this line by bearing and distance.

Parameters
bearingDirection of movement in degrees
distanceDistance to move
Returns
Reference to this line

◆ move() [2/2]

Line & Line::move ( Geo::Direction  direction,
double  distance 
)

Move this line in a cardinal direction by a distance.

Parameters
directionCardinal direction
distanceDistance to move
Returns
Reference to this line

◆ moveDelta()

Line & Line::moveDelta ( double  dx,
double  dy 
)

Translate this line by a (dx, dy) delta.

Parameters
dxHorizontal displacement
dyVertical displacement
Returns
Reference to this line

◆ operator!=()

bool Line::operator!= ( const Line other) const
inline

Inequality comparison.

Parameters
otherLine to compare against
Returns
true if the lines differ

Definition at line 90 of file line.h.

◆ operator==()

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

Equality comparison — endpoints must match exactly.

Parameters
otherLine to compare against
Returns
true if both endpoints match

Definition at line 84 of file line.h.

◆ p1()

Point Line::p1 ( ) const
inline

Return the start point.

Returns
Start Point

Definition at line 103 of file line.h.

◆ p2()

Point Line::p2 ( ) const
inline

Return the end point.

Returns
End Point

Definition at line 115 of file line.h.

◆ points()

Point::List Line::points ( ) const

Return a list containing both endpoints.

Returns
Point::List with p1 and p2

◆ rightMostPoint()

Point Line::rightMostPoint ( ) const

Return the endpoint with the largest X coordinate.

Returns
Right-most Point

◆ rotate()

Line & Line::rotate ( const Point centroid,
double  angle 
)

Rotate this line around a centroid by an angle.

Parameters
centroidCentre of rotation
angleRotation angle in degrees
Returns
Reference to this line

◆ round()

Line & Line::round ( )

Round both endpoints to integer coordinates in place.

Returns
Reference to this line

◆ setP1()

void Line::setP1 ( const Point value)
inline

Set the start point.

Parameters
valueNew start point

Definition at line 109 of file line.h.

◆ setP2()

void Line::setP2 ( const Point value)
inline

Set the end point.

Parameters
valueNew end point

Definition at line 121 of file line.h.

◆ sharesAxisWith()

bool Line::sharesAxisWith ( const Line other) const

Test whether this line shares an axis with other (both horizontal or both vertical).

Parameters
otherLine to compare against
Returns
true if both lines share the same axis

◆ sharesEndpointWith()

bool Line::sharesEndpointWith ( const Line other,
double  maxDistance = 0 
) const

Test whether this line shares an endpoint with other within a tolerance.

Parameters
otherLine to compare
maxDistanceMaximum endpoint distance to consider shared (default 0 = exact)
Returns
true if any endpoint pair is within maxDistance of each other

◆ sharesSameEndpoints()

bool Line::sharesSameEndpoints ( const Line other) const

Test whether this line has exactly the same two endpoints as other.

Parameters
otherLine to compare against
Returns
true if both endpoints match in either order

◆ shorten()

Line & Line::shorten ( double  howMuch)

Shorten this line by removing length from the far end.

Parameters
howMuchAmount to remove
Returns
Reference to this line

◆ slope()

double Line::slope ( ) const

Return the slope (rise/run), or infinity for vertical lines.

Returns
Slope value

◆ topMostPoint()

Point Line::topMostPoint ( ) const

Return the endpoint with the smallest Y coordinate.

Returns
Top-most Point

◆ toQLine()

QLine Line::toQLine ( ) const

Convert to a QLine (integer endpoints).

Returns
QLine equivalent

◆ toQLineF()

QLineF Line::toQLineF ( ) const

Convert to a QLineF (floating-point endpoints).

Returns
QLineF equivalent

◆ toString()

QString Line::toString ( ) const

Format this line as a human-readable string.

Returns
String representation

◆ verticalLines()

static List Line::verticalLines ( const QRectF &  rect)
static

Return vertical lines along the left and right edges of a rectangle.

Parameters
rectRectangle to extract edges from
Returns
List of vertical edge lines

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