Ejemplo n.º 1
0
  /**
   * Uses the Haversine formula to calculate the distnace between to lat-long coordinates
   *
   * @param latitude1 The first point's latitude
   * @param longitude1 The first point's longitude
   * @param latitude2 The second point's latitude
   * @param longitude2 The second point's longitude
   * @return The distance between the two points in meters
   */
  public static double CalculateDistance(
      double latitude1, double longitude1, double latitude2, double longitude2) {
    /*
    Haversine formula:
    A = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
    C = 2.atan2(√a, √(1−a))
    D = R.c
    R = radius of earth, 6371 km.
    All angles are in radians
    */

    double deltaLatitude = Math.toRadians(Math.abs(latitude1 - latitude2));
    double deltaLongitude = Math.toRadians(Math.abs(longitude1 - longitude2));
    double latitude1Rad = Math.toRadians(latitude1);
    double latitude2Rad = Math.toRadians(latitude2);

    double a =
        Math.pow(Math.sin(deltaLatitude / 2), 2)
            + (Math.cos(latitude1Rad)
                * Math.cos(latitude2Rad)
                * Math.pow(Math.sin(deltaLongitude / 2), 2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return 6371 * c * 1000; // Distance in meters
  }
Ejemplo n.º 2
0
  /* CALCULATE COORDINATES: Determine new x-y coords given a start x-y and
  a distance and direction */
  public static void calcCoords(int index, int x, int y, double dist, double dirn) {
    while (dirn < 0.0) dirn = 360.0 + dirn;
    while (dirn > 360.0) dirn = dirn - 360.0;
    // System.out.println("dirn = " + dirn);

    // North-East
    if (dirn <= 90.0) {
      xValues[index] = x + (int) (Math.sin(Math.toRadians(dirn)) * dist);
      yValues[index] = y - (int) (Math.cos(Math.toRadians(dirn)) * dist);
      return;
    }
    // South-East
    if (dirn <= 180.0) {
      xValues[index] = x + (int) (Math.cos(Math.toRadians(dirn - 90)) * dist);
      yValues[index] = y + (int) (Math.sin(Math.toRadians(dirn - 90)) * dist);
      return;
    }
    // South-West
    if (dirn <= 90.0) {
      xValues[index] = x - (int) (Math.sin(Math.toRadians(dirn - 180)) * dist);
      yValues[index] = y + (int) (Math.cos(Math.toRadians(dirn - 180)) * dist);
    }
    // Nort-West
    else {
      xValues[index] = x - (int) (Math.cos(Math.toRadians(dirn - 270)) * dist);
      yValues[index] = y - (int) (Math.sin(Math.toRadians(dirn - 270)) * dist);
    }
  }
Ejemplo n.º 3
0
  private void renderBlackout(Graphics g, int x, int y, int radius) {
    if (radius > 320) return;

    int[] xp = new int[20];
    int[] yp = new int[20];
    for (int i = 0; i < 16; i++) {
      xp[i] = x + (int) (Math.cos(i * Math.PI / 15) * radius);
      yp[i] = y + (int) (Math.sin(i * Math.PI / 15) * radius);
    }
    xp[16] = 320;
    yp[16] = y;
    xp[17] = 320;
    yp[17] = 240;
    xp[18] = 0;
    yp[18] = 240;
    xp[19] = 0;
    yp[19] = y;
    g.fillPolygon(xp, yp, xp.length);

    for (int i = 0; i < 16; i++) {
      xp[i] = x - (int) (Math.cos(i * Math.PI / 15) * radius);
      yp[i] = y - (int) (Math.sin(i * Math.PI / 15) * radius);
    }
    xp[16] = 320;
    yp[16] = y;
    xp[17] = 320;
    yp[17] = 0;
    xp[18] = 0;
    yp[18] = 0;
    xp[19] = 0;
    yp[19] = y;

    g.fillPolygon(xp, yp, xp.length);
  }
Ejemplo n.º 4
0
  double score(IGMap map, DoubleOrientedPoint p, double[] readings) {
    double s = 0;
    int angleIndex = initialBeamsSkip;
    DoubleOrientedPoint lp = new DoubleOrientedPoint(p.x, p.y, p.theta);
    lp.x += Math.cos(p.theta) * laserPose.x - Math.sin(p.theta) * laserPose.y;
    lp.y += Math.sin(p.theta) * laserPose.x + Math.cos(p.theta) * laserPose.y;
    lp.theta += laserPose.theta;
    int skip = 0;
    double freeDelta = map.getDelta() * freeCellRatio;
    for (int rIndex = initialBeamsSkip; rIndex < readings.length; rIndex++, angleIndex++) {
      skip++;
      skip = skip > likelihoodSkip ? 0 : skip;
      if (skip != 0 || readings[rIndex] > usableRange || readings[rIndex] == 0.0) continue;
      DoublePoint phit = new DoublePoint(lp.x, lp.y);
      phit.x += readings[rIndex] * Math.cos(Utils.theta(lp.theta + laserAngles[angleIndex]));
      phit.y += readings[rIndex] * Math.sin(Utils.theta(lp.theta + laserAngles[angleIndex]));
      IntPoint iphit = map.world2map(phit);
      DoublePoint pfree = new DoublePoint(lp.x, lp.y);
      pfree.x +=
          (readings[rIndex] - map.getDelta() * freeDelta)
              * Math.cos(Utils.theta(lp.theta + laserAngles[angleIndex]));
      pfree.y +=
          (readings[rIndex] - map.getDelta() * freeDelta)
              * Math.sin(Utils.theta(lp.theta + laserAngles[angleIndex]));
      pfree.x = pfree.x - phit.x;
      pfree.y = pfree.y - phit.y;

      IntPoint ipfree = map.world2map(pfree);
      boolean found = false;
      DoublePoint bestMu = new DoublePoint(0., 0.);
      for (int xx = -kernelSize; xx <= kernelSize; xx++) {
        for (int yy = -kernelSize; yy <= kernelSize; yy++) {
          IntPoint pr = new IntPoint(iphit.x + xx, iphit.y + yy);
          IntPoint pf = new IntPoint(pr.x + ipfree.x, pr.y + ipfree.y);
          // int ss = map.getStorage().cellState(pr);
          //                    if ((ss) > 0) {
          PointAccumulator cell = (PointAccumulator) map.cell(pr, true);
          PointAccumulator fcell = (PointAccumulator) map.cell(pf, true);
          if (cell != null && fcell != null) {
            if (cell.doubleValue() > fullnessThreshold && fcell.doubleValue() < fullnessThreshold) {
              DoublePoint mu = DoublePoint.minus(phit, cell.mean());
              if (!found) {
                bestMu = mu;
                found = true;
              } else {
                bestMu = DoublePoint.mulD(mu, mu) < DoublePoint.mulD(bestMu, bestMu) ? mu : bestMu;
              }
            }
          }
          //                    }
        }
      }
      if (found) {
        s += Math.exp(-1. / (gaussianSigma * DoublePoint.mulD(bestMu, bestMu)));
      }
    }
    return s;
  }
Ejemplo n.º 5
0
 public void run() {
   while (running) {
     if (!paused) {
       if (mode == 0) {
         for (double i = 0; i < 3.14f; i = i + 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
         for (double i = 3.14f; i >= 0; i = i - 0.1f) {
           for (int j = 0; j < 12; j++) {
             double osc = Math.sin(i) * 127;
             int value = (int) osc;
             MIDILight(j, value);
           }
           delay(30);
         }
       } else if (mode == 1) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 127);
         }
         mode = 1000;
       } else if (mode == 2) {
         for (int j = 0; j < 12; j++) {
           MIDILight(j, 0);
         }
         mode = 1000;
       } else if (mode == 3) {
         int controller = (int) random(12);
         int randomValue = (int) random(127);
         MIDILight(controller, randomValue);
         delay(30);
       } else if (mode == 4) {
         for (int i = 0; i < 64; i++) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
         for (int i = 63; i >= 0; i--) {
           for (int j = 0; j < 12; j++) {
             MIDILight(j, i * 2);
           }
           delay(20);
         }
       } else {
         delay(10);
         // FIX THIS. WITHOUT THIS THE CPU USE GOES THROUGH THE ROOF. NEED A WAY
         // TO SLEEP THIS THREAD WHEN IT'S NOT IN USE.
       }
     }
   }
   System.out.println(id + " thread is done!");
 }
