Cardinal direction algorithm in Java

后端 未结 5 1716
故里飘歌
故里飘歌 2021-02-04 05:22

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

5条回答
  •  广开言路
    2021-02-04 05:41

    in java:

    String _directions[] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
    
    public String getHeading(int hea) {
      return _directions[(int)Math.floor((hea % 360) / 45)];
    }
    

    In "java" case u must need to create a Class.

    in javascript:

    var _directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
    
    function getDirection (hea) {
      return _directions[Math.floor((hea % 360) / 45)];
    };
    

提交回复
热议问题