Horizons: Distance to Horizon
04-09-2019, 05:55 PM (This post was last modified: 04-13-2019 04:35 PM by StephenG1CMZ.)
Post: #1
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
Horizons: Distance to Horizon
(implementing surface area visible at height)
I have implemented distance to horizon functionality on the HP Prime.

Note: The HP Prime may be capable of better accuracy than some of my examples might suggest...
Some examples probably started out with a different radius.

Stephen Lewkowicz (G1CMZ)
04-09-2019, 06:00 PM (This post was last modified: 04-10-2019 08:48 AM by StephenG1CMZ.)
Post: #2
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: Horizons: Distance to Horizon
Version 0.1 implements simple geometric solutions for spheres, taking no account of refraction.

It includes functions for distance to horizon at a given height, and inverse functions.

It also demonstrates some unexpected cas solve behaviour:
The annotated example Print, if included, often seems OK on the 1st run (returning undef), but on later runs all solves return undef, suggestive of an initialisation issue. Earlier versions of the program sometimes generated a large real instead of undef on the 1st run (and on one occasion a large negative, despite the ">=0").
Update: Changing HT to h in the cas solve seems to fix the 2nd run undef problem.
Code:

LOCAL CRID:="Horizons V0.1 © 2019 StephenG1CMZ";
//This version has simple geometric horizons
//No refraction

LOCAL HT;//FOR SOLVE

//MOST OF MY TEST VALUES ASSUME THIS VALUE. CHOOSE YOUR OWN.
//EARTH:WORKING IN m ACCURACY IS NORMALLY WITHIN 10cm (OF LATEST DATA)
LOCAL NA:=0; //"NA", VALUE RETURNED WHEN EG HT<0;

//Definitions
//DD   Direct Distance       (eye-horizon)
//HT   Height above Sphere   (<0=NA)
//SLen Curved Surface Length (paw-horizon)
//Distances and heights in same units

//Return Sphere CAV
BEGIN
LOCAL SpC,SpA,SpV;
RETURN {SpC,SpA,SpV};
END;

//RETURN SPHERE SURFACE AREA
BEGIN
END;

//RETURN D is the straight geometrical eye-hZ dist
//TIMING 58us
BEGIN
RETURN NA;
END;
END;

//INVERSE
//RETURN HT TO SEE DD AWAY
BEGIN

END;

//INV
//RETURN HT TO SEE SLEN (SD) AWAY
// RETURN HT (OR 0 IF SD IMPOSSIBLE TBD)
BEGIN

END;

//INV
//Get Height for Surface Area (SA in units EG m^2,NOT %)//correction saf as a fraction
BEGIN
RETURN HT;
END;

EXPORT GetSurfLenDD(DD,RADIUS) //Surface Length from DD
//D is the straight geometrical eye-hor dist
//RETURN S IS THE SURFACE DISTANCE
//TIMING 55us
BEGIN
RETURN NA;
END;
END;

//BOTH THESE S DISTANCES SHOULD BE THE SAME (EXCEPT ROUNDING)
//THERE IS NO SIGNIFICANT DIFFERENCE
//HINT:
//S(HT) IS QUICKER IF YOU ONLY NEED S
//S(DD) IS QUICKER IF YOU ALSO NEED D (OR KNOW D)

//RETURN S IS THE SURFACE DISTANCE
//TIMING 109us QUICKER THAN D+SD
BEGIN
RETURN NA;
END;
END;

//Surfacevisible at Height
//FP Fraction
//PC Percent
//SA Surface Area (same units as radius)

//Height and Radius in same units. Ht is above surface
//Return.Proportion Surface Visible at Ht (0-0.5,HT≥0)
//For example:
//Radius of Earth = 6378 km
//Distance of Apollo 17 from Earth's surface at 1972-12-07T10:39Z = 29000 km
//Visible percentage of Earth's surface in 1972 "Blue Marble" photograph = 40.99% (41%)
BEGIN
//Guard HT<0:WHAT IS REALLY VISIBLE
RETURN NA;//0 OR 1
END;
END;

BEGIN
END;

//Hint: See EX
BEGIN
LOCAL PLACES:=3;//ROUNDING

