Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FaffyWaffles · Dec 22, 2021 at 09:37 PM · listloopfor-loopforeachfor loop

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;
     }

alt text 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?

distancesexamples.png (73.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
      }
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FaffyWaffles · Dec 23, 2021 at 07:02 AM 0
Share

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!

avatar image FaffyWaffles FaffyWaffles · Dec 23, 2021 at 07:08 AM 0
Share
     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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

dictionary and for each loop and for loop frustrating bug 1 Answer

foreach loop problem 1 Answer

InvalidOperationException Collection was modified; 3 Answers

A node in a childnode? 1 Answer

For loop does not loop C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges