Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7759795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:43:06+00:00 2026-06-01T13:43:06+00:00

Sorry if the title sounds confusing – but this is what I am trying

  • 0

Sorry if the title sounds confusing – but this is what I am trying to do:

I have a large circular button on which I detect touch direction. I am able to find the UP/DOWN/LEFT/RIGHT from the dy and dx of the change in touch input coordinates like this:

          if(Math.abs(dX) > Math.abs(dY)) {
              if(dX>0) direction = 1; //left
              else direction = 2; //right
          } else {
              if(dY>0) direction = 3; //up
              else direction = 4;   //down
          }

But now I would like to handle cases where the button can be slightly rotated and thus the touch direction will also need to adjust for this. For example, if the button is rotated slightly to the left, then UP is now the finger moving northwest instead of just pure north. How do I handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:43:07+00:00Added an answer on June 1, 2026 at 1:43 pm

    Use Math.atan2(dy, dx) to get the angle anticlockwise from the positive horizontal of the coordinate in radians

    double pressed = Math.atan2(dY, dX);
    

    subtract the rotation amount (anticlockwise rotation amount in radians) from this angle, putting the angle into the coordinate system of the button

    pressed -= buttonRotation;
    

    or if you have your angle in degrees, convert it to radians

    pressed -= Math.toRadians(buttonRotation);
    

    You can then calculate an easier direction number from this angle

    int dir = (int)(Math.round(2.0d*pressed/Math.PI) % 4);
    

    This gives right 0, up 1, left 2 and down 3. We need to correct the case where the angle is negative, as the modulo result will also be negative.

    if (dir < 0) {
        dir += 4;
    }
    

    Now supposing that these numbers are bad and you don’t want to use them, you can just switch on the result to return whatever you like for each direction. Putting that all together:

    /**
     * @param dY
     *      The y difference between the touch position and the button
     * @param dX
     *      The x difference between the touch position and the button
     * @param buttonRotationDegrees
     *      The anticlockwise button rotation offset in degrees
     * @return 
     *      The direction number
     *      1 = left, 2 = right, 3 = up, 4 = down, 0 = error
     */
    public static int getAngle(int dY, int dX, double buttonRotationDegrees)
    {
        double pressed = Math.atan2(dY, dX);
        pressed -= Math.toRadians(buttonRotationDegrees);
    
        // right = 0, up = 1, left = 2, down = 3
        int dir = (int)(Math.round(2.0d*pressed/Math.PI) % 4);
    
        // Correct negative angles
        if (dir < 0) {
            dir += 4;
        }
    
        switch (dir) {
            case 0:
                return 2; // right
            case 1:
                return 3; // up
            case 2:
                return 1; // left;
            case 3:
                return 4; // down
        }
        return 0; // Something bad happened
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this title impossible to understand. I have this: <a class=directory> Directory 1
sorry about the title, I'm not sure how to even describe this, which makes
Sorry if my title is confusing. I have an assignment due that has to
Sorry or the confusing title! It's actually a lot simpler than it sounds. I've
Sorry if the title seems confusing, but some examples are in order. Let's say
Sorry about the title, don't know how to phrase this. I have my own
(Sorry if the title is pretty useless) I have this function to get the
First of all Sorry if my question title sounds stupid.... I have the following
Sorry the title is not very clear. This is a follow up to my
Sorry if the title doesn't make sense. Basically I have a series of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.