从学位课程以文本方式解析课程?
Parse course in textual way from course in degrees?
所以我在 node-red 中制作了一个指南针,它应该以度数 (int) 作为输入,以字符串 (course) 作为输出:
所以我需要接受整数并以字符串形式给出标题的函数。怎么做简单又靠谱?
我必须在字符串中转换航向 0-360 度,例如:NORTH,NORTH-EAST,EAST......
我尝试了以下方法:
var course = parseInt(courseFloatDegrees);
var courseTxt = "";
if (course >= 349 && course <= 11 || course <= 359 && course >= 349 || course === 0) courseTxt = "N";
else if (course >= 11 && course <= 33) courseTxt = "NNE";
else if (course >= 33 && course <= 56) courseTxt = "NE";
else if (course >= 56 && course <= 78) courseTxt = "ENE";
else if (course >= 78 && course <= 101 || course == 90) courseTxt = "E";
else if (course >= 101 && course <= 124) courseTxt = "ESE";
else if (course >= 124 && course <= 146) courseTxt = "SE";
else if (course >= 146 && course <= 168) courseTxt = "SSE";
else if (course >= 168 && course <= 191 || course == 180) courseTxt = "S";
else if (course >= 191 && course <= 214) courseTxt = "SSW";
else if (course >= 214 && course <= 236) courseTxt = "SW";
else if (course >= 236 && course <= 258) courseTxt = "WSW";
else if (course >= 258 && course <= 281 || course == 270) courseTxt = "W";
else if (course >= 281 && course <= 303) courseTxt = "WNW";
else if (course >= 303 && course <= 326) courseTxt = "NW";
else if (course >= 326 && course <= 349) courseTxt = "NNW";
else courseTxt = "INVALID"
但有时我什么也得不到(null-empty 字符串)或 "INVALID"。有谁知道在没有其他 if 语句的情况下快速简单的方法吗?
在代码中可以有更多的方法 'simply',但您的方法应该有效。它没有的原因是因为你的 if 语句在 'N' 区域周围乱七八糟。
if ((course >= 349 && course <= 11) || (course <= 359 && course >= 349) || (course === 0)) courseTxt = "N";
如果你只看前两个条件,它们是不合逻辑的。大于 349 但小于 11?那永远不会发生。如果您有 7 个学位的课程,则该课程目前不符合任何指定标准。
所以首先要做的就是解决这个问题。您需要调整行以使用 OR 而不是 AND
if(course < 11 || course > 349) courseTxt = "N";
现在您的代码将能够处理 360/0 度任意一侧的设置。
假设 course 始终小于或等于 360,这应该足以让您当前的代码正常工作。
您询问是否有办法避免所有 if 语句。可能有数百种方法可以做到这一点,但除了 if 或 case 语句之外,最简单的方法可能是使用数组来查找标题。这是如何完成的示例。您显然可以对步长值进行硬编码,但这样您就可以使用任意数量的更细粒度的标题更新您的数组,它仍然会 work.e
function getCourse(course)
{
// define our values
var degs = 360;
var strs =
["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
// make sure course is always within the expected range in case it is incremented past 360
course = course % degs;
// get the step amount based on the number of compass headings we have
var step = degs/strs.length;
// adjust for the last few degrees on the scale which will be north
if(course > degs - (step/2)) course += step/2;
// now just divide the course by the step and read off the relevant heading
var index = Math.floor(course / step);
return strs[index];
}
尼基,
我使用了一种类似于托比的方法——计算课程字符串数组的索引:
var deg = course % 360;
var dirs = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"];
var idx = Math.round(deg * (dirs.length-1)/360);
var dir = dirs[idx];
诀窍是在数组的开头和结尾重复"N"元素,并使用Math.round(...)
跳转到最接近的整数索引号。
所以我在 node-red 中制作了一个指南针,它应该以度数 (int) 作为输入,以字符串 (course) 作为输出:
所以我需要接受整数并以字符串形式给出标题的函数。怎么做简单又靠谱?
我必须在字符串中转换航向 0-360 度,例如:NORTH,NORTH-EAST,EAST......
我尝试了以下方法:
var course = parseInt(courseFloatDegrees);
var courseTxt = "";
if (course >= 349 && course <= 11 || course <= 359 && course >= 349 || course === 0) courseTxt = "N";
else if (course >= 11 && course <= 33) courseTxt = "NNE";
else if (course >= 33 && course <= 56) courseTxt = "NE";
else if (course >= 56 && course <= 78) courseTxt = "ENE";
else if (course >= 78 && course <= 101 || course == 90) courseTxt = "E";
else if (course >= 101 && course <= 124) courseTxt = "ESE";
else if (course >= 124 && course <= 146) courseTxt = "SE";
else if (course >= 146 && course <= 168) courseTxt = "SSE";
else if (course >= 168 && course <= 191 || course == 180) courseTxt = "S";
else if (course >= 191 && course <= 214) courseTxt = "SSW";
else if (course >= 214 && course <= 236) courseTxt = "SW";
else if (course >= 236 && course <= 258) courseTxt = "WSW";
else if (course >= 258 && course <= 281 || course == 270) courseTxt = "W";
else if (course >= 281 && course <= 303) courseTxt = "WNW";
else if (course >= 303 && course <= 326) courseTxt = "NW";
else if (course >= 326 && course <= 349) courseTxt = "NNW";
else courseTxt = "INVALID"
但有时我什么也得不到(null-empty 字符串)或 "INVALID"。有谁知道在没有其他 if 语句的情况下快速简单的方法吗?
在代码中可以有更多的方法 'simply',但您的方法应该有效。它没有的原因是因为你的 if 语句在 'N' 区域周围乱七八糟。
if ((course >= 349 && course <= 11) || (course <= 359 && course >= 349) || (course === 0)) courseTxt = "N";
如果你只看前两个条件,它们是不合逻辑的。大于 349 但小于 11?那永远不会发生。如果您有 7 个学位的课程,则该课程目前不符合任何指定标准。
所以首先要做的就是解决这个问题。您需要调整行以使用 OR 而不是 AND
if(course < 11 || course > 349) courseTxt = "N";
现在您的代码将能够处理 360/0 度任意一侧的设置。
假设 course 始终小于或等于 360,这应该足以让您当前的代码正常工作。
您询问是否有办法避免所有 if 语句。可能有数百种方法可以做到这一点,但除了 if 或 case 语句之外,最简单的方法可能是使用数组来查找标题。这是如何完成的示例。您显然可以对步长值进行硬编码,但这样您就可以使用任意数量的更细粒度的标题更新您的数组,它仍然会 work.e
function getCourse(course)
{
// define our values
var degs = 360;
var strs =
["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
// make sure course is always within the expected range in case it is incremented past 360
course = course % degs;
// get the step amount based on the number of compass headings we have
var step = degs/strs.length;
// adjust for the last few degrees on the scale which will be north
if(course > degs - (step/2)) course += step/2;
// now just divide the course by the step and read off the relevant heading
var index = Math.floor(course / step);
return strs[index];
}
尼基,
我使用了一种类似于托比的方法——计算课程字符串数组的索引:
var deg = course % 360;
var dirs = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"];
var idx = Math.round(deg * (dirs.length-1)/360);
var dir = dirs[idx];
诀窍是在数组的开头和结尾重复"N"元素,并使用Math.round(...)
跳转到最接近的整数索引号。