Esempio n. 1
0
 public double angle(Coord o) {
   Coord c = o.add(this.inv());
   if (c.x == 0) {
     if (c.y < 0) return (-PI / 2);
     else return (PI / 2);
   } else {
     if (c.x < 0) {
       if (c.y < 0) return (-PI + Math.atan((double) c.y / (double) c.x));
       else return (PI + Math.atan((double) c.y / (double) c.x));
     } else {
       return (Math.atan((double) c.y / (double) c.x));
     }
   }
 }
 @Test
 public void atanSafe() {
   assertEquals(Math.PI / 2.0, UtilAngle.atanSafe(0.0, 0.0), 1e-8);
   assertEquals(Math.PI / 2.0, UtilAngle.atanSafe(1.0, 0.0), 1e-8);
   assertEquals(-Math.PI / 2.0, UtilAngle.atanSafe(-1.0, 0.0), 1e-8);
   assertEquals(Math.atan(1), UtilAngle.atanSafe(1.0, 1.0), 1e-8);
 }
Esempio n. 3
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);
  }
Esempio n. 4
0
 public float xyangle(Coord3f o) {
   Coord3f c = o.sub(this);
   if (c.x == 0) {
     if (c.y < 0) {
       return ((float) -PI / 2);
     } else {
       return ((float) PI / 2);
     }
   } else {
     if (c.x < 0) {
       if (c.y < 0) {
         return ((float) (-PI + Math.atan(c.y / c.x)));
       } else {
         return ((float) (PI + Math.atan(c.y / c.x)));
       }
     } else {
       return ((float) Math.atan(c.y / c.x));
     }
   }
 }
Esempio n. 5
0
 /**
  * Returns the heading, in radians, that you have to use to get from this lat/lon to another.
  *
  * @param other the "destination" position
  * @return heading
  */
 public double heading(LatLon other) {
   double rv;
   if (other.lat() == lat()) {
     rv = (other.lon() > lon() ? Math.PI / 2 : Math.PI * 3 / 2);
   } else {
     rv = Math.atan((other.lon() - lon()) / (other.lat() - lat()));
     if (rv < 0) {
       rv += Math.PI;
     }
     if (other.lon() < lon()) {
       rv += Math.PI;
     }
   }
   return rv;
 }
