From 0f1e2a3632b7b1a04c962cfcef1558febc054331 Mon Sep 17 00:00:00 2001
From: wwjjbb <dxt.wjb@gmail.com>
Date: Fri, 14 Jul 2017 16:28:01 +0100
Subject: [PATCH] Interpolate shadow position based on time of quarters

Previously assumed each quarter was 1/4 of the cycle. Which its not.
THis is not ideal either.
---
 package/contents/code/lunacalc.js | 47 ++++++++++++++++---------------
 package/contents/ui/main.qml      |  2 +-
 2 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/package/contents/code/lunacalc.js b/package/contents/code/lunacalc.js
index 1c2d40c..1c6f41d 100644
--- a/package/contents/code/lunacalc.js
+++ b/package/contents/code/lunacalc.js
@@ -78,36 +78,37 @@ function reloadPhases()
 	return getPhasesByLunation(lunation);
 }
 
-function getCurrentPhase() // this function assumes that today is between phases[0] (last new moon) and phases[4] (next new moon)
+function getCurrentPhase(interpolate) // this function assumes that today is between phases[0] (last new moon) and phases[4] (next new moon)
 {
 
 	var oneDay = 1000 * 60 * 60 * 24;
 	var today = new Date().getTime();
 	var phases = getTodayPhases();
 
-  /* -------------------------------------------------------------------------
-  // Estimate where the terminator is - base this on knowing where it
-  // is at each quarter, and interpolating. Cowboy maths.
-  // Determines how far into the current quarter we are, and uses
-  // the result to work out where the terminator is. This allows for
-  // the quarters being different sizes, rather than assuming they are
-  // each one quarter of the cycle time.
-
-  var qnum = 0;
-  while (today > phases[qnum+1] && qnum < 3) {
-     qnum++;
+  var terminator;
+  if (interpolate) {
+		  // Estimate where the terminator is - base this on knowing where it
+		  // is at each quarter, and interpolating. Cowboy maths.
+		  // Determines how far into the current quarter we are, and uses
+		  // the result to work out where the terminator is. This allows for
+		  // the quarters being different sizes, rather than assuming they are
+		  // each one quarter of the cycle time.
+
+		  var qnum = 0;
+		  while (today > phases[qnum+1] && qnum < 3) {
+		     qnum++;
+		  }
+		  var quarterTime = phases[qnum+1].getTime() - phases[qnum].getTime();
+		  var sinceQuarter = today - phases[qnum].getTime();
+		  terminator = Math.floor(((sinceQuarter / quarterTime) + qnum) * 90);
+  }
+	else {
+		  // Work out where the terminator is, 0..359 degrees.
+			// This assumes a constant rate for the month, which is unlikely
+		  var cycleTime = phases[4].getTime() - phases[0].getTime();
+		  var sinceNew = today - phases[0].getTime();
+		  terminator = Math.floor((sinceNew / cycleTime) * 360);
   }
-  var quarterTime = phases[qnum+1].getTime() - phases[qnum].getTime();
-  var sinceQuarter = today - phases[qnum].getTime();
-  var terminator = Math.floor(((sinceQuarter / quarterTime) + qnum) * 90);
-  --------------------------------------------------------------------- */
-
-  // /*
-  // Work out where the terminator is, 0..359 degrees
-  var cycleTime = phases[4].getTime() - phases[0].getTime();
-  var sinceNew = today - phases[0].getTime();
-  var terminator = Math.floor((sinceNew / cycleTime) * 360);
-  // */
 
   // Keep this in the range [0,360):
 	if (terminator >= 360) {
diff --git a/package/contents/ui/main.qml b/package/contents/ui/main.qml
index 6fab7c5..ff0e18f 100644
--- a/package/contents/ui/main.qml
+++ b/package/contents/ui/main.qml
@@ -70,7 +70,7 @@ Item {
 
         function updateDetails() {
             // set the correct image for the moon
-            currentPhase = LunaCalc.getCurrentPhase();
+            currentPhase = LunaCalc.getCurrentPhase(true);
             lunaIcon.phaseNumber = 13; //currentPhase.number;
             lunaIcon.theta = currentPhase.terminator;
             lunaIcon.hemisphere = hemisphere;
-- 
GitLab