您可以标识形成区域边界的多段线线段.获得该温度后,您可以使用给定的温度(x坐标)来查找围绕该温度的线段(每个边界最多一个),并确定这些线段中仍高于给定点的最低线段.如果从下(A)到上(J)迭代边界,则具有高于给定压力海拔高度的线段的第一个边界将标识该区域.
最主要的工作是识别多段线的线段.给你:
[
[[-55.2,29],[-50,160],[-40.7,335],[-30.2,510],[23.5,1239],[29.3,1050],[32.2,496],[38.3,-1370],[40.1,-2012]],
[[-55.2,2230],[10.2,3017],[16,3090],[22.4,3032],[27.6,2595],[31.9,2041],[34.6,1458],[40.1,58],[42.4,-641],[46,-2012]],
[[-55.2,4169],[1.5,4956],[11.9,5087],[19.9,5117],[24.7,4548],[33.1,3178],[37.5,2332],[40.4,1662],[43,1020],[46.8,0],[53.2,-2012]],
[[-55.2,5787],[-20,6210],[7,6516],[15.5,6531],[21.9,6210],[26.4,5510],[36.9,3411],[41,2566],[47.1,1137],[50.2,262],[57.9,-2012]],
[[-55.2,7551],[-0.3,8178],[7.8,8324],[16.3,8149],[23.2,7420],[28.8,6618],[31.9,6020],[40.4,4198],[50,2012],[54,1020],[64.9,-2012]],
[[-55.2,8863],[7.3,9679],[15.5,9344],[22.7,8455],[28.6,7580],[32.5,6880],[34.9,6443],[38.6,5685],[41.5,5015],[52.6,2507],[57.5,1283],[69.4,-2012]],
[[20.5,10029],[27.7,8980],[31.7,8338],[34.1,7901],[38.6,7026],[43.6,6006],[50.1,4548],[56.5,3032],[60.7,2041],[75,-1749]],
[[32.2,10029],[35.6,9519],[40.1,8688],[43.4,8017],[51,6414],[57.2,5015],[63.3,3557],[73.2,1050],[75,525]],
[[36.5,10029],[39.9,9592],[45.5,8513],[50.2,7522],[54.7,6531],[59,5510],[63,4519],[67.1,3528],[71.2,2536],[75,1633]],
]
这里有一个小片段,它使用这些信息来获得给定温度和气压-海拔的区域:
// The region's border lines specified as polyline coordinates:
const borders = [[[-55.2,29],[-50,160],[-40.7,335],[-30.2,510],[23.5,1239],[29.3,1050],[32.2,496],[38.3,-1370],[40.1,-2012]],[[-55.2,2230],[10.2,3017],[16,3090],[22.4,3032],[27.6,2595],[31.9,2041],[34.6,1458],[40.1,58],[42.4,-641],[46,-2012]],[[-55.2,4169],[1.5,4956],[11.9,5087],[19.9,5117],[24.7,4548],[33.1,3178],[37.5,2332],[40.4,1662],[43,1020],[46.8,0],[53.2,-2012]],[[-55.2,5787],[-20,6210],[7,6516],[15.5,6531],[21.9,6210],[26.4,5510],[36.9,3411],[41,2566],[47.1,1137],[50.2,262],[57.9,-2012]],[[-55.2,7551],[-0.3,8178],[7.8,8324],[16.3,8149],[23.2,7420],[28.8,6618],[31.9,6020],[40.4,4198],[50,2012],[54,1020],[64.9,-2012]],[[-55.2,8863],[7.3,9679],[15.5,9344],[22.7,8455],[28.6,7580],[32.5,6880],[34.9,6443],[38.6,5685],[41.5,5015],[52.6,2507],[57.5,1283],[69.4,-2012]],[[20.5,10029],[27.7,8980],[31.7,8338],[34.1,7901],[38.6,7026],[43.6,6006],[50.1,4548],[56.5,3032],[60.7,2041],[75,-1749]],[[32.2,10029],[35.6,9519],[40.1,8688],[43.4,8017],[51,6414],[57.2,5015],[63.3,3557],[73.2,1050],[75,525]],[[36.5,10029],[39.9,9592],[45.5,8513],[50.2,7522],[54.7,6531],[59,5510],[63,4519],[67.1,3528],[71.2,2536],[75,1633]]];
// Function to determine the region (A, B, ...) that corresponds to a
// given temperature (C°) and pressure-altitude (Ft):
function getRegion(temperatureCelcius, pressureAltitudeFeet) {
// Return null when the inputs are out of range:
if (temperatureCelcius < -55 || temperatureCelcius > 75) return null;
if (pressureAltitudeFeet < -2000 || pressureAltitudeFeet > 10000) return null;
// Find the region:
const region = borders.findIndex(border => {
// Find the segment on this polyline that concerns the given temperature
const i = border.findIndex(([x1]) => x1 >= temperatureCelcius);
if (i < 1) return !i; // There is no such segment: can be indication of region or not.
// Get coordinates of the line segment that concerns this temperature
const [[x0, y0], [x1, y1]] = border.slice(i - 1, i + 1);
// Is our point below this line segment? Then we have identified the region
return (pressureAltitudeFeet - y0) * (x1 - x0) < (temperatureCelcius - x0) * (y1 - y0);
});
return "JABCDEFGHI"[region+1]; // Translate the region index to a letter
}
// Demo for a temperature of 30°C and 5000 ft pressure-altitude.
const region = getRegion(30, 5000);
console.log(region); // "E"
如果给定输入的任一维度的值超出范围,则该函数将返回null
.