1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*****
* pair.h
* Andy Hammerlindl 2002/05/16
*
* Stores a two-dimensional point similar to the pair type in MetaPost.
* In some cases, pairs behave as complex numbers.
*
* A pair is a guide as a pair alone can be used to describe a path.
* The solve and subsolve methods are fairly straight forward as solve
* returns a path with just the pair and subsolve just adds the pair to
* the structure.
*****/
#ifndef PAIR_H
#define PAIR_H
#include <cassert>
#include <cmath>
#include <iostream>
#include <fstream>
#include "common.h"
#include "angle.h"
#ifdef HAVE_RPC_RPC_H
#include "xstream.h"
#endif
namespace camp {
class jsofstream : public std::ofstream {
public:
jsofstream() {}
jsofstream(const string& name) : std::ofstream(name.c_str()) {}
void open(const string& name) {std::ofstream::open(name.c_str());}
template<class T>
jsofstream& operator << (const T& x) {
(std::ofstream&)(*this) << x;
return *this;
}
};
class pair : public gc {
double x;
double y;
public:
pair() : x(0.0), y(0.0) {}
pair(double x, double y=0.0) : x(x), y(y) {}
double getx() const { return x; }
double gety() const { return y; }
bool isreal() {return y == 0;}
friend pair operator+ (const pair& z, const pair& w)
{
return pair(z.x+w.x,z.y+w.y);
}
friend pair operator- (const pair& z, const pair& w)
{
return pair(z.x-w.x,z.y-w.y);
}
friend pair operator- (const pair& z)
{
return pair(-z.x,-z.y);
}
// Complex multiplication
friend pair operator* (const pair& z, const pair& w)
{
return pair(z.x*w.x-z.y*w.y,z.x*w.y+w.x*z.y);
}
const pair& operator+= (const pair& w)
{
x += w.x;
y += w.y;
return *this;
}
const pair& operator-= (const pair& w)
{
x -= w.x;
y -= w.y;
return *this;
}
const pair& operator*= (const pair& w)
{
(*this) = (*this) * w;
return (*this);
}
const pair& operator/= (const pair& w)
{
(*this) = (*this) / w;
return (*this);
}
const pair& scale (double xscale, double yscale)
{
x *= xscale;
y *= yscale;
return *this;
}
friend pair operator/ (const pair &z, double t)
{
if (t == 0.0)
reportError("division by 0");
t=1.0/t;
return pair(z.x*t, z.y*t);
}
friend pair operator/ (const pair& z, const pair& w)
{
if (!w.nonZero())
reportError("division by pair (0,0)");
double t = 1.0 / (w.x*w.x + w.y*w.y);
return pair(t*(z.x*w.x + z.y*w.y),
t*(-z.x*w.y + w.x*z.y));
}
friend bool operator== (const pair& z, const pair& w)
{
return z.x == w.x && z.y == w.y;
}
friend bool operator!= (const pair& z, const pair& w)
{
return z.x != w.x || z.y != w.y;
}
double abs2() const
{
return x*x + y*y;
}
double length() const
{
return sqrt(abs2());
}
friend double length(const pair& z)
{
return z.length();
}
double angle(bool warn=true) const
{
return camp::angle(x,y,warn);
}
friend double angle(const pair& z, bool warn=true)
{
return z.angle(warn);
}
friend pair unit(const pair& z)
{
double scale=z.length();
if(scale == 0.0) return z;
scale=1.0/scale;
return pair(z.x*scale,z.y*scale);
}
friend pair conj(const pair& z)
{
return pair(z.x,-z.y);
}
friend double dot(const pair& z, const pair& w)
{
return z.x*w.x+z.y*w.y;
}
friend double cross(const pair& z, const pair& w)
{
return z.x*w.y-z.y*w.x;
}
// Return the principal branch of the square root (non-negative real part).
friend pair Sqrt(const pair& z) {
double mag=z.length();
if(mag == 0.0) return pair(0.0,0.0);
else if(z.x > 0) {
double re=sqrt(0.5*(mag+z.x));
return pair(re,0.5*z.y/re);
} else {
double im=sqrt(0.5*(mag-z.x));
if(z.y < 0) im=-im;
return pair(0.5*z.y/im,im);
}
}
bool nonZero() const
{
return x != 0.0 || y != 0.0;
}
friend istream& operator >> (istream& s, pair& z)
{
char c;
s >> ws;
bool paren=s.peek() == '('; // parenthesis are optional
if(paren) s >> c;
s >> z.x >> ws;
if(!s.eof() && s.peek() == ',') s >> c >> z.y;
else {
if(paren && !s.eof()) s >> z.y;
else z.y=0.0;
}
if(paren) {
s >> ws;
if(s.peek() == ')') s >> c;
}
return s;
}
friend ostream& operator << (ostream& out, const pair& z)
{
out << "(" << z.x << "," << z.y << ")";
return out;
}
friend jsofstream& operator << (jsofstream& out, const pair& z)
{
out << "[" << z.x << "," << z.y << "]";
return out;
}
#ifdef HAVE_RPC_RPC_H
friend xdr::oxstream& operator << (xdr::oxstream& out, pair const& v)
{
out << v.x << v.y;
return out;
}
#endif
friend class box;
};
// Calculates exp(i * theta), useful for unit vectors.
inline pair expi(double theta)
{
if(theta == 0.0) return pair(1.0,0.0); // Frequently occurring case
return pair(cos(theta),sin(theta));
}
// Complex exponentiation
inline pair pow(const pair& z, const pair& w)
{
double u=w.getx();
double v=w.gety();
if(z == 0.0) return w == 0.0 ? 1.0 : 0.0;
double logr=0.5*log(z.abs2());
double th=z.angle();
return exp(logr*u-th*v)*expi(logr*v+th*u);
}
} //namespace camp
GC_DECLARE_PTRFREE(camp::pair);
#endif
|