-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2.cpp
308 lines (283 loc) · 9.54 KB
/
r2.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*============================================================================
* Daniel J. Greenhoe
* normed linear space R^2
*============================================================================*/
/*=====================================
* headers
*=====================================*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<main.h>
#include<r1.h>
#include<r2.h>
#include<r2op.h>
/*-------------------------------------------------------------------------
* list value of opair
*-------------------------------------------------------------------------*/
void opair::list(const char *str1, const char *str2, FILE *ptr){
if(strlen(str1)>0)fprintf(ptr,"%s",str1);
fprintf(ptr,"(%9.6lf,%9.6lf)",getx(),gety());
if(strlen(str2)>0)fprintf(ptr,"%s",str2);
}
/*-------------------------------------------------------------------------
* polar rotation coordinate <theta> of opair point (x,y)
* return value is in the half open interval [0:2pi)
* return -1 on error
*-------------------------------------------------------------------------*/
double vectR2::theta(void){
double x=getx();
double y=gety();
if(x==0){
if(y==0) return -1;
else if(y>0) return +PI/2;
else return 3*PI/2;
}
if(y==0){
if(x>0) return 0;
else return PI;
}
if(x>0&&y>0) return atan( y/x); // 1st quadrant
if(x>0&&y<0) return 2*PI-atan(-y/x); // 4th quadrant
if(x<0&&y>0) return PI-atan(-y/x); // 2nd quadrant
if(x<0&&y<0) return PI+atan( y/x); // 3rd quadrant
else return -1;
}
/*-------------------------------------------------------------------------
* operator: rotate (x,y) counter-clockwise by <phi> radians
*-------------------------------------------------------------------------*/
void vectR2::operator&=(double phi){
vectR2 p(getx(),gety());
p = p & phi;
put(p.getx(),p.gety());
}
/*=====================================
* seqR2
*=====================================*/
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to 0
*-------------------------------------------------------------------------*/
seqR2::seqR2(long M){
long n;
N=M;
xy = (vectR2 *)malloc(N*sizeof(vectR2));
for(n=0; n<N; n++)xy[n].clear();
}
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to <u>
*-------------------------------------------------------------------------*/
seqR2::seqR2(long M, double u){
long n;
N=M;
xy = (vectR2 *)malloc(N*sizeof(vectR2));
for(n=0; n<N; n++){
xy[n].put(u,u);
}
}
/*-------------------------------------------------------------------------
* fill the seqR1 with a value 0
*-------------------------------------------------------------------------*/
void seqR2::clear(void){
long n;
for(n=0; n<N; n++)xy[n].clear();
}
/*-------------------------------------------------------------------------
* fill the seqR1 with a value <u>
*-------------------------------------------------------------------------*/
void seqR2::fill(double u){
long n;
for(n=0; n<N; n++)xy[n].put(u,u);
}
/*-------------------------------------------------------------------------
* fill the seqR1 with (x_0, x_1, x_2, ...)
* where x_n = x_{n-1} + (dx,dy)
*-------------------------------------------------------------------------*/
void seqR2::inc(double x0, double y0,double dx, double dy){
long n;
for(n=0;n<N;n++){
xy[n].put(x0,y0);
x0+=dx;
y0+=dy;
}
}
/*-------------------------------------------------------------------------
* put a single value <u> into the seqR1 x at location n
*-------------------------------------------------------------------------*/
int seqR2::put(long n, double u, double v){
int retval=0;
if(n>=N){
fprintf(stderr,"n=%ld larger than seqR1 size N=%ld\n",n,N);
retval=-1;
}
else xy[n].put(u,v);
return retval;
}
int seqR2::put(long n, vectR2 xya){
int retval=0;
if(n>=N){
fprintf(stderr,"n=%ld larger than seqR1 size N=%ld\n",n,N);
retval=-1;
}
else xy[n]=xya;
return retval;
}
/*-------------------------------------------------------------------------
* get a single value from the seqR1 x at location n
*-------------------------------------------------------------------------*/
vectR2 seqR2::get(long n){
vectR2 xya(0,0);
if(n<N)xya=xy[n];
else fprintf(stderr,"n=%ld larger than seqR1 size N=%ld\n",n,N);
return xya;
}
/*-------------------------------------------------------------------------
* get a single value from the seqR1 x,y, or z at location n
*-------------------------------------------------------------------------*/
double seqR2::getx(long n){
double u=0;
if(n<N)u=xy[n].getx();
else fprintf(stderr,"n=%ld larger than x seqR1 size N=%ld\n",n,N);
return u;
}
double seqR2::gety(long n){
double u=0;
if(n<N)u=xy[n].gety();
else fprintf(stderr,"n=%ld larger than y seqR1 size N=%ld\n",n,N);
return u;
}
/*-------------------------------------------------------------------------
* list contents of sequence
*-------------------------------------------------------------------------*/
void seqR2::list(const long start, const long end, const char *str1, const char *str2, FILE *ptr){
long n,m;
vectR2 x;
if(strlen(str1)>0)fprintf(ptr,"%s",str1);
for(n=start,m=1; n<=end; n++,m++){
x=get(n);
fprintf(ptr," ");
x.list(ptr);
if(m%3==0)fprintf(ptr,"\n");
}
if(strlen(str2)>0)fprintf(ptr,"%s",str2);
}
/*-------------------------------------------------------------------------
* list contents of seqC1 using 1 digit per element
*-------------------------------------------------------------------------*/
void seqR2::list1(void){
long n,m;
for(n=0,m=1; n<N; n++,m++){
printf("(%2.0lf,%2.0lf) ",getx(n),gety(n));
if(m%5==0)printf("\n");
}
}
void seqR2::list1(long start, long end){
long n,m;
for(n=start,m=1; n<=end; n++,m++){
printf("(%2.0lf,%2.0lf) ",getx(n),gety(n));
if(m%50==0)printf("\n");
else if(m%10==0)printf(" ");
}
}
/*-------------------------------------------------------------------------
* return the largest pair of values in the seqR1 as measured by norm()
*-------------------------------------------------------------------------*/
double seqR2::norm(long n){
vectR2 xya=get(n);
return xya.norm();
}
/*-------------------------------------------------------------------------
* return the largest pair of values in the seqR1 as measured by norm()
*-------------------------------------------------------------------------*/
vectR2 seqR2::max(int verbose){
long n;
double maxnorm=0;
long maxn=0;
vectR2 maxpair;
for(n=0; n<N; n++)if(norm(n)>maxnorm){maxnorm=norm(n); maxn=n;}
maxpair=get(maxn);
if(verbose){
for(n=0; n<N; n++)
if(norm(n)>=(maxnorm*0.999))
printf("max=(%lf,%lf) at n=%ld\n",maxpair.getx(),maxpair.gety(),n);
}
return maxpair;
}
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* operator: return p+q
*-------------------------------------------------------------------------*/
vectR2 operator+(vectR2 p, vectR2 q){
double px=p.getx();
double py=p.gety();
double qx=q.getx();
double qy=q.gety();
vectR2 r(px+qx,py+qy);
return r;
}
/*-------------------------------------------------------------------------
* operator: return p-q
*-------------------------------------------------------------------------*/
vectR2 operator-(vectR2 p, vectR2 q){
double px=p.getx();
double py=p.gety();
double qx=q.getx();
double qy=q.gety();
vectR2 r(px-qx,py-qy);
return r;
}
/*-------------------------------------------------------------------------
* operator: return p*q
*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------
* operator: return -p
*-------------------------------------------------------------------------*/
vectR2 operator-(vectR2 p){
double px=p.getx();
double py=p.gety();
vectR2 q(-px,-py);
return q;
}
/*-------------------------------------------------------------------------
* operator: return <p> rotated counter-clockwise by <phi> radians
*-------------------------------------------------------------------------*/
vectR2 operator&(vectR2 p,double phi){
double c,s;
vectR2 q;
mat2x2 R; // rotation matrix
if(phi==0){c=1; s=0;}
else {c=cos(phi); s=sin(phi);}
R.put(c,-s,s,c);
q = R*p;
return q;
}
/*-------------------------------------------------------------------------
* return the angle theta in radians between the two vectors induced by
* the points <p> and <q> in the plane R^2
* on SUCCESS return theta in the closed interval [0:PI]
* on ERROR return negative value or exit with value EXIT_FAILURE
*-------------------------------------------------------------------------*/
double pqtheta(const vectR2 p, const vectR2 q){
const double rp=p.mag(), rq=q.mag();
double y,theta;
if(rp==0) return -1;
if(rq==0) return -2;
y = (p^q)/(rp*rq);
if(y>+1.0) {
if(y-1<0.0000000000001)y=1.0;
else{
fprintf(stderr,"\nERROR using pqtheta(vectR2 p, vectR2 q): (p^q)/(rp*rq)=%.12lf>+1\n",y);
exit(EXIT_FAILURE);
}
}
if(y<-1.0)
if(y+1>-0.0000000000001)y=-1.0;
else{
fprintf(stderr,"\nERROR using pqtheta(vectR2 p, vectR2 q): (p^q)/(rp*rq)=%.12lf<-1\n",y);
exit(EXIT_FAILURE);
}
theta = acos(y);
return theta;
}