Ejemplo n.º 6
0
 public static double distance(double lat1, double lon1, double lat2, double lon2) {
   double theta = lon1 - lon2;
   double dist =
       Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
           + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
   dist = Math.acos(dist);
   dist = rad2deg(dist);
   dist = dist * 60 * 1.85315962;
   return (dist);
 }
Ejemplo n.º 7
0
 public void nudge(int i) {
   x[i] += (double) rand.nextInt(1000) / 8756;
   y[i] += (double) rand.nextInt(1000) / 5432;
   int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
   scale[i] = (double) tmpScale / 10;
   int nudgeX = (int) (((double) getWidth() / 2) * .8);
   int nudgeY = (int) (((double) getHeight() / 2) * .60);
   xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
   yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
 }
Ejemplo n.º 8
0
  /*======== public void generateSphere() ==========
      Inputs:   double cx
                double cy
  double r
  double step
      Returns:

      Generates all the points along the surface of a
      sphere with center (cx, cy) and radius r

      Adds these points to the matrix parameter

      jonalf
      ====================*/
  public void generateSphere(double cx, double cy, double r, double step) {
    double f, t;
    for (f = 0; f < 1; f += step) {
      for (t = 0; t < 1; t += step) {
        double x = r * Math.cos(Math.PI * 2 * t) + cx;
        double y = r * Math.sin(Math.PI * 2 * t) * Math.cos(Math.PI * f) + cy;
        double z = r * Math.sin(Math.PI * 2 * t) * Math.sin(Math.PI * f);
        addPoint(x, y, z);
      }
    }
  }
