Beispiel #1
0
  protected Point2D getCrossPointWithSide(
      Point2D point,
      double rotationAngleInDegrees,
      double fieldWidth,
      double fieldHeight,
      GameState.SystemSides side) {
    double centerX = point.getX(), centerY = point.getY();

    double angle = (rotationAngleInDegrees + 360) % 360;

    if (angle == 90) {
      return new Point2D(centerX, fieldHeight);
    } else if (angle == 270) {
      return new Point2D(centerX, 0);
    }

    double a = Math.tan(Math.toRadians(angle));
    double b = centerY - a * centerX;

    switch (side) {
      case Right:
        return new Point2D(fieldWidth, a * fieldWidth + b);
      case Down:
        return new Point2D((fieldHeight - b) / a, fieldHeight);
      case Left:
        return new Point2D(0, b);
      case Up:
        return new Point2D(-b / a, 0);
      default:
        return new Point2D(0, 0);
    }
  }
  private void solve() throws IOException {
    StringTokenizer st = new StringTokenizer(in.readLine());
    int n = Integer.parseInt(st.nextToken());
    alpha = Integer.parseInt(st.nextToken());
    check((1 <= n) && (n <= 12), "n is not in [1..12]");
    check((0 <= alpha) && (alpha <= 89), "alpha is not in [0..89]");
    tanAlpha = Math.tan(alpha * Math.PI / 180.0);

    hilbert = new boolean[(1 << n) - 1][(1 << n) - 1];
    buildHilbert(n, 0, 0, 0, true);
    int top = (1 << n) - 2;
    for (int i = 0; i <= top; i++) {
      if (hilbert[i][top]) {
        process(i, top, 1, 1.0);
      }
    }
    out.println(volume);
  }
Beispiel #3
0
  public static AffineTransform getTransform(String str) throws IOException {
    AffineTransform t = new AffineTransform();

    if (str != null) {

      StreamTokenizer tt = new StreamTokenizer(new StringReader(str));
      tt.resetSyntax();
      tt.wordChars('a', 'z');
      tt.wordChars('A', 'Z');
      tt.wordChars(128 + 32, 255);
      tt.whitespaceChars(0, ' ');
      tt.whitespaceChars(',', ',');
      tt.parseNumbers();

      while (tt.nextToken() != StreamTokenizer.TT_EOF) {
        if (tt.ttype != StreamTokenizer.TT_WORD) {
          throw new IOException("Illegal transform " + str);
        }
        String type = tt.sval;
        if (tt.nextToken() != '(') {
          throw new IOException("'(' not found in transform " + str);
        }
        if (type.equals("matrix")) {
          double[] m = new double[6];
          for (int i = 0; i < 6; i++) {
            if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
              throw new IOException(
                  "Matrix value "
                      + i
                      + " not found in transform "
                      + str
                      + " token:"
                      + tt.ttype
                      + " "
                      + tt.sval);
            }
            if (tt.nextToken() == StreamTokenizer.TT_WORD && tt.sval.startsWith("E")) {
              double mantissa = tt.nval;
              tt.nval = Double.valueOf(tt.nval + tt.sval);
            } else {
              tt.pushBack();
            }
            m[i] = tt.nval;
          }
          t.concatenate(new AffineTransform(m));

        } else if (type.equals("translate")) {
          double tx, ty;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("X-translation value not found in transform " + str);
          }
          tx = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            ty = tt.nval;
          } else {
            tt.pushBack();
            ty = 0;
          }
          t.translate(tx, ty);

        } else if (type.equals("scale")) {
          double sx, sy;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("X-scale value not found in transform " + str);
          }
          sx = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            sy = tt.nval;
          } else {
            tt.pushBack();
            sy = sx;
          }
          t.scale(sx, sy);

        } else if (type.equals("rotate")) {
          double angle, cx, cy;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Angle value not found in transform " + str);
          }
          angle = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            cx = tt.nval;
            if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
              throw new IOException("Y-center value not found in transform " + str);
            }
            cy = tt.nval;
          } else {
            tt.pushBack();
            cx = cy = 0;
          }
          t.rotate(angle * Math.PI / 180d, cx * Math.PI / 180d, cy * Math.PI / 180d);

        } else if (type.equals("skewX")) {
          double angle;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Skew angle not found in transform " + str);
          }
          angle = tt.nval;
          t.concatenate(new AffineTransform(1, 0, Math.tan(angle * Math.PI / 180), 1, 0, 0));

        } else if (type.equals("skewY")) {
          double angle;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Skew angle not found in transform " + str);
          }
          angle = tt.nval;
          t.concatenate(new AffineTransform(1, Math.tan(angle * Math.PI / 180), 0, 1, 0, 0));

        } else {
          throw new IOException("Unknown transform " + type + " in " + str);
        }
        if (tt.nextToken() != ')') {
          throw new IOException("')' not found in transform " + str);
        }
      }
    }
    return t;
  }
 public double activation(double x) {
   // return (1/(1+Math.exp(-x)));
   return Math.tan(x);
 }
/**
 * A utility class to iterate over the path segments of an rounded rectangle through the
 * PathIterator interface.
 *
 * @version 10 Feb 1997
 * @author Jim Graham
 */
class RoundRectIterator implements PathIterator {
  double x, y, w, h, aw, ah;
  AffineTransform affine;
  int index;

