2014-04-13 86 views
0

怪異的行爲我有這樣的代碼:用max函數

largestDeviation = max(max(max(angleBetweenVectors(robustNormal, normalAtA), 
           angleBetweenVectors(robustNormal, normalAtB)), 
          angleBetweenVectors(robustNormal, normalAtC)), 
         angleBetweenVectors(robustNormal, normalAtD)), 
         angleBetweenVectors(robustNormal, normalAtE); 

的方法:angleBetweenVectors返回float

我看不到這是什麼代碼在做什麼,它爲什麼它實際上正在建設,括號不正確匹配,我怕它會導致錯誤。我怎麼才能得到我所有撥打angleBetweenVectors的電話的最大值?

+1

我會通過使一個'的std ::矢量簡化它',使用'的push_back()'添加你漂浮在矢量上,然後調用'std :: max'找到最大的。它可能不是非常快,但它是可讀的。 – Steve

回答

4

添加一些indetation,你會得到:

largestDeviation = 
max(
    max(
     max(
     angleBetweenVectors(robustNormal, normalAtA), 
     angleBetweenVectors(robustNormal, normalAtB) 
    ), 
     angleBetweenVectors(robustNormal, normalAtC) 
    ), 
    angleBetweenVectors(robustNormal, normalAtD) 
), 
angleBetweenVectors(robustNormal, normalAtE); 

即這將返回值angleBetweenVectors(robustNormal, normalAtE);因爲你缺少一個max

+0

縮進能創造奇蹟,謝謝。 – Ogen

3

您可以使用標準算法的std ::最大的是具有作爲參數std::initializer_list 例如

#include <algorithm> 
//... 

largestDeviation = std::max(
{ 
    angleBetweenVectors(robustNormal, normalAtA), 
    angleBetweenVectors(robustNormal, normalAtB), 
    angleBetweenVectors(robustNormal, normalAtC), 
    angleBetweenVectors(robustNormal, normalAtD), 
    angleBetweenVectors(robustNormal, normalAtE) 
}); 

至於你的代碼,然後它確定由函數angleBetweenVectors的相應調用返回的值中的最大值。該代碼使用標準算法std::max,該算法具有兩個參數並確定兩個參數之間的最大值。

舉例內呼叫

max(angleBetweenVectors(robustNormal, normalAtA), 
    angleBetweenVectors(robustNormal, normalAtB)), 

發現由該函數調用返回的兩個值之間的最大值。函數std::max的結果與下一個調用一起使用,如果該函數依次在封閉的std::max調用中使用。

通過您的代碼編譯錯誤的方式應該是std::max一個多個呼叫

largestDeviation = 

max(
    max(
     max(
     max(angleBetweenVectors(robustNormal, normalAtA), 
       angleBetweenVectors(robustNormal, normalAtB) 
     ), 
     angleBetweenVectors(robustNormal, normalAtC) 
    ), 
     angleBetweenVectors(robustNormal, normalAtD) 
    ), 
    angleBetweenVectors(robustNormal, normalAtE) 
); 
2

你也可以寫你這樣的代碼:

maxAngle = angleBetweenVectors(robustNormal, normalAtA); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtB), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtC), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtD), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtE), maxAngle); 

或者,更一致,當爲maxAngle選擇一個好的初始值,例如我要說-2*M_PI

maxAngle = -2 * M_PI; 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtA), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtB), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtC), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtD), maxAngle); 
maxAngle = max(angleBetweenVectors(robustNormal, normalAtE), maxAngle); 
+0

這不是回答這個問題。 – juanchopanza

+0

是的,沒有回答,但我希望這是幫助。完整的答案由@Ed Heal給出。 –

1

C++ 11

#include <algorithm> 

largestDeviation = std::max({ 
    angleBetweenVectors(robustNormal, normalAtA), 
    angleBetweenVectors(robustNormal, normalAtB), 
    angleBetweenVectors(robustNormal, normalAtC), 
    angleBetweenVectors(robustNormal, normalAtD), 
    angleBetweenVectors(robustNormal, normalAtE) 
}); 
0
// Store all your normals in a vector 
std::vector<normal> normals; 
normals.push_back(normalAtA); 
normals.push_back(normalAtB); 
normals.push_back(normalAtC); 
normals.push_back(normalAtD); 
normals.push_back(normalAtE); 

// Iterate through your vector, and check each angle to see if it is the new max 
max_val = 0; 
vector<normal>::iterator it; 
for (it = normals.begin(); it != normals.end(); ++it) 
{ 
    max_val = max(max_val, angleBetweenVectors(robustNormal, *it)); 
} 
+1

似乎有點矯枉過正 –

+0

@EdHeal是的,這可能是這個簡單的情況。我只是想着想一些更一般的情況,以防他們有更多的法線可供比較。 – CoryKramer