Jump to content



Photo

[c#] Random spacing algorithm


  • Please log in to reply
6 replies to this topic

#1 James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 27 January 2012 - 00:56

Hey gang,

I am in need of a pattern algorithm that i can use as i am loading a window with ellipses that are animating right to left, but not on the y axis. The Y axis is set when the ellipse loads. There are a number of random pieces to each ellipse so im trying to avoid too much grouping. Any thought?


#2 Riva

Riva

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 11-February 07

Posted 27 January 2012 - 01:01

Tried with WPF animations?

#3 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 27 January 2012 - 01:13

Tried with WPF animations?


I am using WPF. What I am looking for is an algorithm that will allow me to space the ellipses randomly in the Y axis without crowding them all in one range. IE: I have 0 to 1000, but I don't want all ellipses to end up in the 0 to 200.

Does that clarify?

#4 Ntrstd

Ntrstd

    Neowinian

  • Joined: 29-July 11

Posted 27 January 2012 - 01:16

Hey gang,

I am in need of a pattern algorithm that i can use as i am loading a window with ellipses that are animating right to left, but not on the y axis. The Y axis is set when the ellipse loads. There are a number of random pieces to each ellipse so im trying to avoid too much grouping. Any thought?


How about dividing the axis into segments, and randomly allocating a single ellipse to each segment. The randomisation algorithm would allocate a blank ellipse to some segments.

#5 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 27 January 2012 - 01:45

Yea, I have thought about making 8 or 10 sections and then keeping a list of how many ellipses are within any section to keep the spread even. I was hoping to find an existing algorithm that will do the work.

Thanks for the thought.

#6 boogerjones

boogerjones

    T.I.P.I

  • Joined: 30-March 04
  • Location: Chicago

Posted 28 January 2012 - 02:58

So you don't want anything random at all (otherwise your objective, placing ellipses along the entire axis, makes no sense). If you wanted random numbers, you would want to generate a uniform distribution; but that provides no guarantee that the ellipses won't be bunched up (since random numbers, by definition, have absolutely no guarantee about their distribution in a range. In other words, if you're generating pseudorandom numbers in the range 0 - 1000, there's no way to guarantee that they won't all end up in the range 0 - 100 without introducing bias). The C++11 standard has a Random spec that allows for introducing these kinds of biases. I'm sure there's a C# library to do so, but I'm pretty sure the .NET Framework doesn't have something like that natively.

One way to do what you want is to divide your range (0-1000) into N blocks (where N is the number of ellipses). Then for each block, generate a pseudorandom spot in that block's range and place an ellipse there. Then move to the next block. This is pretty much what Ntrstd said.

So if you have 10 ellipses, generate a number between 0 and 100 and place an ellipse there. Then generate a number between 100 and 200 and place an ellipses there... and so on. No need to keep track of occupied positions and it guarantees a biased, relatively-even distribution. It also doesn't require some third-party library.

Alternatively, you could put, say, 5 of those 10 ellipses in the above quasi-even distribution and then place the rest anywhere in the range (0-1000) by generating uniformly-distributed pseudorandom numbers in that entire range.

#7 OP James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 28 January 2012 - 23:37

Thanks for the thoughts. I am hoping to avoid TOO many ellipses within an area but you are correct, I can have random or not. I was leaning toword segmenting the Y axis into a number of groups (less than 10) and keep track of the number within that section to avoid such large groupings.

I will review what you wrote and think it over.

Thank you very much.



Click here to login or here to register to remove this ad, it's free!