Ejemplo n.º 9
0
  /*======== public void generateTorus() ==========
      Inputs:   double cx
                double cy
  double r1
  double r2
  double step
      Returns:

      Generates all the points along the surface of a
      tarus with center (cx, cy) and radii r1 and r2

      Adds these points to the matrix parameter

      jonalf
      ====================*/
  public void generateTorus(double cx, double cy, double r1, double r2, double step) {
    double f, t;
    for (f = 0; f < 1; f += step) {
      for (t = 0; t < 1; t += step) {
        double x = Math.cos(Math.PI * 2 * f) * (r2 * Math.cos(Math.PI * 2 * t) + r1) + cx;
        double y = r2 * Math.sin(Math.PI * 2 * t) + cy;
        double z = -1 * Math.sin(Math.PI * 2 * f) * (r2 * Math.cos(Math.PI * 2 * t) + r1);
        addPoint(x, y, z);
      }
    }
  }
Ejemplo n.º 10
0
 static double gcDistance(double pLat, double pLong, double qLat, double qLong, double radius) {
   pLat *= Math.PI / 180;
   pLong *= Math.PI / 180;
   qLat *= Math.PI / 180;
   qLong *= Math.PI / 180;
   return radius
       * Math.acos(
           Math.cos(pLat) * Math.cos(pLong) * Math.cos(qLat) * Math.cos(qLong)
               + Math.cos(pLat) * Math.sin(pLong) * Math.cos(qLat) * Math.sin(qLong)
               + Math.sin(pLat) * Math.sin(qLat));
 }
Ejemplo n.º 11
0
 static long gcDistance(double pLat, double pLong, double qLat, double qLong, double radius) {
   pLat *= PI / 180;
   pLong *= PI / 180;
   qLat *= PI / 180;
   qLong *= PI / 180;
   return Math.round(
       radius
           * Math.acos(
               Math.cos(pLat) * Math.cos(pLong) * Math.cos(qLat) * Math.cos(qLong)
                   + Math.cos(pLat) * Math.sin(pLong) * Math.cos(qLat) * Math.sin(qLong)
                   + Math.sin(pLat) * Math.sin(qLat)));
 }
  public void move() {
    if (last_move_time == null || last_move_time.doubleValue() < world.time) {
      last_move_time = new Double(world.time);
      double max_dist, dist_right, dist_left, theta, x, y, dist_diff;
      double delta_theta, turn_radius, new_theta, new_x, new_y;
      Location location;
      Orientation orientation;

      orientation = orientation();
      location = location();

      max_dist = max_speed / world.ticks_per_second;
      dist_right = right_motor.output() * max_dist;
      dist_left = left_motor.output() * max_dist;
      theta = orientation.theta;
      x = location.x;
      y = location.y;
      old_location.x = x;
      old_location.y = y;
      dist_diff = dist_right - dist_left;

      //			System.out.println("dist_diff: " + dist_diff);

      delta_theta = dist_diff / wheel_base;
      if (Math.abs(dist_diff) < .0001) {
        turn_radius = 0.0;
      } else {
        turn_radius = (dist_right / delta_theta) - (wheel_base / 2);
      }

      //			System.out.println("turn_radius: " + turn_radius);

      new_theta = theta + delta_theta;
      if (turn_radius == 0.0) {

        //				System.out.println("turn_radius == 0");

        new_x = x + Math.cos(theta) * dist_left;
        new_y = y + Math.sin(theta) * dist_left;
      } else {

        //				System.out.println("new_theta= " + new_theta + " theta= " + theta);

        new_x = x + ((Math.sin(new_theta) - Math.sin(theta)) * turn_radius);
        new_y = y - ((Math.cos(new_theta) - Math.cos(theta)) * turn_radius);
      }
      orientation.theta = new_theta;
      location.x = new_x;
      location.y = new_y;

      maybe_fire_guns();
    }
  }
