Results 1 to 10 of 10

Thread: Help large multidimensional arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    @phibermon
    While your proposed solution is perfectly acceptable for 2D owrlds it might not be verry practical for 3D worlds. Why? In 2D world there is no importance of which wixel representing some unit you draw first and which last. But in 3D world the order of drawn units is verry important so that units that are closer to the viewer arent actually blocked by units that are farther away but were rendered later.

    So better approach would be to use combination multiple two dimensional arrays (each array sotres dta for onw Z plane) and one one dimeansional array to store pointers to these two dimensional arrays and actually controll the Z order of the multiple planes that the two dimensional arrays represent.

    This is aproximately how I would do it. WARNIG the code is untested and uncomplete, but it should point out the basic design.
    Code:
    type
      TOrganism = class
        PositionX: integer;
        ColorR : byte;
        ColorG : byte;
        ColorB : byte;
      end;
    
      TWorldLine = class(TObject)
      private
        FYPos: Integer;
        FOrganisms: Array of TOrganism;
      protected
        function GetOrganism(XPos: Integer): TOrganism;
        procedure SetOrganism(XPos: Integer; AOrganism: TOrganism);
      public
        constructor Create(AYPos: Integer);
        procedure AddOrganism(AOrganism: TOrganism);
        procedure DeleteOrganism(XPos: Integer);
        property Organism[Index: Integer]: TOrganism read GetOrganism write SetOrganism;
      end;
    
      TWorldPlain = class(TObject)
      private
        FZPos: Integer;
        FWorldLines: Array of TWorldLine;
      protected
        function GetOrganism(XPos, YPOs: Integer): TOrganism;
        procedure SetOrganism(XPos, YPos: Integer; AOrganism: TOrganism);
      public
        constructor Create(AZPos: Integer);
        procedure AddOrganism(AOrganism: TOrganism; YPos: Integer);
        procedure DeleteOrganism(XPos, YPos: Integer);
        property Organism[XPos, YPos: Integer]: TOrganism read GetOrganism write SetOrganism;
      end;
    
      TWorld = class(TObject)
      private
        FWorldPlains: Array of TWorldPlain;
      protected
        function GetOrganism(XPos, YPos, ZPos: Integer): TOrganism;
        procedure SetOrganism(XPos, YPos, ZPos: Integer; AOrganism: TOrganism);
      public
        constructor Create;
        procedure AddOrganism(AOrganism: TOrganism; YPos, ZPos: Integer);
        procedure DeleteOrganism(XPos, YPos, ZPos: Integer);
        property Organism[XPos, YPos, ZPos: Integer]: TOrganism read GetOrganism write SetOrganism;
      end;
    
    implementation
    
    procedure TWorldLine.AddOrganism(AOrganism: TOrganism);
    begin
      //
    end;
    
    constructor TWorldLine.Create(AYPos: Integer);
    begin
      FYPos := AYPos;
    end;
    
    procedure TWorldLine.DeleteOrganism(XPos: Integer);
    begin
      //
    end;
    
    function TWorldLine.GetOrganism(XPos: Integer): TOrganism;
    var I: Integer;
    begin
      result := nil;
      //Iterate thorugh all stored organisms
      for I := Low(FOrganisms) to High(FOrganisms) do
      begin
        //Check to see if there is organizm with certain X position
        if FOrganisms[I].PositionX = XPos then
          result := FOrganisms[I];
      end;
    end;
    
    procedure TWorldLine.SetOrganism(XPos: Integer; AOrganism: TOrganism);
    begin
      //
    end;
    
    { TWorldPlain }
    
    procedure TWorldPlain.AddOrganism(AOrganism: TOrganism; YPos: Integer);
    begin
      //
    end;
    
    constructor TWorldPlain.Create(AZPos: Integer);
    begin
      FZPos := AZPos;
    end;
    
    procedure TWorldPlain.DeleteOrganism(XPos, YPos: Integer);
    begin
      //
    end;
    
    function TWorldPlain.GetOrganism(XPos, YPOs: Integer): TOrganism;
    var I: Integer;
    begin
      result := nil;
      for I := Low(FWorldLines) to High(FWorldLines) do
      begin
        //Check to see if Y world plane does exist
        if FWorldLines[I].FYPos = YPos then
        begin
          //Retrieve organizsm through organizms property
          result := FWorldLines[I].Organism[XPos];;
        end;
      end;
    end;
    
    procedure TWorldPlain.SetOrganism(XPos, YPos: Integer; AOrganism: TOrganism);
    begin
      //
    end;
    
    { TWorld }
    
    procedure TWorld.AddOrganism(AOrganism: TOrganism; YPos, ZPos: Integer);
    begin
      //
    end;
    
    constructor TWorld.Create;
    begin
      //
    end;
    
    procedure TWorld.DeleteOrganism(XPos, YPos, ZPos: Integer);
    begin
      //
    end;
    
    function TWorld.GetOrganism(XPos, YPos, ZPos: Integer): TOrganism;
    var I: Integer;
    begin
      result := nil;
      for I := Low(FWorldPlains) to High(FWorldPlains) do
      begin
        if FWorldPlains[I].FZPos = ZPos then
        begin
          result := FWorldPlains[I].Organism[XPos,YPos];
        end;
      end;
    end;
    
    procedure TWorld.SetOrganism(XPos, YPos, ZPos: Integer; AOrganism: TOrganism);
    begin
      //
    end;
    Main advantage of this design is that it actually alows dynamic memory alocation as needed.
    Last edited by SilverWarior; 18-01-2015 at 09:03 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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