You can copy and paste the following examples into either:

menu Auto Pilot > per Graphic Update or

menu Auto Pilot > per Iteration

Note that in the dropdown list, Earth is the 4th object and the Moon is the 11th object.

The following code will compute the Moon's elongation from the Sun with respect to a viewer positioned at Earth's center:

**Code:**var earthN = 4, moonN = 11;;
var CamX = objx[earthN];
var CamY = objy[earthN];
var CamZ = objz[earthN];
var ux = CamX - objx[1];
var uy = CamY - objy[1];
var uz = CamZ - objz[1];
var dES = Math.sqrt(ux*ux + uy*uy + uz*uz);
var vx = CamX - objx[moonN];
var vy = CamY - objy[moonN];
var vz = CamZ - objz[moonN];
var dot = ux*vx +uy*vy + uz*vz;
var magU = Math.sqrt(ux*ux + uy*uy + uz*uz);
var magV = Math.sqrt(vx*vx + vy*vy + vz*vz);
var theta = 180*Math.acos(dot / (magU * magV)) / Math.PI;
txtAreaQuickHelp.value = theta.toFixed(3) + " degrees";

First it establishes the two vectors: Earth to Sun (vector u), Earth to Moon (vector v).

It then finds the dot product of these two vectors.

It then finds the product of the magnitude of these vectors.

It then divides the dot product by the magnitude product and takes the arc cosine, and converts the answer from radians to degrees.

It then displays the result in the Quick Help box available from the Help Menu.

==============================

Here is the same example, except it prints it directly on the screen:

**Code:**var earthN = 4;
var CamX = objx[earthN];
var CamY = objy[earthN];
var CamZ = objz[earthN];
var moonN = 11;
var ux = CamX - objx[1];
var uy = CamY - objy[1];
var uz = CamZ - objz[1];
var dES = Math.sqrt(ux*ux + uy*uy + uz*uz);
var vx = CamX - objx[moonN];
var vy = CamY - objy[moonN];
var vz = CamZ - objz[moonN];
var dot = ux*vx +uy*vy + uz*vz;
var magU = Math.sqrt(ux*ux + uy*uy + uz*uz);
var magV = Math.sqrt(vx*vx + vy*vy + vz*vz);
var theta = 180*Math.acos(dot / (magU * magV)) / Math.PI;
pContextA.fillStyle = '#FFFF00';
pContextA.font="20px Verdana";
var x = 65, y = 220;
pContextA.clearRect(x,y-30,500,50);
pContextA.fillText(theta.toFixed(3) + " degrees", x, y);

Using the Quick Help box allows you to copy the text, while the other method prints as a graphic, so you can't copy it.

==================================

For orbital elements, use the function ComputeOrbitalElements(x,y)

This gives you the elements for x with respect to y.

For example, Earth is object 4, and the Sun is object 1. So to get Earth's orbital elements with respect to the Sun you would call ComputeOrbitalElements(4,1);

After calling this function, the elements are available in the following variables:

oeSMA, oeEcc, oeInc, oeLAN, oePeri, oeMA.

Here is an example that displays Earth's Mean Anomaly and eccentricity in the Quick Help box:

**Code:**var earthN = 4, sunN = 1;
var LF = String.fromCharCode(10); // line feed
ComputeOrbitalElements(earthN, sunN);
txtAreaQuickHelp.value = "MA: " + (180 * oeMA/ Math.PI).toFixed(3) + " degrees" + LF + "ecc: " + oeEcc.toFixed(10);

To pause on a certain condition use the If statement, call the pause() function, then delete the pause function from the autopilot so you can manually unpause. Here is the previous example, except that it pauses when Earth's Mean Anomoly exceeds 300 degrees.

**Code:**var earthN = 4, sunN = 1;
var LF = String.fromCharCode(10); // line feed
ComputeOrbitalElements(earthN, sunN);
txtAreaQuickHelp.value = "MA: " + (180 * oeMA/ Math.PI).toFixed(3) + " degrees" + LF + "ecc: " + oeEcc.toFixed(10);
if (180 * oeMA / Math.PI > 300) {
pause();
txtAreaAPperGraphicUpdate.value = txtAreaAPperGraphicUpdate.value.replace("pause()", "");
updateAPperGraphicUpdate();
}

or if you are in Autopilot Per iteration you would use txtAreaAPperIteration and updateAPperIteration()

State vectors are also available to you as objx[n], objy[n], objz[n], objvx[n], objvy[n], objvz[n]

Or rather than pausing every time the Moon's elongation reached 180, you could have it tally the moments in the Quick Help box. This way you could run the simulation slowly at time step = 1 second, so you wouldn't overshoot the desired event between time steps. Your output statements would then look like:

**Code:**txtAreaQuickHelp.value = txtAreaQuickHelp.value + ...

To help you keep track of inflection, the variables apNV1, apNV2, apNV3, and apNV4 are available for your global use in the autopilot.

State vectors are also available to you as objx[n], objy[n], objz[n], objvx[n], objvy[n], objvz[n].

elapsedTime tells you how much time elapsed since the beginning of the simulation. To convert it to a calendar date:

**Code:** var x = UTC(elapsedTime * 1000 + startDate); // UTC calendar
var x = (elapsedTime * 1000 + startDate); // computer local calendar
var x = elapsedTime / 86400 + julianStartDate / 86400; // julian day

Additionally, if you would like your vectors with respect to a city on Earth, rather than geocenter, menu Display > Cities A, choose a city or enter lat lon, and replace the CamX, CamY, and CamZ lines above with

**Code:**var CamX = objx[fromObjA] + dCore * Math.cos(dElta) * Math.cos(aLpha);
var CamY = objy[fromObjA] + dCore * Math.cos(dElta) * Math.sin(aLpha);
var CamZ = objz[fromObjA] - dCore * Math.sin(dElta);