2013-12-22 49 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); 

    } 

} 
相关问题