Advertisement
Janilabo

Move

Nov 13th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.90 KB | None | 0 0
  1. function MoveTPA(var arr: TPointArray; oldIndex, newIndex: Integer): Boolean;
  2. var
  3.   h, i: Integer;
  4.   t: TPoint;
  5. begin
  6.   h := High(arr);
  7.   if ((h > 0) and InRange(oldIndex, 0, h)) then
  8.   begin
  9.     if (newIndex < 0) then
  10.       newIndex := 0
  11.     else
  12.       if (newIndex > h) then
  13.         newIndex := h;
  14.     Result := (oldIndex <> newIndex);
  15.     if Result then
  16.     begin
  17.       t := arr[oldIndex];
  18.       if (oldIndex > newIndex) then
  19.         for i := oldIndex downto (newIndex + 1) do
  20.           arr[i] := arr[(i - 1)]
  21.       else
  22.         for i := oldIndex to (newIndex - 1) do
  23.           arr[i] := arr[(i + 1)];
  24.       arr[newIndex] := t;
  25.     end;
  26.   end else
  27.     Result := False;
  28. end;
  29.  
  30. var
  31.   TPA: TPointArray;
  32.  
  33. begin
  34.   TPA := [Point(0, 1), Point(2, 3), Point(4, 5), Point(6, 7), Point(8, 9)];
  35.   WriteLn('BEFORE: ' + ToStr(TPA));
  36.   MoveTPA(TPA, 1, 4);
  37.   WriteLn('AFTER: ' + ToStr(TPA));
  38. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement