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 4256566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:22:19+00:00 2026-05-21T05:22:19+00:00

I have the following code, and I can see that there is a connection

  • 0

I have the following code, and I can see that there is a connection between the values. I can however not get my head together and figure out how to remove the switch statements with some add subtract modelo haxx. Would really appriciate some help to get this method smoother.

The heading variable is either 0, 1, 2 or 3.

public static int getRotation(Point currentPoint, Point nextPoint, int heading) {
    int rotation = -1;
    if(currentPoint.getX() < nextPoint.getX()) /* DRIVE WEST */  {
        switch(heading) {
            case 0: rotation = 1; break;
            case 1: rotation = 0; break;
            case 2: rotation = 3; break;
            case 3: rotation = 2; break;
        }
    } else if(currentPoint.getX() > nextPoint.getX()) /* DRIVE EAST */ {
        switch(heading) {
            case 0: rotation = 3; break;
            case 1: rotation = 2; break;
            case 2: rotation = 1; break;
            case 3: rotation = 0; break;
        }
    } else if(currentPoint.getY() < nextPoint.getY()) /* DRIVE  NORTH */ {
        switch(heading) {
            case 0: rotation = 0; break;
            case 1: rotation = 3; break;
            case 2: rotation = 2; break;
            case 3: rotation = 1; break;
        }
    }  else if(currentPoint.getY() > nextPoint.getY()) /* DRIVE SOUTH */ {
        switch(heading) {
            case 0: rotation = 2; break;
            case 1: rotation = 1; break;
            case 2: rotation = 0; break;
            case 3: rotation = 3; break;
        }
    }

    return rotation;
}

EDIT: I did forget to mention that nextPoint can only be +-x or y compared to currentPoint. if currentPoint is (0,0) newPoint must be either (-1,0), (1,0), (0,-1) or (0,1).

  • 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-05-21T05:22:19+00:00Added an answer on May 21, 2026 at 5:22 am

    You could try.

    int eastWest = Double.compare(currentPoint.getX(), nextPoint.getX()) 
    if (eastWest == 0) {
        int northSouth = Double.compare(currentPoint.getX() , nextPoint.getX()) 
        /* DRIVE NORTH (-1), SOUTH (+1) */ 
        if (northSouth == 0)
           rotation = -1;
        else
           rotation = (5 + northSouth - heading) % 4;
    } else {
        /* DRIVE WEST (-1), EAST (+1) */  
        rotation = heading ^ (2 + eastWest);
    }
    

    Note: I would expect to see some symetry to these functions are each direction appears to have a different combination and doesn’t sound right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code: As you can see there is a for
I have the following code, but I can't get it to work. As soon
I have the following code from apache's svn. As you can see this is
In my example code below, you can see that I have been trying to
I have the following code and I can't understand what does it mean: var1
i have following code for moving 4 picture around circular way,but it can rotate
How I can start reading from a specific byte? I have following code: try
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
In the following code how can I still have the second and third statement
I have the following code block for validation in PHP and I can't figure

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.