#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);
}
/**************************************************************************/