QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 ) SIP_HOLDGIL
41  : mStart( start )
42  , mEnd( end )
43  {}
44 
49  QgsLineSegment2D( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL
50  : mStart( QgsPointXY( x1, y1 ) )
51  , mEnd( QgsPointXY( x2, y2 ) )
52  {}
53 
58  double length() const SIP_HOLDGIL
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 SIP_HOLDGIL
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 SIP_HOLDGIL
78  {
79  return mStart.x();
80  }
81 
87  double startY() const SIP_HOLDGIL
88  {
89  return mStart.y();
90  }
91 
97  double endX() const SIP_HOLDGIL
98  {
99  return mEnd.x();
100  }
101 
107  double endY() const SIP_HOLDGIL
108  {
109  return mEnd.y();
110  }
111 
119  {
120  return mStart;
121  }
122 
130  {
131  return mEnd;
132  }
133 
140  void setStartX( double x ) SIP_HOLDGIL
141  {
142  mStart.setX( x );
143  }
144 
151  void setStartY( double y ) SIP_HOLDGIL
152  {
153  mStart.setY( y );
154  }
155 
162  void setEndX( double x ) SIP_HOLDGIL
163  {
164  mEnd.setX( x );
165  }
166 
173  void setEndY( double y ) SIP_HOLDGIL
174  {
175  mEnd.setY( y );
176  }
177 
184  void setStart( const QgsPointXY &start ) SIP_HOLDGIL
185  {
186  mStart = start;
187  }
188 
195  void setEnd( const QgsPointXY &end ) SIP_HOLDGIL
196  {
197  mEnd = end;
198  }
199 
211  int pointLeftOfLine( const QgsPointXY &point ) const SIP_HOLDGIL;
212 
217  {
218  std::swap( mStart, mEnd );
219  }
220 
222  bool operator==( const QgsLineSegment2D &other ) const SIP_HOLDGIL
223  {
224  return mStart == other.mStart && mEnd == other.mEnd;
225  }
226 
228  bool operator!=( const QgsLineSegment2D &other ) const SIP_HOLDGIL
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
QgsLineSegment2D::end
QgsPointXY end() const SIP_HOLDGIL
Returns the segment's end point.
Definition: qgslinesegment.h:129
QgsLineSegment2D
Represents a single 2D line segment, consisting of a 2D start and end vertex only.
Definition: qgslinesegment.h:32
QgsLineSegment2D::setStartX
void setStartX(double x) SIP_HOLDGIL
Sets the segment's start x coordinate.
Definition: qgslinesegment.h:140
QgsLineSegment2D::length
double length() const SIP_HOLDGIL
Returns the length of the segment.
Definition: qgslinesegment.h:58
QgsLineSegment2D::reverse
void reverse() SIP_HOLDGIL
Reverses the line segment, so that the start and end points are flipped.
Definition: qgslinesegment.h:216
QgsLineSegment2D::setStartY
void setStartY(double y) SIP_HOLDGIL
Sets the segment's start y coordinate.
Definition: qgslinesegment.h:151
QgsLineSegment2D::endX
double endX() const SIP_HOLDGIL
Returns the segment's end x-coordinate.
Definition: qgslinesegment.h:97
QgsLineSegment2D::startX
double startX() const SIP_HOLDGIL
Returns the segment's start x-coordinate.
Definition: qgslinesegment.h:77
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
QgsLineSegment2D::endY
double endY() const SIP_HOLDGIL
Returns the segment's end y-coordinate.
Definition: qgslinesegment.h:107
QgsLineSegment2D::QgsLineSegment2D
QgsLineSegment2D(double x1, double y1, double x2, double y2) SIP_HOLDGIL
Constructor for a QgsLineSegment2D from the point (x1, y2) to (x2, y2).
Definition: qgslinesegment.h:49
SIP_HOLDGIL
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
QgsLineSegment2D::setEnd
void setEnd(const QgsPointXY &end) SIP_HOLDGIL
Sets the segment's end point.
Definition: qgslinesegment.h:195
QgsLineSegment2D::QgsLineSegment2D
QgsLineSegment2D(const QgsPointXY &start, const QgsPointXY &end) SIP_HOLDGIL
Constructor for a QgsLineSegment2D from the specified start point to the end point.
Definition: qgslinesegment.h:40
QgsLineSegment2D::start
QgsPointXY start() const SIP_HOLDGIL
Returns the segment's start point.
Definition: qgslinesegment.h:118
QgsLineSegment2D::operator!=
bool operator!=(const QgsLineSegment2D &other) const SIP_HOLDGIL
Inequality operator.
Definition: qgslinesegment.h:228
QgsLineSegment2D::operator==
bool operator==(const QgsLineSegment2D &other) const SIP_HOLDGIL
Equality operator.
Definition: qgslinesegment.h:222
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsLineSegment2D::lengthSquared
double lengthSquared() const SIP_HOLDGIL
Returns the squared length of the segment.
Definition: qgslinesegment.h:67
QgsLineSegment2D::setEndY
void setEndY(double y) SIP_HOLDGIL
Sets the segment's end y coordinate.
Definition: qgslinesegment.h:173
QgsLineSegment2D::startY
double startY() const SIP_HOLDGIL
Returns the segment's start y-coordinate.
Definition: qgslinesegment.h:87
QgsLineSegment2D::setEndX
void setEndX(double x) SIP_HOLDGIL
Sets the segment's end x coordinate.
Definition: qgslinesegment.h:162
qgspointxy.h
QgsLineSegment2D::setStart
void setStart(const QgsPointXY &start) SIP_HOLDGIL
Sets the segment's start point.
Definition: qgslinesegment.h:184