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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:27:25+00:00 2026-05-13T13:27:25+00:00

This weekend I spend a few minutes thrashing together an algorithm that would take

  • 0

This weekend I spend a few minutes thrashing together an algorithm that would take in a heading (in degrees) and return a String for the cardinal direction (I’m using it in an android compass application I’m using). What I ended up with was this:

private String headingToString(Float heading)
{
    String strHeading = "?";
    Hashtable<String, Float> cardinal = new Hashtable<String, Float>();
    cardinal.put("North_1", new Float(0));
    cardinal.put("Northeast", new Float(45));
    cardinal.put("East", new Float(90));
    cardinal.put("Southeast", new Float(135));
    cardinal.put("South", new Float(180));
    cardinal.put("Southwest", new Float(225));
    cardinal.put("West", new Float(270));
    cardinal.put("Northwest", new Float(315));
    cardinal.put("North_2", new Float(360));

    for (String key: cardinal.keySet())
    {
        Float value = cardinal.get(key);
        if (Math.abs(heading - value) < 30)
        {
            strHeading = key;
            if (key.contains("North_"))
            {
                strHeading = "North";
            }
            break;
        }
    }
    return strHeading;
}

My question is, is this the best way of doing this? It must have been done many times before although I haven’t done a search for examples on the web yet. Have any other people tried this and found a neater solution?

Edit for The Reverand’s Thilo’s, shinjin’s and Chrstoffer’s responses:

The Solution

public static String headingToString2(double x)
{
    String directions[] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"};
    return directions[ (int)Math.round((  ((double)x % 360) / 45)) ];
}
  • 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-13T13:27:25+00:00Added an answer on May 13, 2026 at 1:27 pm

    That’s fine in most cases, though to make it optimized and (IMO) cleaner, what you could do is find a function to relate the input heading to one used in the map.

    For example:
    (I’m pretty sure this is right, but you’ll want to check it)

    45* (int)Math.round((  ((double)x % 360) / 45))
    

    What this does is first x % 360 makes sure the heading is within a valid range. then

    45 * round(.../45)
    

    finds the closest multiple of 45.

    Now change your map to be

      HashMap<Integer, String> map = new HashMap<Integer, String>()
      map.put(0, "North")
      map.put(45, "Northeast")
      etc...
    

    So, now your algorithm becomes a fast mathemtical calculation rather than iterating through the map. Furthermore, you don’t need as Hashtable here since it provides constructs for concurrency (if I remember correctly) and in your case it would actually cause a performance decrease.

    Once again, the performance hit may be completely negligible for your needs.

    Edit for Thilo’s and shinjin’s suggestions:

    Instead of multiplying by 45, just keep the rest of the equation, which gives you values for 0-7, and make an array of your strings.

    String directions[] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}
    return directions[ (int)Math.round((  ((double)x % 360) / 45)) % 8 ]
    

    and you’ve got your problem solved in two lines.

    One note: Modulus won’t work correctly for negative numbers. If our input heading is negative, you’ll need to make it positive first.

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

Sidebar

Related Questions

I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
This weekend I was working on a project and I needed to use a
Hopefully the last NN question you'll get from me this weekend, but here goes
I am getting my senior project gallery-ready for a show this weekend. It is
The server at my old employer was rooted this past weekend and apparently the
I was plugging away on an open source project this past weekend when I
Okay, I've been struggling with this all weekend, and I've gotten plenty of help
Our networks guys installed this patch over the weekend to our single server sharepoint
I've been frustrated by this for the entire weekend, plus a day or two,
I wanted to make a tryout this weekend with Cassandra + PHP in my

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.