Ejemplo n.º 13
0
  /**
   * Rotate the oriented bounding box of the 3D image about the specified axis with the specified
   * angle.
   *
   * @param transform The transform and its matrix by which to rotate the image.
   */
  public void rotateFrameBy(Transform3D transform) {

    double rY, rX, rZ;
    double sinrX, sinrY, sinrZ, cosrX, cosrY, cosrZ;
    Matrix3d matrix = new Matrix3d();

    transform.get(matrix);

    rY = -Math.asin(matrix.m02);

    if (Math.cos(rY) != 0) {
      rX = -Math.atan2(matrix.m12, matrix.m22);
      rZ = Math.atan2(matrix.m01, matrix.m00);
    } else {
      rX = -Math.atan2(matrix.m10, matrix.m11);
      rZ = 0;
    }

    cosrX = Math.cos(rX);
    sinrX = Math.sin(rX);
    cosrY = Math.cos(rY);
    sinrY = Math.sin(rY);
    cosrZ = Math.cos(rZ);
    sinrZ = Math.sin(rZ);

    matrix.m00 = cosrZ * cosrY;
    matrix.m01 = -sinrZ * cosrY;
    matrix.m02 = sinrY;

    matrix.m10 = (cosrZ * sinrY * sinrX) + (sinrZ * cosrX);
    matrix.m11 = (-sinrZ * sinrY * sinrX) + (cosrZ * cosrX);
    matrix.m12 = -cosrY * sinrX;

    matrix.m20 = (-cosrZ * sinrY * cosrX) + (sinrZ * sinrX);
    matrix.m21 = (sinrZ * sinrY * cosrX) + (cosrZ * sinrX);
    matrix.m22 = cosrY * cosrX;

    m_kRotate.set(matrix);
    m_akAxis[0] = new Vector3f(1.0f, 0.0f, 0.0f);
    m_akAxis[1] = new Vector3f(0.0f, 1.0f, 0.0f);
    m_akAxis[2] = new Vector3f(0.0f, 0.0f, 1.0f);

    for (int i = 0; i < 3; i++) {
      m_kRotate.transform(m_akAxis[i]);
    }

    orthonormalize(m_akAxis);

    for (int i = 0; i < 3; i++) {
      setAxis(i, m_akAxis[i]);
    }
  }
Ejemplo n.º 14
0
    void draw(Graphics2D g) {

      // toX/toY is tip of arrow and fx/fy is a point on the line -
      // fx/fy is used to determine direction & angle

      AffineTransform at = AffineTransform.getTranslateInstance(toX, toY);
      int b = 9;
      double theta = Math.toRadians(20);
      // The idea of using a GeneralPath is so we can
      // create the (three lines that make up the) arrow
      // (only) one time and then use AffineTransform to
      // place it anywhere we want.
      GeneralPath path = new GeneralPath();

      // distance between line and the arrow mark <** not **
      // Start a new line segment from the position of (0,0).
      path.moveTo(0, 0);
      // Create one of the two arrow head lines.
      int x = (int) (-b * Math.cos(theta));
      int y = (int) (b * Math.sin(theta));
      path.lineTo(x, y);

      // distance between line and the arrow mark <** not **
      // Make the other arrow head line.
      int x2 = (int) (-b * Math.cos(-theta));
      int y2 = (int) (b * Math.sin(-theta));
      // path.moveTo(0,0);
      path.lineTo(x2, y2);
      path.closePath();

      // theta is in radians
      double s, t;
      s = toY - fy; // calculate slopes.
      t = toX - fx;
      if (t != 0) {
        s = s / t;
        theta = Math.atan(s);
        if (t < 0) theta += Math.PI;
      } else if (s < 0) theta = -(Math.PI / 2);
      else theta = Math.PI / 2;

      at.rotate(theta);
      // at.rotate(theta,toX,toY);
      Shape shape = at.createTransformedShape(path);
      if (checkStatus == Status.UNCHECKED) g.setColor(Color.BLACK);
      else if (checkStatus == Status.COMPATIBLE) g.setColor(FOREST_GREEN);
      else g.setColor(ORANGE_RED);
      g.fill(shape);
      g.draw(shape);
    }
