QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
line_p.h
Go to the documentation of this file.
1/***************************************************************************
2 line_p.h
3 ---------------------
4 begin : 2025/09/25
5 copyright : (C) 2025 by Julien Cabieces
6 email : julien dot cabieces at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef LINE_P_H
17#define LINE_P_H
18
19#define SIP_NO_FILE
20
22
24
26
27
32class Line
33{
34 public:
35 Line( QPointF p1, QPointF p2 )
36 : mVertical( false )
37 , mIncreasing( false )
38 , mTangent( 0.0 )
39 , mLength( 0.0 )
40 {
41 if ( p1 == p2 )
42 return; // invalid
43
44 // tangent and direction
45 if ( qgsDoubleNear( p1.x(), p2.x() ) )
46 {
47 // vertical line - tangent undefined
48 mVertical = true;
49 mIncreasing = ( p2.y() > p1.y() );
50 }
51 else
52 {
53 mVertical = false;
54 mTangent = ( p2.y() - p1.y() ) / ( p2.x() - p1.x() );
55 mIncreasing = ( p2.x() > p1.x() );
56 }
57
58 mLength = QgsGeometryUtilsBase::distance2D( p1, p2 );
59 }
60
61 // return angle in radians
62 double angle()
63 {
64 double a = ( mVertical ? M_PI_2 : std::atan( mTangent ) );
65
66 if ( !mIncreasing )
67 a += M_PI;
68 return a;
69 }
70
71 // return difference for x,y when going along the line with specified interval
72 QPointF diffForInterval( double interval ) const
73 {
74 if ( mVertical )
75 return ( mIncreasing ? QPointF( 0, interval ) : QPointF( 0, -interval ) );
76
77 double alpha = std::atan( mTangent );
78 double dx = std::cos( alpha ) * interval;
79 double dy = std::sin( alpha ) * interval;
80 return ( mIncreasing ? QPointF( dx, dy ) : QPointF( -dx, -dy ) );
81 }
82
83 double length() const { return mLength; }
84
85 protected:
86 bool mVertical;
87 bool mIncreasing;
88 double mTangent;
89 double mLength;
90};
91
93
94#endif
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900