I want to iterate over a List in a 2D way, basically starting at the center and working my way around in a "circular" fashion. The List is a List of type Chunk and Chunks store a Vector 3 position. I also have a Player which has a Vector3 position. I want to generate the chunks closest to the Player first.

I have considered sorting the list by comparing the position of the chunks to the player position, but I can't even imagine a way to store all of them in a closest, next closest, next closest, etc fashion.

Can I get some sudo code to help me accomplish this?

You could calculate the distance of your vector between all the others and then determine which is closest.

How did you get your chunks in a List?
I would make a class or struct with a Vector3 and a distance field or method and it would already be in a List.

I thought nobody would ever need a loop like this, but I am happy to be mistaken...

This is 'parallel', so you will have to remove the thread pool call, but you should be able to derive something useful from this.

/*Author: overwraith*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ParallelTests {

    class Program {

        static void Main(string[] args) {
            //int i = 0;
            //ParallelExt.For(
            //    i + 1,                                          //Start value. 
            //    j => j != i,                                    //Comparison operation. 
            //    j => { Console.WriteLine("Counter: " + j); },   //Loop body. 
            //    (ref int j) => { if (j == 10) { j = -1; } });   //Loop conclusion. 

            int i = 5;
            ParallelExt.For(
                i + 1,                                          //Start value. 
                j => j != i,                                    //Comparison operation. 
                j => { Console.WriteLine("Counter: " + j); },   //Loop body. 
                (ref int j) => { if (j == 10) { j = -1; } });   //Loop conclusion. 

            Console.Write("Press any key to continue...");
            Console.ReadLine();
        }//end main

    }//end class

    public static class ParallelExt {

        public delegate void LoopConclusion(ref int counter);
        public delegate void LoopBody(int counter);
        public delegate bool LoopConditional(int counter);

        //method for wrap around for loops, 0, 1, 2, 3, 4, ..., 0, 1, 2, 3
        public static void For(int start, LoopConditional condition, LoopBody body, LoopConclusion conclusion) {
            var events = new List<ManualResetEvent>();

            for (int i = start, j = 0; condition.Invoke(i); i++, j++) {
                var resetEvent = new ManualResetEvent(false);

                ThreadPool.QueueUserWorkItem((arg) => {
                    int value = (int)arg;
                    body.Invoke(value);
                    resetEvent.Set();
                }, i);
                events.Add(resetEvent);
                conclusion.Invoke(ref i);
            }//end loop

            WaitHandle.WaitAll(events.ToArray());

        }//end method

    }//end class

}//end namespace

I hope I understand what you want correctly, what I wanted to do was implement a loop which goes like this...

0, 1, [2], 3, 4//start
0, 1, 2, [3], 4
0, 1, 2, 3, [4]
[0], 1, 2, 3, 4
0, [1], 2, 3, 4
0, 1, [2], 3, 4 //stop

Remember that my implementation is parallel, so it will not display in order, you will have to remove the threading. For my purposes the threading is usually benificial if the order doesn't matter as much, and you just need to implement a wrapping motion on a set.

This article has been dead for over six months. Start a new discussion instead.