Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.93 KB | None | 0 0
  1. {$APPTYPE CONSOLE}
  2.  
  3. uses
  4.     System.Math, System.SysUtils;
  5.  
  6. const
  7.     NoLine = -1;
  8.     MaxInt = '2147483648';
  9.     MinInt = '-2147483648';
  10.     MaxDouble = '1.7e308';
  11.     MinDouble = '2.5e-308';
  12.  
  13. type
  14.     TMap = array of array of Integer;
  15.     TTgs = array of array of Double;
  16.     Point = record
  17.         x: Double;
  18.         y: Double;
  19.     end;
  20.     TPoints = array of Point;
  21.  
  22. procedure GetData(var Value: Integer); Overload;
  23. var
  24.     Valid: Boolean;
  25. begin
  26.     Valid := False;
  27.     repeat
  28.         try
  29.             Write('Enter number of points: ');
  30.             Readln(Value);
  31.             Valid := True;    
  32.         except
  33.             Writeln('Invalid size. Value between ', MinInt, ' and ', MaxInt, ' expected');
  34.         end;
  35.     until Valid;
  36. end;
  37.  
  38. procedure GetData(var Points: TPoints; Size: Integer); Overload;
  39. var
  40.     i: Integer;
  41.     Valid: Boolean;
  42. begin
  43.     for i := 0 to Size - 1 do
  44.     begin
  45.         Valid := False;
  46.         repeat
  47.             try
  48.                 Write('Enter Point(x, y) #', i + 1, ': ');
  49.                 Readln(Points[i].x, Points[i].y);
  50.                 Valid := True;
  51.             except
  52.                 Writeln('Invalid point. Values between ', MinDouble, ' and ', MaxDouble, ' expected or same negative')
  53.             end;
  54.         until Valid;
  55.     end;
  56. end;
  57.  
  58. procedure InitArray(var Points: TPoints; Size: Integer); Overload;
  59. begin
  60.     SetLength(Points, Size);
  61. end;
  62.  
  63. procedure InitArray(var Map: TMap; Size: Integer); Overload;
  64. var
  65.     i: Integer;
  66. begin
  67.     SetLength(Map, Size, Size);
  68. end;
  69.  
  70. procedure InitArray(var Tgs: TTgs; Size: Integer); Overload;
  71. var
  72.     i: Integer;
  73. begin
  74.     SetLength(Tgs, Size, Size);
  75. end;
  76.  
  77. function CompareDouble(A, B: Double): Integer;
  78. const
  79.     Epsilon = 0.001;
  80. begin
  81.     if(A = Infinity) and (B = Infinity) then
  82.         CompareDouble := 0
  83.     else
  84.         CompareDouble := CompareValue(A, B, Epsilon);
  85. end;
  86.  
  87. function GetTg(p1, p2: Point): Double;
  88. begin
  89.     if CompareDouble(p2.x, p1.x) = 0 then
  90.         GetTg := Infinity
  91.     else  
  92.         GetTg := (p2.y - p1.y) / (p2.x - p1.x);
  93. end;
  94.  
  95. function GetTgs(Points: TPoints; Size: Integer): TTgs;
  96. var
  97.     Tgs: TTgs;
  98.     i, j: Integer;
  99. begin
  100.     InitArray(Tgs, Size);
  101.     for i := 0 to Size - 1 do
  102.         for j := i + 1 to Size - 1 do
  103.             Tgs[i][j] := GetTg(Points[i], Points[j]);
  104.     GetTgs := Tgs;
  105. end;
  106.  
  107. function GetPath(var Map: TMap; Tgs: TTgs; LineIndex: Integer; Tg: Double; Step: Integer; From: Integer = NoLine): Integer;
  108. var
  109.     i, Counter: Integer;
  110. begin
  111.     Counter := 1;
  112.     for i := LineIndex + 1 to High(Map) do
  113.     begin
  114.         if Map[LineIndex][i] = Step then
  115.         begin
  116.             if From <> NoLine then
  117.                 Map[From][i] := Step;
  118.         end
  119.         else
  120.         if CompareDouble(Tgs[LineIndex][i], Tg) = 0 then
  121.         begin
  122.             Map[LineIndex][i] := Step;
  123.             if From <> NoLine then
  124.                 Map[From][i] := Step;
  125.             Counter := 1 + GetPath(Map, Tgs, i, Tg, Step, LineIndex);
  126.         end;
  127.     end;
  128.     GetPath := Counter;
  129. end;
  130.  
  131. function GetLongestPath(Tgs: TTgs; Size: Integer): Integer;
  132. var
  133.     i, j: Integer;
  134.     Map: TMap;
  135.     Path, MaxPath, Step: Integer;
  136. begin
  137.     Step := 0;
  138.     MaxPath := 0;
  139.     InitArray(Map, Size);
  140.     for i := 0 to Size - 1 do
  141.         for j := i + 1 to Size - 1 do
  142.         begin
  143.             Inc(Step);
  144.             Path := GetPath(Map, Tgs, i, Tgs[i][j], Step);
  145.             if Path > MaxPath then
  146.                 MaxPath := Path;
  147.         end;
  148.     GetLongestPath := MaxPath;    
  149. end;
  150.  
  151. procedure Main;
  152. var
  153.     Points: TPoints;
  154.     Tgs: TTgs;
  155.     N: Integer;
  156. begin
  157.     FormatSettings.DecimalSeparator := '.';
  158.     Writeln('This program counts points on same line');
  159.     GetData(N);
  160.     InitArray(Points, N);
  161.     GetData(Points, N);
  162.     InitArray(Tgs, N);
  163.     Tgs := GetTgs(Points, N);
  164.     Writeln('Number of points: ', GetLongestPath(Tgs, N));
  165. end;
  166.  
  167. begin
  168.     Main;
  169. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement