Soc*_*ers 0 c# arrays min unity-game-engine
我正在添加到我的for循环中,以便在解析文本文件时找到数组中的最小值和最大值.找到最大值很容易,但是对于最小值,我得到零值,它应该是1.这是我用于循环的东西 - (我到目前为止,我只是测试得到最小的温度. X)
for (int i = 0; i < lineCount; i++) {
string line = dataLines [i];
lineValues = line.Split (' ');
Vector4 temp = new Vector4 ();
Vector3 center = new Vector3 ();
temp.x = float.Parse (lineValues [0]);
maxvalueX = float.MinValue;
minvalueX = float.MaxValue;
if (temp.x > maxvalueX) { maxvalueX = temp.x; }
if (temp.x < minvalueX) { minvalueX = temp.x; }
temp.y = float.Parse (lineValues [1]);
if (temp.y > maxvalueY) { maxvalueY = temp.y; }
temp.z = float.Parse (lineValues [2]);
if (temp.z > maxvalueZ) { maxvalueZ = temp.z; }
temp.w = float.Parse (lineValues [3]);
data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
//int value = data.myData [i].x;
center.x = ((maxvalueX-1)/2);
center.y = ((maxvalueY-1)/2);
center.z = ((maxvalueZ-1)/2);
data.dataCenter.Set (scaleFactor*center.x, scaleFactor*center.y, scaleFactor*center.z);
}
Run Code Online (Sandbox Code Playgroud)
关于这里出了什么问题的任何想法将不胜感激!谢谢!仁
找到最小值的最简单方法是使用与最大值相反的方法,即
if (temp.x > maxvalueX) { maxvalueX = temp.x; }
if (temp.x < minvalueX) { minvalueX = temp.x; }
Run Code Online (Sandbox Code Playgroud)
如果最低值可能高于0或最高值低于0,则将最大值和最小值设置为0会导致问题.在循环之前,你应该像这样设置它们:
float maxvalueX = float.MinValue;
float minvalueX = float.MaxValue;
Run Code Online (Sandbox Code Playgroud)
这样,无论遇到什么价值,您都可以确定数字会被重置.确保你处理他们没有值的情况也要考虑.
更新:
我已经包含了代码的完整更新版本以及评论以证明我的意思:
// These should be before the loop just to avoid resetting the max/min values
float maxvalueX = float.MinValue;
float minvalueX = float.MaxValue;
for (int i = 0; i < lineCount; i++) {
string line = dataLines [i];
lineValues = line.Split (' ');
Vector4 temp = new Vector4 ();
Vector3 center = new Vector3 ();
temp.x = float.Parse (lineValues [0]);
if (temp.x > maxvalueX) { maxvalueX = temp.x; }
if (temp.x < minvalueX) { minvalueX = temp.x; }
temp.y = float.Parse (lineValues [1]);
if (temp.y > maxvalueY) { maxvalueY = temp.y; }
temp.z = float.Parse (lineValues [2]);
if (temp.z > maxvalueZ) { maxvalueZ = temp.z; }
temp.w = float.Parse (lineValues [3]);
data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
//int value = data.myData [i].x;
}
// I've moved these out of the loop as they only use the final max/min values
center.x = ((maxvalueX-1)/2);
center.y = ((maxvalueY-1)/2);
center.z = ((maxvalueZ-1)/2);
data.dataCenter.Set (scaleFactor*center.x, scaleFactor*center.y, scaleFactor*center.z);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
279 次 |
最近记录: |