Hey folks,
I could use a wee bit of help on this one...
At the moment I'm trying to code something of a fluid effect using a collection of variably transparent rectangles and an two dimensional array of "densities" expressed in an integer from 0 - 255.
Simple enough – however, when it comes time to move the density values from one position to another I run into problem because of the loop I use to traverse the array.
What happens is that by looping through each of the integers in the Density array and moving their values accordingly I end up risking moving the same value two, thee, or more times.
For example :
If Density [10,3] has a value of 27, and Density[10,4] has a value of 3, then I (to simulate a sort of diffusion) would a portion of Density[10,3] into Density[10,4]. At that point the loop would arrive at the next index of Density[,]…that being Density[10,4]. Finding that Density[10,5] was smaller than Density[10,4] the diffusion would occur again – meaning Density had potentially “traveled” from Density[10,3] to Density[10,5] in one step of time. Skewing the movement of density to one corner and compromising the "accuracy" of the simulation.

In case that doesn't make sense maybe this will :

int[,] Density = new int[300, 300];

            //Representitive of 300x300 Pixel Form
            for (int y = 0; y < 300; y++)
            {

                for (int x = 0; x < 300; x++)
                {   
                    if (Density[y, x] > Density[x + 1, y])
                    {
                        Density[y, x] -= 1;
                        Density[y, x + 1] += 1;
                    }
                }
            }

The problem is in the approach and not the code - but this issue has been the bane of several of my projects...I need to move things symoltaniously and keep the results accurate.

I fear the solution is obvious- but it's managing to kick my but! :-/

If anyone could suggest a better approach I would really appreciate it. Hope the problem and question are clear! Thankyou!

Oh, also, Hello Everybody! I decided to make an account! ;)

Firstly, welcome!
Secondly, huh? LOL -- I've waited all day for someone to have a C# question, and you kill me with this?
Anyway, I have a vague (football and beer induced) sense that your if statement has the y and x comparison interchanged. Could that be it?

OK, I'll try this again..

Say Ive got a two dimensional array (10x10) of integers representing green marbles in buckets. A big grid of buckets.
My intention is to have the same amount of marbles in every bucket - and visually display the process of moving them around over to achieve this.

The catch is that bucket[3,4] has one red marble for the purpose of tracking it - it behaves and is treated exactly like the others.

So, I have a timer tick ever 50 miliseconds(not important) and everytime it ticks it calls SortMarbles().

Each time SortMarbles is called a loop beings - traversing every bucket in the array and giving a marble to its neighboring buckets if it has more then they do. Ideally eventually forcing all buckets to have the same number of marbles.

The issue is, I don't want any of the marbles to move more then one bucket each time SortMarbles is called. The red marble should move only once per timertick at most. However, because of the way the loop is traversed (Starting at [0,0], then [0,1], then eventually [1,0]) there is potential for any marble to move multiple times if it lands in a bucket which has yet to have its contents evaluated by the loop.

ex. Loop is at [3,4], it decides to move a marble from [3,4] into [4,5], the loop continues and eventually reaches [4,5] and decides to move a marble into [5,5] (potentially moving the red marble again), the Loop reaches [6,5] and does the same thing again - By the end of the method the red marble could have moved many times.

----

I hope that makes sense, if not I'll try explaining it in an animated gif or something.

The issue is the approach taken I imagine - all buckets are to somehow evaluate what to do simultaneously.

This is not so much a C# question as much as it is a programming theory one I guess.

Thanks for the effort :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.