PRINT("Geometric visible horizon: "+ROUND(DX,PLACES));
END;

//Work an example
//Exhibits program functionality
//Hint: See Report
BEGIN
PRINT("Direct Length "+DX);
PRINT(" ");
END;

EXAMPLES()
BEGIN
// a feW examples : most test data elsWhere
//HT,D,S (D=STRAIGHT S=CURVED)

PRINT(); PRINT(CRID); PRINT(" ");WAIT(1);

PRINT(" ");

//HT≤0 RETURNS 0

PRINT("//MAN at 2m (D 5.1km)");

PRINT("//ISS at 408 km (around 2373 km)");

PRINT("//Eg APOLLO at 29000 km");
EX(29000ᴇ3,6378000);

PRINT("//40.5 AU Voyager PALE BLUE DOT");

//PRINT("//145 AU Voyager 2019");

PRINT("//as Exoplanet of Proxima Centauri");
//This last verifies, except solve doesnt

//CAUTION:
//INCLUDING THIS PRINT HAS THIS EFFECT:
//1ST RUN AS EXPECTED (THIS VALUE undef)
//2ND RUN ALL solve RETURN undef
END;

EXPORT HORIZONS()
BEGIN
LOCAL DX;
LOCAL HT:=2;
PRINT();
PRINT(CRID);
EXAMPLES();

//PRINT("TEVAL DStraight "+TEVAL(DX:=HZDistanceD(HT,RADIUS)));// 58 CALC D
//PRINT("TEVAL SurfaceH "+TEVAL(HZDistanceSH(HT,RADIUS)));    //109 VS S
END;

Stephen Lewkowicz (G1CMZ)
04-11-2019, 09:10 PM (This post was last modified: 04-15-2019 09:25 PM by StephenG1CMZ.)
Post: #3
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: Horizons: Distance to Horizon
Version 0.2:
Improves cas solve implementation (using h instead of HT)
Improves handling of extreme values
Implements inverse surface area calculations (although these may be approximate and need more checking).
Note: The I at the start of the code is a cut-and-paste error - please delete it.
Code:

// I
LOCAL CRID:="Horizons V0.2 © 2019 StephenG1CMZ";
//This version has simple geometric horizons
//No refraction

LOCAL HT;//FOR SOLVE //Global h used

//MOST OF MY TEST VALUES ASSUME THIS VALUE. CHOOSE YOUR OWN.
//EARTH:WORKING IN m ACCURACY IS NORMALLY WITHIN 10cm (OF LATEST DATA)
LOCAL NA:=0;  // NA OR 0 => ONE OF:
//INPUT<0: NOTHING VISIBLE
//INPUT=0: NOTHING VISIBLE
//INPUT>0.5:OVER 1/2 OF SPHERE CANNOT BE VISIBLE
//INPUT=0.5 (INFINITE HEIGHT LIMITING CASE, use UpperLimits for values)
//AVOID /0 (HOPEFULLY ONE OF THE ABOVE)

//Definitions
//DD   Direct Distance       (eye-horizon)
//HT   Height above Sphere   (<0=NA)
//SLen Curved Surface Length (paw-horizon)
//Distances and heights in same units

//RETURN SPHERE SURFACE AREA
BEGIN
END;

//RETURN D is the straight geometrical eye-hZ dist
//TIMING 58us
BEGIN
RETURN NA;
END;
END;

//INVERSE
//RETURN HT TO SEE DD AWAY
BEGIN
IF DD<0 THEN
//NO UPPER LIMIT TO EYE HEIGHT OR DIST
//(Imp Limit DD>1ᴇ12 WITH EARTH RADIUS NOT CHECKED)
RETURN NA;
END;
RETURN HT;
END;

