Question

I need a way to convert X and Y coordinates to an angle. I have this code but I need this layout:

 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;
    }
}

Edit, to clarify the problem: The problem I had was that I was getting the wrong angles for the x,y angles. For instance: 0/360 degrees is the top side of the circle and 180 degrees is the bottom side of the circle. I tried to fix it using the code above (with different quadrants, but that didn't work). The accepted answer did the trick for me.

Était-ce utile?

La solution

I'm not really sure what are you trying to achieve, but isn't it easier to just use the atan2 function to get an angle?

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

If you are not familiar with the function you can read here:

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

Edit, code example:

So, here is some code I think that is useful, after the discussion

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
}

Basically, it calculates an angle between the negative Y-axis with the positive clockwise direction. I hope that is what you have been looking for.

Autres conseils

	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;
			}
		<div class="fieldwork" onmousemove="mouse_watch(event)">
			<div class='cell' id="cell0" style="width:50px;height:50px;top:200px;left:400px;">
				<div class="angle0">
					<div class="points"></div>
				</div>
			</div>
		</div>

This class converts the input of a joystick to an angle, 0 being the positive y axis being 0 degrees, positive x being 90, negative y as 180, and negative x as 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);

    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top