答案 0 :(得分:1)
“最佳”转换方式是将度数四舍五入到下一个象限,这取决于 指南针有多少基数点......
假设你有一段时间只有4个北,南,西和东,
然后尝试转换180°将与南方完全匹配(假设北方是0°= 360°),但我也预计到南方也会有200°点,因为它更靠近那个方向而不是西方。 ..
相同的160°标准,因为它更靠近南方而不是东方
定义枚举
基数点
enum CPoint {
NORTH,
EAST,
SOUTH,
WEST
}
and test it like:
public static void main(String[] args) {
final int divisor = 360 / CPoint.values().length;
for (int i = 0; i < 360; i++) {
final int coci = i / divisor;
final int resto = i % divisor;
if (resto <= divisor / 2) {
System.out.println(i + "--->" + CPoint.values()[coci % CPoint.values().length]);
} else {
System.out.println(i + "--->" + CPoint.values()[(coci + 1) % CPoint.values().length]);
}
}
}
输出将显示循环如何将 i 的值四舍五入到最近的基数点
这个方法的最好的部分是你可以定义一个方法,它接受一个整数作为参数,并将枚举常量返回到基数点......
那么你可以做一些很好的事情,比如一个案例或者类似的东西: - )
答案 1 :(得分:0)
A&#34;低水平&#34;解决方案是:你实际上想要映射&#34; 360度&#34;进入&#34; 16个不同的班级&#34;。这很简单:你将学位值除以16;然后你使用你从它得到的四舍五入的数字作为索引包含相应的&#34;名称&#34;对于方向(加上一些按摩以获得0和360度相同的结果)。
示例:21度/ 16 - > 1.3舍入到1
该阵列中的第一个条目是&#34; N&#34; ......所以你去。
但正如所说,这是一个非常低级的解决方案;如果你想要将来带给你的东西,以及改变/扩展的要求;您最好创建所涉及概念的真实OO抽象。也许你不会代表那些&#34;方向&#34;作为简单的字符串,也许你真的有代表度数的类,以及代表&#34;方向&#34;的类。
答案 2 :(得分:0)
地图会很好:
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Convert degrees to heading names
* User: mduffy
* Date: 7/21/2016
* Time: 7:49 AM
* @link http://stackoverflow.com/questions/38503399/what-is-the-best-way-to-convert-degrees-to-quadrant-names
*/
public class HeadingConverter {
private static final Map<Double, String> DEGREES_TO_HEADING;
// TODO: You need to add more to the Map
static {
DEGREES_TO_HEADING = new LinkedHashMap<Double, String>() {{
put(0.0, "E");
put(90.0, "N");
put(180.0, "W");
put(270.0, "S");
put(360.0, "E");
}};
}
public static void main(String[] args) {
for (String arg : args) {
double unnormalized = Double.parseDouble(arg);
System.out.println(String.format("angle: %10.3f heading: %s", unnormalized, HeadingConverter.getHeading(unnormalized)));
}
}
public static double normalizeDegrees(double unnormalized) {
// TODO: For you to complete
return unnormalized;
}
public static String getHeading(double unnormalized) {
String heading = "UNKNOWN";
double normalized = normalizeDegrees(unnormalized);
for (Double angle : DEGREES_TO_HEADING.keySet()) {
if (normalized <= angle) {
heading = DEGREES_TO_HEADING.get(angle);
break;
}
}
return heading;
}
}
答案 3 :(得分:0)
我需要此功能以适应天气。由于我没有找到满意的结果,所以这可能是java的类实现,该类需要学位并返回8个方向之一。它不精确,因为它在显示一般天气时用于显示风向。一个可以改善的...
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DegreeToQuadrant {
List<QuadrantItem> quadrants;
String[] QuadrantNames = new String[]{"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
DegreeToQuadrant() {
double leftValue = 360 - 22.5;
double rightValue = 22.5;
double step = 45;
quadrants = new ArrayList<QuadrantItem>();
for (String qname : QuadrantNames) {
QuadrantItem quadrantItem = null;
if (qname == "N")
quadrantItem = new QuadrantItem(qname, leftValue, rightValue, "or");
else {
leftValue = rightValue;
rightValue = leftValue + step;
quadrantItem = new QuadrantItem(qname, leftValue, rightValue, "and");
}
quadrants.add(quadrantItem);
}
}
String getQuadrantName(double degree) {
for (QuadrantItem quadrantItem : quadrants)
if (quadrantItem.isIn(degree))
return quadrantItem.m_quadrantName;
return "";
}
public static class QuadrantItem {
String m_quadrantName;
String m_operation; //or , and
double m_left;
double m_right;
HashMap<String, QuadrantItem> quadrants;
QuadrantItem(String name, double left, double right, String operation) {
m_quadrantName = name;
m_operation = operation;
m_left = left;
m_right = right;
}
boolean isIn(double degree) {
if (m_operation == "and")
return degree > m_left && degree < m_right;
else
return degree > m_left || degree < m_right;
}
}
}
注意:对于N值(北),操作应为度> leftLimit或度 使用方法:
以wind_deg
为风度,获取要显示的象限:DegreeToQuadrant degreeConvertor = new DegreeToQuadrant();
String quadrantName = degreeConvertor.getQuadrantName(wind_deg);