Programming A Random Star Sky

Programming A Random Star Sky

Postby qubodup » 14 Oct 2010, 01:07

Hello,

I would like to have something like this:
this.png
this.png (467 Bytes) Viewed 5472 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?
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: Programming A Random Star Sky

Postby andrewj » 14 Oct 2010, 03:38

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.
User avatar
andrewj
 
Posts: 194
Joined: 15 Dec 2009, 16:32
Location: Tasmania

Re: Programming A Random Star Sky

Postby charlie » 14 Oct 2010, 12:05

Ooo I'll do this later. Just for kicks. :D
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: Programming A Random Star Sky

Postby amuzen » 14 Oct 2010, 16:13

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:
Attachments
stars.py
(1.31 KiB) Downloaded 376 times
User avatar
amuzen
LoS Moderator
 
Posts: 327
Joined: 05 Dec 2009, 02:49

Re: Programming A Random Star Sky

Postby charlie » 15 Oct 2010, 10:28

{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
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: Programming A Random Star Sky

Postby Crendgrim » 15 Oct 2010, 12:06

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
Crendgrim
 
Posts: 227
Joined: 03 May 2011, 21:59

Re: Programming A Random Star Sky

Postby qubodup » 16 Oct 2010, 01:27

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 5398 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 :)
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Who is online

Users browsing this forum: No registered users and 1 guest