Ejemplo n.º 15
0
 /**
  * @author Bogdan Sliptsov
  *     <p>Calculates the distance in km between two lat/long points using the haversine formula
  */
 public double distanceGPS(double lat1, double lng1, double lat2, double lng2) {
   int r = 6371; // average radius of the Earth in km
   double dLat = Math.toRadians(lat2 - lat1);
   double dLon = Math.toRadians(lng2 - lng1);
   double a =
       Math.sin(dLat / 2) * Math.sin(dLat / 2)
           + Math.cos(Math.toRadians(lat1))
               * Math.cos(Math.toRadians(lat2))
               * Math.sin(dLon / 2)
               * Math.sin(dLon / 2);
   double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
   double d = r * c;
   return d;
 }
Ejemplo n.º 16
0
  void drawingCommands() {
    // Rotation about z:
    double theta = Math.PI / 3.0;
    double a11 = Math.cos(theta);
    double a12 = -Math.sin(theta);
    double a21 = Math.sin(theta);
    double a22 = Math.cos(theta);
    double[][] A = {
      {a11, a12, 0, 0},
      {a21, a22, 0, 0},
      {0, 0, 1, 0},
      {0, 0, 0, 1}
    };

    /*
    // INSERT YOUR CODE for rotation about x by 30 degrees
    double[][] B = {

    };

    // INSERT YOUR CODE for translation by (2,3,4)
    double[][] C = {

    };

    double[][] temp = MatrixTool.matrixMult (B,A);
    double[][] transform = MatrixTool.matrixMult (C,temp);
    */

    // Replace this transform with the above after implementing
    // matrices B and C
    double[][] transform = A;

    double x0 = 0, y0 = 0; // Center of original 2D ellipse.
    double a = 7; // Major axis
    double b = 4; // Minor axis
    double delT = 0.25; // t-increment for drawing

    d3.setDrawColor(Color.RED);
    for (double t = 0; t <= 2 * Math.PI + delT; t += delT) {
      double[] x = new double[4];
      x[0] = a * Math.cos(t);
      x[1] = b * Math.sin(t);
      x[2] = 0;
      x[3] = 1;
      double[] z = MatrixTool.matrixVectorMult(transform, x);
      d3.drawPoint(z[0], z[1], z[2]);
    }
  }
Ejemplo n.º 17
0
  void act() {
    if (crashed) return;

    if (missionBuffer == 0) setMissions();

    playerZ += SPEED * Math.cos(turn);
    playerX += SPEED * Math.sin(turn);

    if (!region(1).contains(playerX, playerZ)) {
      for (int i = 0; i < NUM_TURNS - 1; i++) centerHinges[i] = centerHinges[i + 1];
      createTurn(NUM_TURNS - 1);

      if (!region(1).contains(playerX, playerZ)) crash();

      turnIndex++;
      if (missionBuffer > 0) missionBuffer--;
      if (barrierBuffer > 0) barrierBuffer--;
    }
    if (playerY + 100 >= centerHinges[3].barrierY
        && playerY + 100 <= centerHinges[3].barrierY + BARRIER_HEIGHT) {
      crash();
    }

    if (up && playerY > START_Y + 50) playerY--;
    if (down && playerY < START_Y + HEIGHT - 150) playerY++;
    if (left) turn -= .005;
    if (right) turn += .005;
  }
