misc.cpp
author insilmaril
Mon, 24 Aug 2009 14:39:07 +0000
changeset 789 d85834ad8c54
parent 617 7ee5bf3647d3
child 792 7d67be709091
permissions -rw-r--r--
Fixed collision methods, segfault for wrong flagname
     1 #include <math.h>
     2 
     3 #include <qregexp.h>
     4 #include <qpoint.h>
     5 #include <stdlib.h>
     6 
     7 #include "misc.h"
     8 
     9 QString qpointToString (const QPoint &p)
    10 {
    11 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
    12 }
    13 
    14 QString qpointfToString (const QPointF &p)
    15 {
    16 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
    17 }
    18 
    19 ostream &operator<< (ostream &stream, QPoint const &p)
    20 { 
    21 	stream << "("<<p.x()<<","<<p.y()<<")";
    22 	return stream;
    23 }
    24 
    25 ostream &operator<< (ostream &stream, QPointF const &p)
    26 { 
    27 	stream << "("<<p.x()<<","<<p.y()<<")";
    28 	return stream;
    29 }
    30 
    31 qreal getAngle(const QPointF &p)
    32 {	
    33 	// Calculate angle of vector to y-axis
    34 	if (p.y()==0)
    35 	{
    36 		if (p.x()>=0)
    37 			return M_PI_2;
    38 		else
    39 			return 3* M_PI_2;
    40 	} else
    41 	{
    42 		if (p.y()>0) 
    43 			return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    44 		else	
    45 			if (p.x()<0)
    46 				return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    47 			else	
    48 				return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    49 	}	
    50 }
    51 
    52 qreal max(qreal a, qreal b)
    53 {
    54 	if (a>b)
    55 		return a;
    56 	return b;
    57 }
    58