program TransmissionGisements;
// Exercice Transmission de Gisements
// in https://cours-fad-public.ensg.eu/course/view.php?id=51
const INFINITY = 1E18;
type TStation = record
  StFrom   : string;
  StTo     : string;
  Longueur : double;
  LectureAR: double;
  LectureAV: double;
  Gisement : double;
  X: double;
  Y: double;
end;

function SetStation(const QFrom, QTo: string; const QL, QLAR, QLAV: double; const QG: double): TStation;
begin
  with Result do begin
    StFrom    := QFrom;
    StTo      := QTo;
    Longueur  := QL
    LectureAR := QLAR;
    LectureAV := QLAV;
    Gisement  := QG;
    X := 0.00;
    Y := 0.00;
  end;
end;
function StationDebugString(const S: TStation; const Tag: string): string;
begin
  Result := sprintf('%s: %s -> %s: L = %.3f, AR = %.3f, AV = %.3f, G = %.3f - X = %.3f, Y = %.3f',
                    [Tag, S.StFrom, S.StTo, S.Longueur, S.LectureAR, S.LectureAV, S.Gisement, S.X, S.Y]);
end;

procedure Main();

var
  i, Nb, IdxColor    : integer;
  G0, G1      : double;
  dx, dy: double;
  QLongueurStd: double;
  S0, S1: TStation;
  ang: double;
  MesStations: array of TStation;
  toto: string;
  X1, Y1, X2, Y2, MARGE: double;
  EWE: double;
  R, G, B, A: byte;