Ejemplo n.º 18
0
  /**
   * Draws an open star with a specified number of points.<br>
   * The center of this star is specified by centerX,centerY and its size is specified by radius
   * <br>
   * (As in the radius of the circle the star would fit inside). <br>
   * Precondition: points >= 2 <br>
   * Example: <br>
   * Expo.drawStar(g,300,200,100,8); <br>
   * Draws an open star with 8 points and a radius of 100 pixels whose center is located at the
   * coordinate (300,200).
   */
  public static void drawStar(Graphics g, int centerX, int centerY, int radius, int points) {
    int halfRadius = getHalfRadius(radius, points);
    int p = points;
    points *= 2;

    int xCoord[] = new int[points];
    int yCoord[] = new int[points];

    int currentRadius;

    for (int k = 0; k < points; k++) {
      if (k % 2 == 0) currentRadius = radius;
      else currentRadius = halfRadius;

      xCoord[k] = (int) Math.round(Math.cos(twoPI * k / points - halfPI) * currentRadius) + centerX;
      yCoord[k] = (int) Math.round(Math.sin(twoPI * k / points - halfPI) * currentRadius) + centerY;
    }

    int x = (p - 5) / 2 + 1;
    if (p >= 5 && p <= 51)
      switch (p % 4) {
        case 1:
          yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x];
          break;
        case 2:
          yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x];
          yCoord[x + 3] = yCoord[x + 4] = yCoord[points - x - 4] = yCoord[points - x - 3];
          break;
        case 3:
          yCoord[x + 2] = yCoord[x + 3] = yCoord[points - x - 3] = yCoord[points - x - 2];
      }
    g.drawPolygon(xCoord, yCoord, points);
  }
Ejemplo n.º 19
0
 public Object getData(final WModelIndex index, int role) {
   if (role != ItemDataRole.DisplayRole) {
     return super.getData(index, role);
   }
   double delta_y = (this.yEnd_ - this.yStart_) / (this.getColumnCount() - 2);
   if (index.getRow() == 0) {
     if (index.getColumn() == 0) {
       return 0.0;
     }
     return this.yStart_ + (index.getColumn() - 1) * delta_y;
   }
   double delta_x = (this.xEnd_ - this.xStart_) / (this.getRowCount() - 2);
   if (index.getColumn() == 0) {
     if (index.getRow() == 0) {
       return 0.0;
     }
     return this.xStart_ + (index.getRow() - 1) * delta_x;
   }
   double x;
   double y;
   y = this.yStart_ + (index.getColumn() - 1) * delta_y;
   x = this.xStart_ + (index.getRow() - 1) * delta_x;
   return 4
       * Math.sin(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)))
       / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
 }
Ejemplo n.º 20
0
 /**
  * Fisher distribution
  *
  * @param f Fisher value
  * @param n1 N1 value
  * @param n2 N2 value
  * @return P-value associated
  */
 private static double FishF(double f, int n1, int n2) {
   double x = n2 / (n1 * f + n2);
   if ((n1 % 2) == 0) {
     return StatCom(1 - x, n2, n1 + n2 - 4, n2 - 2) * Math.pow(x, n2 / 2.0);
   }
   if ((n2 % 2) == 0) {
     return 1 - StatCom(x, n1, n1 + n2 - 4, n1 - 2) * Math.pow(1 - x, n1 / 2.0);
   }
   double th = Math.atan(Math.sqrt(n1 * f / (1.0 * n2)));
   double a = th / (Math.PI / 2.0);
   double sth = Math.sin(th);
   double cth = Math.cos(th);
   if (n2 > 1) {
     a = a + sth * cth * StatCom(cth * cth, 2, n2 - 3, -1) / (Math.PI / 2.0);
   }
   if (n1 == 1) {
     return 1 - a;
   }
   double c =
       4 * StatCom(sth * sth, n2 + 1, n1 + n2 - 4, n2 - 2) * sth * Math.pow(cth, n2) / Math.PI;
   if (n2 == 1) {
     return 1 - a + c / 2.0;
   }
   int k = 2;
   while (k <= (n2 - 1) / 2.0) {
     c = c * k / (k - .5);
     k = k + 1;
   }
   return 1 - a + c;
 }
