unit Azmuth1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function Sgn (X: Real): Real;
  begin
        if abs(x) > x then
          result := -1.000
        else if abs(x)= x then
          result := 1.000;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
        JJ, W1, W2, Wr, WT, L1, L2: real;
        Lt, LR, W3, HH, GG, H0, H9 : real;
       
        Bearing : real;
        Distance : real;


begin
        //L1 := 33.01;
        //W1 := 96.892;
        L1 := StrToFloat(Edit3.text);   //your latitude
        W1 := StrToFloat(Edit4.text);   //your longitude in degrees west

        //L2 := 35.1;
        //W2 := 96.1;
        L2 := StrToFloat(Edit5.text);   //other latitude
        W2 := StrToFloat(Edit6.text);   //other longitude

        if (L1 = 0) and (L2 = 0) then
        L1 := L1 + 0.00001;     //program crashes if both L1 and L2 are 0

        //  PI := 3.141592;
        JJ := PI/180;
        Wr := -(W1 - 180*(1+ Sgn(W1-0.0001))) * JJ ;
        WT := -(W2 - 180*(1+ Sgn(W2-0.0001))) * JJ ;
        Lt := L1 * JJ;
        LR := L2 * JJ;
        W3 := Wr - Wt + 0.0001;
        W3 := PI * (1 - Sgn(W3)) + W3;
        HH := Sin(Lt)*Sin(Lr)+Cos(Lt)*Cos(Lr)*Cos(W3);
        GG := ArcTan(Sqrt(1-HH*HH)/HH)+PI/2*(1-Sgn(HH));
        H0 := int(1.59 * GG) + 1;
        H9 := (Sin(Lr)-HH*Sin(Lt))/sin(GG)/Cos(Lt);

   begin;
        If H9< -0.9999999 then
                H9 := ArcTan(PI/2*(1-Sgn(H9)))
        Else    H9 := ArcTan(Sqrt(1-H9*H9)/H9)+PI/2*(1- Sgn(H9)); //sgn;
   end;

   H9 := H9* Sgn(W3-PI)* Sgn(PI - GG);
   H9 := H9 + PI * (1- Sgn(H9));
   Bearing := INT(H9/JJ + 0.5);
   Distance := INT(6356.775* GG + 0.5);
   Edit1.text := FloatToStr(Bearing);
   Edit2.Text := FloatToStr(Distance);  

end;

end.


