QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgslinesegment.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslinesegment.h
3  -----------------
4  begin : April 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSLINESEGMENT_H
19 #define QGSLINESEGMENT_H
20 
21 #include "qgis_core.h"
22 #include "qgspointxy.h"
23 
24 class QgsLineString;
25 
31 class CORE_EXPORT QgsLineSegment2D
32 {
33 
34  public:
35 
40  QgsLineSegment2D( const QgsPointXY &start, const QgsPointXY &end )
41  : mStart( start )
42  , mEnd( end )
43  {}
44 
49  QgsLineSegment2D( double x1, double y1, double x2, double y2 )
50  : mStart( QgsPointXY( x1, y1 ) )
51  , mEnd( QgsPointXY( x2, y2 ) )
52  {}
53 
58  double length() const
59  {
60  return std::sqrt( ( mStart.x() - mEnd.x() ) * ( mStart.x() - mEnd.x() ) + ( mStart.y() - mEnd.y() ) * ( mStart.y() - mEnd.y() ) );
61  }
62 
67  double lengthSquared() const
68  {
69  return ( mStart.x() - mEnd.x() ) * ( mStart.x() - mEnd.x() ) + ( mStart.y() - mEnd.y() ) * ( mStart.y() - mEnd.y() );
70  }
71 
77  double startX() const
78  {
79  return mStart.x();
80  }
81 
87  double startY() const
88  {
89  return mStart.y();
90  }
91 
97  double endX() const
98  {
99  return mEnd.x();
100  }
101 
107  double endY() const
108  {
109  return mEnd.y();
110  }
111 
119  {
120  return mStart;
121  }
122 
129  QgsPointXY end() const
130  {
131  return mEnd;
132  }
133 
140  void setStartX( double x )
141  {
142  mStart.setX( x );
143  }
144 
151  void setStartY( double y )
152  {
153  mStart.setY( y );
154  }
155 
162  void setEndX( double x )
163  {
164  mEnd.setX( x );
165  }
166 
173  void setEndY( double y )
174  {
175  mEnd.setY( y );
176  }
177 
184  void setStart( const QgsPointXY &start )
185  {
186  mStart = start;
187  }
188 
195  void setEnd( const QgsPointXY &end )
196  {
197  mEnd = end;
198  }
199 
211  int pointLeftOfLine( const QgsPointXY &point ) const;
212 
216  void reverse()
217  {
218  std::swap( mStart, mEnd );
219  }
220 
222  bool operator==( const QgsLineSegment2D &other ) const
223  {
224  return mStart == other.mStart && mEnd == other.mEnd;
225  }
226 
228  bool operator!=( const QgsLineSegment2D &other ) const
229  {
230  return mStart != other.mStart || mEnd != other.mEnd;
231  }
232 
233  private:
234 
235  QgsPointXY mStart;
236  QgsPointXY mEnd;
237 
238 };
239 
240 #endif // QGSLINESEGMENT_H
bool operator==(const QgsLineSegment2D &other) const
Equality operator.
QgsLineSegment2D(const QgsPointXY &start, const QgsPointXY &end)
Constructor for a QgsLineSegment2D from the specified start point to the end point.
double startY() const
Returns the segment's start y-coordinate.
void setStartY(double y)
Sets the segment's start y coordinate.
QgsPointXY start() const
Returns the segment's start point.
A class to represent a 2D point.
Definition: qgspointxy.h:43
void setEndY(double y)
Sets the segment's end y coordinate.
void setEndX(double x)
Sets the segment's end x coordinate.
void reverse()
Reverses the line segment, so that the start and end points are flipped.
void setEnd(const QgsPointXY &end)
Sets the segment's end point.
QgsLineSegment2D(double x1, double y1, double x2, double y2)
Constructor for a QgsLineSegment2D from the point (x1, y2) to (x2, y2).
double startX() const
Returns the segment's start x-coordinate.
Represents a single 2D line segment, consisting of a 2D start and end vertex only.
void setStartX(double x)
Sets the segment's start x coordinate.
double endY() const
Returns the segment's end y-coordinate.
double endX() const
Returns the segment's end x-coordinate.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
QgsPointXY end() const
Returns the segment's end point.
void setStart(const QgsPointXY &start)
Sets the segment's start point.
double length() const
Returns the length of the segment.
bool operator!=(const QgsLineSegment2D &other) const
Inequality operator.
double lengthSquared() const
Returns the squared length of the segment.