File indexing completed on 2024-04-28 03:40:50
0001 /************************************************************************************* 0002 * Copyright (C) 2007-2009 by Aleix Pol <aleixpol@kde.org> * 0003 * Copyright (C) 2012-2013 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or * 0006 * modify it under the terms of the GNU General Public License * 0007 * as published by the Free Software Foundation; either version 2 * 0008 * of the License, or (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the Free Software * 0017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 0018 *************************************************************************************/ 0019 0020 #include "mathutils.h" 0021 0022 #include <cmath> 0023 0024 double radiansToDegrees(double rad) 0025 { 0026 return rad*180.0/M_PI; 0027 } 0028 0029 double radiansToGradians(double rad) 0030 { 0031 return rad*200.0/M_PI; 0032 } 0033 0034 QPointF polarToCartesian(double radial, double polar) 0035 { 0036 return QPointF(radial*std::cos(polar), radial*std::sin(polar)); 0037 } 0038 0039 void cartesianToPolar(double x, double y, double &radial, double &polar) 0040 { 0041 radial = std::sqrt(x*x+y*y); 0042 polar = std::atan2(y,x); 0043 } 0044 0045 QVector3D cylindricalToCartesian(double radial, double polar, double height) 0046 { 0047 return QVector3D(radial*std::cos(polar), radial*std::sin(polar), height); 0048 } 0049 0050 QVector3D sphericalToCartesian(double radial, double azimuth, double polar) 0051 { 0052 return QVector3D(radial*std::cos(azimuth)*std::sin(polar), radial*std::sin(azimuth)*std::sin(polar), radial*std::cos(polar)); 0053 } 0054 0055 bool traverse(double p1, double p2, double next) 0056 { 0057 static const double delta=3; 0058 double diff=p2-p1, diff2=next-p2; 0059 0060 bool ret = (diff>0 && diff2<-delta) || (diff<0 && diff2>delta); 0061 0062 return ret; 0063 } 0064 0065 bool isSimilar(double a, double b, double diff) 0066 { 0067 return std::fabs(a-b) < diff; 0068 } 0069 0070 bool oppositeSign(double a, double b) 0071 { 0072 return ((a > 0 && b <= 0) || (a <= 0 && b > 0)); 0073 } 0074 0075 bool inInterval(double val, double from, double to) 0076 { 0077 return (from <= val && val <= to); 0078 } 0079 0080 QLineF slopeToLine(const double &der) 0081 { 0082 double arcder = std::atan(der); 0083 const double len=6.*der; 0084 QPointF from, to; 0085 from.setX(len*std::cos(arcder)); 0086 from.setY(len*std::sin(arcder)); 0087 0088 to.setX(-len*std::cos(arcder)); 0089 to.setY(-len*std::sin(arcder)); 0090 0091 return QLineF(from, to); 0092 } 0093 0094 QLineF mirrorXY(const QLineF& l) 0095 { 0096 return QLineF(l.y1(), l.x1(), l.y2(), l.x2()); 0097 } 0098 0099 unsigned int gcd(unsigned int u, unsigned int v) 0100 { 0101 int shift; 0102 0103 /* GCD(0,v) == v; GCD(u,0) == u, GCD(0,0) == 0 */ 0104 if (u == 0) return v; 0105 if (v == 0) return u; 0106 0107 /* Let shift := lg K, where K is the greatest power of 2 0108 dividing both u and v. */ 0109 for (shift = 0; ((u | v) & 1) == 0; ++shift) { 0110 u >>= 1; 0111 v >>= 1; 0112 } 0113 0114 while ((u & 1) == 0) 0115 u >>= 1; 0116 0117 /* From here on, u is always odd. */ 0118 do { 0119 /* remove all factors of 2 in v -- they are not common */ 0120 /* note: v is not zero, so while will terminate */ 0121 while ((v & 1) == 0) /* Loop X */ 0122 v >>= 1; 0123 0124 /* Now u and v are both odd. Swap if necessary so u <= v, 0125 then set v = v - u (which is even). For bignums, the 0126 swapping is just pointer movement, and the subtraction 0127 can be done in-place. */ 0128 if (u > v) { 0129 unsigned int t = v; 0130 v = u; 0131 u = t; 0132 } // Swap u and v. 0133 v = v - u; // Here v >= u. 0134 } while (v != 0); 0135 0136 /* restore common factors of 2 */ 0137 return u << shift; 0138 } 0139 0140 QPair< int unsigned , int unsigned > simplifyFraction(unsigned int n, int unsigned d) 0141 { 0142 const unsigned int cf = gcd(n,d); 0143 0144 return QPair< int unsigned , int unsigned >(n/cf, d/cf); // first=new n, second=new d 0145 }