PARP Research Group University of Murcia, Spain


src/qvmath/qvmath.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <iostream>
00026 #include <qvmath.h>
00027 #include <qvmath/qvrationalnumber.h>
00028 
00029 double norm2(const QPointF &p)
00030         {
00031         return sqrt(p.x() * p.x() + p.y() * p.y());
00032         }
00033 
00034 const int qvFactorial(const int n)
00035         {
00036         int result = 1;
00037         for (int i = 2; i <= n; i++)
00038                 result *= i;
00039 
00040         return result;
00041         };
00042 
00043 const double qvFactorialDivisionDouble(const int numerator, const int denominator)
00044         {
00045         double result = 1;
00046         for (int i = denominator+1; i <= numerator; i++)
00047                 result *= i;
00048 
00049         return result;
00050         }
00051 
00052 const double qvCombination(const int setRange, const int subsetRange)
00053         {
00054         Q_ASSERT(setRange > subsetRange);
00055 
00056         QVRationalNumber subSetFactorial, divisionFactorial;
00057 
00058         for (int i = 2; i <= subsetRange; i++)
00059                 subSetFactorial.mult(i);
00060 
00061         for (int i = setRange - subsetRange + 1; i <= setRange; i++)
00062                 divisionFactorial.mult(i);
00063         
00064         return divisionFactorial/subSetFactorial;
00065         }
00066 
00067 const double qvAngle(const QPointF &point)
00068         {
00069         const double x = point.x(), y = point.y();
00070 
00071         if (x>0)
00072                 if (y>=0)
00073                         return atan(y/x);
00074                 else
00075                         return atan(y/x) + 2*PI;
00076         else if (x == 0)
00077                 if (y>0)
00078                         return PI/2;
00079                 else
00080                         return 3*PI/2;
00081         else // x < 0
00082                 return atan(y/x)+PI;
00083         }
00084 
00085 const int qvRandom(const int minValue, const int maxValue)
00086         { return rand()%(maxValue-minValue+1) + minValue; }
00087 
00088 // This function returns the positive degrees for a given angle
00089 const double qvClockWiseAngle(const QPointF &point1, const QPointF &point2)
00090         {
00091         // This is tricky: qvClockWiseAngle returns angle from point1 to point2, but
00092         // return value is angle of point1 - angle of point2.
00093         const double clockAngle = qvAngle(point1) - qvAngle(point2);
00094         return (clockAngle < 0)? clockAngle + 2*PI:clockAngle;
00095         }
00096 
00097 std::ostream& operator << ( std::ostream &os, const QPointF &point )
00098         {
00099         os << "QPointF (" << point.x() << ", " << point.y() << ")";
00100         return os;
00101         }
00102 
00103 



QVision framework. PARP research group, copyright 2007, 2008.