misc.cpp
author insilmaril
Fri, 11 Sep 2009 12:56:15 +0000
changeset 794 d922fb6ea482
parent 792 7d67be709091
child 802 f076fdec767d
permissions -rw-r--r--
more fixes for collisions
     1 #include <math.h>
     2 
     3 #include <qregexp.h>
     4 //#include <qpoint.h>
     5 #include <stdlib.h>
     6 
     7 #include "misc.h"
     8 
     9 #include "geometry.h"
    10 
    11 QString qpointToString (const QPoint &p)
    12 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
    13 
    14 QString qpointFToString (const QPointF &p)
    15 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
    16 
    17 QString VectorToString (const Vector &p)
    18 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
    19 
    20 ostream &operator<< (ostream &stream, QPoint const &p)
    21 { 
    22 	stream << "("<<p.x()<<","<<p.y()<<")";
    23 	return stream;
    24 }
    25 
    26 ostream &operator<< (ostream &stream, QPointF const &p)
    27 { 
    28 	stream << "("<<p.x()<<","<<p.y()<<")";
    29 	return stream;
    30 }
    31 
    32 ostream &operator<< (ostream &stream, Vector const &p)
    33 { 
    34 	stream << "("<<p.x()<<","<<p.y()<<")";
    35 	return stream;
    36 }
    37 
    38 qreal getAngle(const QPointF &p)
    39 {	
    40 	// Calculate angle of vector to y-axis
    41 	if (p.y()==0)
    42 	{
    43 		if (p.x()>=0)
    44 			return M_PI_2;
    45 		else
    46 			return 3* M_PI_2;
    47 	} else
    48 	{
    49 		if (p.y()>0) 
    50 			return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    51 		else	
    52 			if (p.x()<0)
    53 				return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    54 			else	
    55 				return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
    56 	}	
    57 }
    58 
    59 qreal max(qreal a, qreal b)
    60 {
    61 	if (a>b)
    62 		return a;
    63 	return b;
    64 }
    65