我需要一种方法将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度是圆的底侧。我尝试使用上面的代码修复它(使用不同的象限,但这不起作用)。接受的答案对我有用。
答案 0 :(得分:3)
我不确定你想要达到的目标,但是使用atan2功能获得一个角度是不是更容易?
http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,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轴与正顺时针方向之间的角度。我希望这就是你一直在寻找的。 p>
答案 1 :(得分:0)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().realmName("ad-ldap");
http.formLogin().loginPage("/login").loginProcessingUrl("/loginProcess");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userSearchBase("OU=users,DC=local")
.userSearchFilter("(sAMAccountName={0})")
.groupSearchBase("OU=groups,DC=local")
.groupSearchFilter("sAMAccountName={0}");
}
}
var x,x1,x2,y,y1,y2;
var cells = 'cell0';
var h,w;
var cx,cy;
var dx,dy;
var derajat;
var deg;
var ang;
var light;
var control;
function mouse_watch(event){
x = event.clientX;
y = event.clientY;
cell_data(cells);
koordinat(x2,y2);
busur(derajat);
}
function koordinat(x2,y2){
x2 = x-cx;
y2 = y-cy;
yk = y2;
xk = x2;
}
function busur(derajat){
y1 = Math.sqrt((Math.abs(yk)*Math.abs(yk))+(Math.abs(xk)*(Math.abs(xk))));
x1 = 0;
dy = yk-y1;
dx = xk-x1;
rad = Math.atan2(dy, dx);
derajat = rad * (360 / Math.PI);
cell = document.getElementById(cells);
ang = cell.getElementsByClassName('angle0')[0];
ang.style.transform = 'rotate('+derajat+'deg)';
light = ang.getElementsByClassName('points')[0];
light.style.height = y1+'px';
}
function cell_data(cells){
cell = document.getElementById(cells);
h = Number(cell.style.height.replace('px',''));
w = Number(cell.style.width.replace('px',''));
cy = Number(cell.style.top.replace('px',''))+h/2;
cx = Number(cell.style.left.replace('px',''))+w/2;
}
.preview_engine{
position: absolute;
top: 0;
left: 0;
padding: 10px;
background-color: #2E8AE6;
color: white;
}
body{
cursor: default;
width: 100%;
height: 100%;
font-family: Arial;
font-size: 12px;
}
.fieldwork{
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
}
.cell{
position: relative;
transition : width 2s, height 2s, top 2s, left 2s;
background-color: red;
}
.angle0{
width: 200px;
height: 200px;
position: absolute;
top: -75px;
left: -75px;
background-color: green;
border-radius: 50%;
opacity: 0.5;
transition : width 2s, height 2s, top 2s, left 2s;
}
.points{
width: 10px;
height: 10px;
position: absolute;
left: 95px;
top: 95px;
background-color: red;
border-radius: 1em;
opacity: none;
}
答案 2 :(得分: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);
}
}