2
$\begingroup$

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.

  • 0
    If noon is $1$ and sunrise and sunset are $-1$, what's midnight? It seems to me that you would want noon and midnight to be the maximum and minimum, and sunrise and sunset to be $0$ instead.2012-05-18
  • 0
    in opengl 0,0 is the middle of the screen so the sun would be moving from -1 to 1 to -1 throughout the day2012-05-18
  • 0
    Well, what do you suppose the value should be after sunset and before sunrise?2012-05-18
  • 0
    Robert helped me out below but to answer your question I wont be drawing the sun on the screen during those hours2012-05-18

1 Answers 1

1

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$$

  • 0
    yeah that's what I have now but I have the exact sunrise time. That equation wouldn't work if the sunrise was at 6:45am2012-05-18
  • 0
    bascially what I'm asking is how could I adjust that wave for any given sunrise and sunset time. I think it would have to be two separate equations.2012-05-18
  • 0
    Edited 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