Ejemplo n.º 21
0
  /**
   * Return a midpoint of a helix, calculated from three positions of three adjacent subunit
   * centers.
   *
   * @param p1 center of first subunit
   * @param p2 center of second subunit
   * @param p3 center of third subunit
   * @return midpoint of helix
   */
  private Point3d getMidPoint(Point3d p1, Point3d p2, Point3d p3) {
    Vector3d v1 = new Vector3d();
    v1.sub(p1, p2);
    Vector3d v2 = new Vector3d();
    v2.sub(p3, p2);
    Vector3d v3 = new Vector3d();
    v3.add(v1);
    v3.add(v2);
    v3.normalize();

    // calculat the total distance between to subunits
    double dTotal = v1.length();
    // calculate the rise along the y-axis. The helix axis is aligned with y-axis,
    // therfore, the rise between subunits is the y-distance
    double rise = p2.y - p1.y;
    // use phythagorean theoremm to calculate chord length between two subunit centers
    double chord = Math.sqrt(dTotal * dTotal - rise * rise);
    //		System.out.println("Chord d: " + dTotal + " rise: " + rise + "chord: " + chord);
    double angle = helixLayers.getByLargestContacts().getAxisAngle().angle;

    // using the axis angle and the chord length, we can calculate the radius of the helix
    // http://en.wikipedia.org/wiki/Chord_%28geometry%29
    double radius = chord / Math.sin(angle / 2) / 2; // can this go to zero?
    //		System.out.println("Radius: " + radius);

    // project the radius onto the vector that points toward the helix axis
    v3.scale(radius);
    v3.add(p2);
    //		System.out.println("Angle: " +
    // Math.toDegrees(helixLayers.getByLowestAngle().getAxisAngle().angle));
    Point3d cor = new Point3d(v3);
    return cor;
  }
Ejemplo n.º 22
0
 /**
  * Create a note (sine wave) of the given frequency (Hz), for the given duration (seconds) scaled
  * to the given volume (amplitude).
  */
 public static double[] note(double hz, double duration, double amplitude) {
   int N = (int) (StdAudio.SAMPLE_RATE * duration);
   double[] a = new double[N + 1];
   for (int i = 0; i <= N; i++)
     a[i] = amplitude * Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
   return a;
 }
Ejemplo n.º 23
0
 Point2D rightHinge(int index) {
   Turn turn = centerHinges[index];
   double angle = (turn.angle + turn.prevAngle) / 2 + Math.PI / 2;
   double x = turn.x + WIDTH / 2 * Math.sin(angle);
   double z = turn.z + WIDTH / 2 * Math.cos(angle);
   return new Point2D(x, z);
 }
Ejemplo n.º 24
0
 public void mouseReleased(MouseEvent e) {
   left_mouse_pressed = false;
   double kvadrat = Math.sqrt(angleX * angleX + angleY * angleY);
   double tempx = angleX / kvadrat;
   double tempy = Math.sqrt(1 - tempx * tempx);
   if (angleY < 0) tempy = -tempy;
   Bullet b = new Bullet();
   boolean next = false;
   if (type_bullet1 == true && count_sphere > 0) {
     b = new Sphere();
     count_sphere--;
     next = true;
   }
   if (type_bullet2 == true && count_fireball > 0) {
     b = new FireBall();
     count_fireball--;
     next = true;
   }
   if (type_bullet3 == true && count_rocket > 0) {
     b = new Rocket();
     count_rocket--;
     next = true;
   }
   if (next == true) {
     b.bullet_x = player.getX() + 52 + 5 * Math.cos(angle); // + 50; //koef;
     b.bullet_y = player.getY() + 49 + 5 * Math.sin(angle); // player.getY() + 35; //25;
     b.current_direction_x = tempx;
     b.current_direction_y = tempy;
     bullet.addElement(b);
     allow_shoot = false;
   }
 }
Ejemplo n.º 25
0
  /**
   * Rotate the polygon clockwise about a point by an arbritray angle.
   *
   * @param x X ordinate of point to rotate about
   * @param y Y ordinate of point to rotate about
   * @param angle Angle in radians
   */
  public void rotate(double x, double y, double angle) {
    double theta = -angle;

    for (Enumeration e = vertices(); e.hasMoreElements(); ) {
      Vector2D vertex = (Vector2D) e.nextElement();

      // translate to origin
      double tmpX = vertex.getX() - x;
      double tmpY = vertex.getY() - y;

      // rotate
      double sin = Math.sin(theta);
      double cos = Math.cos(theta);

      double newX = tmpX * cos - tmpY * sin;
      double newY = tmpX * sin + tmpY * cos;

      // translate back to old location
      newX += x;
      newY += y;

      // set teh point to be where we calculated it should be
      vertex.setXY(newX, newY);
    }

    flagModified();
  }