Esempio n. 6
0
 /* Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
  */
 public static SchemaTypeNumber atan(SchemaTypeNumber value) {
   switch (value.numericType()) {
     case SchemaTypeNumber.NUMERIC_VALUE_INT:
       return new SchemaInt((int) java.lang.Math.atan(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_LONG:
       return new SchemaLong((long) java.lang.Math.atan(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_BIGINTEGER:
       return new SchemaInteger(
           (long) java.lang.Math.atan(value.doubleValue())); // note: possible loss of precision
     case SchemaTypeNumber.NUMERIC_VALUE_FLOAT:
       return new SchemaFloat((float) java.lang.Math.atan(value.doubleValue()));
     case SchemaTypeNumber.NUMERIC_VALUE_DOUBLE:
       return new SchemaDouble(java.lang.Math.atan(value.doubleValue()));
   }
   return new SchemaDecimal(java.lang.Math.atan(value.doubleValue()));
 }
  /** onScannedRobot: What to do when you see another robot */
  public void onScannedRobot(ScannedRobotEvent e) {
    System.out.println("START at : " + getTime() + " onScannedRobot----------------------------");
    String nnn = e.getName();
    System.out.println("scan " + nnn);
    double eneX =
        getX() + Math.sin(e.getBearingRadians() + Math.toRadians(getHeading())) * e.getDistance();
    double eneY =
        getY() + Math.cos(e.getBearingRadians() + Math.toRadians(getHeading())) * e.getDistance();
    Enemy_info enem = null;
    if (!isTeammate(nnn)) {
      // 味方への情報送信
      try {
        broadcastMessage(
            nnn
                + ", "
                + e.getBearing()
                + ", "
                + e.getBearingRadians()
                + ", "
                + e.getDistance()
                + ", "
                + e.getEnergy()
                + ", "
                + e.getHeading()
                + ", "
                + e.getHeadingRadians()
                + ", "
                + e.getVelocity()
                + ", "
                + eneX
                + ", "
                + eneY);
      } catch (IOException ignored) {
      }
      // スキャンした車両がLocal敵リストにいるかどうかのフラグ
      boolean flag = false;
      System.out.println("send scanned info");
      // スキャンした敵がLocal敵リストの中に存在するか
      for (Enemy_info temp : enes) {
        if (nnn.equals(temp.get_en_name())) {
          flag = true;
          enem = temp;
          // Local敵リストのアップデート
          temp.updateInformation(
              e.getBearing(),
              e.getBearingRadians(),
              e.getDistance(),
              e.getEnergy(),
              e.getHeading(),
              e.getHeadingRadians(),
              e.getVelocity(),
              eneX,
              eneY);
          System.out.println("	update scanned Info");
        }
      }
      // スキャンした敵がLocal敵リストの中に存在しない場合
      if (!flag) {
        // Local敵リストに新規追加
        enem =
            new Enemy_info(
                nnn,
                e.getBearing(),
                e.getBearingRadians(),
                e.getDistance(),
                e.getEnergy(),
                e.getHeading(),
                e.getHeadingRadians(),
                e.getVelocity(),
                eneX,
                eneY);
        enes.add(enem);
        System.out.println("	add scanned info");
      }
      if (enemy_detected == false) {
        // 共通の敵が設定されていない場合
        enemy_detected = true;
        target_enemy = enem;
        try {
          broadcastMessage("Kill , " + target_enemy.get_en_name() + ", !!");
        } catch (IOException ignored) {
        }
      }

      if (enemy_detected == true) {
        try {
          double enemyX = target_enemy.get_en_expX();
          double enemyY = target_enemy.get_en_expY();
          setTurnRadarLeftRadians(getRadarTurnRemainingRadians());

          System.out.println("abs : eX " + enemyX + " : " + "eY " + enemyY);

          // 共通の敵が設定されている場合
          double enemyBearing = Math.atan((enemyX - getX()) / (enemyY - getY()));
          // if(enemyBearing<0){
          // 	System.out.println("change");
          // 	enemyBearing = Math.PI*2 + enemyBearing;
          // }else if (enemyBearing < Math.PI){

          // }
          // System.out.println("atan " + Math.atan((eneY-getY())/(eneX-getX())));
          // System.out.println("atan1 " + Math.atan((eneX-getX())/(eneY-getY())));
          // System.out.println("trueeee " + (e.getBearingRadians() + this.getHeadingRadians()));
          System.out.println(
              "enerad"
                  + enemyBearing
                  + " ?= "
                  + "enemyBearing "
                  + (this.getHeadingRadians() + e.getBearingRadians()));
          System.out.println(enemyBearing + Math.PI);
          double enemyHeading = target_enemy.get_en_heading(); // 敵の向き
          System.out.println("enemy heading:" + enemyHeading);
          // double enemyBearing = this.getHeadingRadians() +
          // target_enemy.get_en_bearingRadians();// 自分と敵の角度

          // double enemyX = target_enemy.get_en_distance() * Math.sin(enemyBearing);
          // double enemyY = target_enemy.get_en_distance() * Math.cos(enemyBearing);

          enemyX = enemyX - getX();
          enemyY = enemyY - getY();
          System.out.println("Relative : eX " + enemyX + " : " + "eY " + enemyY);

          double battlefieldWidh = getBattleFieldWidth(); // フィールド幅
          double battlefieldHeight = getBattleFieldHeight(); // フィールド高さ

          boolean isHeadingToCenter = (int) enemyHeading % 90 == 0; // 中心を向いている
          boolean isOnWall =
              nearlyEquals(enemyX, 18)
                  || nearlyEquals(enemyX + 18, battlefieldWidh)
                  || nearlyEquals(enemyY, 18)
                  || nearlyEquals(enemyY + 18, battlefieldHeight); // 壁に張り付いている

          // 中心を向いている&&壁際にいる(=Walls)なら射撃
          if (isHeadingToCenter && isOnWall) {
            System.out.println("Walls!!");
          }

          double dis = 0;
          double heading = lastEnemyHeading;
          do {
            dis += Rules.getBulletSpeed(power);
            heading += target_enemy.get_en_headingRadians() - lastEnemyHeading;
            enemyX += target_enemy.get_en_velocity() * Math.sin(heading);
            enemyY += target_enemy.get_en_velocity() * Math.cos(heading);
          } while (dis < Point2D.distance(0, 0, enemyX, enemyY)); //

          // 相対角度に変換した上で砲塔の向きを変える
          setTurnGunRightRadians(
              Utils.normalRelativeAngle(Math.atan2(enemyX, enemyY) - getGunHeadingRadians()));
          setFire(power);
          // lastEnemyHeading = e.getHeadingRadians();
          lastEnemyHeading = target_enemy.get_en_headingRadians();
          System.out.println("lastEnemyHeading " + e.getHeadingRadians());
          System.out.println(lastEnemyHeading);

          // 敵の居る方向へターンする
          // setTurnRightRadians(e.getBearingRadians());
          setTurnRightRadians(enemyBearing - this.getHeadingRadians());
          System.out.println("setTurnRightRadians " + e.getBearingRadians());
          System.out.println(enemyBearing - this.getHeadingRadians());

          // 前進する
          setAhead(moveAmount);
        } catch (NullPointerException ee) {
          System.out.println("NullPointerException");
          System.out.println(target_enemy);
        }
      }
    }

    System.out.println("enemy_detected = " + enemy_detected);
    System.out.println("target is " + target_enemy.get_en_name());
    System.out.println(target_enemy.get_en_expX() + " " + target_enemy.get_en_expY());
    System.out.println("END at : " + getTime() + " onScannedRobot----------------------------");
  }
Esempio n. 8
0
  private int[] Translated_Point(double constant, int[] arrow_x, int[] arrow_y, double gradient) {
    double[] translated = new double[2];
    double[][] trans_matrix = new double[2][2];
    double ang_rot;
    double[] point2_temp = new double[4];
    int cnt, row, col;
    int[] pt = new int[2];

    translated[0] = 0.00;
    translated[1] = 0.00;
    trans_matrix[0][0] = 0.00;
    trans_matrix[0][1] = 0.00;
    trans_matrix[1][0] = 0.00;
    trans_matrix[1][1] = 0.00;
    point2_temp[0] = 0.00;
    point2_temp[1] = 0.00;
    point2_temp[2] = 0.00;
    point2_temp[3] = 0.00;
    cnt = 0;
    row = 0;
    col = 0;
    ang_rot = 0.00;

    // translate the line to the origin
    // thus the translated points to be found are:
    translated[0] = arrow_x[1];
    if (constant < 0) {
      translated[1] = arrow_y[1] + Math.abs(constant);
    } else {
      translated[1] = arrow_y[1] - Math.abs(constant);
    }

    // find the angle of rotation
    ang_rot = Math.atan(gradient);

    if ((end_x - start_x) == 0) {
      if (arrow_x[0] > 0) {
        translated[0] = arrow_x[1] - arrow_x[0];
      } else {
        translated[0] = arrow_x[1] + arrow_x[0];
      }
      translated[1] = arrow_y[1];
      trans_matrix[0][0] = -1;
      trans_matrix[0][1] = 0;
      trans_matrix[1][0] = 0;
      trans_matrix[1][1] = 1;
    } else {
      // declare the transformation matrix
      trans_matrix[0][0] = Math.cos(2 * ang_rot);
      trans_matrix[0][1] = Math.sin(2 * ang_rot);
      trans_matrix[1][0] = Math.sin(2 * ang_rot);
      trans_matrix[1][1] = Math.cos(2 * ang_rot) * (-1);
    }

    // multiply the transformation matrix with the point
    // store it in an array
    for (row = 0; row < 2; row++) {
      for (col = 0; col < 2; col++) {
        point2_temp[cnt] = trans_matrix[row][col] * translated[col];
        cnt++;
      }
    }

    if ((end_x - start_x) == 0) {
      if (arrow_x[0] > 0) {
        arrow_x[2] = (int) Math.round(point2_temp[0] + point2_temp[1] + arrow_x[0]);
      } else {
        arrow_x[2] = (int) Math.round(point2_temp[2] + point2_temp[1] - arrow_x[0]);
      }
      arrow_y[2] = (int) Math.round(point2_temp[2] + point2_temp[3]);
    } else {
      // from the array, get the reflected point
      arrow_x[2] = (int) Math.round(point2_temp[0] + point2_temp[1]);
      if (constant < 0) {
        arrow_y[2] = (int) Math.round(point2_temp[2] + point2_temp[3] - Math.abs(constant));
      } else {
        arrow_y[2] = (int) Math.round(point2_temp[2] + point2_temp[3] + Math.abs(constant));
      }
    }
    pt[0] = arrow_x[2];
    pt[1] = arrow_y[2];
    return pt;
  }
Esempio n. 9
0
  public void find() {

    t = Math.atan((x - xc) / (y - yc));
    r = distPts2D(x, y, xc, yc);
  }
Esempio n. 10
0
 public static void main(String[] args) {
   double deg = 30;
   double rad = Math.toRadians(deg);
   double ans = Math.atan(rad);
   System.out.println(ans);
 }