/* 
   Programmer:    Konstantin Lukin
   E-mail    :    lukink@ug.cs.sunysb.edu
*/ 


public class Bragg {

public static Point2D distanceLimit = new Point2D(.5f, 5f);
public static Point2D thetaLimit    = new Point2D(5f, 50f);
public static Point2D lambdaLimit   = new Point2D(.5f, 5f);

public static int GRAPH   = 0;
public static int FORMULA = 1;

public static float GraphToFormula = .05f; // 5/100
public static float FormulaToGraph = 20f;  // 100/5

private float lambda, distance, theta;
public final static int precision = 2;

public Bragg(int form, float Lambda, float Distance, float Theta) {  
   float c = (form==GRAPH) ? GraphToFormula : 1;
   lambda   = Lambda   * c;
   distance = Distance * c;
   theta    = Theta;
}
   

public static boolean DistanceOutOfRange(float distance, int form) {
  boolean b;
  float c = (form==GRAPH) ? GraphToFormula : 1;
  return distance * c < distanceLimit.x ||
         distance * c > distanceLimit.y;
}


public static boolean ThetaOutOfRange(float theta) { // in degrees
  return theta < thetaLimit.x || theta > thetaLimit.y;
}


public static boolean LambdaOutOfRange(float lambda, int form) {
   boolean b;
   float c = (form==GRAPH) ? GraphToFormula : 1;
   return lambda*c < lambdaLimit.x || lambda*c > lambdaLimit.y;
}


public float getDistance(int form) {
  float c = (form==GRAPH) ? FormulaToGraph : 1;
  return distance * c;
}


public float getTheta() {
  return theta;
}


public float getLambda(int form) {
  float c = (form==GRAPH) ? FormulaToGraph : 1;
  return lambda * c;
}


public void setDistance(float dist, int form) {
  float c = (form==GRAPH) ? GraphToFormula : 1;
  distance = dist * c;
}


public void setTheta(float thet) {
  theta = thet;
}


public void setLambda(float lam, int form) {
  float c = (form==GRAPH) ? GraphToFormula : 1;
  lambda = lam * c;
}


public boolean satisfied(int i) {   
   int right, left;
   right = Math.round((float)Math.pow(10, precision)*
           2*distance*MyMath.sin(MyMath.toRadians(theta)));
   left = Math.round(lambda * (float)Math.pow(10, precision));
   return (right%left==0);
}


public String toString()  {
   String s = new String();
   s = "" + MyMath.round(lambda, precision);
   if (satisfied(1)) s+= " = "; else s+= " != ";
   s+= "2 * " + MyMath.round(distance, precision) + " * ";
   s+= "sin(" + MyMath.round(theta, precision) + ")";
   return s;
}



}



