Permalink
Browse files

Better prediction:

Let the user choose for how long to predict upcoming events.
Show a pretty string (4 days, 8 hours) for the prediction interval.
Detect transits among the listed events.
Notice multiple events, and bold them in the list.
  • Loading branch information...
1 parent a4d4c4c commit 7817efe7e89a8287bfbd629a5877a6b4f119a4a3 @akkana committed Mar 4, 2013
Showing with 81 additions and 27 deletions.
  1. +23 −5 jsjupiter/index.html
  2. +58 −22 jsjupiter/jupiter.js
View
@@ -70,6 +70,7 @@
var d = jup.getDate();
d.setTime(d.getTime() + 60 * 60 * hrs * 1000);
drawJupiter(jup, d);
+ predictUpcoming();
}
/*
@@ -117,6 +118,9 @@
if (animating) {
animateStep();
}
+ else {
+ predictUpcoming();
+ }
}
function drawJupiter(jup, date) {
@@ -238,11 +242,18 @@
var upcoming = document.getElementById("upcoming");
if (!upcoming) {
- alert("No place to note upcoming events");
+ //alert("No place to note upcoming events");
return;
}
+}
- upcoming.innerHTML = upcomingEvents(date, 8);
+function predictUpcoming()
+{
+ var upcoming = document.getElementById("upcoming");
+ if (!upcoming) return;
+ upcoming.innerHTML =
+ upcomingEvents(date,
+ parseInt(document.getElementById("upcoming-hrs").value));
}
</script>
@@ -311,7 +322,7 @@
<br>
-->
<input id="datefield" type="datetime"
- onChange="drawJupiter(jup, new Date(document.getElementById('datefield').value)); return false;"
+ onChange="drawJupiter(jup, new Date(document.getElementById('datefield').value)); predictUpcoming(); return false;"
size=40>
<input type="button" name="changeDate" value="Recalculate" onClick="setDate();">
<p>
@@ -322,7 +333,7 @@
<input type="button" name="nextHour" value="+1 hour" onClick="addHours(1);">
<input type="button" name="nextDay" value="+1 day" onClick="addHours(24);">
<input type="button" name="now" value="Now"
- onClick="drawJupiter(jup, new Date());">
+ onClick="drawJupiter(jup, new Date()); predictUpcoming(); return true">
<p>
<input type="button" name="animate" value="Animate"
onClick="toggleAnimation();">
@@ -331,7 +342,13 @@
<span id="animDelay">Delay: 250 milliseconds</span>
<p>
-<!-- <textarea id="upcoming" readonly cols=80 rows=30></textarea> -->
+Show events for
+<input id="upcoming-hrs" type="text" value="8" size=4
+ onChange="predictUpcoming();">
+hours.
+<input type="button" name="predict" value="Predict"
+ onClick="predictUpcoming();">
+<p>
<pre id="upcoming"></pre>
<script type="text/javascript">
@@ -344,6 +361,7 @@
//var date = new Date('Mon Mar 10 2013 20:02:00 GMT-0700 (PDT)');
//var date = new Date('Sat Nov 10 2012 16:10:00 GMT-0800 (PDT)');
drawJupiter(jup, date);
+predictUpcoming();
</script>
<hr>
View
@@ -9,6 +9,7 @@
// Code below is from Jupiter.java and is in the process of being
// translated to javascript.
+
function Jupiter()
{
const NUM_MOONS = 4;
@@ -181,7 +182,8 @@ function Jupiter()
//
// Returns the moon position in units of Jupiter radii.
- // Also calculate the shadows, and whether the moon is eclipsed by Jupiter.
+ // Also calculate the shadows, and whether the moon is eclipsed
+ // by Jupiter's shadow or transiting in front of Jupiter.
//
function getMoonXYData(whichmoon)
{
@@ -198,14 +200,13 @@ function Jupiter()
return xy;
}
- function dist(x, y)
- {
- return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- }
-
moondata.moonx = r * Math.sin(moonAngles[whichmoon]);
moondata.moony = r * Math.cos(moonAngles[whichmoon]) * Math.sin(De);
+ // Is the moon directly in front of or behind Jupiter's disk?
+ // Then this distance will be <= 1.
+ diskdist = dist(moondata.moonx, moondata.moony);
+
s = "moon " + whichmoon;
s += "\nDist = " + r;
s += "\nmoonAngle = " + moonAngles[whichmoon];
@@ -217,6 +218,11 @@ function Jupiter()
if (moonAngles[whichmoon] < Math.PI * .5
|| moonAngles[whichmoon] > Math.PI * 1.5)
{
+ // Is it transiting? Leave a little slop, consider a moon
+ // transiting when it's just starting its transit.
+ if (diskdist < .9)
+ moondata.transit = true;
+
// Since the moon is on the near side, check for shadows
// cast by the moon on the planet.
s += "\nNear side of the planet";
@@ -234,7 +240,7 @@ function Jupiter()
// Is the moon blocked by the planet, so it's invisible?
//else if (moondata.moonx < 1. && moondata.moonx > -1.)
- else if (dist(moondata.moonx, moondata.moony) < 1.)
+ else if (diskdist < 1.0)
{
moondata.farside = true;
moondata.moonx = moondata.moony = NaN;
@@ -305,23 +311,28 @@ function Jupiter()
}
}
+function dist(x, y)
+{
+ return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
+}
+
function prettytime(tothrs)
{
if (tothrs < 24)
- prettytime = tothrs + " hours";
+ pt = tothrs + " hours";
else {
hrs = tothrs % 24;
days = (tothrs-hrs) / 24;
- prettytime = days + " day";
+ pt = days + " day";
if (days != 1)
- prettytime += "s";
+ pt += "s";
if (hrs == 1)
- prettytime += ", 1 hour";
- else if (hrs != 1)
- prettytime += ", " + hrs + " hours";
+ pt += ", 1 hour";
+ else if (hrs > 0)
+ pt += ", " + hrs + " hours";
}
- return prettytime;
+ return pt;
}
//
@@ -351,39 +362,64 @@ function upcomingEvents(date, tothrs)
jup.setDate(d);
if (verbose)
upcoming += "\n" + d + "\n";
+
+ // Keep track of how many moons are involved in events
+ nshadows = 0;
+ ntransits = 0;
+
for (var whichmoon = 0; whichmoon < 4; ++whichmoon) {
moondata = jup.getMoonXYData(whichmoon);
if (verbose) {
upcoming += " (" + whichmoon + "):\n";
upcoming += JSON.stringify(moondata) + "\n";
}
+
if (lastmoondata[whichmoon]) {
+ // Count total events
+ if (moondata.shadowx)
+ ++nshadows;
+ if (moondata.transit)
+ ++ntransits;
+ thisevent = "";
if (!moondata.moonx && lastmoondata[whichmoon].moonx)
- upcoming += d + ": "
+ thisevent += d + ": "
+ moonnames[whichmoon] + " disappears\n";
else if (moondata.moonx && ! lastmoondata[whichmoon].moonx) {
if (! moondata.eclipse)
- upcoming += d + ": "
+ thisevent += d + ": "
+ moonnames[whichmoon] + " reappears\n";
}
+ else if (moondata.transit && ! lastmoondata[whichmoon].transit)
+ thisevent += d + ": " + moonnames[whichmoon]
+ + " begins transit\n";
+ else if (! moondata.transit && lastmoondata[whichmoon].transit)
+ thisevent += d + ": " + moonnames[whichmoon]
+ + " ends transit\n";
+
else if (moondata.eclipse && ! lastmoondata[whichmoon].eclipse)
- upcoming += d + ": " + moonnames[whichmoon]
+ thisevent += d + ": " + moonnames[whichmoon]
+ " enters eclipse\n";
else if (! moondata.eclipse && lastmoondata[whichmoon].eclipse)
- upcoming += d + ": " + moonnames[whichmoon]
+ thisevent += d + ": " + moonnames[whichmoon]
+ " leaves eclipse\n";
if (!moondata.shadowx && lastmoondata[whichmoon].shadowx)
- upcoming += d + ": " + moonnames[whichmoon]
+ thisevent += d + ": " + moonnames[whichmoon]
+ "'s shadow disappears\n";
else if (moondata.shadowx && !lastmoondata[whichmoon].shadowx)
- upcoming += d + ": " + moonnames[whichmoon]
+ thisevent += d + ": " + moonnames[whichmoon]
+ "'s shadow appears\n";
- if (verbose)
- upcoming += JSON.stringify(lastmoondata[whichmoon]) + "\n"
+ if (nshadows + ntransits > 1)
+ upcoming += "<b>";
+ upcoming += thisevent;
+ if (nshadows + ntransits > 1)
+ upcoming += "</b>";
+
+ //if (verbose)
+ // upcoming += JSON.stringify(lastmoondata[whichmoon]) + "\n"
}
// Ick! This is supposedly the most efficient way to clone

0 comments on commit 7817efe

Please sign in to comment.