//INVERSE
//Get Height for Surface Area (SAF Fraction of sphere visible, 0..0.5)
//OR NA IF IMPOSSIBLE
BEGIN
IF SAF≤0 OR SAF≥0.5 THEN //INCLUDE SAF=0 TRAP
RETURN NA;
END;
RETURN HT; //MAX(HT,0);
//MAX CLIPS SMALL NEGATIVES: NEEDED WHEN SAF=0 UNLESS TRAPPED
END;
//NB INVERSE SA RESULTS ARE APPROXIMATE
//BUT NEED CHECKING TBD
//INVERSE
//Get Height for Surface Area (SA in units^2)
//OR NA IF IMPOSSIBLE
BEGIN
LOCAL SAF;
RETURN NA;
END;
PRINT(100*SAF);//diagnostic
RETURN HT;
END;

//INVERSE
//RETURN HT TO SEE SURFLEN AWAY (OR NA IF SURFLEN IMPOSSIBLE)
BEGIN
RETURN NA;
END;
RETURN HT;
END;

EXPORT GetSurfLenDD(DD,RADIUS) //Surface Length from DD
//D is the straight geometrical eye-hor dist
//RETURN S IS THE SURFACE DISTANCE
//TIMING 55us
BEGIN
RETURN NA;
END;
END;

//BOTH THESE S DISTANCES SHOULD BE THE SAME (EXCEPT ROUNDING)
//THERE IS NO SIGNIFICANT DIFFERENCE
//HINT:
//S(HT) IS QUICKER IF YOU ONLY NEED S
//S(DD) IS QUICKER IF YOU ALSO NEED D (OR KNOW D)

//RETURN S IS THE SURFACE DISTANCE
//TIMING 109us QUICKER THAN D+SD
BEGIN
RETURN NA;
END;
END;

//Surfacevisible at Height
//FP Fraction
//PC Percent
//SA Surface Area (same units as radius)

//Height and Radius in same units. Ht is above surface
//Return.Proportion Surface Visible at Ht (0-0.5,HT≥0)
//For example:
//Radius of Earth = 6378 km
//Distance of Apollo 17 from Earth's surface at 1972-12-07T10:39Z = 29000 km
//Visible percentage of Earth's surface in 1972 "Blue Marble" photograph = 40.99% (41%)
BEGIN
//Guard HT<0:WHAT IS REALLY VISIBLE
RETURN NA;//0 OR 1
END;
END;

BEGIN
END;

//Hint: See EX
BEGIN
LOCAL PLACES:=3;//ROUNDING

PRINT("Geometric visible horizon: "+ROUND(DX,PLACES));
END;

//Work an example
//Exhibits program functionality
//Hint: See Report
BEGIN
PRINT("Direct Length "+DX);
PRINT(" ");
END;

//Upper Limits = Half Sphere = Infinite Height
//Appply to SurfLen,SurfArea. Not DD and HT
BEGIN
LOCAL SpC,SpA;
RETURN {SpC,SpA};//HALF SPHERE
END;

EXAMPLES()
BEGIN
// a feW examples : most test data elsWhere
//HT,D,S (D=STRAIGHT S=CURVED)

PRINT(); PRINT(CRID); PRINT(" ");WAIT(1);

PRINT("Sphere: R "+RADIUS/1000+" km. Circ & Area:");
PRINT("Upper limits: SurfLen & SurfArea");
PRINT(" ");

//HT≤0 RETURNS 0

PRINT("//MAN at 2m (D 5.1km)");

PRINT("//ISS at 408 km (around 2373 km)");

PRINT("//Eg APOLLO at 29000 km");
EX(29000ᴇ3,6378000);
PRINT("Note the differences in these calculations...");
PRINT("My inverse surface area may be inaccurate");
PRINT(" ");

PRINT("//40.5 AU Voyager PALE BLUE DOT");

//PRINT("//145 AU Voyager 2019");

PRINT("//Viewed as Exoplanet from Proxima Centauri");
//This last verifies, except solve doesnt

//Test Traps
END;

EXPORT HORIZONS()
BEGIN
LOCAL DX;
LOCAL HT:=2;
PRINT();
PRINT(CRID);
EXAMPLES();

//PRINT("TEVAL DStraight "+TEVAL(DX:=HZDistanceD(HT,RADIUS)));// 58 CALC D
//PRINT("TEVAL SurfaceH "+TEVAL(HZDistanceSH(HT,RADIUS)));    //109 VS S
END;
Note: Some of my examples are incorrect - I mangled km and m. In particular, the AU2KM AU2M conversion.