  RoundRectIterator(RoundRectangle2D rr, AffineTransform at) {
    this.x = rr.getX();
    this.y = rr.getY();
    this.w = rr.getWidth();
    this.h = rr.getHeight();
    this.aw = Math.min(w, Math.abs(rr.getArcWidth()));
    this.ah = Math.min(h, Math.abs(rr.getArcHeight()));
    this.affine = at;
    if (aw < 0 || ah < 0) {
      // Don't draw anything...
      index = ctrlpts.length;
    }
  }

  /**
   * Return the winding rule for determining the insideness of the path.
   *
   * @see #WIND_EVEN_ODD
   * @see #WIND_NON_ZERO
   */
  public int getWindingRule() {
    return WIND_NON_ZERO;
  }

  /**
   * Tests if there are more points to read.
   *
   * @return true if there are more points to read
   */
  public boolean isDone() {
    return index >= ctrlpts.length;
  }

  /**
   * Moves the iterator to the next segment of the path forwards along the primary direction of
   * traversal as long as there are more points in that direction.
   */
  public void next() {
    index++;
  }

  private static final double angle = Math.PI / 4.0;
  private static final double a = 1.0 - Math.cos(angle);
  private static final double b = Math.tan(angle);
  private static final double c = Math.sqrt(1.0 + b * b) - 1 + a;
  private static final double cv = 4.0 / 3.0 * a * b / c;
  private static final double acv = (1.0 - cv) / 2.0;

  // For each array:
  //     4 values for each point {v0, v1, v2, v3}:
  //         point = (x + v0 * w + v1 * arcWidth,
  //                  y + v2 * h + v3 * arcHeight);
  private static double ctrlpts[][] = {
    {0.0, 0.0, 0.0, 0.5},
    {0.0, 0.0, 1.0, -0.5},
    {
      0.0, 0.0, 1.0, -acv,
      0.0, acv, 1.0, 0.0,
      0.0, 0.5, 1.0, 0.0
    },
    {1.0, -0.5, 1.0, 0.0},
    {
      1.0, -acv, 1.0, 0.0,
      1.0, 0.0, 1.0, -acv,
      1.0, 0.0, 1.0, -0.5
    },
    {1.0, 0.0, 0.0, 0.5},
    {
      1.0, 0.0, 0.0, acv,
      1.0, -acv, 0.0, 0.0,
      1.0, -0.5, 0.0, 0.0
    },
    {0.0, 0.5, 0.0, 0.0},
    {
      0.0, acv, 0.0, 0.0,
      0.0, 0.0, 0.0, acv,
      0.0, 0.0, 0.0, 0.5
    },
    {},
  };
  private static int types[] = {
    SEG_MOVETO,
    SEG_LINETO,
    SEG_CUBICTO,
    SEG_LINETO,
    SEG_CUBICTO,
    SEG_LINETO,
    SEG_CUBICTO,
    SEG_LINETO,
    SEG_CUBICTO,
    SEG_CLOSE,
  };

  /**
   * Returns the coordinates and type of the current path segment in the iteration. The return value
   * is the path segment type: SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE. A
   * float array of length 6 must be passed in and may be used to store the coordinates of the
   * point(s). Each point is stored as a pair of float x,y coordinates. SEG_MOVETO and SEG_LINETO
   * types will return one point, SEG_QUADTO will return two points, SEG_CUBICTO will return 3
   * points and SEG_CLOSE will not return any points.
   *
   * @see #SEG_MOVETO
   * @see #SEG_LINETO
   * @see #SEG_QUADTO
   * @see #SEG_CUBICTO
   * @see #SEG_CLOSE
   */
  public int currentSegment(float[] coords) {
    if (isDone()) {
      throw new NoSuchElementException("roundrect iterator out of bounds");
    }
    double ctrls[] = ctrlpts[index];
    int nc = 0;
    for (int i = 0; i < ctrls.length; i += 4) {
      coords[nc++] = (float) (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
      coords[nc++] = (float) (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
    }
    if (affine != null) {
      affine.transform(coords, 0, coords, 0, nc / 2);
    }
    return types[index];
  }

  /**
   * Returns the coordinates and type of the current path segment in the iteration. The return value
   * is the path segment type: SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE. A
   * double array of length 6 must be passed in and may be used to store the coordinates of the
   * point(s). Each point is stored as a pair of double x,y coordinates. SEG_MOVETO and SEG_LINETO
   * types will return one point, SEG_QUADTO will return two points, SEG_CUBICTO will return 3
   * points and SEG_CLOSE will not return any points.
   *
   * @see #SEG_MOVETO
   * @see #SEG_LINETO
   * @see #SEG_QUADTO
   * @see #SEG_CUBICTO
   * @see #SEG_CLOSE
   */
  public int currentSegment(double[] coords) {
    if (isDone()) {
      throw new NoSuchElementException("roundrect iterator out of bounds");
    }
    double ctrls[] = ctrlpts[index];
    int nc = 0;
    for (int i = 0; i < ctrls.length; i += 4) {
      coords[nc++] = (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
      coords[nc++] = (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
    }
    if (affine != null) {
      affine.transform(coords, 0, coords, 0, nc / 2);
    }
    return types[index];
  }
}
 // 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");
 }