Exemple #1
0
  /**
   * Perform a great circle (spherical linear) interpolation between this quaternion and the
   * quaternion parameter.
   *
   * @param q The other quaternion.
   * @param alpha The interpolation parameter from the interval [0, 1].
   * @return The interpolated quaternion.
   */
  public final Quaternion interpolate(Quaternion q, float alpha) {
    // From "Advanced Animation and Rendering Techniques"
    // by Watt and Watt pg. 364, function as implemented appeared to be
    // incorrect. Fails to choose the same quaternion for the float
    // covering. Resulting in change of direction for rotations.
    // Fixed function to negate the first quaternion in the case that the
    // dot product of q and this is negative. Second case was not needed.
    float dot, s1, s2, om, sinom;

    dot = x * q.x + y * q.y + z * q.z + w * q.w;

    if (dot < 0) {
      q = q.negate();
      dot = -dot;
    }

    if ((1.0 - dot) > EPS) {
      om = (float) Math.acos(dot);
      sinom = (float) Math.sin(om);
      s1 = (float) Math.sin((1.0 - alpha) * om) / sinom;
      s2 = (float) Math.sin(alpha * om) / sinom;
    } else {
      s1 = 1.0f - alpha;
      s2 = alpha;
    }

    return new Quaternion(
        s1 * w + s2 * q.w, s1 * x + s2 * q.x, s1 * y + s2 * q.y, s1 * z + s2 * q.z);
  }
