23#include <geos/export.h>
24#include <geos/geom/Coordinate.h>
25#include <geos/geom/LineSegment.h>
26#include <geos/algorithm/Distance.h>
27#include <geos/algorithm/Orientation.h>
34#include <unordered_set>
39class CoordinateSequence;
73 LineSegment(
double x0,
double y0,
double x1,
double y1)
81 void setCoordinates(
const Coordinate& c0,
const Coordinate& c1)
87 void setCoordinates(
const LineSegment& ls)
89 setCoordinates(ls.p0, ls.p1);
95 const Coordinate& operator[](std::size_t i)
const
104 Coordinate& operator[](std::size_t i)
116 return std::min(p0.x, p1.x);
122 return std::max(p0.x, p1.x);
128 return std::min(p0.y, p1.y);
134 return std::max(p0.y, p1.y);
140 return p0.distance(p1);
188 return orientationIndex(*seg);
238 if(p1.compareTo(p0) < 0) {
246 return std::atan2(p1.y - p0.y, p1.x - p0.x);
256 return midPoint(p0, p1);
259 static CoordinateXY midPoint(
const CoordinateXY& pt0,
const CoordinateXY& pt1)
263 (pt0.y + pt1.y) / 2);
290 return p0.distance(p);
324 p0.x + segmentLengthFraction * (p1.x - p0.x),
325 p0.y + segmentLengthFraction * (p1.y - p0.y),
326 p0.
z + segmentLengthFraction * (p1.
z - p0.
z));
329 static CoordinateXY pointAlong(
double segmentLengthFraction,
const CoordinateXY& p0,
const CoordinateXY& p1)
331 return { p0.x + segmentLengthFraction * (p1.x - p0.x),
332 p0.y + segmentLengthFraction * (p1.y - p0.y) };
335 static Coordinate pointAlong(
double segmentLengthFraction,
const Coordinate& p0,
const Coordinate& p1)
337 return { p0.x + segmentLengthFraction * (p1.x - p0.x),
338 p0.y + segmentLengthFraction * (p1.y - p0.y),
339 p0.z + segmentLengthFraction * (p1.z - p0.z) };
342 static CoordinateXYM pointAlong(
double segmentLengthFraction,
const CoordinateXYM& p0,
const CoordinateXYM& p1)
344 return { p0.x + segmentLengthFraction * (p1.x - p0.x),
345 p0.y + segmentLengthFraction * (p1.y - p0.y),
346 p0.m + segmentLengthFraction * (p1.m - p0.m) };
349 static CoordinateXYZM pointAlong(
double segmentLengthFraction,
const CoordinateXYZM& p0,
const CoordinateXYZM& p1)
351 return { p0.x + segmentLengthFraction * (p1.x - p0.x),
352 p0.y + segmentLengthFraction * (p1.y - p0.y),
353 p0.z + segmentLengthFraction * (p1.z - p0.z),
354 p0.m + segmentLengthFraction * (p1.m - p0.m) };
382 double offsetDistance,
420 static double projectionFactor(
const CoordinateXY& p0,
const CoordinateXY& p1,
const CoordinateXY& q);
449 CoordinateXY project(
const CoordinateXY& p)
const;
451 static CoordinateXY project(
const CoordinateXY& p0,
const CoordinateXY& p1,
const CoordinateXY& q);
478 static CoordinateXY closestPoint(
const CoordinateXY& p0,
const CoordinateXY& p1,
479 const CoordinateXY& q);
500 std::array<Coordinate, 2> closestPoints(
const LineSegment* line)
503 return closestPoints(*line);
506 static std::array<CoordinateXY, 2> closestPoints(
const CoordinateXY& p0,
507 const CoordinateXY& p1,
508 const CoordinateXY& q0,
509 const CoordinateXY& q1);
565 int comp0 = p0.compareTo(other.p0);
569 return p1.compareTo(other.
p1);
572 std::ostream& operator<< (std::ostream& o);
574 inline bool operator==(
const LineSegment& rhs)
const {
575 return compareTo(rhs) == 0;
578 inline bool operator<(
const LineSegment& rhs)
const {
579 return compareTo(rhs) < 0;
582 inline bool operator>(
const LineSegment& rhs)
const {
583 return compareTo(rhs) > 0;
587 inline std::size_t operator()(
const LineSegment & s)
const {
588 std::size_t h = std::hash<double>{}(s.p0.x);
589 h ^= (std::hash<double>{}(s.p0.y) << 1);
590 h ^= (std::hash<double>{}(s.p1.x) << 1);
591 return h ^ (std::hash<double>{}(s.p1.y) << 1);
594 inline std::size_t operator()(
const LineSegment * s)
const {
595 std::size_t h = std::hash<double>{}(s->p0.x);
596 h ^= (std::hash<double>{}(s->p0.y) << 1);
597 h ^= (std::hash<double>{}(s->p1.x) << 1);
598 return h ^ (std::hash<double>{}(s->p1.y) << 1);
603 using UnorderedSet = std::unordered_set<LineSegment, HashCode>;
607 static CoordinateXY project(
const CoordinateXY& p0,
const CoordinateXY& p1,
double factor);
609 void project(
double factor, CoordinateXY& ret)
const;
static double pointToSegment(const geom::CoordinateXY &p, const geom::CoordinateXY &A, const geom::CoordinateXY &B)
static double segmentToSegment(const geom::CoordinateXY &A, const geom::CoordinateXY &B, const geom::CoordinateXY &C, const geom::CoordinateXY &D)
static double pointToLinePerpendicular(const geom::CoordinateXY &p, const geom::CoordinateXY &A, const geom::CoordinateXY &B)
static int index(const geom::CoordinateXY &p1, const geom::CoordinateXY &p2, const geom::CoordinateXY &q)
Returns the orientation index of the direction of the point q relative to a directed infinite line sp...
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:220
double z
z-coordinate
Definition: Coordinate.h:244
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:72
Definition: LineSegment.h:61
bool isVertical() const
Definition: LineSegment.h:156
LineSegment(const Coordinate &c0, const Coordinate &c1)
Segment end.
Definition: LineSegment.h:68
double minX() const
gets the minimum X ordinate value
Definition: LineSegment.h:114
int compareTo(const LineSegment &other) const
Compares this object with the specified object for order.
Definition: LineSegment.h:563
int orientationIndex(const CoordinateXY &p) const
Definition: LineSegment.h:202
void pointAlongOffset(double segmentLengthFraction, double offsetDistance, Coordinate &ret) const
Computes the Coordinate that lies a given fraction along the line defined by this segment and offset ...
bool project(const LineSegment &seg, LineSegment &ret) const
Project a line segment onto this line segment and return the resulting line segment.
double minY() const
gets the minimum Y ordinate value
Definition: LineSegment.h:126
void pointAlong(double segmentLengthFraction, Coordinate &ret) const
Computes the Coordinate that lies a given fraction along the line defined by this segment.
Definition: LineSegment.h:321
void project(const Coordinate &p, Coordinate &ret) const
Compute the projection of a point onto the line determined by this line segment.
double maxX() const
gets the maximum X ordinate value
Definition: LineSegment.h:120
bool equalsTopo(const LineSegment &other) const
Returns true if other is topologically equal to this LineSegment (e.g. irrespective of orientation).
void reverse()
Reverses the direction of the line segment.
CoordinateXY midPoint() const
Computes the midpoint of the segment.
Definition: LineSegment.h:254
double distance(const LineSegment &ls) const
Computes the distance between this line segment and another one.
Definition: LineSegment.h:267
int orientationIndex(const LineSegment &seg) const
std::unique_ptr< LineString > toGeometry(const GeometryFactory &gf) const
void normalize()
Puts the line segment into a normalized form.
Definition: LineSegment.h:236
std::array< Coordinate, 2 > closestPoints(const LineSegment &line)
bool isHorizontal() const
Definition: LineSegment.h:147
double getLength() const
Computes the length of the line segment.
Definition: LineSegment.h:138
Coordinate p1
Segment start.
Definition: LineSegment.h:66
double distancePerpendicular(const CoordinateXY &p) const
Definition: LineSegment.h:287
Coordinate lineIntersection(const LineSegment &line) const
Computes the intersection point of the lines defined by two segments, if there is one.
LineSegment offset(double offsetDistance)
int orientationIndex(const Coordinate &p) const
Determines the orientation index of a Coordinate relative to this segment.
Definition: LineSegment.h:223
double angle() const
Definition: LineSegment.h:244
Coordinate intersection(const LineSegment &line) const
double maxY() const
gets the maximum Y ordinate value
Definition: LineSegment.h:132
double distance(const CoordinateXY &p) const
Computes the distance between this line segment and a point.
Definition: LineSegment.h:273
void closestPoint(const CoordinateXY &p, CoordinateXY &ret) const
Computes the closest point on this line segment to another point.
double distancePerpendicularOriented(const CoordinateXY &p) const
double segmentFraction(const CoordinateXY &inputPt) const
Computes the fraction of distance (in [0.0, 1.0]) that the projection of a point occurs along this li...
double projectionFactor(const CoordinateXY &p) const
Compute the projection factor for the projection of the point p onto this LineSegment.
bool operator<(const CoordinateXY &a, const CoordinateXY &b)
Strict weak ordering operator for Coordinate.
Definition: Coordinate.h:470
Basic namespace for all GEOS functionalities.
Definition: geos.h:38