Stephen Lewkowicz (G1CMZ)
04-15-2019, 09:28 PM (This post was last modified: 04-15-2019 09:50 PM by StephenG1CMZ.)
Post: #4
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: Horizons: Distance to Horizon
New in Version 0.3:
GetAngular - Gets angular size of a distant sphere (suitable for celestial use)
GetDip2Horizon (from eye height)

Improves: Examples, PrintReport

Code:

LOCAL CRID:="Horizons V0.3 © 2019 StephenG1CMZ";
//This version has simple geometric horizons
//No refraction

LOCAL HT;//FOR SOLVE //Global h used

LOCAL AU2M:=1000*AU2KM;
LOCAL KR2D:=180/π;
//MOST OF MY TEST VALUES ASSUME THIS VALUE. CHOOSE YOUR OWN.
//EARTH:WORKING IN m ACCURACY IS NORMALLY WITHIN 10cm (OF LATEST DATA)

LOCAL NA:=0;  // NA OR 0 => ONE OF:
//INPUT<0: NOTHING VISIBLE
//INPUT=0: NOTHING VISIBLE
//INPUT>0.5:OVER 1/2 OF SPHERE CANNOT BE VISIBLE
//INPUT=0.5 (INFINITE HEIGHT LIMITING CASE, use UpperLimits for values)
//AVOID /0 OR √(<0) (HOPEFULLY ONE OF THE ABOVE)

//Definitions //Distances and heights in same units
//DD    Direct Distance       (eye-horizon)
//DistC DistanceToC=HT+RADIUS (eye to centre of sphere)
//HT    Height above Sphere   (eye to surface. <0=NA)
//SLen  Curved Surface Length (paw-horizon)

//AU    Astronomical Unit
//mas   milliarcseconds

//RETURN SPHERE SURFACE AREA
BEGIN
END;

//The observed angular diameter at the eye (ignoring physiology)
///This DistC ASIN implement is suitable for celestial use (observing 3D spheres)
///(An alternate using HT ATAN is known for observing 2D disks)
BEGIN
//LOCAL AS;
IF DistC==0 THEN
RETURN NA;//IF INSTEAD HT=0 WERE TRAPPED return π
END;

IF RADIUS>DistC THEN //TOO WIDE TO SEE??
//IF NOT TRAPPED OUTPUT JUST STOPS!
RETURN NA; //π/2; //BEST VALUE TO RETURN TBC
END;
RETURN 2*ASIN(DIAMETER/(2*DistC));
END;
BEGIN
END;

//Dip to horizon
BEGIN
RETURN NA;
END;
END;
BEGIN
END;

//Dip to horizon
BEGIN
RETURN NA;
END;
END;
BEGIN
END;

//RETURN D is the straight geometrical eye-hZ dist
//TIMING 58us
BEGIN
RETURN NA;
END;
END;

//INVERSE
//RETURN HT TO SEE DD AWAY
BEGIN
IF DD<0 THEN
//NO UPPER LIMIT TO EYE HEIGHT OR DIST
//(Imp Limit DD>1ᴇ12 WITH EARTH RADIUS NOT CHECKED)
RETURN NA;
END;
RETURN HT;
END;

//INVERSE
//Get Height for Surface Area (SAF Fraction of sphere visible, 0..0.5)
//OR NA IF IMPOSSIBLE
BEGIN
IF SAF≤0 OR SAF≥0.5 THEN //INCLUDE SAF=0 TRAP
RETURN NA;
END;
RETURN HT; //MAX(HT,0);
//MAX CLIPS SMALL NEGATIVES: NEEDED WHEN SAF=0 UNLESS TRAPPED
END;
//NB INVERSE SA RESULTS ARE APPROXIMATE
//BUT NEED CHECKING TBD
//INVERSE
//Get Height for Surface Area (SA in units^2)
//OR NA IF IMPOSSIBLE
BEGIN
LOCAL SAF;
RETURN NA;
END;
//PRINT(100*SAF);//DIAGNOSTIC % INACCURATE
RETURN HT;
END;