Exemple #2
0
 /**
  * Sets segment current orientation
  *
  * @param sensor sensor of corresponding sensor
  */
 public void setSegmentOrientation(Sensor sensor) {
   float[] n = new float[3]; // rotation axis
   float[] vec = {0, 0, 1}; // Z axis
   float fi = 0; // rotation angle
   float[] q = new float[4]; // quaternion
   float[] accnorm = sensor.getAccNorm();
   SensorDataProcessing.crossProduct(sensor.getAccNorm(), vec, n); // rotation axis
   SensorDataProcessing.normalizeVector(n); // normalize rotation axis			
   fi =
       (float)
           Math.acos(
               SensorDataProcessing.dotProduct(vec, sensor.getAccNorm())); // get rotation angle
   SensorDataProcessing.quaternion(n, fi, q); // get quaternion
   for (int i = 0; i < 4; i++) {
     SensorDataProcessing.quatRotate(q, initialCross[i], cross[i]);
   }
 }
  public static void main(String[] args) {
    try (Scanner sc = new Scanner(new File("angle2.in"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("angle2.out"))) {
      int v1x = sc.nextInt();
      int v1y = sc.nextInt();
      int v2x = sc.nextInt();
      int v2y = sc.nextInt();

      double angle =
          Math.acos(
              (double) (v1x * v2x + v1y * v2y)
                  / (Math.sqrt(v1x * v1x + v1y * v1y) * Math.sqrt(v2x * v2x + v2y * v2y)));

      bw.write(Double.toString(angle));

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemple #4
0
  // ---------------------------------------------------------------------------
  private String Calculate1(String oper, String str1) throws Exception {
    double n1 = Values.StringToDouble(str1);
    double val = 0;

    if (oper.equalsIgnoreCase("SEN")) val = java.lang.Math.sin(n1);
    else if (oper.equalsIgnoreCase("COS")) val = java.lang.Math.cos(n1);
    else if (oper.equalsIgnoreCase("TAN")) val = java.lang.Math.tan(n1);
    else if (oper.equalsIgnoreCase("CTG")) val = 1.0 / java.lang.Math.tan(n1);
    else if (oper.equalsIgnoreCase("ASEN")) val = java.lang.Math.asin(n1);
    else if (oper.equalsIgnoreCase("ACOS")) val = java.lang.Math.acos(n1);
    else if (oper.equalsIgnoreCase("ATAN")) val = java.lang.Math.atan(n1);
    else if (oper.equalsIgnoreCase("ACTG")) val = 1.0 / java.lang.Math.atan(n1);
    else if (oper.equalsIgnoreCase("SENH")) val = java.lang.Math.sinh(n1);
    else if (oper.equalsIgnoreCase("COSH")) val = java.lang.Math.cosh(n1);
    else if (oper.equalsIgnoreCase("TANH")) val = java.lang.Math.tanh(n1);
    else if (oper.equalsIgnoreCase("CTGH")) val = 1.0 / java.lang.Math.tanh(n1);
    else if (oper.equalsIgnoreCase("EXP")) val = java.lang.Math.exp(n1);
    // valor absoluto de inteiros s�o inteiros
    else if (oper.equalsIgnoreCase("ABS")) {
      val = java.lang.Math.abs(n1);
      if (Values.IsInteger(str1)) return Values.IntegerToString(val);
    } else if (oper.equalsIgnoreCase("RAIZ")) val = java.lang.Math.sqrt(n1);
    else if (oper.equalsIgnoreCase("LOG")) val = java.lang.Math.log10(n1);
    else if (oper.equalsIgnoreCase("LN")) val = java.lang.Math.log(n1);
    // parte inteira do numeros
    else if (oper.equalsIgnoreCase("INT")) {
      return Values.IntegerToString(n1);
    }
    // parte real
    else if (oper.equalsIgnoreCase("FRAC")) {
      String num = Values.DoubleToString(n1);
      return num.substring(num.indexOf('.') + 1);
    }

    // parte real
    else if (oper.equalsIgnoreCase("ARRED")) {
      double vm = java.lang.Math.ceil(n1);
      if (n1 - vm >= 0.5) return Values.IntegerToString((int) n1 + 1);
      return Values.IntegerToString((int) n1);
    } else throw new Exception("ERRO fun��o Desconhecida 1 [" + oper + "]");
    return Values.DoubleToString(val);
  }
  /**
   * Calculates the length of a given day. Uses the Calendar object to pass in the target date. The
   * hour, if not specified in the Calendar object, will default to 12.00 NOON. We need to convert
   * the dates between Julian & Gregorian and hour of the day impacts the generated Julian Day. Ref:
   * http://users.electromagnetic.net/bu/astro/sunrise-set.php
   *
   * @param cal - Gregorian calendar day for which the day length is to be calculated.
   * @param location - a lat/long position for which the DayLength is to be calculated.
   * @return - A DayLength object with with the Sunrise and Sunset for the given day.
   */
  public DayLength getLengthOfDay(Calendar cal, IGeoLocation location) {
    double latitudeNorth = location.getLatitude().getDegrees();
    double longitudeWest = location.getLatitude().getDegrees();

    latitudeNorth =
        location.getLatitude().getOrientation() == GeoOrientation.NORTH
            ? latitudeNorth
            : latitudeNorth * -1; // for degrees to the south, inverse sign.
    longitudeWest =
        location.getLongitude().getOrientation() == GeoOrientation.WEST
            ? longitudeWest
            : longitudeWest * -1; // for degrees to the east, inverse sign.

    Calendar gregorianDate = makeDefensiveCopy(cal);
    double julianDay = Calculations.toJulianValue(gregorianDate);
    long julianCycle = round((julianDay - JAN012000 - CYCLE_CONST) - (longitudeWest / 360));
    double approxSolarNoon = JAN012000 + CYCLE_CONST + longitudeWest / 360 + julianCycle;
    double meanSolarAnomaly = (357.5291 + 0.98560028 * (approxSolarNoon - JAN012000)) % 360;
    double equationOfCenter =
        (1.9148 * sin(meanSolarAnomaly))
            + (0.0200 * sin(2 * meanSolarAnomaly))
            + (0.0003 * sin(3 * meanSolarAnomaly));
    double longitudeOfSun = (meanSolarAnomaly + 102.9372 + equationOfCenter + 180) % 360;
    double julianSolarNoon =
        approxSolarNoon + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2 * longitudeOfSun));
    double sunDeclination = Math.asin(sin(longitudeOfSun) * sin(23.45));
    double hourAngle =
        Math.acos(
            (sin(-0.83) - sin(latitudeNorth) * sin(sunDeclination))
                / (cos(latitudeNorth) * cos(sunDeclination)));
    approxSolarNoon = JAN012000 + CYCLE_CONST + ((hourAngle + longitudeWest) / 360) + julianCycle;
    double julianSunSet =
        approxSolarNoon + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2 * longitudeOfSun));
    double julianRise = julianSolarNoon - (julianSunSet - julianSolarNoon);

    assert julianSunSet > julianRise
        : "The sunset value for " + julianDay + " is higher than the sunrise value";

    return new DayLength(julianRise, julianSunSet);
  }
