2013-12-22 47 views
1

我需要一種將X和Y座標轉換爲角度的方法。我有這樣的代碼,但我需要這個佈局:將X,Y轉換爲角度

180 

90 270

360

private double getAngle(double xTouch, double yTouch) { 
    double x = xTouch - (slideBtn.getWidth()/2d); 
    double y = slideBtn.getHeight() - yTouch - (slideBtn.getHeight()/2d); 
    switch (getQuadrant(x, y)) { 
     case 1: 
      return Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 2: 
      return 180 - Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 3: 
      return 180 + (-1 * Math.asin(y/Math.hypot(x, y)) * 180/Math.PI); 
     case 4: 
      return 360 + Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     default: 
      return 0; 
    } 
} 

/** 
* @return The selected quadrant. 
*/ 
private static int getQuadrant(double x, double y) { 
    if (x >= 0) { 
     return y >= 0 ? 1 : 4; 
    } else { 
     return y >= 0 ? 2 : 3; 
    } 
} 

編輯,澄清問題:我的問題是,我得到了錯誤的角度x,y角度。例如:0/360度是圓的頂邊,180度是圓的底邊。我試圖使用上面的代碼修復它(使用不同的象限,但那不起作用)。接受的答案爲我做了訣竅。

回答

3

我不太確定你想要達到什麼目的,但是使用atan2函數來獲得角度並不容易嗎?

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,雙)

如果你不熟悉,你可以在這裏閱讀功能:

http://en.wikipedia.org/wiki/Atan2

編輯,代碼示例:

所以,這裏是一些代碼,我認爲是有用的,討論後

public static double getAngle(int x, int y) 
{ 
    return 1.5 * Math.PI - Math.atan2(y,x); //note the atan2 call, the order of paramers is y then x 
} 

基本上,它計算負Y軸與正向順時針方向之間的角度。我希望這是你一直在尋找的。

+0

當我做的方法getAngle()我得到了錯誤的角度佈局。所以我需要上面指定的角度佈局。 atan2沒有我想要的東西...頂部180度而不是90度。所以旋轉整個角度90度,但我不知道該怎麼做 – matthijs2704

+0

當你得到atan2的結果時,你應該只是添加PI/2逆時針旋轉角度90度,如果這是你的問題。但問題是我沒有得到它在你的佈局0度,角度增加順時針或逆時針? –

+0

0度與360度相同,但我會嘗試atan2!謝謝,我會在工作時接受你的答案 – matthijs2704

0

\t var x,x1,x2,y,y1,y2; 
 
\t \t var cells = 'cell0'; 
 
\t \t var h,w; 
 
\t \t var cx,cy; 
 
\t \t var dx,dy; 
 
\t \t var derajat; 
 
\t \t var deg; 
 
\t \t var ang; 
 
\t \t var light; 
 
\t \t var control; 
 
\t \t \t function mouse_watch(event){ 
 
\t \t \t \t x = event.clientX; 
 
\t \t \t \t y = event.clientY; 
 
\t \t \t \t cell_data(cells); 
 
\t \t \t \t koordinat(x2,y2); 
 
\t \t \t \t busur(derajat); 
 
\t \t \t } 
 
\t \t \t function koordinat(x2,y2){ 
 
\t \t \t \t x2 = x-cx; 
 
\t \t \t \t y2 = y-cy; 
 
\t \t \t \t yk = y2; 
 
\t \t \t \t xk = x2; 
 
\t \t \t } 
 
\t \t \t function busur(derajat){ 
 

 
\t \t \t \t y1 = Math.sqrt((Math.abs(yk)*Math.abs(yk))+(Math.abs(xk)*(Math.abs(xk)))); 
 
\t \t \t \t x1 = 0; 
 
\t \t \t \t dy = yk-y1; 
 
\t \t \t \t dx = xk-x1; 
 
\t \t \t \t rad = Math.atan2(dy, dx); 
 
\t \t \t \t derajat = rad * (360/Math.PI); 
 
\t \t \t \t cell = document.getElementById(cells); 
 
\t \t \t \t ang = cell.getElementsByClassName('angle0')[0]; 
 
\t \t \t \t ang.style.transform = 'rotate('+derajat+'deg)'; 
 
\t \t \t \t light = ang.getElementsByClassName('points')[0]; 
 
\t \t \t \t light.style.height = y1+'px'; 
 
\t \t \t } 
 
