第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

從角度數(shù)組順時(shí)針和逆時(shí)針獲取最接近的角度

從角度數(shù)組順時(shí)針和逆時(shí)針獲取最接近的角度

Smart貓小萌 2022-10-21 10:55:43
我從原點(diǎn)有四個(gè)角度的 div 角,假設(shè) θ 與這些角度比較,我還有一個(gè)新角度,現(xiàn)在我需要逆時(shí)針和順時(shí)針最接近 θ 的角度。θ = -26 // new anglea = [-15, 15, -165, -195]; // array of anglesanticlockangle = ? // closest angle anticlockwise to θ from arrayclockangle = ? // closest angle clockwise to θ from array
查看完整描述

4 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊

在我看來,這是最優(yōu)化的解決方案。我們按順序?qū)?shù)組進(jìn)行排序,然后根據(jù) find 和我們的條件查找數(shù)組的下一個(gè)值。


const θ = 90 // new angle

let a = [-15, 15, -165, -195]; // array of angle

a.sort((a, b) => a-b);


let anticlockangle;

for (i = θ; i > -360; i -= 1) {

  if (a.find(element => element === i)) {

    anticlockangle = i;

    break;

  }

}

if (anticlockangle === undefined) {

 anticlockangle = a[a.length - 1]; 

}


let clockangle;

for (i = θ; i < 360; i += 1) {

  if (a.find(element => element === i)) {

    clockangle = i;

    break;

  }

}

if (clockangle === undefined) {

  clockangle = a[0];

}


console.log(anticlockangle);

console.log(clockangle);


查看完整回答
反對 回復(fù) 2022-10-21
?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊

好的,根據(jù) ag-dev 在修訂歷史中的第一個(gè)答案,我想出了以下內(nèi)容,


θ = -26 // new angle

a = [-15, 15, -165, -195]; // array of angles


anticlockangle = getClosestAngle(θ,a,false);

clockangle = getClosestAngle(θ,a,true);


console.log(anticlockangle);

console.log(clockangle);


 function getClosestAngle(θ, arr, is_clock) {

    arr.sort();

    return is_clock ? arr.find(element => element < θ) : arr.find(element => element > θ);

  }


查看完整回答
反對 回復(fù) 2022-10-21
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

我的解決方案考慮了環(huán)繞并適用于任何角度。即使輸入范圍超出標(biāo)準(zhǔn)范圍 0°...360° 和 -180° ... +180°(就像示例中的 -195 值一樣)或者如果值是浮點(diǎn)數(shù)而不是整數(shù)。我現(xiàn)在修復(fù)了程序并用適當(dāng)?shù)?javascript 編寫了它 - 并在https://playcode.io/上對其進(jìn)行了測試

θi = -26;

ai = [-15, 15, -165, -195];

// loop test: ai = [-161.5, -82.6, +53.7, +174.8]; // array of angles


a = [0, 0, 0, 0]

for (i = 0; i < ai.length; i++)

{

    a[i] = getValueBetween0And360(ai[i]);

}

console.log(`a = ${ai} (${a})`);


// loop test: for(θi = -400; θi <= +400; θi += 33.3) // new angle

{

    θ = getValueBetween0And360(θi);

    //console.log(`θi = ${θi}, θ = ${θ}`);

    

    

    ccw = ai[getIndexOfNearestAngle(a, θ, -1)]; // closest angle anticlockwise to θ from array

    cw = ai[getIndexOfNearestAngle(a, θ, +1)]; // closest angle clockwise to θ from array

    console.log(`θ = ${θi.toFixed(1)} (${θ.toFixed(1)}),\tccw = ${ccw.toFixed(1)} (${getValueBetween0And360(ccw).toFixed(1)}),\tcw = ${cw.toFixed(1)} (${getValueBetween0And360(cw).toFixed(1)})`);


}


function getValueBetween0And360(input)

{

    if (input < 0)

    {

        return (input + (Math.trunc(-input / 360) + 1.0) * 360.0) % 360.0;

    }

    return input % 360.0;

}


function getValueBetweenPlusMinus180(input)

{

    in360 = getValueBetween0And360(input);

    return 180 < in360

             ? in360 - 360

             : in360;

}


// sign = +1 for clock wise (cw), -1 for counter clock wise (ccw)

