Page 1 of 1

Programming A Random Star Sky

PostPosted: 14 Oct 2010, 01:07
by qubodup
Hello,

I would like to have something like this:
this.png
this.png (467 Bytes) Viewed 6973 times

generated automatically; a predefined (number = n% of the screen size) amount of coordinates - efficiently.

My naive approach is
{l Code}: {l Select All Code}
while (listOfCoordinates < number)
  randomCoordinates = generateRandomCoordinates()
  if (randomCoordinates is not in listOfCoordinates)
    insert randomCoordinates into listOfCoordinates

and is vastly inefficient.

Any ideas?

Re: Programming A Random Star Sky

PostPosted: 14 Oct 2010, 03:38
by andrewj
for each x coordinate, generate between A and B coords (e.g. between 0 and 3). Start with a random y value, and add random values to it to generate those coords.

Re: Programming A Random Star Sky

PostPosted: 14 Oct 2010, 12:05
by charlie
Ooo I'll do this later. Just for kicks. :D

Re: Programming A Random Star Sky

PostPosted: 14 Oct 2010, 16:13
by amuzen
I tried to code something simple that executes reasonably fast even with 100% coverage. There are certainly faster algorithms than mine if you're guaranteed to have small coverage ratios or if the image doesn't need to conform to the coverage ratio precisely. If the coverage ratio doesn't need to be precise, you can just use your own algorithm without the check for the already occupied pixels. That would be pretty much the fastest possible algorithm, I think, assuming that you can live with the small theoretical possibility of a significant number of stars overlapping.

If you need to get precisely the right coverage ratio and only need to support low coverage ratios, you can use your own algorithm, but with a different data structure for checking for occupied pixels. You could, for example, create an array of 0s the size of the image (or use the image itself as the array) and mark an element as 1 when you fill a pixel. That way you could simply index the array to see if the pixel is occupied. As long as the the coverage ratio is low, that should work pretty well since the number of collisions would remain small.

Consider the attached Python script public domain. Use it as a template for other algorithms, print it out and use it for target practice or stick it to the wall and throw darts at it. Enjoy the freedom while it lasts. :twisted:

Re: Programming A Random Star Sky

PostPosted: 15 Oct 2010, 10:28
by charlie
{l Code}: {l Select All Code}
[charles@charles Download]$ python stars.py
Traceback (most recent call last):
  File "stars.py", line 5, in <module>
    import argparse
ImportError: No module named argparse

Re: Programming A Random Star Sky

PostPosted: 15 Oct 2010, 12:06
by Crendgrim
http://code.google.com/p/argparse/

What's about this? ;)
You also could delete all those argument parsing stuff and simply set some variables - this would do the same, but not in such an adjustable way.


Crend

Re: Programming A Random Star Sky

PostPosted: 16 Oct 2010, 01:27
by qubodup
andrewj {l Wrote}:for each x coordinate, generate between A and B coords (e.g. between 0 and 3). Start with a random y value, and add random values to it to generate those coords.

I think I might have implemented this. Not bad, but the resulting amount of stars is random.
javascript canvas random stars
this.png
this.png (1.96 KiB) Viewed 6899 times

charlie {l Wrote}:Ooo I'll do this later. Just for kicks. :D

Looking forward ;)
amuzen {l Wrote}:I tried to code something simple that executes reasonably fast even with 100% coverage
...
Enjoy the freedom while it lasts. :twisted:

I will have to take a look after bed :)