Class ViewTransform

java.lang.Object
it.univr.di.cstnu.gui.ViewTransform

public final class ViewTransform extends Object
Affine pan+zoom transform between model and view coordinates.

Scale is limited to [0.01, 50.0]. No rotation or shear is supported: the transform only scales and translates.

Author:
posenato
  • Field Details

    • DEFAULT_SCALE

      public static final double DEFAULT_SCALE
      Default scale (1:1).
      See Also:
    • MAX_SCALE

      public static final double MAX_SCALE
      Maximum allowed scale factor.
      See Also:
    • MIN_SCALE

      public static final double MIN_SCALE
      Minimum allowed scale factor.
      See Also:
  • Constructor Details

    • ViewTransform

      public ViewTransform()
      Creates a new transform initialized to identity (scale 1, no translation).
  • Method Details

    • fitTo

      public void fitTo(@Nonnull Rectangle2D modelBounds, @Nonnull Dimension viewSize, double marginPx)
      Fit modelBounds into viewSize with marginPx margin on each side.
      Parameters:
      modelBounds - the bounding box in model space to fit, not null
      viewSize - the size of the view in pixels, not null
      marginPx - margin, in pixels, to leave on each side
    • getScale

      public double getScale()
      Returns:
      the current scale factor
    • panBy

      public void panBy(double viewDx, double viewDy)
      Pan by (viewDx, viewDy) in view space.
      Parameters:
      viewDx - horizontal displacement in view coordinates
      viewDy - vertical displacement in view coordinates
    • reset

      public void reset()
      Reset to identity (scale=1, no translation).
    • toAffineTransform

      @Nonnull public AffineTransform toAffineTransform()
      Returns:
      an AffineTransform equivalent to this transform
    • toModel

      @Nonnull public Point2D toModel(@Nonnull Point2D viewPoint)
      Convert view coordinates to model coordinates.
      Parameters:
      viewPoint - a point in view space, not null
      Returns:
      the corresponding point in model space
    • toView

      @Nonnull public Point2D toView(@Nonnull Point2D modelPoint)
      Convert model coordinates to view coordinates.
      Parameters:
      modelPoint - a point in model space, not null
      Returns:
      the corresponding point in view space
    • zoomBy

      public void zoomBy(double factor, @Nonnull Point2D pivotInView)
      Zoom by factor keeping pivotInView stationary. The model point that was under pivotInView before the call stays under pivotInView after the call.
      Parameters:
      factor - the multiplicative zoom factor (> 1 zooms in, < 1 zooms out)
      pivotInView - the pivot point in view coordinates, not null