[go: up one dir, main page]

Menu

[r377]: / gr_scat / src / disc.cc  Maximize  Restore  History

Download this file

145 lines (119 with data), 3.9 kB

  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
#include <math.h>
#include "disc.h"
/*This include file contains the object definitions for a simulation of the three-disc scattering
system*/
/************************************************************************/
disc::disc() {
rad=0;
cx=0;
cy=0;
}
disc::disc(float radius, float centerx, float centery) {
rad=radius;
cx=centerx;
cy=centery;
}
void disc::init(float radius, float centerx, float centery) {
rad=radius;
cx=centerx;
cy=centery;
}
/*______________________________________________________________________*/
void disc::intercept(float begx, float begy, float dirx, float diry,
float *t1, float *t2)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, find the intercept points between that line and the
circle, and return in the form of the parameters of the line, t1 & t2.
Returns -1 for each parameter if there are no intercepts*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float A; /* " */
float c;
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
A=dirx*dirx+diry*diry;
c=dirx*Y-diry*X;
discriminant=-c*c+rad*rad*A;
if (discriminant >= 0) {
*t1=(-B+sqrt(discriminant))/A;
*t2=(-B-sqrt(discriminant))/A;
} else {
*t1=-1;
*t2=-1;
}
}
/*______________________________________________________________________*/
int disc::int_near(float begx, float begy, float dirx, float diry, float *t)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, returns the closest intercept in the form of the
parameter of the line, t.
Returns -1 for each parameter if there are no intercepts*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float A; /* " */
float c;
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
A=(dirx*dirx+diry*diry);
c=dirx*Y-diry*X;
discriminant=-c*c+rad*rad*A;
//discriminant=-pow(dirx*Y-diry*X,2)+pow(dirx*rad,2)+pow(diry*rad,2);
if (discriminant >= 0) {
*t=(-B-sqrt(discriminant))/A;
return 1;
} else {
*t=-1;
return 0;
}
}
/*______________________________________________________________________*/
int disc::int_far(float begx, float begy, float dirx, float diry, float *t)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, returns the farthest intercept in the form of the
parameter of the line, t.
/*Returns 0, *t=-1 if there are no intercepts 1 if there are*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float A; /* " */
float c;
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
A=(dirx*dirx+diry*diry);
c=dirx*Y-diry*X;
discriminant=-c*c+rad*rad*A;
//discriminant=-pow(dirx*Y-diry*X,2)+pow(dirx*rad,2)+pow(diry*rad,2);
if (discriminant >= 0) {
*t=(-B+sqrt(discriminant))/A;
return 1;
} else {
*t=-1;
return 0;
}
}
/*______________________________________________________________________*/
void disc::bounce(float interceptx, float intercepty, float dirx, float diry,
float *reflectxhat, float *reflectyhat) {
float normx, normy; /*x,y coords of normal to radius vector*/
float reflectlength; /*projection of direction vector on normal*/
normx=intercepty-cy;
normy=cx-interceptx;
reflectlength=(normx*dirx+normy*diry)/(normx*normx+normy*normy);
*reflectxhat=-dirx+2*reflectlength*normx;
*reflectyhat=-diry+2*reflectlength*normy;
}
void disc::plot() {
// con.disc(cx,cy,rad);
}
/**************************************************************************/