//INVERSE
//RETURN HT TO SEE SURFLEN AWAY (OR NA IF SURFLEN IMPOSSIBLE)
BEGIN
RETURN NA;
END;
RETURN HT;
END;

EXPORT GetSurfLenDD(DD,RADIUS) //Surface Length from DD
//D is the straight geometrical eye-hor dist
//RETURN S IS THE SURFACE DISTANCE
//TIMING 55us
BEGIN
RETURN NA;
END;
END;

//BOTH THESE S DISTANCES SHOULD BE THE SAME (EXCEPT ROUNDING)
//THERE IS NO SIGNIFICANT DIFFERENCE
//HINT:
//S(HT) IS QUICKER IF YOU ONLY NEED S
//S(DD) IS QUICKER IF YOU ALSO NEED D (OR KNOW D)

//RETURN S IS THE SURFACE DISTANCE
//TIMING 109us QUICKER THAN D+SD
BEGIN
RETURN NA;
END;
END;

//Surfacevisible at Height
//FP Fraction
//PC Percent
//SA Surface Area (same units as radius)

//Height and Radius in same units. Ht is above surface
//Return.Proportion Surface Visible at Ht (0-0.5,HT≥0)
//For example:
//Radius of Earth = 6378 km
//Distance of Apollo 17 from Earth's surface at 1972-12-07T10:39Z = 29000 km
//Visible percentage of Earth's surface in 1972 "Blue Marble" photograph = 40.99% (41%)
BEGIN
//Guard HT<0:WHAT IS REALLY VISIBLE
RETURN NA;//0 OR 1
END;
END;

BEGIN
END;

//Hint: See EX
BEGIN
LOCAL PLACES:=3;//ROUNDING
LOCAL UNITS:=" ";

PRINT("Eye-horizon : "+ROUND(DX,PLACES)+UNITS);
END;

//Work an example
//Exhibits program functionality
//Hint: See Report
BEGIN
PRINT("Direct Length "+DX);
PRINT(" ");
END;

//Upper Limits = Half Sphere = Infinite Height
//Appply to SurfLen,SurfArea. Not DD and HT
BEGIN
LOCAL SpC,SpA;
RETURN {SpC,SpA};//HALF SPHERE
END;

EXAMPLES()
BEGIN
// a few examples : most test data elsewhere
//HT,D,S (D=STRAIGHT S=CURVED)

PRINT(); PRINT(CRID); PRINT(" ");WAIT(1);

PRINT("Sphere: R "+RADIUS/1000+" km. Circ & Area:");
PRINT("Upper limits: SurfLen & SurfArea");
PRINT(" ");

//HT≤0 RETURNS 0

PRINT("//MAN at 2m (D 5.1km)");

PRINT("//ISS at about 408 km ( ~2373 km)");

PRINT("//51ᴇ3 mas,140 °");
PRINT(60*60*1000*GetAngularDegrees(427200,108.5/2)+" mas"); //ISS fom Earth
//The next yields an ASIN error unless trapped.
//PRINT(           GetAngularDegrees(427200,       RADIUS )+" °");   //Earth from ISS - Wrong Parameters?
// OK

PRINT("//GPS SATELLITES (38%)");

PRINT("//Eg APOLLO at 29000 km");
EX(29000ᴇ3,6378000);
PRINT("Note the differences in these calculations...");
PRINT("My inverse surface area may be inaccurate");
PRINT(" ");

PRINT("//Sun 31-32");
PRINT(60*GetAngularDegrees(AU2M,RSUN));

PRINT("//40.5 AU Voyager PALE BLUE DOT");
//PRINT("//145 AU Voyager 2019");

PRINT("//Viewed as Exoplanet from Proxima Centauri");
//This last verifies, except solve doesnt

PRINT("Proxima Centauri from here:");
PRINT("//angular: (1.02 mas)");
//Test Traps
END;

EXPORT HORIZONS()
BEGIN
//LOCAL DD;
LOCAL HT:=2;
PRINT();
PRINT(CRID);
EXAMPLES();

//PRINT("TEVAL DStraight "+TEVAL(DD:=HZDistanceD(HT,RADIUS)));// 58 CALC D