insilmaril@2
|
1 |
#include <math.h>
|
insilmaril@2
|
2 |
|
insilmaril@0
|
3 |
#include <qregexp.h>
|
insilmaril@0
|
4 |
#include <qpoint.h>
|
insilmaril@2
|
5 |
#include <stdlib.h>
|
insilmaril@0
|
6 |
|
insilmaril@0
|
7 |
#include "misc.h"
|
insilmaril@0
|
8 |
|
insilmaril@2
|
9 |
QString qpointToString (const QPoint &p)
|
insilmaril@2
|
10 |
{
|
insilmaril@2
|
11 |
return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
|
insilmaril@2
|
12 |
}
|
insilmaril@0
|
13 |
|
insilmaril@0
|
14 |
ostream &operator<< (ostream &stream, QPoint const &p)
|
insilmaril@0
|
15 |
{
|
insilmaril@2
|
16 |
stream << "("<<p.x()<<","<<p.y()<<")";
|
insilmaril@2
|
17 |
return stream;
|
insilmaril@0
|
18 |
}
|
insilmaril@0
|
19 |
|
insilmaril@0
|
20 |
float getAngle(const QPoint &p)
|
insilmaril@0
|
21 |
{
|
insilmaril@0
|
22 |
// Calculate angle of vector to y-axis
|
insilmaril@0
|
23 |
if (p.y()==0)
|
insilmaril@0
|
24 |
{
|
insilmaril@0
|
25 |
if (p.x()>=0)
|
insilmaril@0
|
26 |
return M_PI_2;
|
insilmaril@0
|
27 |
else
|
insilmaril@0
|
28 |
return 3* M_PI_2;
|
insilmaril@0
|
29 |
} else
|
insilmaril@0
|
30 |
{
|
insilmaril@0
|
31 |
if (p.y()>0)
|
insilmaril@0
|
32 |
return (float)(M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
|
insilmaril@0
|
33 |
else
|
insilmaril@0
|
34 |
if (p.x()<0)
|
insilmaril@0
|
35 |
return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
|
insilmaril@0
|
36 |
else
|
insilmaril@0
|
37 |
return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
|
insilmaril@0
|
38 |
}
|
insilmaril@0
|
39 |
}
|
insilmaril@0
|
40 |
|
insilmaril@0
|
41 |
QPoint normalise(const QPoint &p)
|
insilmaril@0
|
42 |
{
|
insilmaril@0
|
43 |
// Calculate normalised position (fixed length)
|
insilmaril@0
|
44 |
|
insilmaril@0
|
45 |
double px=p.x();
|
insilmaril@0
|
46 |
double py=p.y();
|
insilmaril@0
|
47 |
double x;
|
insilmaril@0
|
48 |
double y;
|
insilmaril@0
|
49 |
double r=150;
|
insilmaril@0
|
50 |
|
insilmaril@0
|
51 |
if (px==0)
|
insilmaril@0
|
52 |
{
|
insilmaril@0
|
53 |
x=0;
|
insilmaril@0
|
54 |
if (py>=0)
|
insilmaril@0
|
55 |
y=r;
|
insilmaril@0
|
56 |
else
|
insilmaril@0
|
57 |
y=-r;
|
insilmaril@0
|
58 |
} else
|
insilmaril@0
|
59 |
{
|
insilmaril@0
|
60 |
double sign;
|
insilmaril@0
|
61 |
double a;
|
insilmaril@0
|
62 |
if (px>0)
|
insilmaril@0
|
63 |
sign=1;
|
insilmaril@0
|
64 |
else
|
insilmaril@0
|
65 |
sign=-1;
|
insilmaril@0
|
66 |
|
insilmaril@0
|
67 |
a=atan (py / px);
|
insilmaril@0
|
68 |
x=cos (a) * r *sign;
|
insilmaril@0
|
69 |
y=sin (a) * r *sign;
|
insilmaril@0
|
70 |
}
|
insilmaril@0
|
71 |
return QPoint ((int) (x),(int) (y));
|
insilmaril@0
|
72 |
}
|
insilmaril@0
|
73 |
|
insilmaril@0
|
74 |
|
insilmaril@0
|
75 |
// returns masked "<" ">" "&"
|
insilmaril@0
|
76 |
QString quotemeta(const QString &s)
|
insilmaril@0
|
77 |
{
|
insilmaril@0
|
78 |
QString r=s;
|
insilmaril@0
|
79 |
QRegExp rx("&(?!amp;)");
|
insilmaril@0
|
80 |
r.replace ( rx,"&");
|
insilmaril@0
|
81 |
rx.setPattern( ">");
|
insilmaril@0
|
82 |
r.replace ( rx,">");
|
insilmaril@0
|
83 |
rx.setPattern( "<");
|
insilmaril@0
|
84 |
r.replace ( rx,"<");
|
insilmaril@0
|
85 |
rx.setPattern( "\"");
|
insilmaril@0
|
86 |
r.replace ( rx,""");
|
insilmaril@0
|
87 |
return r;
|
insilmaril@0
|
88 |
}
|
insilmaril@0
|
89 |
|
insilmaril@0
|
90 |
int max(int a, int b)
|
insilmaril@0
|
91 |
{
|
insilmaril@0
|
92 |
if (a>b)
|
insilmaril@0
|
93 |
return a;
|
insilmaril@0
|
94 |
return b;
|
insilmaril@0
|
95 |
}
|
insilmaril@0
|
96 |
|
insilmaril@0
|
97 |
int xmlObj::actindent=0; // make instance of actindent
|
insilmaril@0
|
98 |
|
insilmaril@0
|
99 |
xmlObj::xmlObj()
|
insilmaril@0
|
100 |
{
|
insilmaril@0
|
101 |
indentwidth=4;
|
insilmaril@0
|
102 |
}
|
insilmaril@0
|
103 |
|
insilmaril@0
|
104 |
|
insilmaril@0
|
105 |
// returns <s at />
|
insilmaril@0
|
106 |
QString xmlObj::singleElement(QString s, QString at)
|
insilmaril@0
|
107 |
{
|
insilmaril@0
|
108 |
return indent() + "<" + s +" " + at +" " + "/>\n";
|
insilmaril@0
|
109 |
}
|
insilmaril@0
|
110 |
|
insilmaril@0
|
111 |
// returns <s>
|
insilmaril@0
|
112 |
QString xmlObj::beginElement(QString s)
|
insilmaril@0
|
113 |
{
|
insilmaril@0
|
114 |
return indent() + "<" + s + ">\n";
|
insilmaril@0
|
115 |
}
|
insilmaril@0
|
116 |
|
insilmaril@0
|
117 |
// returns <s at>
|
insilmaril@0
|
118 |
QString xmlObj::beginElement(QString s, QString at)
|
insilmaril@0
|
119 |
{
|
insilmaril@0
|
120 |
return indent() + "<" + s + " " + at + ">\n";
|
insilmaril@0
|
121 |
}
|
insilmaril@0
|
122 |
|
insilmaril@0
|
123 |
// returns </s>
|
insilmaril@0
|
124 |
QString xmlObj::endElement(QString s)
|
insilmaril@0
|
125 |
{
|
insilmaril@0
|
126 |
return indent() + "</" + s + ">\n";
|
insilmaril@0
|
127 |
}
|
insilmaril@0
|
128 |
|
insilmaril@0
|
129 |
// returns at="val"
|
insilmaril@0
|
130 |
QString xmlObj::attribut(QString at, QString val)
|
insilmaril@0
|
131 |
{
|
insilmaril@0
|
132 |
return " " + at + "=\"" + quotemeta (val) + "\"";
|
insilmaril@0
|
133 |
}
|
insilmaril@0
|
134 |
|
insilmaril@0
|
135 |
// returns <s> val </s>
|
insilmaril@0
|
136 |
QString xmlObj::valueElement(QString el, QString val)
|
insilmaril@0
|
137 |
{
|
insilmaril@0
|
138 |
return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
|
insilmaril@0
|
139 |
}
|
insilmaril@0
|
140 |
|
insilmaril@0
|
141 |
// returns <s at> val </s>
|
insilmaril@0
|
142 |
QString xmlObj::valueElement(QString el, QString val, QString at)
|
insilmaril@0
|
143 |
{
|
insilmaril@0
|
144 |
return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
|
insilmaril@0
|
145 |
}
|
insilmaril@0
|
146 |
|
insilmaril@0
|
147 |
void xmlObj::incIndent()
|
insilmaril@0
|
148 |
{
|
insilmaril@0
|
149 |
actindent++;
|
insilmaril@0
|
150 |
}
|
insilmaril@0
|
151 |
|
insilmaril@0
|
152 |
void xmlObj::decIndent()
|
insilmaril@0
|
153 |
{
|
insilmaril@0
|
154 |
actindent--;
|
insilmaril@0
|
155 |
if (actindent<0) actindent=0;
|
insilmaril@0
|
156 |
}
|
insilmaril@0
|
157 |
|
insilmaril@0
|
158 |
QString xmlObj::indent()
|
insilmaril@0
|
159 |
{
|
insilmaril@0
|
160 |
QString s;
|
insilmaril@0
|
161 |
int i;
|
insilmaril@0
|
162 |
for (i=0; i<actindent*indentwidth; i++)
|
insilmaril@0
|
163 |
{
|
insilmaril@0
|
164 |
s= s + " ";
|
insilmaril@0
|
165 |
}
|
insilmaril@0
|
166 |
return s;
|
insilmaril@0
|
167 |
}
|
insilmaril@0
|
168 |
|