Ejemplo n.º 26
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (dst == null) dst = createCompatibleDestImage(src, null);

    int width = src.getWidth();
    int height = src.getHeight();
    int numScratches = (int) (density * width * height / 100);
    ArrayList<Line2D> lines = new ArrayList<Line2D>();
    {
      float l = length * width;
      Random random = new Random(seed);
      Graphics2D g = dst.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setColor(new Color(color));
      g.setStroke(new BasicStroke(this.width));
      for (int i = 0; i < numScratches; i++) {
        float x = width * random.nextFloat();
        float y = height * random.nextFloat();
        float a = angle + ImageMath.TWO_PI * (angleVariation * (random.nextFloat() - 0.5f));
        float s = (float) Math.sin(a) * l;
        float c = (float) Math.cos(a) * l;
        float x1 = x - c;
        float y1 = y - s;
        float x2 = x + c;
        float y2 = y + s;
        g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
        lines.add(new Line2D.Float(x1, y1, x2, y2));
      }
      g.dispose();
    }

    if (false) {
      //		int[] inPixels = getRGB( src, 0, 0, width, height, null );
      int[] inPixels = new int[width * height];
      int index = 0;
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          float sx = x, sy = y;
          for (int i = 0; i < numScratches; i++) {
            Line2D.Float l = (Line2D.Float) lines.get(i);
            float dot = (l.x2 - l.x1) * (sx - l.x1) + (l.y2 - l.y1) * (sy - l.y1);
            if (dot > 0) inPixels[index] |= (1 << i);
          }
          index++;
        }
      }

      Colormap colormap = new LinearColormap();
      index = 0;
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          float f = (float) (inPixels[index] & 0x7fffffff) / 0x7fffffff;
          inPixels[index] = colormap.getColor(f);
          index++;
        }
      }
      setRGB(dst, 0, 0, width, height, inPixels);
    }
    return dst;
  }
Ejemplo n.º 27
0
 public void update() {
   rad += increment;
   if (rad > (Math.PI * 2)) {
     rad = 0.0;
   }
   coords[0] = 0.8 * Math.cos(rad);
   coords[1] = 0.8 * Math.sin(rad);
 }
Ejemplo n.º 28
0
  public static void main(String[] argv) {
    // Random seed for generation.
    UniformRandom.set_seed(System.currentTimeMillis());

    // Start with a random unit length vector: a random angle.
    double theta = UniformRandom.uniform(0, 2.0 * Math.PI);
    double[] x = new double[2];
    x[0] = Math.cos(theta);
    x[1] = Math.sin(theta);

    // Find perpendicular unit length vector by adding 90-deg
    double[] y = new double[2];
    y[0] = Math.cos(theta + Math.PI / 2);
    y[1] = Math.sin(theta + Math.PI / 2);

    // Test.
    double xDoty = MatrixTool.dotProduct(x, y);
    System.out.println("x dot y = " + xDoty);

    // Make the matrix with x and y as columns.
    double[][] A = new double[2][2];
    A[0][0] = x[0];
    A[1][0] = x[1];
    A[0][1] = y[0];
    A[1][1] = y[1];

    double delTheta = 0.1;
    double[] u = new double[2];
    Function F = new Function("alpha vs theta");
    for (theta = 0; theta <= 2 * Math.PI; theta += delTheta) {
      // Generate next vector along unit circle.
      u[0] = Math.cos(theta);
      u[1] = Math.sin(theta);
      // Apply A to u.
      double[] z = MatrixTool.matrixVectorMult(A, u);
      double alpha = Math.atan2(z[1], z[0]);
      // Get z's angle.
      alpha = fixAngle(alpha);
      // Add to data set.
      F.add(theta, alpha);
    }

    // Plot data set.
    F.show();
  }
Ejemplo n.º 29
0
 // Bharat's collision system
 public void bounce(Player b) {
   double trueAngle = Math.atan(b.getVel().getX() / b.getVel().getY());
   double incAngle = trueAngle + angle;
   double newAngle = incAngle + angle + Math.PI;
   Point p = new Point();
   p.x = (int) (b.VEL * Math.cos(newAngle));
   p.y = (int) (b.VEL * Math.sin(newAngle));
   b.setVel(p);
 }
Ejemplo n.º 30
0
 /**
  * Specifies the angle of the texture.
  *
  * @param angle the angle of the texture.
  * @angle
  * @see #getAngle
  */
 public void setAngle(float angle) {
   this.angle = angle;
   float cos = (float) Math.cos(angle);
   float sin = (float) Math.sin(angle);
   m00 = cos;
   m01 = sin;
   m10 = -sin;
   m11 = cos;
 }