misc.cpp
author insilmaril
Fri, 01 Feb 2008 16:59:09 +0000
changeset 668 b63ff428c781
parent 617 7ee5bf3647d3
child 789 d85834ad8c54
permissions -rw-r--r--
Added simplified chinese by Shaodong
insilmaril@139
     1
#include <math.h>
insilmaril@139
     2
insilmaril@0
     3
#include <qregexp.h>
insilmaril@0
     4
#include <qpoint.h>
insilmaril@164
     5
#include <stdlib.h>
insilmaril@0
     6
insilmaril@0
     7
#include "misc.h"
insilmaril@0
     8
insilmaril@129
     9
QString qpointToString (const QPoint &p)
insilmaril@129
    10
{
insilmaril@129
    11
	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
insilmaril@129
    12
}
insilmaril@0
    13
insilmaril@408
    14
QString qpointfToString (const QPointF &p)
insilmaril@408
    15
{
insilmaril@408
    16
	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
insilmaril@408
    17
}
insilmaril@408
    18
insilmaril@0
    19
ostream &operator<< (ostream &stream, QPoint const &p)
insilmaril@0
    20
{ 
insilmaril@366
    21
	stream << "("<<p.x()<<","<<p.y()<<")";
insilmaril@366
    22
	return stream;
insilmaril@0
    23
}
insilmaril@0
    24
insilmaril@408
    25
ostream &operator<< (ostream &stream, QPointF const &p)
insilmaril@408
    26
{ 
insilmaril@408
    27
	stream << "("<<p.x()<<","<<p.y()<<")";
insilmaril@408
    28
	return stream;
insilmaril@408
    29
}
insilmaril@408
    30
insilmaril@408
    31
qreal getAngle(const QPointF &p)
insilmaril@0
    32
{	
insilmaril@0
    33
	// Calculate angle of vector to y-axis
insilmaril@0
    34
	if (p.y()==0)
insilmaril@0
    35
	{
insilmaril@0
    36
		if (p.x()>=0)
insilmaril@0
    37
			return M_PI_2;
insilmaril@0
    38
		else
insilmaril@0
    39
			return 3* M_PI_2;
insilmaril@0
    40
	} else
insilmaril@0
    41
	{
insilmaril@0
    42
		if (p.y()>0) 
insilmaril@408
    43
			return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
insilmaril@0
    44
		else	
insilmaril@0
    45
			if (p.x()<0)
insilmaril@408
    46
				return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
insilmaril@0
    47
			else	
insilmaril@408
    48
				return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
insilmaril@0
    49
	}	
insilmaril@0
    50
}
insilmaril@0
    51
insilmaril@408
    52
QPointF normalise(const QPointF &p)
insilmaril@0
    53
{	
insilmaril@0
    54
	// Calculate normalised position (fixed length) 
insilmaril@0
    55
insilmaril@408
    56
	qreal px=p.x();
insilmaril@408
    57
	qreal py=p.y();
insilmaril@408
    58
	qreal x;
insilmaril@408
    59
	qreal y;
insilmaril@408
    60
	qreal r=150;
insilmaril@0
    61
insilmaril@0
    62
	if (px==0)
insilmaril@0
    63
	{
insilmaril@0
    64
		x=0;
insilmaril@0
    65
		if (py>=0)
insilmaril@0
    66
			y=r;
insilmaril@0
    67
		else
insilmaril@0
    68
			y=-r;
insilmaril@0
    69
	} else
insilmaril@0
    70
	{
insilmaril@408
    71
		qreal sign;
insilmaril@408
    72
		qreal a;
insilmaril@0
    73
		if (px>0) 
insilmaril@0
    74
			sign=1; 
insilmaril@0
    75
		else 
insilmaril@0
    76
			sign=-1;
insilmaril@0
    77
		
insilmaril@0
    78
		a=atan (py / px);
insilmaril@0
    79
		x=cos (a) * r *sign;
insilmaril@0
    80
		y=sin (a) * r *sign;
insilmaril@0
    81
	}	
insilmaril@0
    82
	return QPoint ((int) (x),(int) (y));
insilmaril@0
    83
}
insilmaril@0
    84
insilmaril@166
    85
insilmaril@408
    86
qreal max(qreal a, qreal b)
insilmaril@0
    87
{
insilmaril@0
    88
	if (a>b)
insilmaril@0
    89
		return a;
insilmaril@0
    90
	return b;
insilmaril@0
    91
}
insilmaril@0
    92