00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <iostream>
00026 #include <qvmath/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
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
00089 const double qvClockWiseAngle(const QPointF &point1, const QPointF &point2)
00090 {
00091
00092
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