Advertisement
Egor_Vakar

(Delphi) lab 7.1

May 28th, 2022
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 19.27 KB | None | 0 0
  1. unit MainUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.     Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.     Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  8.     Vcl.Samples.Spin, Vcl.Menus, Vcl.Grids, System.Generics.Collections,System.RegularExpressions,
  9.     Vcl.Imaging.jpeg, Vcl.ExtDlgs, Vcl.Imaging.pngimage;
  10.  
  11. type
  12.     Coords = Record
  13.         X: Integer;
  14.         Y: Integer;
  15.     End;
  16.  
  17.     TPList = ^TList;
  18.     TList = Record
  19.         Node: Integer;
  20.         Next: TPList;
  21.     End;
  22.  
  23.     TMatrix = Array Of Array Of Integer;
  24.     VertexList = TList<Coords>;
  25.     TArr = Array of Integer;
  26.  
  27.   TMainForm = class(TForm)
  28.     MainMenu: TMainMenu;
  29.     FileMenu: TMenuItem;
  30.     OpenFromFileMenu: TMenuItem;
  31.     Instruction: TMenuItem;
  32.     SaveToFileMenu: TMenuItem;
  33.     OpenFromFile: TOpenDialog;
  34.     SaveDialog: TSaveDialog;
  35.     InfoLabel1: TLabel;
  36.     SpinEdit: TSpinEdit;
  37.     StringGrid: TStringGrid;
  38.     InfoLabel: TLabel;
  39.     AboutDeveloperMenu: TMenuItem;
  40.     ShowGraphButton: TButton;
  41.     Visualizer: TImage;
  42.     VertexSpinEdit: TSpinEdit;
  43.     SearchButton: TButton;
  44.     procedure InstructionClick(Sender: TObject);
  45.     procedure AboutDeveloperMenuClick(Sender: TObject);
  46.     procedure FormCreate(Sender: TObject);
  47.     procedure ClearStringGrid;
  48.     procedure SpinEditChange(Sender: TObject);
  49.     procedure StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  50.       var CanSelect: Boolean);
  51.     procedure StringGridKeyPress(Sender: TObject; var Key: Char);
  52.     procedure ShowGraphButtonClick(Sender: TObject);
  53.     procedure ClearScreen;
  54.     procedure ChekGrid(Key: Char);
  55.     function FindMatrix: TMatrix;
  56.     procedure DrawGraph(AdjMatrix: TMatrix);
  57.     procedure DrawVertexes(Amount: Integer; Var VertexCoords: VertexList);
  58.     procedure DrawLines(AdjMatrix: TMatrix; VertexCoords: VertexList);
  59.     procedure DrawArrows(i,j: Integer; VertexCoords: VertexList);
  60.     procedure OpenFromFileMenuClick(Sender: TObject);
  61.     function IsInFileCorrect(const Path: String): Boolean;
  62.     function IsMatrixCorrect(Matrix: TMatrix): Boolean;
  63.     procedure SaveToFileMenuClick(Sender: TObject);
  64.     procedure VertexSpinEditChange(Sender: TObject);
  65.     procedure SearchButtonClick(Sender: TObject);
  66.     procedure CreateList(Matrix: TMatrix);
  67.     procedure AddNode(FromVertex: Integer; ToVertex: Integer);
  68.     function FindArray(Vertex: Integer; Length: Integer): TArr;
  69.     function IsAllFind(NewArr: TArr): Boolean;
  70.     procedure ShowAnswer(AnsArr: TArr; Vertex: Integer);
  71.     procedure ClearList;
  72.   private
  73.     { Private declarations }
  74.   public
  75.     { Public declarations }
  76.   end;
  77.  
  78. var
  79.   MainForm: TMainForm;
  80.  
  81. implementation
  82.  
  83. {$R *.dfm}
  84.  
  85. type
  86.     TPNode = ^TNode;
  87.     TNode = Record
  88.         FromVertex: Integer;
  89.         ToVertex: Integer;
  90.         Next: TPNode;
  91.     End;
  92.  
  93. var
  94.     Head: TPNode;
  95.     Xprev, YPrev: Integer;
  96.     Arr, prevArr: TArr;
  97.     MyVertexCoords: VertexList;
  98.     MyMatrix: TMatrix;
  99.  
  100. const
  101.     DEFAULT_WIDTH = 490;
  102.     DEFAULT_LEFT = 532;
  103.     EXTENDED_WIDTH = 1200;
  104.     VERTEXES_COLOR = $00B3B300;
  105.     BACKGROUND_COLOR = $001C1A13;
  106.     TREE_COLOR =  $004F009D;
  107.     POINT_RAD = 10;
  108.     LINE_WIDTH = 5;
  109.     GRAPH_RAD = 150;
  110.     VERTEX_RAD = 30;
  111.     FONT_SIZE = 20;
  112.  
  113. procedure TMainForm.AboutDeveloperMenuClick(Sender: TObject);
  114. begin
  115.     Application.MessageBox('Данная программа написана Вакарём Егором'#13#10'студентом группы 151002.','О разработчике');
  116. end;
  117.  
  118. procedure TMainForm.ClearStringGrid;
  119. var
  120.     i,j: Integer;
  121. begin
  122.     for i := 1 to StringGrid.ColCount do
  123.         for j := 1 to StringGrid.RowCount do
  124.             StringGrid.Cells[i, j] := '0';
  125. end;
  126.  
  127. procedure TMainForm.FormCreate(Sender: TObject);
  128. var
  129.     S: TGridRect;
  130.     i: Integer;
  131. begin
  132.     SaveToFileMenu.Enabled := False;
  133.     MainForm.Width := 545;
  134.     MainForm.Height := 491;
  135.     MainForm.Position := poDesktopCenter;
  136.     InfoLabel.Left := 14;
  137.     for i := 1 to 2 do
  138.     begin
  139.         StringGrid.Cells[i, 0] := IntToStr(i);
  140.         StringGrid.Cells[0, i] := IntToStr(i);
  141.     end;
  142.     ClearStringGrid;
  143.     StringGrid.Selection := S;
  144. end;
  145.  
  146. procedure TMainForm.InstructionClick(Sender: TObject);
  147. begin
  148.     Application.MessageBox('Граф задается матрицей смежности.'#13#10'Принимает ключи ''1'', ''0''.'#13#10'Диапазон значений кол-ва вершин [2; 13].'#13#10'Для начала алгоритма выберите вершину'#13#10'и нажмите кнопку "Начать поиск".', 'Инструкция');
  149. end;
  150.  
  151. function TMainForm.IsInFileCorrect(const Path: String): Boolean;
  152. const
  153.     MIN_SIZE = 1;
  154.     MAX_SIZE = 13;
  155. var
  156.     InFile: TextFile;
  157.     Size, Temp, i, j: Integer;
  158.     IsCorrect: Boolean;
  159. begin
  160.     IsCorrect := True;
  161.     Try
  162.         AssignFile(InFile, Path);
  163.         Reset(InFile);
  164.         Size := 0;
  165.         try
  166.             Read(InFile,Size);
  167.         except
  168.             IsCorrect := False;
  169.         end;
  170.     except
  171.         IsCorrect := False;
  172.     End;
  173.     if (IsCorrect) then
  174.     begin
  175.         if (Size < MIN_SIZE) or (Size > MAX_SIZE) then
  176.         begin
  177.             IsCorrect := False;
  178.         end
  179.     end;
  180.     i := 0;
  181.     if IsCorrect then
  182.     begin
  183.         While (IsCorrect and (i < Size) and (not Eof(InFile))) do
  184.         begin
  185.             try
  186.                 for j := 0 to Size - 1 do
  187.                     Read(InFile, Temp);
  188.                 if not ((Temp = 1) or (Temp = 0))then
  189.                     IsCorrect := False;
  190.             except
  191.                 IsCorrect := False;
  192.             end;
  193.             Inc(i);
  194.         end;
  195.     end;
  196.     if (IsCorrect and ((i < Size))) then
  197.     begin
  198.         IsCorrect := False;
  199.     end;
  200.     try
  201.         CloseFile(InFile);
  202.     except
  203.         IsCorrect := False;
  204.     end;
  205.     IsInFileCorrect := IsCorrect;
  206. end;
  207.  
  208. function TMainForm.IsMatrixCorrect(Matrix: TMatrix): Boolean;
  209. var
  210.     i,j: Integer;
  211.     Answer: Boolean;
  212. begin
  213.     Answer := True;
  214.     i := 0;
  215.     j := 0;
  216.     while (Answer and (i < Length(Matrix)))  do
  217.     begin
  218.         while (Answer and (j < Length(Matrix[0])))do
  219.         begin
  220.             if (((i = j) and (Matrix[i,j] <> 0)) or ((Matrix[i,j] = 1) and (Matrix[j,i] <> 0))) then
  221.                 Answer := False;
  222.             Inc(j);
  223.         end;
  224.         Inc(i);
  225.     end;
  226.     Result := Answer;
  227. end;
  228.  
  229. procedure TMainForm.OpenFromFileMenuClick(Sender: TObject);
  230. var
  231.     i,j, Size: Integer;
  232.     Matrix: TMatrix;
  233.     inFile: TextFile;
  234.     IsCorrect: Boolean;
  235. begin
  236.     IsCorrect := True;
  237.     if OpenFromFile.Execute() then
  238.     begin
  239.         if (IsInFileCorrect(OpenFromFile.FileName)) then
  240.         begin
  241.             AssignFile(InFile, OpenFromFile.FileName);
  242.             Reset(InFile);
  243.             Read(InFile, Size);
  244.             SpinEdit.Value := Size;
  245.             SetLength(Matrix, Size, Size);
  246.             for i := 0 to Size - 1 do
  247.             begin
  248.                 for j := 0 to Size - 1 do
  249.                 begin
  250.                     Read(InFile,Matrix[i,j]);
  251.                     StringGrid.Cells[j + 1,i + 1] := IntToStr(Matrix[i,j]);
  252.                 end;
  253.  
  254.             end;
  255.             CloseFile(InFile);
  256.             if IsMatrixCorrect(Matrix) then
  257.             begin
  258.                 SaveToFileMenu.Enabled := True;
  259.                 ShowGraphButton.Click;
  260.             end
  261.             else
  262.             begin
  263.                 FormCreate(MainForm);
  264.                 SpinEdit.Value := 2;
  265.             end;
  266.         end
  267.         else
  268.             IsCorrect := False;
  269.     end
  270.     else
  271.         IsCorrect := False;
  272.     if not IsCorrect then
  273.         Application.MessageBox('Работа с файлом некорректна', 'Ошибка', MB_ICONERROR);
  274. end;
  275.  
  276. procedure TMainForm.ClearScreen;
  277. Begin
  278.     With Visualizer.Canvas Do
  279.     Begin
  280.         Brush.Color := BACKGROUND_COLOR;
  281.         Pen.Color := BACKGROUND_COLOR;
  282.         Rectangle(0,0,Visualizer.Width,Visualizer.Height);
  283.     End;
  284. End;
  285.  
  286. function TMainForm.FindMatrix: TMatrix;
  287. var
  288.     N,i,j: Integer;
  289.     Matrix: TMatrix;
  290. begin
  291.     N := SpinEdit.Value;
  292.     SetLength(Matrix, N, N);
  293.     for i := 1 to N do
  294.         for j := 1 to N do
  295.             Matrix[i-1,j-1] := StrToInt(StringGrid.Cells[j,i]);
  296.     Result := Matrix;
  297. end;
  298.  
  299. Procedure TMainForm.DrawVertexes(Amount: Integer; Var VertexCoords: VertexList);
  300. Var
  301.     I, X, Y: Integer;
  302.     Center: Coords;
  303.     CurrPhi, Phi: Extended;
  304.     CurrCoords: Coords;
  305. Begin
  306.     VertexCoords := TList<Coords>.Create;
  307.     Phi := (2 * Pi) / Amount;
  308.     Center.X := Visualizer.Width Div 2;
  309.     Center.Y := Visualizer.Height Div 2;
  310.     With Visualizer.Canvas Do
  311.     Begin
  312.         Pen.Color := VERTEXES_COLOR;
  313.         Pen.Width := 1;
  314.         X := Center.X;
  315.         Y := Center.Y;
  316.         Font.Name := 'Segoe UI';
  317.         Font.Style := [FsBold];
  318.         Font.Color := VERTEXES_COLOR;
  319.         Font.Height := FONT_SIZE;
  320.         CurrPhi := 0;
  321.         For I := 0 To Amount - 1 Do
  322.         Begin
  323.             Brush.Color := VERTEXES_COLOR;
  324.             CurrPhi := CurrPhi + Phi;
  325.             Y := Round(Center.Y - GRAPH_RAD * Sin(CurrPhi));
  326.             X := Round(Center.X - GRAPH_RAD * Cos(CurrPhi));
  327.             Ellipse(X - VERTEX_RAD, Y - VERTEX_RAD, X + VERTEX_RAD, Y + VERTEX_RAD);
  328.             CurrCoords.X := X;
  329.             CurrCoords.Y := Y;
  330.             VertexCoords.Add(CurrCoords);
  331.             Brush.Color := BACKGROUND_COLOR;
  332.             If CurrPhi < Pi Then
  333.                 TextOut(X - 5 , Y - 64, IntToStr(I + 1))
  334.             Else
  335.                 TextOut(X - 5, Y + 32, IntToStr(I + 1));
  336.         End;
  337.     End;
  338.     XPrev := VertexCoords.Items[0].X;
  339.     YPrev := VertexCoords.Items[0].Y;
  340. End;
  341.  
  342. procedure TMainForm.DrawGraph(AdjMatrix: TMatrix);
  343. var
  344.     VertexCoords: VertexList;
  345. begin
  346.     VertexCoords := TList<Coords>.Create;
  347.     DrawVertexes(SpinEdit.Value, VertexCoords);
  348.     DrawLines(AdjMatrix, VertexCoords);
  349.     DrawVertexes(SpinEdit.Value, VertexCoords);
  350.     MyVertexCoords := VertexCoords;
  351.     MyMatrix := AdjMatrix;
  352. end;
  353.  
  354. procedure TMainForm.DrawArrows(i,j: Integer; VertexCoords: VertexList);
  355. var
  356.     Hypotenuse, Cos, XForArrow, YForArrow: Real;
  357.     MinY,MinX,MaxY,MaxX: Integer;
  358. begin
  359.     with Visualizer.Canvas do
  360.     begin
  361.         Pen.Color := clGreen;
  362.         MoveTo((VertexCoords.Items[I].X + 3* VertexCoords.Items[J].X) div 4,(VertexCoords.Items[I].Y + 3 * VertexCoords.Items[J].Y) div 4);
  363.         LineTo(VertexCoords.Items[J].X, VertexCoords.Items[J].Y);
  364.     end;
  365. end;
  366.  
  367. procedure TMainForm.DrawLines(AdjMatrix: TMatrix; VertexCoords: VertexList);
  368. var
  369.     I, J: Byte;
  370.     Hypotenuse, Cos, XForArrow, YForArrow: Real;
  371.     InciedenceList: TList;
  372.     Line: String;
  373. begin
  374.     for I := 0 to High(AdjMatrix) do
  375.     begin
  376.         for J := 0 to High(AdjMatrix) do
  377.         begin
  378.             if AdjMatrix[I, J] = 1 then
  379.             begin
  380.                 with Visualizer.Canvas do
  381.                 begin
  382.                     Pen.Color := VERTEXES_COLOR;
  383.                     Pen.Width := 4;
  384.                     MoveTo(VertexCoords.Items[I].X, VertexCoords.Items[I].Y);
  385.                     LineTo(VertexCoords.Items[J].X, VertexCoords.Items[J].Y);
  386.                     DrawArrows(i,j,VertexCoords);
  387.                 end;
  388.             end;
  389.         end;
  390.     end;
  391. end;
  392.  
  393. procedure TMainForm.SaveToFileMenuClick(Sender: TObject);
  394. var
  395.     OutputFile: TextFile;
  396.     i,j: Integer;
  397.     Ans: PWideChar;
  398. begin
  399.     ClearList;
  400.     CreateList(MyMatrix);
  401.     Arr := nil;
  402.     Arr := FindArray(VertexSpinEdit.Value, SpinEdit.Value);
  403.     Ans := PWideChar('Список расстояний из вершины ' + IntToStr(VertexSpinEdit.Value) + ':'#13#10);
  404.     for i := 0 to Length(Arr) - 1 do
  405.     begin
  406.         if i <> VertexSpinEdit.Value - 1 then
  407.             if Arr[i] = 10000 then
  408.                 Ans := PWideChar(Ans + IntToStr(i+1) + ': Невозможно достичь'#13#10)
  409.             else
  410.                 Ans := PWideChar(Ans + IntToStr(i+1) + ': ' + IntToStr(Arr[i]) + #13#10);
  411.     end;
  412.     if SaveDialog.Execute() and FileExists(SaveDialog.FileName) then
  413.     begin
  414.         AssignFile(OutputFile, SaveDialog.FileName);
  415.         try
  416.             Rewrite(OutputFile);
  417.             Writeln(OutputFile,Ans);
  418.             CloseFile(OutputFile);
  419.             Application.MessageBox('Данные успешно записаны в файл!', 'Сохранение', MB_ICONINFORMATION);
  420.         except
  421.             Application.MessageBox('Отказано в доступе! Измените параметры файла! ', 'Ошибка!', MB_ICONERROR);
  422.         end;
  423.     end
  424.     else
  425.         Application.MessageBox('Введено некорректное имя файла', 'Ошибка!', MB_ICONERROR);
  426. end;
  427.  
  428. procedure TMainForm.AddNode(FromVertex: Integer; ToVertex: Integer);
  429. var
  430.     Temp, Current: TPNode;
  431. begin
  432.     New(Temp);
  433.     Temp.FromVertex := FromVertex;
  434.     Temp.ToVertex := ToVertex;
  435.     Temp.Next := nil;
  436.     if Head = nil then
  437.         Head := Temp
  438.     else
  439.     begin
  440.         Current := Head;
  441.         while Current.Next <> nil do
  442.             Current := Current.Next;
  443.         Current.Next := Temp;
  444.     end;
  445. end;
  446.  
  447. procedure TMainForm.CreateList(Matrix: TMatrix);
  448. var
  449.     i,j: Integer;
  450. begin
  451.     for i := 0 to Length(Matrix) - 1 do
  452.         for j := 0 to Length(Matrix[0]) - 1 do
  453.             if (matrix[i][j] = 1) then
  454.                     AddNode(i+1, j+1);
  455. end;
  456.  
  457. function TMainForm.IsAllFind(NewArr: TArr): Boolean;
  458. var
  459.     Answer: Boolean;
  460.     i: Integer;
  461. begin
  462.     i := 0;
  463.     Answer := True;
  464.     while (Answer and (i < Length(NewArr))) do
  465.     begin
  466.         if (NewArr[i] = 10000) then
  467.             Answer := false;
  468.         Inc(i);
  469.     end;
  470.     Result := Answer;
  471. end;
  472.  
  473. function TMainForm.FindArray(Vertex: Integer; Length: Integer): TArr;
  474. var
  475.     NewArr: TArr;
  476.     i: Integer;
  477.     Temp: TPNode;
  478.     AreSame: Boolean;
  479. begin
  480.     SetLength(NewArr, Length);
  481.     for i := 0 to Length - 1 do
  482.         NewArr[i] := 10000;
  483.     NewArr[vertex - 1] := 0;
  484.     repeat
  485.         Temp := Head;
  486.         while (temp <> nil) do
  487.         begin
  488.             if (NewArr[Temp.ToVertex - 1] > NewArr[Temp.FromVertex - 1] + 1) then
  489.                 NewArr[Temp.ToVertex - 1] := NewArr[Temp.FromVertex - 1] + 1;
  490.             Temp := Temp.Next;
  491.         end;
  492.         AreSame := (NewArr = PrevArr);
  493.         PrevArr := Arr;
  494.     until (IsAllFind(arr) or areSame);
  495.     Result := NewArr;
  496. end;
  497.  
  498. procedure TMainForm.ShowAnswer(AnsArr: TArr; Vertex: Integer);
  499. var
  500.     i: Integer;
  501.     Ans: PWideChar;
  502. begin
  503.     Ans := PWideChar('Список расстояний из вершины ' + IntToStr(Vertex) + ':'#13#10);
  504.     for i := 0 to Length(AnsArr) - 1 do
  505.     begin
  506.         if i <> Vertex - 1 then
  507.             if AnsArr[i] = 10000 then
  508.                 Ans := PWideChar(Ans + IntToStr(i+1) + ': Невозможно достичь'#13#10)
  509.             else
  510.                 Ans := PWideChar(Ans + IntToStr(i+1) + ': ' + IntToStr(AnsArr[i]) + #13#10);
  511.     end;
  512.      Application.MessageBox(Ans, 'Ответ');
  513. end;
  514.  
  515. procedure TMainForm.ClearList;
  516. var
  517.     Current, Prev: TPNode;
  518.     i: Integer;
  519. begin
  520.     while Head <> nil do
  521.     begin
  522.         Current := Head;
  523.         while Current.Next <> nil do
  524.         begin
  525.             Prev := Current;
  526.             Current := Current.Next;
  527.         end;
  528.         if Current <> Head then
  529.         begin
  530.             Prev.Next := nil;
  531.         end
  532.         else
  533.             Head := nil;
  534.         Dispose(Current);
  535.     end;
  536. end;
  537.  
  538. procedure TMainForm.SearchButtonClick(Sender: TObject);
  539. begin
  540.     ClearList;
  541.     CreateList(MyMatrix);
  542.     Arr := nil;
  543.     Arr := FindArray(VertexSpinEdit.Value, SpinEdit.Value);
  544.     ShowAnswer(Arr, VertexSpinEdit.Value);
  545. end;
  546.  
  547. procedure TMainForm.ShowGraphButtonClick(Sender: TObject);
  548. var
  549.     Matrix: TMatrix;
  550. begin
  551.     MainForm.Width := 1100;
  552.     MainForm.Height := 530;
  553.     InfoLabel.Left := 302;
  554.     MainForm.Position := poDesktopCenter;
  555.     ClearScreen;
  556.     Matrix := FindMatrix;
  557.     DrawGraph(Matrix);
  558.     VertexSpinEdit.MaxValue := SpinEdit.Value;
  559.     VertexSpinEdit.Value := 1;
  560.     SaveToFileMenu.Enabled := True;
  561. end;
  562.  
  563. procedure TMainForm.SpinEditChange(Sender: TObject);
  564. var
  565.     S: TGridRect;
  566. begin
  567.     if MainForm.Width > 545 then
  568.     begin
  569.         FormCreate(MainForm);
  570.     end;
  571.     StringGrid.Selection := S;
  572.     if (SpinEdit.Value >= StringGrid.ColCount) then
  573.     begin
  574.         repeat
  575.             ClearStringGrid;
  576.             StringGrid.ColCount := StringGrid.ColCount + 1;
  577.             StringGrid.RowCount := StringGrid.RowCount + 1;
  578.             StringGrid.Cells[StringGrid.RowCount - 1, 0] := IntToStr(StringGrid.RowCount - 1);
  579.             StringGrid.Cells[0, StringGrid.RowCount - 1] := IntToStr(StringGrid.RowCount - 1);
  580.         until(SpinEdit.Value = StringGrid.ColCount - 1);
  581.     end
  582.     else
  583.     begin
  584.         repeat
  585.             ClearStringGrid;
  586.             StringGrid.Cells[StringGrid.RowCount, 0] := '';
  587.             StringGrid.Cells[0, StringGrid.RowCount] := '';
  588.             StringGrid.ColCount := StringGrid.ColCount - 1;
  589.             StringGrid.RowCount := StringGrid.RowCount - 1;
  590.         until(SpinEdit.Value = StringGrid.ColCount - 1);
  591.     end;
  592. end;
  593.  
  594. procedure TMainForm.ChekGrid(Key: Char);
  595. var
  596.     isChanged: Boolean;
  597.     Value: String;
  598. begin
  599.     Value := Copy(StringGrid.Cells[StringGrid.Col, StringGrid.Row],1,1);
  600.     if ((Key = '0') or (Key = '1')) and (Key <> Value) and (MainForm.Width > 545) then
  601.     begin
  602.         MainForm.Width := 545;
  603.         MainForm.Height := 491;
  604.         MainForm.Position := poDesktopCenter;
  605.         InfoLabel.Left := 14;
  606.     end;
  607. end;
  608.  
  609. procedure TMainForm.StringGridKeyPress(Sender: TObject; var Key: Char);
  610. begin
  611.     ChekGrid(Key);
  612.     if Key = '0' then
  613.     begin
  614.         StringGrid.Cells[StringGrid.Col, StringGrid.Row] := '0';
  615.         StringGrid.Cells[StringGrid.Row, StringGrid.Col] := '1';
  616.     end
  617.     else
  618.         if StringGrid.Col <> StringGrid.Row then
  619.             if Key = '1' then
  620.             begin
  621.                 StringGrid.Cells[StringGrid.Row, StringGrid.Col] := '0';
  622.                 StringGrid.Cells[StringGrid.Col, StringGrid.Row] := '1';
  623.             end;
  624.     Key := #0;
  625. end;
  626.  
  627. procedure TMainForm.StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  628.   var CanSelect: Boolean);
  629. begin
  630.     if (ACol < 1) or (ARow < 1) or (ARow = ACol) then
  631.         CanSelect := False;
  632. end;
  633.  
  634. procedure TMainForm.VertexSpinEditChange(Sender: TObject);
  635. var
  636.     Vertex: Integer;
  637.     XCoord, YCoord: Integer;
  638. begin
  639.     Vertex := StrToInt(VertexSpinEdit.Text);
  640.     XCoord := MyVertexCoords.Items[Vertex - 1].X;
  641.     YCoord := MyVertexCoords.Items[Vertex - 1].Y;
  642.     with Visualizer.Canvas Do
  643.     begin
  644.         Pen.Color := VERTEXES_COLOR;
  645.         Pen.Width := LINE_WIDTH;
  646.         Brush.Color := VERTEXES_COLOR;
  647.         Ellipse(XPrev - VERTEX_RAD, YPrev - VERTEX_RAD, XPrev + VERTEX_RAD, YPrev + VERTEX_RAD);
  648.         Pen.Color := ClYellow;
  649.         Ellipse(XCoord - VERTEX_RAD, YCoord - VERTEX_RAD, XCoord + VERTEX_RAD, YCoord + VERTEX_RAD);
  650.         Brush.Color := ClYellow;
  651.         Ellipse(XCoord - POINT_RAD, YCoord - POINT_RAD, XCoord + POINT_RAD, YCoord + POINT_RAD);
  652.     end;
  653.     XPrev := MyVertexCoords.Items[Vertex - 1].X;
  654.     YPrev := MyVertexCoords.Items[Vertex - 1].Y;
  655. end;
  656.  
  657. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement