该法典是 Java,因为我可以记住VB syntax。
斜度。 不要重复这一法典,使之成为编号方法。 或者具有两个参数的头等功能,无论贵国的语文如何支持。
Number.prototype.toDegrees = function (unit) {
const u = (unit || "d").toLowerCase().charAt(0);
switch (u) {
case r : //radians
return this * 180 / Math.PI;
case g : //grads
return this * 200 / Math.PI;
case d : //degrees
return this;
default: //radians
return this * 180 / Math.PI;
}
};
平均范围。
Number.prototype.toNormalisedDegrees = function () {
return (this - 360 * (Math.trunc(this / 360) - 1)) % 360;
}
浏览90度,扭转方向。
Number.prototype.toCompassBearing = function (unit) {
const degrees = unit && unit.toLowerCase().charAt(0) === "r" ? this.toDegrees() : this;
return (90 - degrees).toNormalisedDegrees();
}
Test with some known values
Number.prototype.toDegrees = function (unit) {
const u = (unit || "d").toLowerCase().charAt(0);
switch (u) {
case r :
return this * 180 / Math.PI;
case g :
return this * 200 / Math.PI;
default:
return this;
}
};
Number.prototype.toNormalisedDegrees = function () {
return (this - 360 * (Math.trunc(this / 360) - 1)) % 360;
}
Number.prototype.toCompassBearing = function (unit) {
return (90 - this.toDegrees(unit)).toNormalisedDegrees();
}
console.log(`0 cartesian (E) => ${(0).toCompassBearing("d")} compass bearing`);
console.log(`90 cartesian (N) => ${(90).toCompassBearing("d")} compass bearing`);
console.log(`90-22.5 cartesian (NNE) => ${(90-22.5).toCompassBearing("d")} compass bearing`);
console.log(`180 cartesian (W) => ${(180).toCompassBearing("d")} compass bearing`);
console.log(`-68 cartesian => ${(-68).toCompassBearing("d")} compass bearing`);
console.log(`-68 compass bearing => ${(-68).toNormalisedDegrees()} normalised degrees`);
console.log(`270 cartesian (S) => ${(270).toCompassBearing("d")} compass bearing`);
console.log(`0 cartesian (E) => ${(0).toCompassBearing("r")} compass bearing`);
console.log(`PI/2 cartesian (N) => ${(0.5 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`PI cartesian (W) => ${Math.PI.toCompassBearing("r")} compass bearing`);
console.log(`1.5 PI cartesian (S) => ${(1.5 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`2 PI cartesian (E) => ${(2 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`20 PI cartesian (E) => ${(20 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`-2 PI cartesian (E) => ${(-2 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`-2.5 PI cartesian (S) => ${(-2.5 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`-3 PI cartesian (W) => ${(-3 * Math.PI).toCompassBearing("r")} compass bearing`);
console.log(`-3.5 PI cartesian (N) => ${(-3.5 * Math.PI).toCompassBearing("r")} compass bearing`);
If 292 is right, what s this 248 business?
您询问如何将客体标题改为正常程度,但你提供的样本代码以及你认为正确的结果都表明,你实际上想要做的是确定拉坦、Lng pairs的起始点和终点点所确定的标题,并以正常程度表示。
There s an article here suggesting a method and your code looks like a botched attempt to implement this.
如果你在 Java文中正确的话,就是这样。
Number.prototype.toDegrees = function(unit) {
const u = (unit || "d").toLowerCase().charAt(0);
switch (u) {
case r :
return this * 180 / Math.PI;
case g :
return this * 200 / Math.PI;
default:
return this;
}
};
Number.prototype.toRadians = function() {
return this * Math.PI / 180;
}
Number.prototype.toNormalisedDegrees = function() {
return (this - 360 * (Math.trunc(this / 360) - 1)) % 360;
}
function bearing(lat1, lng1, lat2, lng2) {
const X = Math.cos(lat2) * Math.sin(lng2 - lng1);
const Y = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lng2 - lng1);
return Math.atan2(X, Y);
}
const lat1 = (7.337361).toRadians();
const lng1 = (81.626198).toRadians();
const lat2 = (7.000667).toRadians();
const lng2 = (80.773737).toRadians();
const bearingRadians = bearing(lat1, lng1, lat2, lng2);
const bearingDegrees = bearingRadians.toDegrees("radians");
console.log(`bearingRadians = ${bearingRadians}`);
console.log(`bearingDegrees = ${bearingDegrees}`);
console.log(`normalisedDegrees = ${bearingDegrees.toNormalisedDegrees()}`);
有两点值得注意。
- Latitude and Longitude are in the compass frame of reference and do not require conversion from the cartesian frame of reference.
- This code is written so that the result is in the compass frame of reference.
- The result is in radians and then converted to degrees.
- The degrees are then normalised to the 0-360 range.
- Using your inputs I get your expected output.