// starting from angle θ towards found angle in array a

function getIndexOfNearestAngle(a, θ, sign)

{

    var iF = -1;

    var diffF = 1000;


    for (var i = 0; i < a.length; i++)

    {

        var diff = sign * getDiffClockWise(a[i], θ);

        var diffPos = getValueBetween0And360(diff);

        //console.log(`start a[${i}] = ${a[i]}, diffPos = ${diffPos}, iF = ${iF}, diffF = ${diffF}, sign = ${sign}`);

        

        if (diffPos < diffF)

        {

            diffF = diffPos;

            iF = i;

        }

        //console.log(`end   a[${i}] = ${a[i]}, diffPos = ${diffPos}, iF = ${iF}, diffF = ${diffF}`);

    }

    

    return iF;

}

 

function getDiffClockWise(a, θ)

{

    //console.log(`diff = ${a - θ}, a = ${a}, θ = ${θ}`)

    return a - θ;

}

結(jié)果是:


a = -15,15,-165,-195 (345,15,195,165)

θ = -26.0 (334.0),  ccw = -165.0 (195.0),   cw = -15.0 (345.0)

如果// loop test:在代碼中沒有替換,它會報(bào)告這些示例顯示環(huán)繞和浮動工作:


a = -161.5,-82.6,53.7,174.8 (198.5,277.4,53.7,174.8)

θ = -400.0 (320.0), ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = -366.7 (353.3), ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = -333.4 (26.6),  ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = -300.1 (59.9),  ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = -266.8 (93.2),  ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = -233.5 (126.5), ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = -200.2 (159.8), ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = -166.9 (193.1), ccw = 174.8 (174.8),    cw = -161.5 (198.5)

θ = -133.6 (226.4), ccw = -161.5 (198.5),   cw = -82.6 (277.4)

θ = -100.3 (259.7), ccw = -161.5 (198.5),   cw = -82.6 (277.4)

θ = -67.0 (293.0),  ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = -33.7 (326.3),  ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = -0.4 (359.6),   ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = 32.9 (32.9),    ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = 66.2 (66.2),    ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = 99.5 (99.5),    ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = 132.8 (132.8),  ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = 166.1 (166.1),  ccw = 53.7 (53.7),  cw = 174.8 (174.8)

θ = 199.4 (199.4),  ccw = -161.5 (198.5),   cw = -82.6 (277.4)

θ = 232.7 (232.7),  ccw = -161.5 (198.5),   cw = -82.6 (277.4)

θ = 266.0 (266.0),  ccw = -161.5 (198.5),   cw = -82.6 (277.4)

θ = 299.3 (299.3),  ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = 332.6 (332.6),  ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = 365.9 (5.9),    ccw = -82.6 (277.4),    cw = 53.7 (53.7)

θ = 399.2 (39.2),   ccw = -82.6 (277.4),    cw = 53.7 (53.7)


查看完整回答
反對 回復(fù) 2022-10-21
?
動漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

這不是最佳解決方案,但這樣的事情應(yīng)該會讓你接近:


const theta = -26

const angles = [-15, 15, -165, -195]

let lowestAngle = null

let highestAngle = null

let previousAngle = null

let nextAngle = null

for (let index = 0; index < a.length; index++) {

  const angle = angles[index]

  if (lowestAngle === null || angle < lowestAngle) {

    lowestAngle = angle

  }

  if (highestAngle === null || angle > highestAngle) {

    highestAngle = angle

  }

  if (angle < theta) {

    // If this is the first angle less than theta or if this angle is closer to theta than the current previous, save it

    if (previousAngle === null || angle > previousAngle) {

      previousAngle = angle

    }

  }

  else if (angle > theta) {

    // If this is the first angle greater than theta or if this angle is closer to theta than the current next, save it

    if (nextAngle === null || angle < nextAngle) {

      nextAngle = angle

    }

  }

  else {

    // angle matches theta...what do you want to do here?

  }

}

// No previous angle found; loop around to the highest angle

if (previousAngle === null) {

  previousAngle = highestAngle

}

// No next angle found; loop around to the lowest angle

if (nextAngle === null) {

  nextAngle = lowestAngle

}


查看完整回答
反對 回復(fù) 2022-10-21
  • 4 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號