I'm working with opengl to create a sunrise and sunset based on the actual times. I'm having trouble coming up with a sin/cos formula that goes from -1 to 1 to -1 for any given sunrise and sunset time with sunrise and sunset equaling -1 and high noon equaling 1.
sine/cosine wave for sunrise and sunset
-
0Robert helped me out below but to answer your question I wont be drawing the sun on the screen during those hours – 2012-05-18
1 Answers
EDITED LIKE CRAZY: Yeah, use two separate equations. For each you have some requirements:
$f(0)=1$ $f(12)=1$ $f(s)=-1, f'(s)=0$
Since you'll switch functions every 12 hours, you don't need something like $f'(12)=0$ because it's guaranteed to be a maximum anyway when you switch to the decreasing function on the other half. It's not smooth, but it is continuous, and since I assume the effect will be minimal I doubt it would be noticed much. The thing is that you don't really get customizable flowy behaviour with sines/cosines (you could technically use fourier transforms, but that seems wildly excessive). With 4 conditions though, a cubic can be specified to flow smoothly like this:
$f(x)=ax^3+bx^2+cx+d$ $f'(x)=3ax^2+2bx+c$
It will start at 1, dip down to -1 at the specified time, and come back up to 1. I'm letting the 12 hour period be over [0,1] instead of [0,12] (so $t_{day}=12x$) to simplify calculations. The first requirement means $d=1$. The second means $a+b+c=0$
The requirements on sunset are $as^3+bs^2+cs=-2$ $3as^2+2bs+c=0$
solving this system of equations (and converting $x$ to $t$) gives a cubic describing amount of sunset in terms of time:
$At^3+Bt^2+Ct+D$ where $A=\frac{3456(2s-1)}{s^2(s-1)^2}$ $B=\frac{288(3s^2-1)}{s^2(s-1)^2}$ $C=\frac{24(3s-2)}{s^2(s-1)^2}$ $D=1$
-
0Edited with an adjustable equation. sines and cosines are a bit more restrictive in the sens you want: you can't really *skew* them the way you can with polynomials. I originally tried with a quintic to get smooth rates even at noon/midnight, but the equations for the coefficients were awful. – 2012-05-18