From 5da341d1fc14d4c007ddd1bdf37f7bddf29f25c2 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 26 Apr 2016 14:14:26 +0200 Subject: [PATCH] Optimize VECTOR2::Rotation for 0, 90, 180 and -90 degrees by avoiding time consumming calculations. --- include/math/vector2d.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/math/vector2d.h b/include/math/vector2d.h index 764534add9..b9ca1d22ce 100644 --- a/include/math/vector2d.h +++ b/include/math/vector2d.h @@ -358,6 +358,19 @@ VECTOR2& VECTOR2::operator-=( const T& aScalar ) template VECTOR2 VECTOR2::Rotate( double aAngle ) const { + // fast calculation of some rotations, very frequently found + if( aAngle == 0.0 ) + return VECTOR2 ( T( x ), T( y ) ); + + if( aAngle == 90.0 ) + return VECTOR2 ( T( -y ), T( x ) ); + + if( aAngle == -90.0 ) + return VECTOR2 ( T( y ), T( -x ) ); + + if( aAngle == 180.0 || aAngle == -180.0 ) + return VECTOR2 ( T( -x ), T( -y ) ); + double sa = sin( aAngle ); double ca = cos( aAngle );