3 Dimensional Object Field lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2021-07-16 07:22 Hello Guys, Sorry for the Title didn't know how to name this issue. I do not know how to do this (help pls): I Have a class TElement wich Describes 1 Object. Then i have an Array Field : Array of Array of Array of TElement. now there is a Field height x width x length. Each field has Coordinates (hx, wy, lz). Now every Element gets connected to each other (Something like a snake wich cant be split for Example Snake the game). but when i move 1 Element from this coords to the next how do i keep everything together and Connected ? If i change Coords of 1 Element i still have the others connected as PrevElement and NextElement but the coordinates of those wont be correct anymore. Can some1 give me general advice about behaviour of 3d fields ? I'm missing the Basics like Move, Remove, Insert. The "Snake" could also have a "U" Shape or a "L" Shape. I did this once with Java Lists but can remember how i did it. Here is my Class "TElement": Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  TDirection = (dirLeft, dirRight, dirUp, dirDown, dirForward, dirBackward, dirNone, dirRolled, dirSwapped);    RDrw = record      X: Double;    Y: Double;      Width: Integer;      Height: Integer;      XScle: Double;      YScle: Double;      ScaleWidth: Double;      ScaleLeft: Double;      ScaleHeight: Double;      ScaleTop: Double;      XDpI: Double;      YDpI: Double;      WkzWidth: Double;   end;    CoordRet = Record      X: Double;      Y: Double;   end;    IntCoordRet = Record      X: Integer;      Y: Integer;   end;    VecPoint = Record      X: Double;      Y: Double;      Z: Double;   end;    IntPoint = Record    X: Integer;      Y: Integer;      Z: Integer;   end;    TAxis = (aX, aY, aZ, aNone);    TDrw = class(TObject)      private         WideX, WideY: Integer;         ScX, ScY, ScLeft, ScTop: Double;      protected         Function CalcX(pX: Integer): Integer;      public          X: function(pX: Integer): Integer;         Y, AcX, AcY: Integer;         constructor create(xPos, yPos, Breit, Hoch: Integer; ScaleX, ScaleY, LeftX, TopY: Double);   end;    { TElement }    TElement = class(TObject)      private         fBtlNum: Integer;       fBtlArtNum: Integer;         fZLAL: Integer;         fMoved: TDirection;         fnextBtl: TElement;         fprevBtl: TElement;       fCoords: IntPoint;         fData: Integer;      public         property BtlNum: Integer read fBtlNum write fBtlNum;         property BtlArtNum: Integer read fBtlArtNum write fBtlArtNum;         //Luftstrang: 0 = Zuluft ; 1 = Abluft         property ZLAL: Integer read fZLAL write fZLAL;         property Moved: TDirection read fMoved write fMoved;         property nextBtl: TElement read fnextBtl write fnextBtl;         property prevBtl: TElement read fprevBtl write fprevBtl;         property Data: Integer read fData write fData;       property Coords: IntPoint read fCoords write fCoords;         constructor Create();         destructor Destroy(); override;         procedure Remove();         procedure CopyTo(Element: TElement);       procedure SwapWith(Element: TElement);         procedure RemoveNextConn();         procedure RemovePrevConn();         procedure RemoveConnections();         function HasNextBtl(): Boolean;         function HasPrevBtl(): Boolean;         procedure MoveConnection(Dir: TDirection); overload;         procedure MoveConnection(Dir: IntPoint); overload;         function IsOnNextBtl(BtlPos: IntPoint): Boolean;         function IsOnPrevBtl(BtlPos: IntPoint): Boolean;   end;    function CoordToIntPoint(x, y, z: Integer): IntPoint;   function CompareIntPoints(p1, p2: IntPoint): Boolean;   function AddIntPotrunc(Point1, Point2: IntPoint): IntPoint;   function MirrorIntPointZAxis(Point: IntPoint; XShift: Integer): IntPoint;   function DetectAxis(Point1, Point2: IntPoint): TAxis;   function DetectDirection(Point1, Point2: IntPoint; RestrictOnAxis: TAxis): TDirection; implementation function CoordToIntPoint(x, y, z: Integer): IntPoint;begin   Result.X := x;   Result.Y := y;   Result.Z := z;end; {vergleicht zwei übergebene IntPoints (p1, p2) auf inhaltliche Gleichheitgibt True zurück, wenn p1 inhaltlich gleich p2, andernfalls False}function CompareIntPoints(p1, p2: IntPoint): Boolean;begin   if (p1.X = p2.X) and (p1.Y = p2.Y) and (p1.Z = p2.Z) then begin      Result := True   end   else begin      Result := False;   end;end; function AddIntPotrunc(Point1, Point2: IntPoint): IntPoint;begin   Result.X := Point1.X + Point2.X;   Result.Y := Point1.Y + Point2.Z;   Result.Z := Point1.Y + Point2.Z;end; function MirrorIntPointZAxis(Point: IntPoint; XShift: Integer): IntPoint;begin   Result.X := 2*XShift - Point.X;   Result.Y := Point.Y;   Result.Z := Point.Z;end; {überpurüft, ob Gerade durch übergebene Punkte parallel zur x-, y, oder z-Achse verläuftwenn ja, gibt entsprechende Axe zurück, andernfalls nil}function DetectAxis(Point1, Point2: IntPoint): TAxis;begin  if not CompareIntPoints(Point1, Point2) and (Point1.X = Point2.X) and (Point1.Y = Point2.Y) then  begin    Result := TAxis.aZ;  end  else if not CompareIntPoints(Point1, Point2) and (Point1.X = Point2.X) and (Point1.Z = Point2.Z) then  begin    Result := TAxis.aY  end  else if not CompareIntPoints(Point1, Point2) and (Point1.Y = Point2.Y) and (Point1.Z = Point2.Z) then  begin    Result := TAxis.aX;  end  else    Result := TAxis.aNone;end; {ermittelt die Richtung von Point1 bezogen auf Point2RestrictOnAxis = TAxis.aNone   der Vergleich betrifft alle 3 Dimensionen der Punktkoordinaten, wobei NICHT 'none' zurückgegeben wird,   wenn sich die Punkte in nur einer Dimension unterscheiden   Beispiel: DetectDirection(Point1, Point2, TAxis.aNone) = 'left' wenn (Point1.X < Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z = Point2.Z)RestrictOnAxis <> TAxis.aNone   der Vergleich betrifft nur die übergebene Dimension   Beispiel: DetectDirection(Point1, Point2, TAxis.aX) = 'left' wenn (Point1.X < Point2.X)}function DetectDirection(Point1, Point2: IntPoint; RestrictOnAxis: TAxis): TDirection;begin   if RestrictOnAxis = TAxis.aNone then begin      if (Point1.X = Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z = Point2.Z) then begin         Result := dirNone;      end      else if (Point1.X < Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z = Point2.Z) then begin         Result := dirRight;      end      else if (Point1.X > Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z = Point2.Z) then begin         Result := dirLeft;      end      else if (Point1.X = Point2.X) and (Point1.Y < Point2.Y) and (Point1.Z = Point2.Z) then begin         Result := dirForward;      end      else if (Point1.X = Point2.X) and (Point1.Y > Point2.Y) and (Point1.Z = Point2.Z) then begin         Result := dirBackward;      end      else if (Point1.X = Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z < Point2.Z) then begin         Result := dirUp;      end      else if (Point1.X = Point2.X) and (Point1.Y = Point2.Y) and (Point1.Z > Point2.Z) then begin         Result := dirDown;      end      else begin         Result := dirNone;      end;   end   else if RestrictOnAxis = TAxis.aX then begin      if Point1.X < Point2.X then begin         Result := dirLeft;      end    else if Point1.X > Point2.X then begin         Result := dirRight;    end      else begin         Result := dirNone;      end;   end   else if RestrictOnAxis = TAxis.aY then begin      if Point1.Y < Point2.Y then begin         Result := dirBackward;      end      else if Point1.Y > Point2.Y then begin         Result := dirForward;      end      else begin         Result := dirNone;      end;   end   else if RestrictOnAxis = TAxis.aZ then begin      if Point1.Z < Point2.Z then begin         Result := dirDown;      end      else if Point1.Z > Point2.Z then begin         Result := dirUp;      end      else begin         Result := dirNone;      end;   end;end; constructor TDrw.create(xPos, yPos, Breit, Hoch: Integer; ScaleX, ScaleY, LeftX, TopY: Double);Begin   inherited create;   AcX := xPos;   AcY := yPos;   WideX := Breit;   WideY := Hoch;   ScX := ScaleX;   ScY := ScaleY;   ScLeft := LeftX;   ScTop := TopY;end; function TDrw.CalcX(pX: Integer): Integer;   Var   PrcX: Double;  begin   PrcX := AcX / WideX;   Result := Trunc(WideX * PrcX - ScLeft);end; {procedure TDrw.scaledPos(Var pX, pY: Integer); Var   PrcX, PrcY: Double;Begin   PrcX := AcX / WideX;   PrcY := AcY / WideY;   X := Trunc(WideX * PrcX - ScLeft);end;} constructor TElement.Create();begin   inherited Create;   fBtlNum := 0;   fBtlArtNum := 0;   fZLAL := 0;   fMoved := dirNone;   fData := 0;   RemoveConnections();end; destructor TElement.Destroy();begin   inherited Destroy;end; procedure TElement.Remove();begin   // Bauteil löschen   fBtlNum := 0;   fBtlArtNum := 0;   fZLAL := 0;   fMoved := dirNone;   fData := 0;   RemoveConnections();end; procedure TElement.CopyTo(Element: TElement);begin   // Bauteil nach angegebene Position kopieren   Element.BtlNum := fBtlNum;   Element.BtlArtNum := fBtlArtNum;   Element.ZLAL := fZLAL;   Element.Moved := fMoved;   Element.prevBtl := fprevBtl;   Element.nextBtl := fnextBtl;   Element.Data := fData;end; procedure TElement.SwapWith(Element: TElement);var   tmpBtl: TElement;begin   // Bauteil mit übergebenen Bauteil tauschen   tmpBtl := TElement.Create();   try     self.CopyTo(tmpBtl);     Element.CopyTo(self);     tmpBtl.CopyTo(Element);   finally     tmpBtl.Free;   end;end; procedure TElement.RemoveNextConn();begin   // Verbindung zu nachfolgendem Bauteil löschen   fnextBtl := NIl;end; procedure TElement.RemovePrevConn();begin   // Verbindung zu vorherigem Bauteil löschen   fprevBtl := Nil;end; procedure TElement.RemoveConnections();begin   // alle Verbindungen löschen   RemoveNextConn();   RemovePrevConn();end; function TElement.HasNextBtl(): Boolean;begin  // besteht eine Verbindung zu einem nachfolgendem Bauteil   Result := False;   if (fnextBtl <> Nil) then      Result := True;end; function TElement.HasPrevBtl(): Boolean;begin  // besteht eine Verbindung zu einem vorherigem Bauteil?   Result := False;   if (fprevBtl <> Nil) then      Result := True;end; procedure TElement.MoveConnection(Dir: TDirection); overload;begin   // Verbindungen um angegebene Beträge verschieben   if HasPrevBtl() then begin      case Dir of         dirRight:         fprevBtl.fCoords.X := fprevBtl.fCoords.X + 1;         dirLeft:         fprevBtl.fCoords.X := fprevBtl.fCoords.X - 1;         dirForward:           fprevBtl.fCoords.Y := fprevBtl.fCoords.Y + 1;         dirBackward:           fprevBtl.fCoords.Y := fprevBtl.fCoords.Y - 1;         dirUp:           fprevBtl.fCoords.Z := fprevBtl.fCoords.Z + 1;         dirDown:           fprevBtl.fCoords.Z := fprevBtl.fCoords.Z - 1;      end;   end;   if HasNextBtl() then begin      case Dir of       dirRight:           fnextBtl.fCoords.X := fnextBtl.fCoords.X + 1;       dirLeft:           fnextBtl.fCoords.X := fnextBtl.fCoords.X - 1;         dirForward:           fnextBtl.fCoords.Y := fnextBtl.fCoords.Y + 1;         dirBackward:           fnextBtl.fCoords.Y := fnextBtl.fCoords.Y - 1;         dirUp:         fnextBtl.fCoords.Z := fnextBtl.fCoords.Z + 1;         dirDown:         fnextBtl.fCoords.Z := fnextBtl.fCoords.Z - 1;      end;   end;end; procedure TElement.MoveConnection(Dir: IntPoint); overload;begin   // Verbindungen um angegebene Beträge verschieben   if HasPrevBtl() then begin      fprevBtl.fCoords.X := fprevBtl.fCoords.X + Dir.X;      fprevBtl.fCoords.Y := fprevBtl.fCoords.Y + Dir.Y;      fprevBtl.fCoords.Z := fprevBtl.fCoords.Z + Dir.Z;   end;   if HasNextBtl() then begin      fnextBtl.fCoords.X := fnextBtl.fCoords.X + Dir.X;      fnextBtl.fCoords.Y := fnextBtl.fCoords.Y + Dir.Y;      fnextBtl.fCoords.Z := fnextBtl.fCoords.Z + Dir.Z;   end;end; function TElement.IsOnNextBtl(BtlPos: IntPoint): Boolean;begin  // liegt nachfolgendes Bauteil an diesem Punkt?   Result := False;   if (fnextBtl.fCoords.X = BtlPos.X) and (fnextBtl.fCoords.Y = BtlPos.Y) and (fnextBtl.fCoords.Z = BtlPos.Z) then      Result := True;end; function TElement.IsOnPrevBtl(BtlPos: IntPoint): Boolean;begin  //liegt vorheriges Bauteil an diesem Punkt?  Result := False;  if (fprevBtl.fCoords.X = BtlPos.X) and (fprevBtl.fCoords.Y = BtlPos.Y) and (fprevBtl.fCoords.Z = BtlPos.Z) then      Result := True;end;   Thanks for any Replay ---------------------------------------------------------------------- Handoko · 2021-07-16 09:56 Quote from: Weitentaaal on July 16, 2021, 09:22:01 amNow every Element gets connected to each other (Something like a snake wich cant be split for Example Snake the game). but when i move 1 Element from this coords to the next how do i keep everything together and Connected ? If i change Coords of 1 Element i still have the others connected as PrevElement and NextElement but the coordinates of those wont be correct anymore. If you're serious want to go deep into the technique, you should learn Single Link List and/or Double Link List. I have done it, there are boring. But it's worth your time, you will be able to solve many programming problems. https://en.wikipedia.org/wiki/Linked_list Or you can use ready made containers like TList or TFPList to store the link/connection information. https://wiki.lazarus.freepascal.org/Data_Structures,_Containers,_Colle ctions Are you writing a snake game? This is a detailed explanation about snake game, maybe this can help you: https://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.h tml#msg258381 ---------------------------------------------------------------------- Weitentaaal · 2021-07-19 07:53 Hey thanks No im not writing a Game. https://en.wikipedia.org/wiki/Linked_list this was what i was looking for thanks