示例#1
0
文件: Rules.java 项目: bitsai/courses
  /** Returns angle in degrees from specified origin to specified destination. */
  public static int getAngle(Point origin, Point destination) // origin != destination
      {
    double distance = getDistance(origin, destination);
    int add = 0;
    int x = destination.getx() - origin.getx();
    int y = origin.gety() - destination.gety(); // Java flips things around

    double angleRad = Math.asin(Math.abs(y) / distance);
    double angleDeg = Math.toDegrees(angleRad);

    if ((x >= 0) && (y >= 0)) // Quadrant 1
    {
      angleDeg = angleDeg;
    }
    if ((x < 0) && (y > 0)) // Quadrant 2
    {
      angleDeg = 180 - angleDeg;
    }
    if ((x <= 0) && (y <= 0)) // Quadrant 3
    {
      angleDeg = 180 + angleDeg;
    }
    if ((x > 0) && (y < 0)) // Quadrant 4
    {
      angleDeg = 360 - angleDeg;
    }

    float angleFloat = Math.round(angleDeg);
    int angleInt = Math.round(angleFloat);

    return (angleInt);
  }
示例#2
0
 public Fraction(BigDecimal original) {
   super();
   double A, C, D, X, Y, M;
   C = original.doubleValue();
   A = C;
   D = C;
   X = 1;
   Y = 0;
   do {
     M = Y;
     Y = X + Y * Math.round(C);
     X = M;
     if (C != Math.round(C)) {
       C = 1 / (C - Math.round(C));
     }
     M = Math.round(D * Y);
   } while (M != MathUtilities.toFix(D * Y, 10));
   if ((Math.abs(M) > 100000) || (Math.abs(Y) > 100000) || (Math.abs(Y) == 1)) {
     this.numerator = original;
     this.denominator = BigInteger.ONE;
   } else {
     this.numerator =
         BigDecimal.valueOf(Math.round((Math.abs(M) / M) * (Math.abs(Y) / Y) * Math.abs(M)));
     this.denominator = new BigInteger(Long.toString(Math.round(Math.abs(Y))));
   }
 }
 // To add/remove functions change evaluateOperator() and registration
 public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException {
   switch (Character.toLowerCase(fncnam.charAt(0))) {
     case 'a':
       {
         if (fncnam.equalsIgnoreCase("abs")) {
           return Math.abs(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("acos")) {
           return Math.acos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("asin")) {
           return Math.asin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("atan")) {
           return Math.atan(fncargs.next());
         }
       }
       break;
     case 'c':
       {
         if (fncnam.equalsIgnoreCase("cbrt")) {
           return Math.cbrt(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("ceil")) {
           return Math.ceil(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cos")) {
           return Math.cos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cosh")) {
           return Math.cosh(fncargs.next());
         }
       }
       break;
     case 'e':
       {
         if (fncnam.equalsIgnoreCase("exp")) {
           return Math.exp(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("expm1")) {
           return Math.expm1(fncargs.next());
         }
       }
       break;
     case 'f':
       {
         if (fncnam.equalsIgnoreCase("floor")) {
           return Math.floor(fncargs.next());
         }
       }
       break;
     case 'g':
       {
         //              if(fncnam.equalsIgnoreCase("getExponent"   )) { return
         // Math.getExponent(fncargs.next());                } needs Java 6
       }
       break;
     case 'l':
       {
         if (fncnam.equalsIgnoreCase("log")) {
           return Math.log(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log10")) {
           return Math.log10(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log1p")) {
           return Math.log1p(fncargs.next());
         }
       }
       break;
     case 'm':
       {
         if (fncnam.equalsIgnoreCase("max")) {
           return Math.max(fncargs.next(), fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("min")) {
           return Math.min(fncargs.next(), fncargs.next());
         }
       }
       break;
     case 'n':
       {
         //              if(fncnam.equalsIgnoreCase("nextUp"        )) { return Math.nextUp
         // (fncargs.next());                } needs Java 6
       }
       break;
     case 'r':
       {
         if (fncnam.equalsIgnoreCase("random")) {
           return Math.random();
         } // impure
         if (fncnam.equalsIgnoreCase("round")) {
           return Math.round(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("roundHE")) {
           return Math.rint(fncargs.next());
         } // round half-even
       }
       break;
     case 's':
       {
         if (fncnam.equalsIgnoreCase("signum")) {
           return Math.signum(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sin")) {
           return Math.sin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sinh")) {
           return Math.sinh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sqrt")) {
           return Math.sqrt(fncargs.next());
         }
       }
       break;
     case 't':
       {
         if (fncnam.equalsIgnoreCase("tan")) {
           return Math.tan(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("tanh")) {
           return Math.tanh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toDegrees")) {
           return Math.toDegrees(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toRadians")) {
           return Math.toRadians(fncargs.next());
         }
       }
       break;
     case 'u':
       {
         if (fncnam.equalsIgnoreCase("ulp")) {
           return Math.ulp(fncargs.next());
         }
       }
       break;
       // no default
   }
   throw new UnsupportedOperationException(
       "MathEval internal function setup is incorrect - internal function \""
           + fncnam
           + "\" not handled");
 }