Exemple #6
0
 /* Returns the arc cosine of an angle, in the range of -pi/2 through pi/2.
  */
 public static SchemaTypeNumber acos(SchemaTypeNumber value) {
   switch (value.numericType()) {
     case SchemaTypeNumber.NUMERIC_VALUE_INT:
       return new SchemaInt((int) java.lang.Math.acos(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_LONG:
       return new SchemaLong((long) java.lang.Math.acos(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_BIGINTEGER:
       return new SchemaInteger(
           (long) java.lang.Math.acos(value.doubleValue())); // note: possible loss of precision
     case SchemaTypeNumber.NUMERIC_VALUE_FLOAT:
       return new SchemaFloat((float) java.lang.Math.acos(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_DOUBLE:
       return new SchemaDouble(java.lang.Math.acos(value.doubleValue()));
   }
   return new SchemaDecimal(java.lang.Math.acos(value.doubleValue()));
 }
 /**
  * Returns the (n-space) angle in radians between this vector and the vector parameter; the return
  * value is constrained to the range [0,PI].
  *
  * @param v1 The other vector
  * @return The angle in radians in the range [0,PI]
  */
 public final double angle(GVector v1) {
   return (Math.acos(this.dot(v1) / (this.norm() * v1.norm())));
 }
Exemple #8
0
public class Main implements Runnable {

  final String filename = "in";

  public static void main(String[] args) {
    // new Thread(new Main()).start();
    new Thread(null, new Main(), "1", 1 << 25).start();
  }

  public void run() {
    try {
      // in = new BufferedReader(new InputStreamReader(System.in));
      out = new BufferedWriter(new OutputStreamWriter(System.out));
      in = new BufferedReader(new FileReader(filename + ".in"));
      // out = new BufferedWriter(new FileWriter(filename+".out"));
      int kase = iread();
      for (int i = 1; i <= kase; ++i) {
        // TODO CODE HERE
        solve1216(i);
      }
      out.flush();
    } catch (Exception e) {
      System.exit(1);
    }
  }

  final double pii = ((double) (2.)) * Math.acos(0);

  public void solve1216(int kaseno) throws Exception {
    int r1 = iread(), r2 = iread(), h = iread(), p = iread();

    double R1 = (double) r2;
    double R2 = ((double) ((r1 - r2) * p)) / h;
    R2 += r2;
    double H = (double) p;

    double ret = pii * (R1 * R1 + R2 * R2 + R1 * R2) * H;
    ret /= 3.;
    Format df = new DecimalFormat("0.0000000000");
    String res = df.format(ret);
    out.write("Case " + kaseno + ": " + res + "\n");
  }

  public void debug(Object... o) {
    System.err.println(Arrays.deepToString(o));
  }

  public int iread() throws Exception {
    return Integer.parseInt(readword());
  }

  public double dread() throws Exception {
    return Double.parseDouble(readword());
  }

  public long lread() throws Exception {
    return Long.parseLong(readword());
  }

  BufferedReader in;
  BufferedWriter out;

  public String readword() throws IOException {
    StringBuilder b = new StringBuilder();
    int c;
    c = in.read();
    while (c >= 0 && c <= ' ') {
      c = in.read();
    }
    if (c < 0) {
      return "";
    }
    while (c > ' ') {
      b.append((char) c);
      c = in.read();
    }
    return b.toString();
  }
}
Exemple #9
0
 /**
  * Returns the angle between the other vector, in radians. (result is ranged 0-PI).
  *
  * @param vec Other vector
  */
 public double angle(Vector vec) {
   double dot = dot(vec);
   return Math.acos(dot / (this.magnitude() * vec.magnitude()));
 }
  public double computeForces() {
    double ab[],
        bc[],
        abdotbc,
        abdotab,
        bcdotbc,
        dotnorm,
        angle,
        dedr,
        deds,
        dedth,
        s,
        r,
        drda[],
        drdb[],
        dsdb[],
        dsdc[],
        dthda[],
        dthdb[],
        dthdc[],
        denominator;
    ab = new double[3];
    bc = new double[3];
    drda = new double[3];
    drdb = new double[3];
    dsdb = new double[3];
    dsdc = new double[3];
    dthda = new double[3];
    dthdb = new double[3];
    dthdc = new double[3];

    for (int i = 0; i < 3; i++) {
      ab[i] = myAtoms[0].x[i] - myAtoms[1].x[i];
      bc[i] = myAtoms[2].x[i] - myAtoms[1].x[i];
    }

    abdotbc = ab[0] * bc[0] + ab[1] * bc[1] + ab[2] * bc[2];
    abdotab = ab[0] * ab[0] + ab[1] * ab[1] + ab[2] * ab[2];
    bcdotbc = bc[0] * bc[0] + bc[1] * bc[1] + bc[2] * bc[2];
    r = Math.sqrt(abdotab);
    s = Math.sqrt(bcdotbc);

    angle = Math.acos(abdotbc / (r * s));

    dedr = deds = stbnunit * kBondAngle * (angle - idealAngle);
    dedth = stbnunit * kBondAngle * (r - idealLength1 + s - idealLength2);

    denominator = Math.sqrt(abdotab * bcdotbc - abdotbc * abdotbc);

    for (int i = 0; i < 3; i++) {
      drda[i] = ab[i] / r;
      drdb[i] = -drda[i];
      dsdc[i] = bc[i] / r;
      dsdb[i] = -dsdc[i];
      dthda[i] = (bc[i] - ab[i] * abdotbc / abdotab) / denominator;
      dthdc[i] = (ab[i] - bc[i] * abdotbc / abdotab) / denominator;
      dthdb[i] = -(dthda[i] + dthdc[i]);

      myAtoms[0].f[i] += dedr * drdb[i] + deds * dsdb[i] + dedth * dthdb[i];
      myAtoms[1].f[i] += dedr * drda[i] + dedth * dthda[i];
      myAtoms[2].f[i] += deds * dsdc[i] + dedth * dthdc[i];
    }

    return myAtoms[0].f[0];
  }
Exemple #11
0
  /**
   * method for compensating for tilt
   *
   * @param refferenceStateInitial
   * @param currentState
   * @param refferenceState
   * @param referenceRow
   * @param referenceCol
   */
  public static void compansateCentersForTilt(
      Vector<Vector<Segment>> refferenceStateInitial,
      Vector<Vector<Segment>> currentState,
      Vector<Vector<Segment>> refferenceState,
      int referenceRow,
      int referenceCol) {
    float n[] = new float[3];
    float n2[] = new float[3];
    float fi;
    float fi2;
    float[] q = new float[4];
    float[] q2 = new float[4];

    SensorDataProcessing.crossProduct(
        refferenceStateInitial.get(referenceRow).get(referenceCol).cross[0],
        currentState.get(referenceRow).get(referenceCol).cross[0],
        n);

    SensorDataProcessing.normalizeVector(n);

    fi =
        (float)
            Math.acos(
                SensorDataProcessing.dotProduct(
                        refferenceStateInitial.get(referenceRow).get(referenceCol).cross[0],
                        currentState.get(referenceRow).get(referenceCol).cross[0])
                    / (SensorDataProcessing.getVectorLength(
                            refferenceStateInitial.get(referenceRow).get(referenceCol).cross[0])
                        * SensorDataProcessing.getVectorLength(
                            currentState.get(referenceRow).get(referenceCol).cross[0])));

    SensorDataProcessing.quaternion(n, fi, q);

    for (int i = 0; i < refferenceState.size(); i++) {
      for (int j = 0; j < refferenceState.get(0).size(); j++) {
        SensorDataProcessing.quatRotate(
            q, refferenceStateInitial.get(i).get(j).center, refferenceState.get(i).get(j).center);
        for (int k = 0; k < 4; k++) {
          SensorDataProcessing.quatRotate(
              q,
              refferenceStateInitial.get(i).get(j).cross[k],
              refferenceState.get(i).get(j).cross[k]);
        }
      }
    }
    SensorDataProcessing.crossProduct(
        refferenceState.get(referenceRow).get(referenceCol).cross[1],
        currentState.get(referenceRow).get(referenceCol).cross[1],
        n2);
    SensorDataProcessing.normalizeVector(n2);

    fi2 =
        (float)
            Math.acos(
                SensorDataProcessing.dotProduct(
                        refferenceState.get(referenceRow).get(referenceCol).cross[1],
                        currentState.get(referenceRow).get(referenceCol).cross[1])
                    / (SensorDataProcessing.getVectorLength(
                            refferenceState.get(referenceRow).get(referenceCol).cross[1])
                        * SensorDataProcessing.getVectorLength(
                            currentState.get(referenceRow).get(referenceCol).cross[1])));
    SensorDataProcessing.quaternion(n2, fi2, q2);

    for (int i = 0; i < refferenceState.size(); i++) {
      for (int j = 0; j < refferenceState.get(0).size(); j++) {
        Segment temp = new Segment();
        temp.setInitialCross2(
            refferenceStateInitial.get(0).get(0).getVerticalDistance(),
            refferenceStateInitial.get(0).get(0).getHorizontalDistance());
        for (int k = 0; k < 3; k++) {
          temp.center[k] = refferenceState.get(i).get(j).center[k];
        }
        SensorDataProcessing.quatRotate(q2, temp.center, refferenceState.get(i).get(j).center);
      }
    }
  }
Exemple #12
0
  /**
   * method for compensating for tilt
   *
   * @param refferenceStateInitial
   * @param currentState
   * @param refferenceState
   * @param referenceRow
   * @param referenceCol
   */
  public static void compansateCentersForTilt(
      Segment[][] refferenceStateInitial,
      Segment[][] currentState,
      Segment[][] refferenceState,
      int referenceRow,
      int referenceCol) {
    float n[] = new float[3];
    float n2[] = new float[3];
    float fi;
    float fi2;
    float[] q = new float[4];
    float[] q2 = new float[4];

    SensorDataProcessing.crossProduct(
        refferenceStateInitial[referenceRow][referenceCol].cross[0],
        currentState[referenceRow][referenceCol].cross[0],
        n);

    SensorDataProcessing.normalizeVector(n);

    fi =
        (float)
            Math.acos(
                SensorDataProcessing.dotProduct(
                        refferenceStateInitial[referenceRow][referenceCol].cross[0],
                        currentState[referenceRow][referenceCol].cross[0])
                    / (SensorDataProcessing.getVectorLength(
                            refferenceStateInitial[referenceRow][referenceCol].cross[0])
                        * SensorDataProcessing.getVectorLength(
                            currentState[referenceRow][referenceCol].cross[0])));

    SensorDataProcessing.quaternion(n, fi, q);

    for (int i = 0; i < refferenceState.length; i++) {
      for (int j = 0; j < refferenceState[0].length; j++) {
        SensorDataProcessing.quatRotate(
            q, refferenceStateInitial[i][j].center, refferenceState[i][j].center);
        for (int k = 0; k < 4; k++) {
          SensorDataProcessing.quatRotate(
              q, refferenceStateInitial[i][j].cross[k], refferenceState[i][j].cross[k]);
        }
      }
    }
    SensorDataProcessing.crossProduct(
        refferenceState[referenceRow][referenceCol].cross[1],
        currentState[referenceRow][referenceCol].cross[1],
        n2);
    SensorDataProcessing.normalizeVector(n2);

    fi2 =
        (float)
            Math.acos(
                SensorDataProcessing.dotProduct(
                        refferenceState[referenceRow][referenceCol].cross[1],
                        currentState[referenceRow][referenceCol].cross[1])
                    / (SensorDataProcessing.getVectorLength(
                            refferenceState[referenceRow][referenceCol].cross[1])
                        * SensorDataProcessing.getVectorLength(
                            currentState[referenceRow][referenceCol].cross[1])));
    SensorDataProcessing.quaternion(n2, fi2, q2);

    for (int i = 0; i < refferenceState.length; i++) {
      for (int j = 0; j < refferenceState[0].length; j++) {
        Segment temp = new Segment();
        for (int k = 0; k < 3; k++) {
          temp.center[k] = refferenceState[i][j].center[k];
        }
        SensorDataProcessing.quatRotate(q2, temp.center, refferenceState[i][j].center);
      }
    }
  }
  /**
   * Solve the cubic whose coefficients are in the <code>eqn</code> array and place the non-complex
   * roots into the <code>res</code> array, returning the number of roots. The cubic solved is
   * represented by the equation: eqn = {c, b, a, d} dx^3 + ax^2 + bx + c = 0 A return value of -1
   * is used to distinguish a constant equation, which may be always 0 or never 0, from an equation
   * which has no zeroes.
   *
   * @param eqn the specified array of coefficients to use to solve the cubic equation
   * @param res the array that contains the non-complex roots resulting from the solution of the
   *     cubic equation
   * @return the number of roots, or -1 if the equation is a constant
   * @since 1.3
   */
  public static int solveCubic(double eqn[], double res[]) {
    // From Graphics Gems:
    // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
    final double d = eqn[3];
    if (d == 0) {
      return QuadCurve2D.solveQuadratic(eqn, res);
    }

    /* normal form: x^3 + Ax^2 + Bx + C = 0 */
    final double A = eqn[2] / d;
    final double B = eqn[1] / d;
    final double C = eqn[0] / d;

    //  substitute x = y - A/3 to eliminate quadratic term:
    //     x^3 +Px + Q = 0
    //
    // Since we actually need P/3 and Q/2 for all of the
    // calculations that follow, we will calculate
    // p = P/3
    // q = Q/2
    // instead and use those values for simplicity of the code.
    double sq_A = A * A;
    double p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
    double q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);

    /* use Cardano's formula */

    double cb_p = p * p * p;
    double D = q * q + cb_p;

    final double sub = 1.0 / 3 * A;

    int num;
    if (D < 0) {
      /* Casus irreducibilis: three real solutions */
      // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
      double phi = 1.0 / 3 * Math.acos(-q / Math.sqrt(-cb_p));
      double t = 2 * Math.sqrt(-p);

      if (res == eqn) {
        eqn = Arrays.copyOf(eqn, 4);
      }

      res[0] = (t * Math.cos(phi));
      res[1] = (-t * Math.cos(phi + Math.PI / 3));
      res[2] = (-t * Math.cos(phi - Math.PI / 3));
      num = 3;

      for (int i = 0; i < num; ++i) {
        res[i] -= sub;
      }

    } else {
      // Please see the comment in fixRoots marked 'XXX' before changing
      // any of the code in this case.
      double sqrt_D = Math.sqrt(D);
      double u = Math.cbrt(sqrt_D - q);
      double v = -Math.cbrt(sqrt_D + q);
      double uv = u + v;

      num = 1;

      double err = 1200000000 * ulp(abs(uv) + abs(sub));
      if (iszero(D, err) || within(u, v, err)) {
        if (res == eqn) {
          eqn = Arrays.copyOf(eqn, 4);
        }
        res[1] = -(uv / 2) - sub;
        num = 2;
      }
      // this must be done after the potential Arrays.copyOf
      res[0] = uv - sub;
    }

    if (num > 1) { // num == 3 || num == 2
      num = fixRoots(eqn, res, num);
    }
    if (num > 2 && (res[2] == res[1] || res[2] == res[0])) {
      num--;
    }
    if (num > 1 && res[1] == res[0]) {
      res[1] = res[--num]; // Copies res[2] to res[1] if needed
    }
    return num;
  }