Example
Let's use the straight 45 degree line from above as our example. Here's one of those situations where we don't really need Calculus to determine the answer. The line forms a right triangle with the X-Axis and Y-Axis. The area of a triangle is easy to calculate. We make our range of desired values equal to the width of the triangle and also make the simplifying assumption that the width and height are the same. Let's create a new random function to replace the one we us in the galaxy generator above. We solve g(x) with the quadratic formula and simplify.
This is the galaxy with the new random function. I also increased the value of fAngularSpread to bring the edges of the arms closer together. It's better but not quite there yet. Here is the new random function: float fLineRandom(float fRange) { static float fRandMax = 1.0f * RAND_MAX; float fArea = fRange*fRange/2; float fP = fArea * (rand()/fRandMax); return fRange-sqrt(fRange*fRange - 2*fP); } A hat like function
Ok one more try. I won't bother you with the math. The plot of the function we are using is on the right.
Here's the random function and the results. float fHatRandom(float fRange) { static float fRandMax = 1.0f * RAND_MAX; static float fArea = 4*atan(6.0); float fP = fArea * (rand()/fRandMax); return tan(fP/4)*fRange/6.0; } This bunches the stars up in the middle more. It's still not perfect but I think it probably because of the poor simulation of the greater apparent luminosity of the stars in the middle. But we will leave that problem as an exercise to the user. I hope at lease that this little exercise gives you a few more techniques to use in generating various simulations. Many thanks to Bryan Mau for lending me his galaxy plotting code. All text and images Copyright © 2001 Patrick Down |