Class Snapper

java.lang.Object
com.digitizer.core.Snapper

public final class Snapper extends Object
Utility that provides snapping behavior for X values.

The Snapper maintains a sorted list of configured X values and exposes a tolerance-based match for snapping input X coordinates to the nearest configured value. The tolerance is relative to the magnitude of the value and defaults to 1e-6. The snapPoint(com.digitizer.core.Point) method returns a new Point with the snapped X but preserved Y.

Complexity: Operations that modify the list (add/set) are O(n log n) due to sorting. A snap operation currently performs a linear search O(n); this is intentional for small lists (typical scientific datasets). For larger lists a binary search could be substituted without changing the public API.

  • Constructor Details

    • Snapper

      public Snapper()
  • Method Details

    • setSnapXValues

      public void setSnapXValues(List<Double> values)
      Sets the list of X values to snap to. The list is copied and kept sorted.
      Parameters:
      values - the X values to snap to
    • clearSnapXValues

      public void clearSnapXValues()
      Clears any configured snap X values.
    • addSnapXValue

      public void addSnapXValue(double x)
      Adds a single X value to the snap list. Keeps the internal list sorted.
      Parameters:
      x - the x value to add
    • getSnapXValues

      public List<Double> getSnapXValues()
      Returns an immutable copy of the configured snap X values.
      Returns:
      list of X values (sorted)
    • setTolerance

      public void setTolerance(double tolerance)
      Sets the relative tolerance used when matching an input x to a configured snap x. The match condition uses: |x - target| <= tolerance * max(1.0, |x|)
      Parameters:
      tolerance - relative tolerance (must be >= 0)
    • snapX

      public double snapX(double x)
      Snaps the provided x to a configured snap x if one is within tolerance. Snaps the provided x to the nearest configured snap x (simple rounding). If no snap values are configured the original x is returned.

      Complexity: O(n) over the number of configured snap values.

      Parameters:
      x - the input x coordinate
      Returns:
      the snapped x (or the original x if no match)
    • snapPoint

      public Point snapPoint(Point p)
      Returns a new Point with the x coordinate snapped (y unchanged).
      Parameters:
      p - the original point
      Returns:
      a new Point with snapped x