Class Trajectory3D


public class Trajectory3D extends PolyLine3D
A dynamic 3D trajectory item that grows by appending points over time.

This class extends PolyLine3D by adding simple trajectory semantics: points can be appended one at a time, the trajectory can be cleared, and an optional maximum point count can be enforced.

Internally, the trajectory is stored as a flat coordinate array in the form:

 [x1, y1, z1, x2, y2, z2, ..., xn, yn, zn]
 
Consecutive points are connected by straight line segments when drawn.

If a maximum point count is specified and the trajectory exceeds that limit, the oldest point is discarded so that the most recent points remain visible. This implementation uses a simple array shift when discarding old points. That keeps the code straightforward and is usually adequate for demo-scale trajectories.

Allocation strategy

A single backing buffer is owned by this class and never exposed to PolyLine3D's drawing path as a trimmed copy. Instead, PolyLine3D.setCoords(float[], int) is used so the draw loop reads only the live slice of the buffer, eliminating the per-append allocation that the naive Arrays.copyOf approach would incur.

  • Constructor Details

    • Trajectory3D

      public Trajectory3D(Panel3D panel3D, Color color, float lineWidth)
      Creates an unbounded trajectory with a default initial capacity.
      Parameters:
      panel3D - the owning 3D panel
      color - the trajectory color
      lineWidth - the trajectory line width
    • Trajectory3D

      public Trajectory3D(Panel3D panel3D, Color color, float lineWidth, int maxPoints)
      Creates a trajectory with an optional maximum number of points.
      Parameters:
      panel3D - the owning 3D panel
      color - the trajectory color
      lineWidth - the trajectory line width
      maxPoints - the maximum number of points to retain; values less than or equal to zero mean no explicit maximum
    • Trajectory3D

      public Trajectory3D(Panel3D panel3D, Color color, float lineWidth, int maxPoints, int initialCapacity)
      Creates a trajectory with full control over maximum point count and initial storage capacity.
      Parameters:
      panel3D - the owning 3D panel
      color - the trajectory color
      lineWidth - the trajectory line width
      maxPoints - the maximum number of points to retain; values less than or equal to zero mean no explicit maximum
      initialCapacity - the initial storage capacity in points; values less than 2 are promoted to 2
  • Method Details

    • append

      public void append(float x, float y, float z)
      Appends a new point to the end of the trajectory.

      If the trajectory has a fixed maximum size and is already full, the oldest point is discarded before the new point is appended. No allocation occurs unless the backing buffer needs to grow.

      Parameters:
      x - the x coordinate of the new point
      y - the y coordinate of the new point
      z - the z coordinate of the new point
    • append

      public void append(float[] xyz)
      Appends a new point to the end of the trajectory.
      Parameters:
      xyz - the point coordinates as [x, y, z]
      Throws:
      IllegalArgumentException - if the array is null or has length less than 3
    • clear

      public void clear()
      Removes all points from the trajectory.

      After calling this method, nothing will be drawn until at least two points have been appended. The backing buffer is retained so that a subsequent fill does not re-allocate.

    • getPointCount

      public int getPointCount()
      Gets the current number of points in the trajectory.
      Returns:
      the number of stored points
    • getMaxPoints

      public int getMaxPoints()
      Gets the maximum allowed number of points.
      Returns:
      the maximum number of points, or a value less than or equal to zero if the trajectory is unbounded
    • isEmpty

      public boolean isEmpty()
      Checks whether the trajectory is empty.
      Returns:
      true if no points are stored, false otherwise