begin
  (* Your code here *)
  //ClearInputConsole();
  //ReadLn(toto);

  //ConsoleEof();
  print(toto);
  print('Exercice cheminement par transport des gisements (théodolite)');
  print('-------------------------------------------------------------');
  G0 := 251.324;
  printf('Gisement entre le point A et le point 1.0: G0 = %.3f grades', [G0]);
  print('');
  print ('Test de TransmissionGisement');
  G1 := TransmissionGisement(G0, 0, 39.432, 400.00);
  printf('G1 = %.3f', [G1]);
  print('');
  // données
  Nb := 12;
  QLongueurStd := 100.00;
  SetLength(MesStations, Nb);
  MesStations[0] := SetStation('A'  , '1.0', QLongueurStd, 0.00, 0.00, G0);
  MesStations[1] := SetStation('1.0', '1.1', QLongueurStd, 0.00,  39.432, -1);
  MesStations[2] := SetStation('1.1', '1.2', QLongueurStd, 0.00, 219.887, -1);
  MesStations[3] := SetStation('1.2', '1.3', QLongueurStd, 0.00, 182.143, -1);
  MesStations[4] := SetStation('1.3', '1.4', QLongueurStd, 0.00, 228.478, -1);
  MesStations[5] := SetStation('1.4', '1.5', QLongueurStd, 0.00, 151.738, -1);
  MesStations[6] := SetStation('1.5', '1.6', QLongueurStd, 0.00, 257.128, -1);
  MesStations[7] := SetStation('1.6', '1.7', QLongueurStd, 0.00, 220.000, -1);
  MesStations[8] := SetStation('1.7', '1.8', QLongueurStd, 0.00, 100.000, -1);
  MesStations[9] := SetStation('1.8', '1.9', QLongueurStd, 0.00, 200.000, -1);
  MesStations[10] := SetStation('1.9', '1.10', QLongueurStd, 0.00, 300.000, -1);
  MesStations[11] := SetStation('1.10', '1.11', QLongueurStd, 0.00, 300.000, -1);

  // Calcul des gisements et des coordonnées
  for i := 1 to Nb-1 do
  begin
    S0 := MesStations[i-1];
    S1 := MesStations[i];
    S1.Gisement := TransmissionGisement(S0.Gisement, S1.LectureAR, S1.LectureAV, 400.00);

    ang := 100 - S1.Gisement;
    dx  := S1.Longueur * cosg(ang);
    dy  := S1.Longueur * sing(ang);
    S1.X := S0.X + dx;
    S1.Y := S0.Y + dy;
    MesStations[i] := S1;
  end;
  print('');
  printf('Résultats: %d stations', [Nb]);
   // BBox
  X1 :=  INFINITY;
  Y1 :=  INFINITY;
  X2 := -INFINITY;
  Y2 := -INFINITY;
  for i := 0 to Nb-1 do
  begin
    S1 := MesStations[i];
    print(StationDebugString(S1, 'Station %d: '));
    // BBox
    if (S1.X < X1) then X1 := S1.X;
    if (S1.Y < Y1) then Y1 := S1.Y;
    if (S1.X > X2) then X2 := S1.X;
    if (S1.Y > Y2) then Y2 := S1.Y;
  end;
  MARGE := 0.05 * Hypot2D(X2-X1, Y2-Y1);
  X1 := X1 - MARGE;
  Y1 := Y1 - MARGE;
  X2 := X2 + MARGE;
  Y2 := Y2 + MARGE;
  printf('BBox: (%.3f, %.3f)-(%.3f, %.3f)', [X1, Y1, X2, Y2]);
  print('');
  // représentation graphique
  DRAW2D_BEGINDRAWING(800, 800, X1, Y1, X2, Y2, 'Transmission des gisements: Polygonale');
    Draw2D_SetBackGroundColor(255, 255, 255, 255);
    Draw2D_AddStyleSheet('StyleLinePolygo',
                          0,0,255, 255, 3, 0.35, // pen
                          0,0,255, 128, 1, // brush
                          'Arial', 0,0,0,255, 14, 3.5, 'B---');

    Draw2D_SetStyleSheet(1);
    Draw2D_BeginPolyline(0, 'Polygo', false);
      S0 := MesStations[0];
      Draw2D_AddVertex(S0.X, S0.Y);
      for i := 1 to Nb-1 do
      begin
        S1 := MesStations[i];
        Draw2D_AddVertex(S1.X, S1.Y);
      end;
    Draw2D_EndPolyline();
    // marqueurs
    for i := 0 to Nb-1 do
    begin
      S1 := MesStations[i];
      Draw2D_AddMarker(0, S1.StTo, S1.X, S1.Y, 4, 0, S1.StTo);
    end;
  DRAW2D_ENDANDDISPLAYDRAWING();
  // test de la fonction Eval
  printf('Eval classique : %.5f', [Eval('340+30' , false, 0.0)]);
  printf('Eval classique : %.5f', [Eval('340-400', false, 0.0)]);
  // Eval supporte le calcul modulaire
  printf('Eval sur Z/360 Z: %.5f', [Eval('340+30', True, 360.0)]);

  printf('Eval sur Z/360 Z: %.5f', [Eval('0'  , True, 360.0)]);
  printf('Eval sur Z/360 Z: %.5f', [Eval('359', True, 360.0)]);
  printf('Eval sur Z/360 Z: %.5f', [Eval('360', True, 360.0)]);
  printf('Eval sur Z/360 Z: %.5f', [Eval('361', True, 360.0)]);



  print('');
  printf('Eval sur Z/400 Z: %.5f', [Eval('0', True, 400.0)]);
  printf('Eval sur Z/400 Z: %.5f', [Eval('50', True, 400.0)]);

  printf('Eval sur Z/400 Z: %.5f', [Eval('340+80', True, 400.0)]);
  printf('Eval sur Z/400 Z: %.5f', [Eval('740+80', True, 400.0)]);
  printf('Eval sur Z/400 Z: %.5f', [Eval('1140+80', True, 400.0)]);


  printf('Eval sur Z/400 Z: %.5f', [Eval('-20', True, 400.0)]);
  printf('Eval sur Z/400 Z: %.5f', [Eval('-420', True, 400.0)]);
  printf('Eval sur Z/400 Z: %.5f', [Eval('-820', True, 400.0)]);
  printf('Eval sur Z/400Z: %.5f', [Eval('-1220', True, 400.0)]);


  print('');
  print('-------------------');
  IdxColor := PALETTE_256_GetIdxNearestColor(255, 0, 115, 255);
  PALETTE_256_GetColorByIndex(IdxColor, R, G, B, A);
  printf('IdxColor: %d - R: %d, G: %d, B: %d, A: %d', [IdxColor, R, G, B, A]);





end;
(* *************************************** *)
(* Don't edit after this                   *)
begin
  Main();
  print('');
  print('================================');
  print('*** Script terminated ***');
end.
