Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moon Phase / Mondphase
#1
Hi guys! happy.gif
Here's my new brand script! And it's my first one! We're dealing with... moon phases! Easy, huh?
The main function draws the moon with the given phase. Simple as that.

Here's the output on my CFAG12864B-TMI-V:
user posted image
It the same moon of these days! Check outside the window! biggrin.gif
An animated sample:
user posted image
It also works for other graphic LCDs. Best results with dark background/bright pixels, improvements are scheduled (check the comments).

And here's the script. Sorry for the length! But each function is remarked, at least.
CODE
#Header
/*** FMod v1.00
 Usage:
   %FMod(value,quotient)
 Returns a floating point modulo operation using two parameters.
  Possible improvements:
 - Making it work also for negative quotients./
%DefFunc(FMod=
 %If(%FMod->Para0() < 0) Then{
  %Dec(%FMod->Para0(),%Mul(%Dec(%Round(%Div(%FMod->Para0(),%FMod->Para1())),1),%FMod->Para1()))
 }Else{
  %Dec(%FMod->Para0(),%Mul(%Round(%Div(%FMod->Para0(),%FMod->Para1())),%FMod->Para1()))
 }
)

/*** From70 v1.00
 Usage:
   %From70(year,month,day,hour,minute,second,millisecond)
 Returns the number of seconds from 1-1-1970, 12:00 am.
 The function accepts 7 parameters: year, month (January is 1),
 day, hour (0-23), minute, second, millisecond. If no parameter
 is passed, then the system time is used.
  Known bugs:
 - Works only for not-too-weird dates (i.e., it considers 2100
   as a leap year.). I hope it's the only bug!
  Possible improvements:
 - Solving the bug!/
%DefFunc(From70, Local=
 %DefList(dm,Local)
 %List.Add(dm,0)  %List.Add(dm,31)  %List.Add(dm,59)  %List.Add(dm,90)
 %List.Add(dm,120)  %List.Add(dm,151)  %List.Add(dm,181)  %List.Add(dm,212)
 %List.Add(dm,243)  %List.Add(dm,273)  %List.Add(dm,304)  %List.Add(dm,334)
 %If(%From70->Para0() = '') Then{
  %DefVar(year,Local=%Dec(%System.GetDateTime(yyyy),1970))
  %DefVar(month,Local=%Dec(%System.GetDateTime(m),1))
  %DefVar(day,Local=%Dec(%System.GetDateTime(d),1))
  %DefVar(hour,Local=%System.GetDateTime(h))
  %DefVar(minute,Local=%System.GetDateTime(n))
  %DefVar(second,Local=%System.GetDateTime(s))
  %DefVar(millis,Local=%System.GetDateTime(z))
 }Else{
  %DefVar(year,Local=%Mod(%From70->Para0(),1970))
  %DefVar(month,Local=%Dec(%From70->Para1(),1))
  %DefVar(day,Local=%Dec(%From70->Para2(),1))
  %DefVar(hour,Local=%From70->Para3())
  %DefVar(minute,Local=%From70->Para4())
  %DefVar(second,Local=%From70->Para5())
  %DefVar(millis,Local=%From70->Para6())
 }
 %DefVar(leapy,Local=%Round(%Div(%Inc(%year(),2),4)))
 %If((%Mod(%year(),4) = 2) AND (%month() < 2)) Then{
   %Assign(leapy,%Dec(%leapy(),1))
 }
 %Inc(%Inc(%Inc(%Inc(%Inc(%Mul(%year(),31536000),%Mul(%Inc(%Inc(%day(),%dm(%month())),%leapy()),86400)),%Mul(%hour(),3600)),%Mul(%minute(),60)),%second()),%Div(%millis(),1000)) /Veeery long and obscure piece of code!/
)

/*** MoonPhase v1.00
 Usage:
   %MoonPhase()
 Determines the moon phase based on system time.
 0 = full moon, 0.25 = last quarter,
 0.5 = new moon, 0.75 = first quarter
  Possible improvements:
 - Calculation on given date and time
  Notes:
 - On June 22nd, 2005 the moon is full. It's my birthday! Smile
   And 10:10pm is my "birth time"!/
%DefFunc(MoonPhase, Local=
 %DefVar(RefTime,Local=%From70(2005,6,22,22,10,0,0))
 %Div(%FMod(%Dec(%From70(),%RefTime()),2551443.84),2551443.84)
)

/*** DrawMoon v0.5
 Usage:
   %DrawMoon(X,Y,diameter,phase)
 Draw the "moon". X,Y of the upper left corner are the first two
 arguments, the third is the diameter in pixel. The 4th argument is
 a number expressing the moon phase, as specified in the %MoonPhase()
 function. If omitted, it's calculated using the system time.
  Possible improvements:
 - Mirror bit flag (for Aussies Smile
 - Negative drawing
 - Color selection/
%DefFunc(DrawMoon, Local=
 %DefVar(P1,Local=0)
 %DefVar(P2,Local=0)
 %If(%DrawMoon->Para3() = '') Then{
  %DefVar(MP,Local=%MoonPhase())
 }Else{
  %DefVar(MP,Local=%DrawMoon->Para3())
 }
 %DefVar(rad, Local=%DrawMoon->Para2())
 %DefVar(SX,Local=%Inc(%DrawMoon->Para0(),%Div(%rad(),2)))
 %DefVar(MF,Local=%Cos(%Mul(%MP(),360)))
 %Loop.For(row,0,%Dec(%rad(),1),1){
  %Assign(P1,%Mul(%Sqrt(%Dec(.25,%Sqr(%Dec(%Div(%Inc(%row(),.5),%rad()),.5)))),%rad()))
  %If(%MP() > .5) Then{
   %Graph.Pen(%Dec(%SX(),%Mul(%P1(),%MF())),%Inc(%DrawMoon->Para1(),%row()),1)
   %Graph.LineTo(%Dec(%Round(%Inc(%SX(),%P1())),1),%Inc(%DrawMoon->Para1(),%row()),1)
  }Else{
   %Graph.Pen(%Round(%Dec(%SX(),%P1())),%Inc(%DrawMoon->Para1(),%row()),1)
   %Graph.LineTo(%Inc(%SX(),%Mul(%P1(),%MF())),%Inc(%DrawMoon->Para1(),%row()),1)
  }
 }
)
#EndHeader

#TextMode
/insert textmode script code here/
#EndTextMode

#GfxMode
%Graph.Rect(32,0,95,63,0,0)

/Demo: one-minute lunar revolution!/
/%DrawMoon(32,0,64,%Div(%Inc(%System.GetDateTime(s),%Div(%System.GetDateTime(z),1000)),60))/

/Demo: actual moon/
%DrawMoon(32,0,64)
#EndGfxMode


Any opinion? Bug report? Insult? smile.gif

Have a nice scripting!


Forum Jump:


Users browsing this thread: 1 Guest(s)