1 /*************************************************************************** 2 * Copyright (C) 2009 Tim Vandermeersch * 3 * * 4 * This program is free software; you can redistribute it and/or modify * 5 * it under the terms of the GNU General Public License as published by * 6 * the Free Software Foundation; either version 2 of the License, or * 7 * (at your option) any later version. * 8 * * 9 * This program is distributed in the hope that it will be useful, * 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 * GNU General Public License for more details. * 13 * * 14 * You should have received a copy of the GNU General Public License * 15 * along with this program; if not, write to the * 16 * Free Software Foundation, Inc., * 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 18 ***************************************************************************/ 19 20 #ifndef MSK_MATH2D_H 21 #define MSK_MATH2D_H 22 23 #include <QtMath> 24 #include <QPointF> 25 26 namespace Molsketch { 27 length(const QPointF & v)28 inline qreal length(const QPointF &v) 29 { 30 return sqrt(v.x() * v.x() + v.y() * v.y()); 31 } 32 normalized(const QPointF & v)33 inline QPointF normalized(const QPointF &v) 34 { 35 QPointF n = v; 36 qreal length = sqrt(n.x() * n.x() + n.y() * n.y()); 37 n /= length; 38 return n; 39 } 40 angle(const QPointF & v1,const QPointF & v2)41 inline qreal angle(const QPointF &v1, const QPointF &v2) 42 { 43 QPointF n1 = normalized(v1); 44 QPointF n2 = normalized(v2); 45 46 qreal dot = n1.x() * n2.x() + n1.y() * n2.y(); 47 return acos(dot); 48 } 49 angleSign(const QPointF & v1,const QPointF & v2)50 inline qreal angleSign(const QPointF &v1, const QPointF &v2) 51 { 52 return v1.x() * v2.y() - v1.y() * v2.x(); 53 } 54 triangleSign(const QPointF & a,const QPointF & b,const QPointF & c)55 inline double triangleSign(const QPointF &a, const QPointF &b, const QPointF &c) 56 { 57 return (a.x() - c.x()) * (b.y() - c.y()) - (a.y() - c.y()) * (b.x() - c.x()); 58 } 59 60 } 61 62 #endif 63