Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {$APPTYPE CONSOLE}
- uses
- System.Math, System.SysUtils;
- const
- NoLine = -1;
- MaxInt = '2147483648';
- MinInt = '-2147483648';
- MaxDouble = '1.7e308';
- MinDouble = '2.5e-308';
- type
- TMap = array of array of Integer;
- TTgs = array of array of Double;
- Point = record
- x: Double;
- y: Double;
- end;
- TPoints = array of Point;
- procedure GetData(var Value: Integer); Overload;
- var
- Valid: Boolean;
- begin
- Valid := False;
- repeat
- try
- Write('Enter number of points: ');
- Readln(Value);
- Valid := True;
- except
- Writeln('Invalid size. Value between ', MinInt, ' and ', MaxInt, ' expected');
- end;
- until Valid;
- end;
- procedure GetData(var Points: TPoints; Size: Integer); Overload;
- var
- i: Integer;
- Valid: Boolean;
- begin
- for i := 0 to Size - 1 do
- begin
- Valid := False;
- repeat
- try
- Write('Enter Point(x, y) #', i + 1, ': ');
- Readln(Points[i].x, Points[i].y);
- Valid := True;
- except
- Writeln('Invalid point. Values between ', MinDouble, ' and ', MaxDouble, ' expected or same negative')
- end;
- until Valid;
- end;
- end;
- procedure InitArray(var Points: TPoints; Size: Integer); Overload;
- begin
- SetLength(Points, Size);
- end;
- procedure InitArray(var Map: TMap; Size: Integer); Overload;
- var
- i: Integer;
- begin
- SetLength(Map, Size, Size);
- end;
- procedure InitArray(var Tgs: TTgs; Size: Integer); Overload;
- var
- i: Integer;
- begin
- SetLength(Tgs, Size, Size);
- end;
- function CompareDouble(A, B: Double): Integer;
- const
- Epsilon = 0.001;
- begin
- if(A = Infinity) and (B = Infinity) then
- CompareDouble := 0
- else
- CompareDouble := CompareValue(A, B, Epsilon);
- end;
- function GetTg(p1, p2: Point): Double;
- begin
- if CompareDouble(p2.x, p1.x) = 0 then
- GetTg := Infinity
- else
- GetTg := (p2.y - p1.y) / (p2.x - p1.x);
- end;
- function GetTgs(Points: TPoints; Size: Integer): TTgs;
- var
- Tgs: TTgs;
- i, j: Integer;
- begin
- InitArray(Tgs, Size);
- for i := 0 to Size - 1 do
- for j := i + 1 to Size - 1 do
- Tgs[i][j] := GetTg(Points[i], Points[j]);
- GetTgs := Tgs;
- end;
- function GetPath(var Map: TMap; Tgs: TTgs; LineIndex: Integer; Tg: Double; Step: Integer; From: Integer = NoLine): Integer;
- var
- i, Counter: Integer;
- begin
- Counter := 1;
- for i := LineIndex + 1 to High(Map) do
- begin
- if Map[LineIndex][i] = Step then
- begin
- if From <> NoLine then
- Map[From][i] := Step;
- end
- else
- if CompareDouble(Tgs[LineIndex][i], Tg) = 0 then
- begin
- Map[LineIndex][i] := Step;
- if From <> NoLine then
- Map[From][i] := Step;
- Counter := 1 + GetPath(Map, Tgs, i, Tg, Step, LineIndex);
- end;
- end;
- GetPath := Counter;
- end;
- function GetLongestPath(Tgs: TTgs; Size: Integer): Integer;
- var
- i, j: Integer;
- Map: TMap;
- Path, MaxPath, Step: Integer;
- begin
- Step := 0;
- MaxPath := 0;
- InitArray(Map, Size);
- for i := 0 to Size - 1 do
- for j := i + 1 to Size - 1 do
- begin
- Inc(Step);
- Path := GetPath(Map, Tgs, i, Tgs[i][j], Step);
- if Path > MaxPath then
- MaxPath := Path;
- end;
- GetLongestPath := MaxPath;
- end;
- procedure Main;
- var
- Points: TPoints;
- Tgs: TTgs;
- N: Integer;
- begin
- FormatSettings.DecimalSeparator := '.';
- Writeln('This program counts points on same line');
- GetData(N);
- InitArray(Points, N);
- GetData(Points, N);
- InitArray(Tgs, N);
- Tgs := GetTgs(Points, N);
- Writeln('Number of points: ', GetLongestPath(Tgs, N));
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement