## [Freeorion-programmers] SF.net SVN: freeorion:[3438] trunk/FreeOrion/util

 [Freeorion-programmers] SF.net SVN: freeorion:[3438] trunk/FreeOrion/util From: - 2010-04-07 04:38:10 ```Revision: 3438 http://freeorion.svn.sourceforge.net/freeorion/revision/?rev=3438&view=rev Author: tzlaine Date: 2010-04-07 04:38:03 +0000 (Wed, 07 Apr 2010) Log Message: ----------- Added new util/Math.h function, PointInPartialEllipse(), and fixed an indexing error in PointInEllipse(). Modified Paths: -------------- trunk/FreeOrion/util/Math.cpp trunk/FreeOrion/util/Math.h Modified: trunk/FreeOrion/util/Math.cpp =================================================================== --- trunk/FreeOrion/util/Math.cpp 2010-04-06 21:49:05 UTC (rev 3437) +++ trunk/FreeOrion/util/Math.cpp 2010-04-07 04:38:03 UTC (rev 3438) @@ -3,34 +3,58 @@ #include -bool PointInEllipse(double x, double y, - double ellipse_x, double ellipse_y, - double major, double minor, - double theta_major) -{ - typedef boost::numeric::ublas::matrix Matrix; - Matrix v(2, 1); - v(0, 0) = x - ellipse_x; - v(0, 1) = y - ellipse_y; +namespace { + bool PointInEllipseImpl(double x, double y, + double ellipse_x, double ellipse_y, + double major, double minor, + double theta_major, + double theta_begin, + double theta_end) + { + typedef boost::numeric::ublas::matrix Matrix; + Matrix v(2, 1); + v(0, 0) = x - ellipse_x; + v(1, 0) = y - ellipse_y; - const double SIN = std::sin(-theta_major); - const double COS = std::cos(-theta_major); - Matrix rotate(2, 2); - rotate(0, 0) = COS; - rotate(0, 1) = -SIN; - rotate(1, 0) = SIN; - rotate(1, 1) = COS; + const double SIN = std::sin(-theta_major); + const double COS = std::cos(-theta_major); + Matrix rotate(2, 2); + rotate(0, 0) = COS; + rotate(0, 1) = -SIN; + rotate(1, 0) = SIN; + rotate(1, 1) = COS; - v = prod(rotate, v); + v = prod(rotate, v); - Matrix scale(2, 2); - scale(0, 0) = 1.0 / major; - scale(0, 1) = 0.0; - scale(1, 0) = 0.0; - scale(1, 1) = 1.0 / minor; + if (theta_begin != theta_end) { + double theta = std::atan2(y, x); + if (theta < theta_begin || theta_end < theta) + return false; + } - v = prod(scale, v); + Matrix scale(2, 2); + scale(0, 0) = 1.0 / major; + scale(0, 1) = 0.0; + scale(1, 0) = 0.0; + scale(1, 1) = 1.0 / minor; - return v(0, 0) * v(0, 0) + v(0, 1) * v(0, 1) < 1.0; + v = prod(scale, v); + + return v(0, 0) * v(0, 0) + v(1, 0) * v(1, 0) < 1.0; + } + } +bool PointInEllipse(double x, double y, + double ellipse_x, double ellipse_y, + double major, double minor, + double theta_major) +{ return PointInEllipseImpl(x, y, ellipse_x, ellipse_y, major, minor, theta_major, 0, 0); } + +bool PointInPartialEllipse(double x, double y, + double ellipse_x, double ellipse_y, + double major, double minor, + double theta_major, + double theta_begin, + double theta_end) +{ return PointInEllipseImpl(x, y, ellipse_x, ellipse_y, major, minor, theta_major, theta_begin, theta_end); } Modified: trunk/FreeOrion/util/Math.h =================================================================== --- trunk/FreeOrion/util/Math.h 2010-04-06 21:49:05 UTC (rev 3437) +++ trunk/FreeOrion/util/Math.h 2010-04-07 04:38:03 UTC (rev 3438) @@ -10,4 +10,16 @@ double major, double minor, double theta_major); +/** Returns true iff (x, y) falls inside the angular portion + (theta_begin, theta_end) ellipse centered at (ellipse_x, + ellipse_y), with major and minor axes \a major and \a minor, with the + major axis at angle \a theta_major. The angular region (theta_begin, + theta_end) is taken to be relative to \a theta_major. */ +bool PointInPartialEllipse(double x, double y, + double ellipse_x, double ellipse_y, + double major, double minor, + double theta_major, + double theta_begin, + double theta_end); + #endif // _Math_h_ ```

## Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks