Skip to content
Permalink
Browse files

Make more use of math constants

  • Loading branch information
nyalldawson committed Aug 25, 2017
1 parent 7f44737 commit 725301ad584967b1c8705f7bc3cf8fc6d358d09a
@@ -179,7 +179,7 @@ Constructor
:rtype: float
%End

double measureLineProjected( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI / 2, QgsPointXY *projectedPoint /Out/ = 0 ) const;
double measureLineProjected( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2, QgsPointXY *projectedPoint /Out/ = 0 ) const;
%Docstring
Calculates the distance from one point with distance in meters and azimuth (direction)
When the sourceCrs() is geographic, computeSpheroidProject() will be called
@@ -279,7 +279,7 @@ Constructor
:rtype: float
%End

QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI / 2 ) const;
QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2 ) const;
%Docstring
Given a location, an azimuth and a distance, computes the
location of the projected point. Based on Vincenty's formula
@@ -388,18 +388,18 @@ double QgsComposerArrow::computeMarkerMargin() const
{
if ( mMarkerMode == DefaultMarker )
{
margin = mPen.widthF() / std::sqrt( 2.0 ) + mArrowHeadWidth / 2.0;
margin = mPen.widthF() * M_SQRT1_2 + mArrowHeadWidth / 2.0;
}
else if ( mMarkerMode == NoMarker )
{
margin = mPen.widthF() / std::sqrt( 2.0 );
margin = mPen.widthF() * M_SQRT1_2;
}
else if ( mMarkerMode == SVGMarker )
{
double startMarkerMargin = std::sqrt( 0.25 * ( mStartArrowHeadHeight * mStartArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
double stopMarkerMargin = std::sqrt( 0.25 * ( mStopArrowHeadHeight * mStopArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
double markerMargin = std::max( startMarkerMargin, stopMarkerMargin );
margin = std::max( mPen.widthF() / std::sqrt( 2.0 ), markerMargin );
margin = std::max( mPen.widthF() * M_SQRT1_2, markerMargin );
}
}
return margin;
@@ -56,8 +56,8 @@ void QgsShadowEffect::draw( QgsRenderContext &context )
double offsetDist = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );

double angleRad = mOffsetAngle * M_PI / 180; // to radians
QPointF transPt( -offsetDist * std::cos( angleRad + M_PI / 2 ),
-offsetDist * std::sin( angleRad + M_PI / 2 ) );
QPointF transPt( -offsetDist * std::cos( angleRad + M_PI_2 ),
-offsetDist * std::sin( angleRad + M_PI_2 ) );

//transparency, scale
QgsImageOperation::multiplyOpacity( colorisedIm, mOpacity );
@@ -2645,9 +2645,9 @@ static QVariant fcnAzimuth( const QVariantList &values, const QgsExpressionConte
if ( pt1->y() == pt2->y() )
{
if ( pt1->x() < pt2->x() )
return M_PI / 2;
return M_PI_2;
else if ( pt1->x() > pt2->x() )
return M_PI + ( M_PI / 2 );
return M_PI + ( M_PI_2 );
else
return 0;
}
@@ -2661,7 +2661,7 @@ static QVariant fcnAzimuth( const QVariantList &values, const QgsExpressionConte
else /* ( pt1->y() > pt2->y() ) - equality case handled above */
{
return std::atan( std::fabs( pt1->y() - pt2->y() ) / std::fabs( pt1->x() - pt2->x() ) )
+ ( M_PI / 2 );
+ ( M_PI_2 );
}
}

@@ -2675,7 +2675,7 @@ static QVariant fcnAzimuth( const QVariantList &values, const QgsExpressionConte
else /* ( pt1->y() < pt2->y() ) - equality case handled above */
{
return std::atan( std::fabs( pt1->y() - pt2->y() ) / std::fabs( pt1->x() - pt2->x() ) )
+ ( M_PI + ( M_PI / 2 ) );
+ ( M_PI + ( M_PI_2 ) );
}
}
}
@@ -3791,7 +3791,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "degrees" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "radians" ) ), fcnDegrees, QStringLiteral( "Math" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "azimuth" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "point_a" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "point_b" ) ), fcnAzimuth, QStringList() << QStringLiteral( "Math" ) << QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "inclination" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "point_a" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "point_b" ) ), fcnInclination, QStringList() << QStringLiteral( "Math" ) << QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "project" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "point" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "distance" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "azimuth" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "elevation" ), true, M_PI / 2 ), fcnProject, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "project" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "point" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "distance" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "azimuth" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "elevation" ), true, M_PI_2 ), fcnProject, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "abs" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnAbs, QStringLiteral( "Math" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "cos" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "angle" ) ), fcnCos, QStringLiteral( "Math" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "sin" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "angle" ) ), fcnSin, QStringLiteral( "Math" ) )
@@ -772,7 +772,7 @@ void QgsCircularString::sumUpArea( double &sum ) const
bool circlePointLeftOfLine = QgsGeometryUtils::leftOfLine( p2.x(), p2.y(), p1.x(), p1.y(), p3.x(), p3.y() ) < 0;
bool centerPointLeftOfLine = QgsGeometryUtils::leftOfLine( centerX, centerY, p1.x(), p1.y(), p3.x(), p3.y() ) < 0;

