misc.cpp
author insilmaril
Mon, 20 Apr 2009 10:42:05 +0000
changeset 754 db0ec4bcf416
parent 617 7ee5bf3647d3
child 789 d85834ad8c54
permissions -rw-r--r--
minor fixes
     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 QPointF normalise(const QPointF &p)
    53 {	
    54 	// Calculate normalised position (fixed length) 
    55 
    56 	qreal px=p.x();
    57 	qreal py=p.y();
    58 	qreal x;
    59 	qreal y;
    60 	qreal r=150;
    61 
    62 	if (px==0)
    63 	{
    64 		x=0;
    65 		if (py>=0)
    66 			y=r;
    67 		else
    68 			y=-r;
    69 	} else
    70 	{
    71 		qreal sign;
    72 		qreal a;
    73 		if (px>0) 
    74 			sign=1; 
    75 		else 
    76 			sign=-1;
    77 		
    78 		a=atan (py / px);
    79 		x=cos (a) * r *sign;
    80 		y=sin (a) * r *sign;
    81 	}	
    82 	return QPoint ((int) (x),(int) (y));
    83 }
    84 
    85 
    86 qreal max(qreal a, qreal b)
    87 {
    88 	if (a>b)
    89 		return a;
    90 	return b;
    91 }
    92