Results 1 to 7 of 7

Thread: Way to copy TPA...

  1. #1
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Red face Way to copy TPA...

    I have trouble with copying TPA...

    Simba Code:
    program new;
    var
    tpa1 ,tpa2 : TPointArray;
    a ,b :integer;
    begin
      tpa1 := [Point(1,1),Point(2,2)  ];
      tpa2 := tpa1;
      OffsetTPA(tpa2,Point(2,2));

      writeln(tpa1);
      writeln(tpa2);

      a := 2;
      b := a;
      b := 3;
      writeln(a);
      writeln(b);
    end.

    Code:
    [(3, 3), (4, 4)]
    [(3, 3), (4, 4)]
    2
    3
    As you see tpa2 := tpa1; doesn't copy tpa1 to tpa2 ,but copy tpa1's pointer...differently then with simple types. There is already function CopyTPA ,but it's leaking and don't have wrapper , so I won't use it ,because I want to do this operation very frequently. Only possible way I see is iteration ,but it will slow down my script.
    Iteration I mean:
    Simba Code:
    hi := high(tpa1);
    SetLength(tpa2,hi+1);
    for a:=0 to hi do
      tpa2[a] := tpa1[a];
    any ideas?
    Last edited by bg5; 05-13-2012 at 12:44 PM.

  2. #2
    Join Date
    Jan 2008
    Location
    C:\
    Posts
    1,483
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    I messed around with it for a bit, and this seems to be the only way I can find that works:

    Simba Code:
    program new;
    var
    tpa1 ,tpa2 : TPointArray;
    begin
      tpa1 := [Point(1,1), Point(2,2)];
      AppendTPA(tpa2, tpa1);
      OffsetTPA(tpa2, Point(2,2));

      writeln(tpa1);
      writeln(tpa2);
    end.

    it outputs:

    Code:
    [(1, 1), (2, 2)]
    [(3, 3), (4, 4)]
    E: there are obvious downfalls to this, but it looks like it should work as long as the second TPA is empty.
    Last edited by Runaway; 05-13-2012 at 12:59 PM.

  3. #3
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Simba Code:
    a := b;
    l := Length(a);
    SetLength(b, l + 1);
    SetLength(b, l);
    Hup Holland Hup!

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Result:= CopyTPA(TPA);
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Quote Originally Posted by Runaway View Post
    I messed around with it for a bit, and this seems to be the only way I can find that works:

    Simba Code:
    program new;
    var
    tpa1 ,tpa2 : TPointArray;
    begin
      tpa1 := [Point(1,1), Point(2,2)];
      AppendTPA(tpa2, tpa1);
      OffsetTPA(tpa2, Point(2,2));

      writeln(tpa1);
      writeln(tpa2);
    end.

    it outputs:

    Code:
    [(1, 1), (2, 2)]
    [(3, 3), (4, 4)]
    E: there are obvious downfalls to this, but it looks like it should work as long as the second TPA is empty.
    Thanks it's what I'm looking for. I will add tpa2:=[]; to make sure it's empty.

    Simba Code:
    a := b;
    l := Length(a);
    SetLength(b, l + 1);
    SetLength(b, l);

    I have no idea ,how it works ,but it's nice too

    Result:= CopyTPA(TPA);
    It's leaking.

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Simba Code:
    Function CopyTPAX(StartIndex, EndIndex: Integer; TPAToCopy: TPointArray): TPointArray;
    var
      I: Integer;
      TPA: TPointArray;
    begin
      SetLength(TPA, iAbs(EndIndex - StartIndex) + 1);
      For I:= StartIndex To EndIndex do
      begin
        TPA[I]:= TPAToCopy[I];
      end;
      Result:= TPA;
    end;

    var
      TPA, T: TPointArray;
    begin
      TPA:= [Point(1, 2), Point(3, 4), Point(4, 6)];

      T:= CopyTPAX(0, High(TPA), TPA);
      writeln(T);
    end.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Small fix

    Simba Code:
    Function CopyTPAX(StartIndex, EndIndex: Integer; TPAToCopy: TPointArray): TPointArray;
    var
      I: Integer;
      TPA: TPointArray;
    begin
      SetLength(TPA, iAbs(EndIndex - StartIndex) + 1);
      For I:= StartIndex To EndIndex do
      begin
        TPA[I-StartIndex]:= TPAToCopy[I];
      end;
      Result:= TPA;
    end;
    Working on: Tithe Farmer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •