2013-01-26 17:49:48 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-01-06 01:40:23 +00:00
|
|
|
* Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-01-26 17:49:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#ifndef TRIGO_H
|
|
|
|
#define TRIGO_H
|
2016-01-12 16:33:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file trigo.h
|
|
|
|
*/
|
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cmath>
|
2017-10-19 21:15:13 +00:00
|
|
|
#include <math/vector2d.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <wx/gdicmn.h> // For wxPoint
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2013-09-27 12:30:35 +00:00
|
|
|
/**
|
2020-03-04 13:35:33 +00:00
|
|
|
* Test if \a aTestPoint is on line defined by \a aSegStart and \a aSegEnd.
|
|
|
|
*
|
|
|
|
* This function is faster than #TestSegmentHit() because \a aTestPoint should be exactly on
|
|
|
|
* the line. This works fine only for H, V and 45 degree line segments.
|
|
|
|
*
|
|
|
|
* @param aSegStart The first point of the line segment.
|
|
|
|
* @param aSegEnd The second point of the line segment.
|
|
|
|
* @param aTestPoint The point to test.
|
|
|
|
*
|
|
|
|
* @return true if the point is on the line segment.
|
2013-09-27 12:30:35 +00:00
|
|
|
*/
|
|
|
|
bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd,
|
|
|
|
const wxPoint& aTestPoint );
|
|
|
|
|
2013-09-21 18:09:41 +00:00
|
|
|
/**
|
2020-03-04 13:35:33 +00:00
|
|
|
* Test if two lines intersect.
|
2013-09-21 18:09:41 +00:00
|
|
|
*
|
|
|
|
* @param a_p1_l1 The first point of the first line.
|
|
|
|
* @param a_p2_l1 The second point of the first line.
|
|
|
|
* @param a_p1_l2 The first point of the second line.
|
|
|
|
* @param a_p2_l2 The second point of the second line.
|
2019-05-25 19:01:54 +00:00
|
|
|
* @param aIntersectionPoint is filled with the intersection point if it exists
|
2013-09-21 18:09:41 +00:00
|
|
|
* @return bool - true if the two segments defined by four points intersect.
|
|
|
|
* (i.e. if the 2 segments have at least a common point)
|
|
|
|
*/
|
2021-07-26 19:35:12 +00:00
|
|
|
bool SegmentIntersectsSegment( const wxPoint& a_p1_l1, const wxPoint& a_p2_l1,
|
|
|
|
const wxPoint& a_p1_l2, const wxPoint& a_p2_l2,
|
2019-05-25 19:01:54 +00:00
|
|
|
wxPoint* aIntersectionPoint = nullptr );
|
2013-09-21 18:09:41 +00:00
|
|
|
|
2011-09-20 13:57:40 +00:00
|
|
|
/*
|
|
|
|
* Calculate the new point of coord coord pX, pY,
|
|
|
|
* for a rotation center 0, 0, and angle in (1 / 10 degree)
|
|
|
|
*/
|
2011-12-14 04:29:25 +00:00
|
|
|
void RotatePoint( int *pX, int *pY, double angle );
|
2011-09-20 13:57:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the new point of coord coord pX, pY,
|
|
|
|
* for a rotation center cx, cy, and angle in (1 / 10 degree)
|
|
|
|
*/
|
2011-12-14 04:29:25 +00:00
|
|
|
void RotatePoint( int *pX, int *pY, int cx, int cy, double angle );
|
2011-09-20 13:57:40 +00:00
|
|
|
|
|
|
|
/*
|
2021-07-26 19:35:12 +00:00
|
|
|
* Calculate the new coord point point for a rotation angle in (1 / 10 degree).
|
2011-09-20 13:57:40 +00:00
|
|
|
*/
|
2013-05-01 17:32:36 +00:00
|
|
|
inline void RotatePoint( wxPoint* point, double angle )
|
2012-02-19 04:02:19 +00:00
|
|
|
{
|
|
|
|
RotatePoint( &point->x, &point->y, angle );
|
|
|
|
}
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2017-10-19 21:15:13 +00:00
|
|
|
inline void RotatePoint( VECTOR2I& point, double angle )
|
|
|
|
{
|
|
|
|
RotatePoint( &point.x, &point.y, angle );
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotatePoint( VECTOR2I& point, const VECTOR2I& centre, double angle );
|
|
|
|
|
2011-09-20 13:57:40 +00:00
|
|
|
/*
|
2021-07-26 19:35:12 +00:00
|
|
|
* Calculate the new coord point point for a center rotation center and angle in (1 / 10 degree).
|
2011-09-20 13:57:40 +00:00
|
|
|
*/
|
2011-12-14 04:29:25 +00:00
|
|
|
void RotatePoint( wxPoint *point, const wxPoint & centre, double angle );
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2011-12-14 04:29:25 +00:00
|
|
|
void RotatePoint( double *pX, double *pY, double angle );
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2011-12-14 04:29:25 +00:00
|
|
|
void RotatePoint( double *pX, double *pY, double cx, double cy, double angle );
|
2009-06-13 17:06:07 +00:00
|
|
|
|
2018-12-08 15:26:47 +00:00
|
|
|
/**
|
2020-03-04 13:35:33 +00:00
|
|
|
* Determine the center of an arc or circle given three points on its circumference.
|
|
|
|
*
|
2018-12-08 15:26:47 +00:00
|
|
|
* @param aStart The starting point of the circle (equivalent to aEnd)
|
|
|
|
* @param aMid The point on the arc, half-way between aStart and aEnd
|
|
|
|
* @param aEnd The ending point of the circle (equivalent to aStart)
|
|
|
|
* @return The center of the circle
|
|
|
|
*/
|
|
|
|
const VECTOR2I GetArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
|
2020-02-28 14:03:09 +00:00
|
|
|
const VECTOR2D GetArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd );
|
|
|
|
const wxPoint GetArcCenter( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
|
2021-07-26 23:47:26 +00:00
|
|
|
const wxPoint GetArcCenter( const VECTOR2I& aStart, const VECTOR2I& aEnd, double aAngle );
|
2018-12-08 15:26:47 +00:00
|
|
|
|
2020-08-29 16:12:40 +00:00
|
|
|
/**
|
2021-07-26 19:35:12 +00:00
|
|
|
* Return the subtended angle for a given arc.
|
2020-08-29 16:12:40 +00:00
|
|
|
*/
|
|
|
|
double GetArcAngle( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
|
|
|
|
|
2021-01-03 01:21:20 +00:00
|
|
|
/**
|
2021-07-26 19:35:12 +00:00
|
|
|
* Return the middle point of an arc, half-way between aStart and aEnd. There are two possible
|
2021-01-10 13:04:21 +00:00
|
|
|
* solutions which can be found by toggling aMinArcAngle. The behaviour is undefined for
|
2021-01-03 01:21:20 +00:00
|
|
|
* semicircles (i.e. 180 degree arcs).
|
2021-01-10 13:04:21 +00:00
|
|
|
*
|
2021-01-03 01:21:20 +00:00
|
|
|
* @param aStart The starting point of the arc (for calculating the radius)
|
|
|
|
* @param aEnd The end point of the arc (for determining the arc angle)
|
|
|
|
* @param aCenter The center point of the arc
|
|
|
|
* @param aMinArcAngle If true, returns the point that results in the smallest arc angle.
|
|
|
|
* @return The middle point of the arc
|
|
|
|
*/
|
|
|
|
const VECTOR2I GetArcMid( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter,
|
2021-01-10 13:04:21 +00:00
|
|
|
bool aMinArcAngle = true );
|
2021-01-03 01:21:20 +00:00
|
|
|
|
2009-11-23 20:18:47 +00:00
|
|
|
/* Return the arc tangent of 0.1 degrees coord vector dx, dy
|
|
|
|
* between -1800 and 1800
|
|
|
|
* Equivalent to atan2 (but faster for calculations if
|
2013-05-01 17:32:36 +00:00
|
|
|
* the angle is 0 to -1800, or + - 900)
|
|
|
|
* Lorenzo: In fact usually atan2 already has to do these optimizations
|
|
|
|
* (due to the discontinuity in tan) but this function also returns
|
|
|
|
* in decidegrees instead of radians, so it's handier
|
2009-11-23 20:18:47 +00:00
|
|
|
*/
|
2013-05-01 17:32:36 +00:00
|
|
|
double ArcTangente( int dy, int dx );
|
|
|
|
|
|
|
|
//! @brief Euclidean norm of a 2D vector
|
|
|
|
//! @param vector Two-dimensional vector
|
|
|
|
//! @return Euclidean norm of the vector
|
|
|
|
inline double EuclideanNorm( const wxPoint &vector )
|
|
|
|
{
|
|
|
|
// this is working with doubles
|
|
|
|
return hypot( vector.x, vector.y );
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
inline double EuclideanNorm( const wxSize &vector )
|
|
|
|
{
|
|
|
|
// this is working with doubles, too
|
|
|
|
return hypot( vector.x, vector.y );
|
|
|
|
}
|
|
|
|
|
2010-02-04 17:46:12 +00:00
|
|
|
//! @brief Compute the distance between a line and a reference point
|
|
|
|
//! Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
|
|
|
|
//! @param linePointA Point on line
|
|
|
|
//! @param linePointB Point on line
|
|
|
|
//! @param referencePoint Reference point
|
2021-07-26 19:35:12 +00:00
|
|
|
inline double DistanceLinePoint( const wxPoint& linePointA,
|
|
|
|
const wxPoint& linePointB,
|
|
|
|
const wxPoint& referencePoint )
|
2013-05-01 17:32:36 +00:00
|
|
|
{
|
|
|
|
// Some of the multiple double casts are redundant. However in the previous
|
|
|
|
// definition the cast was (implicitly) done too late, just before
|
|
|
|
// the division (EuclideanNorm gives a double so from int it would
|
|
|
|
// be promoted); that means that the whole expression were
|
|
|
|
// vulnerable to overflow during int multiplications
|
2020-10-05 10:41:14 +00:00
|
|
|
return fabs( ( static_cast<double>( linePointB.x - linePointA.x ) *
|
|
|
|
static_cast<double>( linePointA.y - referencePoint.y ) -
|
|
|
|
static_cast<double>( linePointA.x - referencePoint.x ) *
|
|
|
|
static_cast<double>( linePointB.y - linePointA.y) )
|
2013-05-01 17:32:36 +00:00
|
|
|
/ EuclideanNorm( linePointB - linePointA ) );
|
|
|
|
}
|
2010-02-04 17:46:12 +00:00
|
|
|
|
|
|
|
//! @brief Test, if two points are near each other
|
|
|
|
//! @param pointA First point
|
|
|
|
//! @param pointB Second point
|
|
|
|
//! @param threshold The maximum distance
|
|
|
|
//! @return True or false
|
2021-07-26 19:35:12 +00:00
|
|
|
inline bool HitTestPoints( const wxPoint& pointA, const wxPoint& pointB, double threshold )
|
2013-05-01 17:32:36 +00:00
|
|
|
{
|
|
|
|
wxPoint vectorAB = pointB - pointA;
|
|
|
|
|
|
|
|
// Compare the distances squared. The double is needed to avoid
|
|
|
|
// overflow during int multiplication
|
2020-11-10 18:07:27 +00:00
|
|
|
double sqdistance = (double)vectorAB.x * vectorAB.x + (double)vectorAB.y * vectorAB.y;
|
2013-05-01 17:32:36 +00:00
|
|
|
|
|
|
|
return sqdistance < threshold * threshold;
|
|
|
|
}
|
2010-02-04 17:46:12 +00:00
|
|
|
|
|
|
|
//! @brief Determine the cross product
|
|
|
|
//! @param vectorA Two-dimensional vector
|
|
|
|
//! @param vectorB Two-dimensional vector
|
2021-07-26 19:35:12 +00:00
|
|
|
inline double CrossProduct( const wxPoint& vectorA, const wxPoint& vectorB )
|
2013-05-01 17:32:36 +00:00
|
|
|
{
|
|
|
|
// As before the cast is to avoid int overflow
|
|
|
|
return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2010-11-12 16:59:16 +00:00
|
|
|
/**
|
2020-03-04 13:35:33 +00:00
|
|
|
* Test if \a aRefPoint is with \a aDistance on the line defined by \a aStart and \a aEnd..
|
|
|
|
*
|
2013-01-26 17:49:48 +00:00
|
|
|
* @param aRefPoint = reference point to test
|
2009-06-13 17:06:07 +00:00
|
|
|
* @param aStart is the first end-point of the line segment
|
|
|
|
* @param aEnd is the second end-point of the line segment
|
|
|
|
* @param aDist = maximum distance for hit
|
|
|
|
*/
|
2021-07-26 19:35:12 +00:00
|
|
|
bool TestSegmentHit( const wxPoint& aRefPoint, const wxPoint& aStart, const wxPoint& aEnd,
|
|
|
|
int aDist );
|
2009-06-13 17:06:07 +00:00
|
|
|
|
2011-03-25 19:16:05 +00:00
|
|
|
/**
|
2020-03-04 13:35:33 +00:00
|
|
|
* Return the length of a line segment defined by \a aPointA and \a aPointB.
|
|
|
|
*
|
|
|
|
* See also EuclideanNorm and Distance for the single vector or four scalar versions.
|
|
|
|
*
|
2013-05-01 17:32:36 +00:00
|
|
|
* @return Length of a line (as double)
|
2011-03-25 19:16:05 +00:00
|
|
|
*/
|
2013-05-01 17:32:36 +00:00
|
|
|
inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
|
|
|
|
{
|
|
|
|
// Implicitly casted to double
|
2021-07-26 19:35:12 +00:00
|
|
|
return hypot( aPointA.x - aPointB.x, aPointA.y - aPointB.y );
|
2013-05-01 17:32:36 +00:00
|
|
|
}
|
2010-02-04 17:46:12 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
// These are the usual degrees <-> radians conversion routines
|
|
|
|
inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
|
|
|
|
inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
|
|
|
|
|
|
|
|
// These are the same *but* work with the internal 'decidegrees' unit
|
|
|
|
inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
|
|
|
|
inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
|
|
|
|
|
2020-02-28 14:03:09 +00:00
|
|
|
/* These are templated over T (and not simply double) because Eeschema
|
2013-05-02 18:06:58 +00:00
|
|
|
is still using int for angles in some place */
|
|
|
|
|
2017-10-23 13:35:03 +00:00
|
|
|
/// Normalize angle to be >=-360.0 and <= 360.0
|
|
|
|
/// Angle can be equal to -360 or +360
|
|
|
|
template <class T> inline T NormalizeAngle360Max( T Angle )
|
|
|
|
{
|
|
|
|
while( Angle < -3600 )
|
|
|
|
Angle += 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-10-23 13:35:03 +00:00
|
|
|
while( Angle > 3600 )
|
|
|
|
Angle -= 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-10-23 13:35:03 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalize angle to be > -360.0 and < 360.0
|
|
|
|
/// Angle equal to -360 or +360 are set to 0
|
|
|
|
template <class T> inline T NormalizeAngle360Min( T Angle )
|
2013-05-02 18:06:58 +00:00
|
|
|
{
|
2016-04-19 18:35:47 +00:00
|
|
|
while( Angle <= -3600 )
|
2013-05-02 18:06:58 +00:00
|
|
|
Angle += 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2016-04-19 18:35:47 +00:00
|
|
|
while( Angle >= 3600 )
|
2013-09-27 12:30:35 +00:00
|
|
|
Angle -= 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
2013-05-02 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 17:12:08 +00:00
|
|
|
|
2021-07-26 19:35:12 +00:00
|
|
|
/// Normalize angle to be in the 0.0 .. -360.0 range: angle is in 1/10 degrees.
|
2020-08-16 17:12:08 +00:00
|
|
|
template <class T>
|
|
|
|
inline T NormalizeAngleNeg( T Angle )
|
|
|
|
{
|
2020-09-08 18:46:20 +00:00
|
|
|
while( Angle <= -3600 )
|
2020-08-16 17:12:08 +00:00
|
|
|
Angle += 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2020-09-08 18:46:20 +00:00
|
|
|
while( Angle > 0 )
|
2020-08-16 17:12:08 +00:00
|
|
|
Angle -= 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2020-08-16 17:12:08 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 19:35:12 +00:00
|
|
|
/// Normalize angle to be in the 0.0 .. 360.0 range: angle is in 1/10 degrees.
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline T NormalizeAnglePos( T Angle )
|
2013-05-02 18:06:58 +00:00
|
|
|
{
|
|
|
|
while( Angle < 0 )
|
|
|
|
Angle += 3600;
|
|
|
|
while( Angle >= 3600 )
|
|
|
|
Angle -= 3600;
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
|
|
|
|
{
|
|
|
|
Angle = NormalizeAnglePos( Angle );
|
2013-05-02 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
|
2021-07-26 19:35:12 +00:00
|
|
|
/// Normalize angle to be in the 0.0 .. 360.0 range: angle is in degrees.
|
2017-01-23 20:30:11 +00:00
|
|
|
inline double NormalizeAngleDegreesPos( double Angle )
|
2016-05-02 10:49:14 +00:00
|
|
|
{
|
|
|
|
while( Angle < 0 )
|
|
|
|
Angle += 360.0;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2016-05-02 10:49:14 +00:00
|
|
|
while( Angle >= 360.0 )
|
|
|
|
Angle -= 360.0;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
2017-10-23 13:35:03 +00:00
|
|
|
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
|
|
|
|
{
|
|
|
|
Angle = NormalizeAngleDegreesPos( Angle );
|
2016-05-02 10:49:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 03:17:42 +00:00
|
|
|
|
|
|
|
inline double NormalizeAngleRadiansPos( double Angle )
|
|
|
|
{
|
|
|
|
while( Angle < 0 )
|
|
|
|
Angle += (2 * M_PI );
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2018-02-28 03:17:42 +00:00
|
|
|
while( Angle >= ( 2 * M_PI ) )
|
|
|
|
Angle -= ( 2 * M_PI );
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2018-02-28 03:17:42 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:35:12 +00:00
|
|
|
/// Normalize angle to be aMin < angle <= aMax angle is in degrees.
|
2018-03-14 17:48:55 +00:00
|
|
|
inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
|
|
|
|
{
|
|
|
|
while( Angle < aMin )
|
|
|
|
Angle += 360.0;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2018-03-14 17:48:55 +00:00
|
|
|
while( Angle >= aMax )
|
|
|
|
Angle -= 360.0;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2018-03-14 17:48:55 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
/// Add two angles (keeping the result normalized). T2 is here
|
|
|
|
// because most of the time it's an int (and templates don't promote in
|
|
|
|
// that way)
|
|
|
|
template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
|
|
|
|
{
|
|
|
|
a1 += a2;
|
|
|
|
NORMALIZE_ANGLE_POS( a1 );
|
|
|
|
return a1;
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:35:03 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
|
2013-05-02 18:06:58 +00:00
|
|
|
{
|
|
|
|
Angle = -Angle;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
while( Angle < 0 )
|
|
|
|
Angle += 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
while( Angle >= 3600 )
|
|
|
|
Angle -= 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
|
|
|
|
{
|
|
|
|
Angle = NegateAndNormalizeAnglePos( Angle );
|
2013-05-02 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
/// Normalize angle to be in the -90.0 .. 90.0 range
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline T NormalizeAngle90( T Angle )
|
2013-05-02 18:06:58 +00:00
|
|
|
{
|
|
|
|
while( Angle < -900 )
|
|
|
|
Angle += 1800;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
while( Angle > 900 )
|
|
|
|
Angle -= 1800;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
|
|
|
}
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
|
|
|
|
{
|
|
|
|
Angle = NormalizeAngle90( Angle );
|
2013-05-02 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
/// Normalize angle to be in the -180.0 .. 180.0 range
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline T NormalizeAngle180( T Angle )
|
2013-05-02 18:06:58 +00:00
|
|
|
{
|
|
|
|
while( Angle <= -1800 )
|
|
|
|
Angle += 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
while( Angle > 1800 )
|
|
|
|
Angle -= 3600;
|
2021-07-26 19:35:12 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
return Angle;
|
2013-05-02 18:06:58 +00:00
|
|
|
}
|
2020-02-28 14:03:09 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
|
|
|
|
{
|
|
|
|
Angle = NormalizeAngle180( Angle );
|
|
|
|
}
|
|
|
|
|
2020-02-28 14:03:09 +00:00
|
|
|
/**
|
|
|
|
* Test if an arc from \a aStartAngle to \a aEndAngle crosses the positive X axis (0 degrees).
|
|
|
|
*
|
|
|
|
* Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise).
|
|
|
|
*
|
|
|
|
* @param aStartAngle The arc start angle in degrees.
|
|
|
|
* @param aEndAngle The arc end angle in degrees.
|
|
|
|
*/
|
|
|
|
inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
|
|
|
|
{
|
|
|
|
double end = aEndAngle;
|
|
|
|
|
2020-03-04 13:35:33 +00:00
|
|
|
if( aStartAngle > aEndAngle )
|
2020-02-28 14:03:09 +00:00
|
|
|
end += 360.0;
|
|
|
|
|
2020-03-04 13:35:33 +00:00
|
|
|
return aStartAngle < 360.0 && end > 360.0;
|
2020-02-28 14:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if an arc from \a aStartAngle to \a aEndAngle crosses the negative X axis (180 degrees).
|
|
|
|
*
|
|
|
|
* Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise).
|
|
|
|
*
|
|
|
|
* @param aStartAngle The arc start angle in degrees.
|
|
|
|
* @param aEndAngle The arc end angle in degrees.
|
|
|
|
*/
|
|
|
|
inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
|
|
|
|
{
|
|
|
|
double end = aEndAngle;
|
|
|
|
|
2020-03-04 13:35:33 +00:00
|
|
|
if( aStartAngle > aEndAngle )
|
2020-02-28 14:03:09 +00:00
|
|
|
end += 360.0;
|
|
|
|
|
2020-03-04 13:35:33 +00:00
|
|
|
return aStartAngle < 180.0 && end > 180.0;
|
2020-02-28 14:03:09 +00:00
|
|
|
}
|
2013-05-02 18:06:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Circle generation utility: computes r * sin(a)
|
|
|
|
* Where a is in decidegrees, not in radians.
|
|
|
|
*/
|
|
|
|
inline double sindecideg( double r, double a )
|
|
|
|
{
|
|
|
|
return r * sin( DECIDEG2RAD( a ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Circle generation utility: computes r * cos(a)
|
|
|
|
* Where a is in decidegrees, not in radians.
|
|
|
|
*/
|
|
|
|
inline double cosdecideg( double r, double a )
|
|
|
|
{
|
|
|
|
return r * cos( DECIDEG2RAD( a ) );
|
|
|
|
}
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#endif
|