2013-06-25 71 views
2

我讀這個post關於得到2個點之間的角度,並想知道。我認爲atan2是爲atan2(y,x)定義的,它是atan2(deltaX,deltaY),現在爲什麼是x?2點與atan2之間的角度

public float getAngle(Point target) { 
    float angle = (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y)); 

    if (angle < 0) { 
     angle += 360; 
    } 

    return angle; 
} 
+0

出於好奇:你怎麼定義兩個點之間的角度? –

+0

@MarcoForberg,通過定義一個通過其中一個點的軸並測量該軸與連接點的線段之間的角度。通常這被稱爲[方位角](https://en.wikipedia.org/wiki/Bearing_(navigation))。 – Richard

回答

11

Math.java它定義爲

public static double atan2(double y, double x) { 
    return StrictMath.atan2(y, x); // default impl. delegates to StrictMath 
    } 

,這將返回該反時針角度相對於X軸。

如果你換了這兩個,你會得到相對於X軸的時鐘方向角。

在笛卡爾座標系中,我們考慮了相對於X軸的反時鐘方位角。這就是Math.java如上所述使用它的原因。

+0

謝謝,我真的很困惑 – vuvu

+0

@vuvu歡迎您 –

2

交換的參數的順序意味着,而不是(逆時針)的與X軸的夾角你與Y軸(順時針方向)的角度。這沒有錯,只是不尋常的。

0

嘗試了這一點:

// ... code 
Target start = new Target(); 
     start.setX(0); 
     start.setY(0); 
     Target aLine = new Target(); 
     Target bLine = new Target(); 
     aLine.setX(-65000); 
     aLine.setY(ress.getObstacle().getLine()); 
     bLine.setX(65000); 
     bLine.setY(ress.getObstacle().getLine()); 
     Line2D line = new Line2D.Float(aLine.getX(), aLine.getY(), bLine.getX(), bLine.getY()); 

     List<Target> list = new ArrayList<Target>(); 

     if (!(ress.getObstacle().getLine() == 0)) { 
      //check if points are there , if yes just reinitialize a linea-lineb and calculate the same in for: 

      String a = ""; 
      try { 
       a = ress.getObstacle().getA().toStrin`enter code here`g(); 
      } catch (NullPointerException e) { 

      } 
      if (!(a == "")) { 
       aLine.setX(ress.getObstacle().getA().getX()); 
       aLine.setY(ress.getObstacle().getLine()); 
       bLine.setX(ress.getObstacle().getB().getX()); 
       bLine.setY(ress.getObstacle().getLine()); 
       Line2D lineNew = new Line2D.Float(aLine.getX(), aLine.getY(), bLine.getX(), bLine.getY()); 

       for (Target t : ress.getTargets()) { 
        Line2D line2 = new Line2D.Float(start.getX(), start.getY(), t.getX(), t.getY()); 
        if (!line2.intersectsLine(lineNew)) { 
         list.add(t); 
        } 
       } 
      } else { 
     //-------------------start old part---------------------------------- 
       for (Target t : ress.getTargets()) { 
         Line2D line2 = new Line2D.Float(start.getX(), start.getY(), t.getX(), t.getY()); 
         if (!line2.intersectsLine(line)) { 
          list.add(t); 
         } 
       } 
       ///////-------end old part 
      } 

     } else { 
      double angA = Math.toDegrees(StrictMath.atan2(ress.getObstacle().getA().getX() - start.getX(), ress.getObstacle().getA().getY() - start.getY())); 
      double angB = Math.toDegrees(StrictMath.atan2(ress.getObstacle().getB().getX() - start.getX(), ress.getObstacle().getB().getY() - start.getY())); 

      Boolean up = (ress.getObstacle().getA().getY()>0)&(ress.getObstacle().getB().getY()>0); 
      Boolean left = (ress.getObstacle().getA().getX()<0)&(ress.getObstacle().getB().getX()<0); 
      Boolean right = (ress.getObstacle().getA().getX()>0)&(ress.getObstacle().getB().getX()>0); 

      for (Target t : ress.getTargets()) { 
       double angT = Math.toDegrees(StrictMath.atan2(t.getX() - start.getX(), t.getY() - start.getY())); 

       if (up) { 

        if (!((angT > Math.min(angA,angB)) & (angT < Math.max(angB,angA)))) 
         list.add(t); 
       } else 
        if (right || left) { 
        if (!((angT > Math.min(angA,angB)) & (angT< Math.max(angB,angA)))) { 
         list.add(t); 
        } 
       } else 
        { 
         if (((angT > Math.min(angA,angB)) & (angT< Math.max(angB,angA)))) { 
          list.add(t); 
         } 
        } 
      } 
     } 


     sol.setTargets(list);