Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to generate a starfield

I just learned about Logo yesterday. Being born in the nineties I never came across it before. So I started using an online Logo Interpreter written by Joshua Bell and I decided to write a circle function to make concentric circles. This is what I wrote:

cs

to circle
penup forward :radius right 90
pendown repeat 360 [forward 3.14 * :radius / 180 right 1]
penup left 90 back :radius pendown
end

make "radius 30

repeat 160 [circle make "radius :radius + 30]

Interestingly the drawing canvas is a toroidal array. Hence the circles end up overlapping. By drawing 160 concentric circles with increasing radii in multiples of 30 I ended up getting an image like this:

Logo starfield generated by 160 concentric circles with increasing radii in multiples of 30.

This is truly astonishing. At first glance it looks like a genuine picture of the night sky, and it got me thinking - is there a general algorithm to draw a starfield?

BTW if you look closely you can see a grid of 30 x 30 pixel squares. The boundaries are black so it's a little hard to notice.

like image 522
Aadit M Shah Avatar asked Dec 20 '22 06:12

Aadit M Shah


2 Answers

The normal way to do a starfield is to choose points randomly in the x/y plane, in psuedocode something like:

fill image with black
for (however many stars you want)
   x = random() * width
   y = random() * height
   plot star at position x,y 
loop 

If you want to get fancy, you can plot stars with random brightnesses as as well. Also remember that stars flicker slightly, so if you are doing an animation then varying the brightness a bit each frame can make them look a more realistic.

like image 120
mikera Avatar answered Jan 01 '23 18:01

mikera


As an avid player of Dwarf Fortress, I would go for a fractal formula to generate the stars. Computer random is just not good enough, in my opinion. Borrowing from DF game again, I would add various steps to enhance the starfield such as randomizing the shape, color, size, glow, cluster (intentionally adding a clump of stars together).

If you wanna go with realism, also simulate gas background. Space is not white dots on a solid black background.

Finally, I would suggest you get some space images (from our old friend Hubble telescope) and try to copy it as faithfully as possible. Then play with the parameters to get to your liking (perhaps you want to make a game out of this).

P.S: I have not specified any coding language because I believe its not a question regarding code (its pretty basic) but a question of design. You WOULD be doing this much easier in Flash / Actionscript than Java, trust me.

like image 24
Discipol Avatar answered Jan 01 '23 17:01

Discipol