//Horizon distances calculator
//units: [m], [rad]
Re=6.371e6; //earth radius
h=1.65; //eye level height

phi=linspace(0,1e-3,1000); //angle from center of earth
x=sin(phi)*Re; //points of view in earth coordinates
y=(cos(phi)-1)*Re; //y=0 at observer

l=sqrt(x.^2+y.^2); //straight-line (tunnel) distance to earth point
d=sqrt(h^2+l.^2+2*h*l.*sin(phi*0.5)); //eye height to earth point distance
th=acos((h^2+d.^2-l.^2)./(2*h*d)); //theta angle from eye height to earth point

[a b]=max(th);