double cov = 0.5 - d * std::sqrt( r2 - d * d ) / ( M_PI * r2 ) - 1 / M_PI * std::asin( d / radius );
double cov = 0.5 - d * std::sqrt( r2 - d * d ) / ( M_PI * r2 ) - M_1_PI * std::asin( d / radius );
double circleChordArea = 0;
if ( circlePointLeftOfLine == centerPointLeftOfLine )
{
@@ -1093,7 +1093,7 @@ QgsLineString QgsGeometryUtils::perpendicularSegment( const QgsPoint &p, const Q
double QgsGeometryUtils::lineAngle( double x1, double y1, double x2, double y2 )
{
double at = std::atan2( y2 - y1, x2 - x1 );
double a = -at + M_PI / 2.0;
double a = -at + M_PI_2;
return normalizedAngle( a );
}

@@ -1107,7 +1107,7 @@ double QgsGeometryUtils::angleBetweenThreePoints( double x1, double y1, double x
double QgsGeometryUtils::linePerpendicularAngle( double x1, double y1, double x2, double y2 )
{
double a = lineAngle( x1, y1, x2, y2 );
a += ( M_PI / 2.0 );
a += M_PI_2;
return normalizedAngle( a );
}

@@ -386,9 +386,9 @@ QVector<double> QgsTriangle::angles() const
double a2 = std::fmod( QgsGeometryUtils::angleBetweenThreePoints( ax, ay, bx, by, cx, cy ), M_PI );
double a3 = std::fmod( QgsGeometryUtils::angleBetweenThreePoints( bx, by, cx, cy, ax, ay ), M_PI );

angles.append( ( a1 > M_PI / 2 ? a1 - M_PI / 2 : a1 ) );
angles.append( ( a2 > M_PI / 2 ? a2 - M_PI / 2 : a2 ) );
angles.append( ( a3 > M_PI / 2 ? a3 - M_PI / 2 : a3 ) );
angles.append( ( a1 > M_PI_2 ? a1 - M_PI_2 : a1 ) );
angles.append( ( a2 > M_PI_2 ? a2 - M_PI_2 : a2 ) );
angles.append( ( a3 > M_PI_2 ? a3 - M_PI_2 : a3 ) );

return angles;
}
@@ -419,7 +419,7 @@ bool QgsTriangle::isRight( double angleTolerance ) const
QVector<double>::iterator ita = a.begin();
while ( ita != a.end() )
{
if ( qgsDoubleNear( *ita, M_PI / 2.0, angleTolerance ) )
if ( qgsDoubleNear( *ita, M_PI_2, angleTolerance ) )
return true;
ita++;
}
@@ -443,7 +443,7 @@ int FeaturePart::createCandidatesAroundPoint( double x, double y, QList< LabelPo
double candidateAngleIncrement = 2 * M_PI / numberCandidates; /* angle bw 2 pos */

/* various angles */
double a90 = M_PI / 2;
double a90 = M_PI_2;
double a180 = M_PI;
double a270 = a180 + a90;
double a360 = 2 * M_PI;
@@ -470,7 +470,7 @@ int FeaturePart::createCandidatesAroundPoint( double x, double y, QList< LabelPo

int i;
double angleToCandidate;
for ( i = 0, angleToCandidate = M_PI / 4; i < numberCandidates; i++, angleToCandidate += candidateAngleIncrement )
for ( i = 0, angleToCandidate = M_PI_4; i < numberCandidates; i++, angleToCandidate += candidateAngleIncrement )
{
double labelX = x;
double labelY = y;
@@ -763,12 +763,12 @@ int FeaturePart::createCandidatesAlongLineNearStraightSegments( QList<LabelPosit
else
angle = std::atan2( candidateEndY - candidateStartY, candidateEndX - candidateStartX );

beta = angle + M_PI / 2;
beta = angle + M_PI_2;

if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Line )
{
// find out whether the line direction for this candidate is from right to left
bool isRightToLeft = ( angle > M_PI / 2 || angle <= -M_PI / 2 );
bool isRightToLeft = ( angle > M_PI_2 || angle <= -M_PI_2 );
// meaning of above/below may be reversed if using map orientation and the line has right-to-left direction
bool reversed = ( ( flags & FLAG_MAP_ORIENTATION ) ? isRightToLeft : false );
bool aboveLine = ( !reversed && ( flags & FLAG_ABOVE_LINE ) ) || ( reversed && ( flags & FLAG_BELOW_LINE ) );
@@ -908,12 +908,12 @@ int FeaturePart::createCandidatesAlongLineNearMidpoint( QList<LabelPosition *> &
else
angle = std::atan2( candidateEndY - candidateStartY, candidateEndX - candidateStartX );

beta = angle + M_PI / 2;
beta = angle + M_PI_2;

if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Line )
{
// find out whether the line direction for this candidate is from right to left
bool isRightToLeft = ( angle > M_PI / 2 || angle <= -M_PI / 2 );
bool isRightToLeft = ( angle > M_PI_2 || angle <= -M_PI_2 );
// meaning of above/below may be reversed if using map orientation and the line has right-to-left direction
bool reversed = ( ( flags & FLAG_MAP_ORIENTATION ) ? isRightToLeft : false );
bool aboveLine = ( !reversed && ( flags & FLAG_ABOVE_LINE ) ) || ( reversed && ( flags & FLAG_BELOW_LINE ) );
@@ -1125,7 +1125,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
while ( render_angle >= 2 * M_PI ) render_angle -= 2 * M_PI;
while ( render_angle < 0 ) render_angle += 2 * M_PI;

if ( render_angle > M_PI / 2 && render_angle < 1.5 * M_PI )
if ( render_angle > M_PI_2 && render_angle < 1.5 * M_PI )
slp->incrementUpsideDownCharCount();
}
// END FOR
@@ -1136,7 +1136,7 @@ LabelPosition *FeaturePart::curvedPlacementAtOffset( PointSet *path_positions, d
static LabelPosition *_createCurvedCandidate( LabelPosition *lp, double angle, double dist )
{
LabelPosition *newLp = new LabelPosition( *lp );
newLp->offsetPosition( dist * std::cos( angle + M_PI / 2 ), dist * std::sin( angle + M_PI / 2 ) );
newLp->offsetPosition( dist * std::cos( angle + M_PI_2 ), dist * std::sin( angle + M_PI_2 ) );
return newLp;
}

@@ -1424,18 +1424,18 @@ int FeaturePart::createCandidatesForPolygon( QList< LabelPosition *> &lPos, Poin
}
else if ( box->length > 1.5 * labelWidth && box->width > 1.5 * labelWidth )
{
if ( box->alpha <= M_PI / 4 )
if ( box->alpha <= M_PI_4 )
{
alpha = box->alpha;
}
else
{
alpha = box->alpha - M_PI / 2;
alpha = box->alpha - M_PI_2;
}
}
else if ( box->length > box->width )
{
alpha = box->alpha - M_PI / 2;
alpha = box->alpha - M_PI_2;
}
else
{
@@ -1464,8 +1464,8 @@ int FeaturePart::createCandidatesForPolygon( QList< LabelPosition *> &lPos, Poin
for ( py = py0; py <= box->length; py += dy )
{

rx = std::cos( box->alpha ) * px + std::cos( box->alpha - M_PI / 2 ) * py;
ry = std::sin( box->alpha ) * px + std::sin( box->alpha - M_PI / 2 ) * py;
rx = std::cos( box->alpha ) * px + std::cos( box->alpha - M_PI_2 ) * py;
ry = std::sin( box->alpha ) * px + std::sin( box->alpha - M_PI_2 ) * py;

rx += box->x[0];
ry += box->y[0];
@@ -329,7 +329,7 @@ bool GeomFunction::containsCandidate( const GEOSPreparedGeometry *geom, double x
GEOSCoordSeq_setY_r( geosctxt, coord, 0, y );
if ( !qgsDoubleNear( alpha, 0.0 ) )
{
double beta = alpha + ( M_PI / 2 );
double beta = alpha + M_PI_2;
double dx1 = std::cos( alpha ) * width;
double dy1 = std::sin( alpha ) * width;
double dx2 = std::cos( beta ) * height;
@@ -70,7 +70,7 @@ LabelPosition::LabelPosition( int id, double x1, double y1, double w, double h,
while ( this->alpha < 0 )
this->alpha += 2 * M_PI;

double beta = this->alpha + ( M_PI / 2 );
double beta = this->alpha + M_PI_2;

double dx1, dx2, dy1, dy2;

@@ -94,7 +94,7 @@ LabelPosition::LabelPosition( int id, double x1, double y1, double w, double h,

// upside down ? (curved labels are always correct)
if ( !feature->layer()->isCurved() &&
this->alpha > M_PI / 2 && this->alpha <= 3 * M_PI / 2 )
this->alpha > M_PI_2 && this->alpha <= 3 * M_PI_2 )
{
if ( feature->showUprightLabels() )
{
@@ -656,7 +656,7 @@ CHullBox *PointSet::compute_chull_bbox()
for ( i = 0; i < 16; i += 4 )
{

alpha_seg = ( ( i / 4 > 0 ? ( i / 4 ) - 1 : 3 ) ) * M_PI / 2 + alpha;
alpha_seg = ( ( i / 4 > 0 ? ( i / 4 ) - 1 : 3 ) ) * M_PI_2 + alpha;

best_cp = DBL_MAX;
for ( j = 0; j < nbPoints; j++ )
@@ -662,7 +662,7 @@ void QgsDistanceArea::computeAreaInit()
m_QbarC = - ( 3.0 / 25.0 ) * e4 - ( 12.0 / 35.0 ) * e6;
m_QbarD = ( 4.0 / 49.0 ) * e6;

m_Qp = getQ( M_PI / 2 );
m_Qp = getQ( M_PI_2 );
m_E = 4 * M_PI * m_Qp * m_AE;
if ( m_E < 0.0 )
m_E = -m_E;
@@ -193,7 +193,7 @@ class CORE_EXPORT QgsDistanceArea
* \see sourceCrs()
* \see computeSpheroidProject()
*/
double measureLineProjected( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI / 2, QgsPointXY *projectedPoint SIP_OUT = nullptr ) const;
double measureLineProjected( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2, QgsPointXY *projectedPoint SIP_OUT = nullptr ) const;

/**
* Returns the units of distance for length calculations made by this object.
@@ -286,7 +286,7 @@ class CORE_EXPORT QgsDistanceArea
* \param azimuth - azimuth in radians, clockwise from North
* \return p2 - location of projected point as longitude/latitude.
*/
QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI / 2 ) const;
QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2 ) const;

private:

@@ -2198,8 +2198,8 @@ void QgsTextRenderer::drawBackground( QgsRenderContext &context, QgsTextRenderer
else if ( background.type() == QgsTextBackgroundSettings::ShapeEllipse )
{
// start with label bound by ellipse
h = h / std::sqrt( 2.0 ) * 2;
w = w / std::sqrt( 2.0 ) * 2;
h = h * M_SQRT1_2 * 2;
w = w * M_SQRT1_2 * 2;
}

double bufferWidth = context.convertToPainterUnits( background.size().width(), background.sizeUnit(),
@@ -2388,8 +2388,8 @@ void QgsTextRenderer::drawShadow( QgsRenderContext &context, const QgsTextRender
angleRad -= ( component.rotation * M_PI / 180 + component.rotationOffset * M_PI / 180 );
}

QPointF transPt( -offsetDist * std::cos( angleRad + M_PI / 2 ),
-offsetDist * std::sin( angleRad + M_PI / 2 ) );
QPointF transPt( -offsetDist * std::cos( angleRad + M_PI_2 ),
-offsetDist * std::sin( angleRad + M_PI_2 ) );

p->save();
p->setRenderHint( QPainter::SmoothPixmapTransform );
@@ -672,7 +672,7 @@ class MyLine
// return angle in radians
double angle()
{
double a = ( mVertical ? M_PI / 2 : std::atan( mT ) );
double a = ( mVertical ? M_PI_2 : std::atan( mT ) );

if ( !mIncreasing )
a += M_PI;
@@ -201,7 +201,7 @@ void normalizeCoordinates( const QVector<QgsPointXY> &coords, QVector<QgsPointXY
}
meanDist *= 1.0 / coords.size();

double OOD = meanDist / std::sqrt( 2.0 );
double OOD = meanDist * M_SQRT1_2;
double D = 1.0 / OOD;
normalizedCoords.resize( coords.size() );
for ( int i = 0; i < coords.size(); i++ )
@@ -1321,7 +1321,7 @@ void QgsWmsProjectParser::addLayers( QDomDocument &doc,
if ( version == QLatin1String( "1.1.1" ) )
{
double OGC_PX_M = 0.00028; // OGC reference pixel size in meter, also used by qgis
double SCALE_TO_SCALEHINT = OGC_PX_M * std::sqrt( 2.0 );
double SCALE_TO_SCALEHINT = OGC_PX_M * M_SQRT2;

QDomElement scaleHintElem = doc.createElement( QStringLiteral( "ScaleHint" ) );
scaleHintElem.setAttribute( QStringLiteral( "min" ), QString::number( currentLayer->maximumScale() * SCALE_TO_SCALEHINT ) );
@@ -1001,7 +1001,7 @@ namespace QgsWms
if ( version == QLatin1String( "1.1.1" ) )
{
double OGC_PX_M = 0.00028; // OGC reference pixel size in meter, also used by qgis
double SCALE_TO_SCALEHINT = OGC_PX_M * std::sqrt( 2.0 );
double SCALE_TO_SCALEHINT = OGC_PX_M * M_SQRT2;

QDomElement scaleHintElem = doc.createElement( QStringLiteral( "ScaleHint" ) );
scaleHintElem.setAttribute( QStringLiteral( "min" ), QString::number( l->maximumScale() * SCALE_TO_SCALEHINT ) );

0 comments on commit 725301a

Please sign in to comment.
You can’t perform that action at this time.