\t \t \t function cell_data(cells){ 
 
\t \t \t \t cell = document.getElementById(cells); 
 
\t \t \t \t h = Number(cell.style.height.replace('px','')); 
 
\t \t \t \t w = Number(cell.style.width.replace('px','')); 
 
\t \t \t \t cy = Number(cell.style.top.replace('px',''))+h/2; 
 
\t \t \t \t cx = Number(cell.style.left.replace('px',''))+w/2; 
 
\t \t \t }
.preview_engine{ 
 
\t \t \t \t position: absolute; 
 
\t \t \t \t top: 0; 
 
\t \t \t \t left: 0; 
 
\t \t \t \t padding: 10px; 
 
\t \t \t \t background-color: #2E8AE6; 
 
\t \t \t \t color: white; 
 
\t \t \t } 
 
\t \t \t body{ 
 
\t \t \t \t cursor: default; 
 
\t \t \t \t width: 100%; 
 
\t \t \t \t height: 100%; 
 
\t \t \t \t font-family: Arial; 
 
\t \t \t \t font-size: 12px; 
 
\t \t \t } 
 
\t \t \t .fieldwork{ 
 
\t \t \t \t width: 100%; 
 
\t \t \t \t height: 100%; 
 
\t \t \t \t position: absolute; 
 
\t \t \t \t top: 0px; 
 
\t \t \t \t left: 0px; 
 
\t \t \t } 
 
\t \t \t .cell{ 
 
\t \t \t \t position: relative; 
 
\t \t \t \t transition : width 2s, height 2s, top 2s, left 2s; 
 
\t \t \t \t background-color: red; 
 
\t \t \t } 
 
\t \t \t .angle0{ 
 
\t \t \t \t width: 200px; 
 
\t \t \t \t height: 200px; 
 
\t \t \t \t position: absolute; 
 
\t \t \t \t top: -75px; 
 
\t \t \t \t left: -75px; 
 
\t \t \t \t background-color: green; 
 
\t \t \t \t border-radius: 50%; 
 
\t \t \t \t opacity: 0.5; 
 
\t \t \t \t transition : width 2s, height 2s, top 2s, left 2s; 
 
\t \t \t } 
 
\t \t \t .points{ 
 
\t \t \t \t width: 10px; 
 
\t \t \t \t height: 10px; 
 
\t \t \t \t position: absolute; 
 
\t \t \t \t left: 95px; 
 
\t \t \t \t top: 95px; 
 
\t \t \t \t background-color: red; 
 
\t \t \t \t border-radius: 1em; 
 
\t \t \t \t opacity: none; 
 
\t \t \t }
\t \t <div class="fieldwork" onmousemove="mouse_watch(event)"> 
 
\t \t \t <div class='cell' id="cell0" style="width:50px;height:50px;top:200px;left:400px;"> 
 
\t \t \t \t <div class="angle0"> 
 
\t \t \t \t \t <div class="points"></div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div>

+0

運行並看到結果, –

+0

謝謝,但我問了一個java答案,而不是一個JavaScript的答案;)但我可能能夠重寫它的Java,所以謝謝! – matthijs2704

+0

你能幫我寫我的遊戲在Java? 我做了一個在瀏覽器中運行的JavaScript遊戲, ,但我堅持用於運行遊戲的gpu內存,如果遊戲創建更多對象,它會使Flash運行緩慢。 –

0

此類操縱桿的輸入轉換成一角度,0是正y軸爲0度,正x爲90,負y爲180,和負x 270

public class GetAngle { 

    public static void main(String[] args) { 

     System.out.println(getAngle(-1,0)); 

    } 

    public static double getAngle(double x, double y) { 

     // Checking if the joystick is at center (0,0) and returns a 'stand still' 
     // command by setting the return to 99999 
     if (x == 0 && y == 0) { 

      return (99999); 

      // Returns a value based on the quadrant and does some math to deliver the angle 
      // of the coordinates 
     } else if (x >= 0 && y > 0) { 

      return (90 - (Math.atan(y/x) * 180/Math.PI)); 

     } else if (x > 0 && y <= 0) { 

      return (90 - (Math.atan(y/x) * 180/Math.PI)); 

     } else if (x <= 0 && y < 0) { 

      return (180 + (Math.atan(y/x) * 180/Math.PI)); 

     } else if (x < 0 && y >= 0) { 

      return (270 - (Math.atan(y/x) * 180/Math.PI)); 

     } else 
      return (99999); 

    } 

} 
相關問題