- Home /
How to have a loop run itself again if returned value is a known exception, with a new return value.
I'm having a bit of a hard time trying describe what i'm trying to accomplish...
I'm trying to create a loop that will throw out a known exception value, then run itself again for a new desired value.
For an example, lets say I want to find the Closest point, 2nd Closest and 3rd Closest point from m_directions[i] Finding the closest is easy enough.
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class QuestionThrowLoop : MonoBehaviour
{
[Range(1f, 100f)] public int viewResolution = 1000;
public List<float> distances = new List<float>();
///Pseudocode. i already have a method for generating all the Vector3 points, its not relevant.
public List<Vector3> m_directions = new List<Vector3>();
///Just know that the number of elements in m_directions == viewResolution.
public void LateUpdate()
{
for (int i = 0; i < viewResolution; i++)
{
int minIndex = FindNearestIndex(viewResolution, i);
Debug.DrawLine(m_directions[i], m_directions[minIndex], Color.red);
}
}
private int FindNearestIndex(int viewResolution, int originIndex)
{
distances = new List<float>(new float[viewResolution]);
int minIndex = 0;
for (int i = 0; i < viewResolution; i++)
{
distances[originIndex] = float.MaxValue;
distances[i] = Vector3.Distance(m_directions[originIndex], m_directions[i]);
minIndex = distances.IndexOf(distances.Min());
}
return minIndex;
}
}
However, the way i find the 2nd and 3rd Closest points is very crude and suboptimal. (Its literally just the FindNearestIndex copied and pasted with slight modifications of each.)
private int FindSecondNearestIndex(int viewResolution, int originIndex, int exception)
{
distances = new List<float>(new float[viewResolution]);
int minIndex = 0;
for (int i = 0; i < fov3D.viewResolution; i++)
{
distances[originIndex] = float.MaxValue;
distances[i] = Vector3.Distance(fov3D.m_directions[originIndex], fov3D.m_directions[i]);
distances[exception] = float.MaxValue;
minIndex = distances.IndexOf(distances.Min());
}
return minIndex;
}
private int FindThirdNearestIndex(int viewResolution, int originIndex, int exception, int exception2)
{
distances = new List<float>(new float[viewResolution]);
int minIndex = 0;
for (int i = 0; i < fov3D.viewResolution; i++)
{
distances[originIndex] = float.MaxValue;
distances[i] = Vector3.Distance(fov3D.m_directions[originIndex], fov3D.m_directions[i]);
distances[exception] = float.MaxValue;
distances[exception2] = float.MaxValue;
minIndex = distances.IndexOf(distances.Min());
}
return minIndex;
}
Ideally, I would like to be able to have a list of indexes classified as "exceptions". Then, if the returned int minIndex is a known exception, it will throw out that index and run the loop again, giving a new returned value.
For the 2/3 closest points, it could just run twice.
Or say, I have a list of exception indexes defined as [1,3,5], so if the minIndex returns one of those values, it throws it out, and loops again for a new value.
oy vey, i'm having trouble explaining this. I wonder if maybe a coroutine?
Answer by Hellium · Dec 22, 2021 at 10:17 PM
Following code NOT tested
Not even written in an IDE
private List<int> FindNearestIndices(int viewResolution, int originIndex, int indicesCount)
{
if(indicesCount <= 0)
throw new System.ArgumentOutOfRangeException(nameof(indicesCount), nameof(indicesCount) + " must be greater than 0");
List<int> indices = new List<int>(indicesCount);
List<float> distances = new List<float>(indicesCount);
for (int i = 0; i < fov3D.viewResolution && i < fov3D.m_directions.Count; i++)
{
if(i == originIndex) continue;
float sqrDistance = (fov3D.m_directions[originIndex] - fov3D.m_directions[i]).sqrMagnitude;
// Add index if none added
if(indices.Count == 0)
{
indices.Add(i);
distances.Add(sqrDistance);
}
// Check if there is still room or
// if the computed distance is less than the greatest computed distance (at last position in list)
else if(indices.Count < indicesCount || sqrDistance < distances[indices.Count - 1])
{
// Insert in lists while keeping the order
for(int index = 0 ; index < indices.Count ; ++index)
{
if(sqrDistance < distances[index])
{
indices.Insert(index, i);
distances.Insert(index, sqrDistance);
// Remove index of greatest value if list size is greater than the number of expected indices
if(indices.Count > indicesCount)
{
indices.RemoveAt(indices.Count - 1);
distances.RemoveAt(indices.Count - 1);
}
break;
}
}
}
}
return indices;
}
public void LateUpdate()
{
for (int i = 0; i < viewResolution; i++)
{
List<int> minIndices = FindNearestIndices(viewResolution, i, 3);
if(minIndices.Count > 0) Debug.DrawLine(m_directions[i], m_directions[minIndices[0]], Color.red);
// if(minIndices.Count > 1) Debug.DrawLine(m_directions[i], m_directions[minIndices[1]], Color.blue);
// if(minIndices.Count > 2) Debug.DrawLine(m_directions[i], m_directions[minIndices[2]], Color.green);
}
}
Wow, this is great! For not using an ide, this is very impressive. I could only spot 2 errors.
On your line 9, you use Length instead of Count, I think.
for (int i = 0; i < fov3D.viewResolution && i < fov3D.m_directions.Length; i++)
for (int i = 0; i < fov3D.viewResolution && i < fov3D.m_directions.Count; i++)
Then on lines 53-54, $$anonymous$$Index should be $$anonymous$$Indices, I think.
if($$anonymous$$Index.Count > 0) Debug.DrawLine(m_directions[i], m_directions[$$anonymous$$Index[0]], Color.red);
if($$anonymous$$Indices.Count > 0) Debug.DrawLine(m_directions[i], m_directions[$$anonymous$$Indices[0]], Color.red);
But other than that, amazing implementation sir, fantastic. Especially for not even using an IDE. Thanks a ton!
private int viewResolution;
private List<Vector3> m_directions = new List<Vector3>();
private List<int> FindNearestIndices(int viewResolution, int originIndex, int indicesCount)
{
if (indicesCount <= 0)
throw new System.ArgumentOutOfRangeException(nameof(indicesCount), nameof(indicesCount) + " must be greater than 0");
List<int> indices = new List<int>(indicesCount);
List<float> distances = new List<float>(indicesCount);
for (int i = 0; i < viewResolution && i < m_directions.Count; i++)
{
if (i == originIndex) continue;
float sqrDistance = (m_directions[originIndex] - m_directions[i]).sqrMagnitude;
// Add index if none added
if (indices.Count == 0)
{
indices.Add(i);
distances.Add(sqrDistance);
}
// Check if there is still room or
// if the computed distance is less than the greatest computed distance (at last position in list)
else if (indices.Count < indicesCount || sqrDistance < distances[indices.Count - 1])
{
// Insert in lists while keeping the order
for (int index = 0; index < indices.Count; ++index)
{
if (sqrDistance < distances[index])
{
indices.Insert(index, i);
distances.Insert(index, sqrDistance);
// Remove index of greatest value if list size is greater than the number of expected indices
if (indices.Count > indicesCount)
{
indices.RemoveAt(indices.Count - 1);
distances.RemoveAt(indices.Count - 1);
}
break;
}
}
}
}
return indices;
}
public void LateUpdate()
{
for (int i = 0; i < viewResolution; i++)
{
List<int> $$anonymous$$Indices = FindNearestIndices(viewResolution, i, 3);
if ($$anonymous$$Indices.Count > 0) Debug.DrawLine(m_directions[i], m_directions[$$anonymous$$Indices[0]], Color.red);
if($$anonymous$$Indices.Count > 1) Debug.DrawLine(m_directions[i], m_directions[$$anonymous$$Indices[1]], Color.blue);
if($$anonymous$$Indices.Count > 2) Debug.DrawLine(m_directions[i], m_directions[$$anonymous$$Indices[2]], Color.green);
}
}
Your answer
Follow this Question
Related Questions
InvalidOperationException Collection was modified; 3 Answers
A node in a childnode? 1 Answer
For loop does not loop C# 2 Answers
Copy values between two classes in two lists. 1 Answer
dictionary and for each loop and for loop frustrating bug 1 Answer