ModelToViewCoordinates

public struct ModelToViewCoordinates

A structure to help us convert between two coordinate systems. Points in Model Coordinates are zoomed and shifted to produce points in View Coordinates.

The abstract state for a conversion is:

  • zoomScale: the scale facter when going from model to view.
  • viewOffset: where the original in model coordinates appears in view coordinates.

Abstraction Invariant: zoomScale > 0

  • Effects: Creates a new ModelToViewCoordinates object for the unit transform (zoomScale == 1, viewOffset == (0,0)).

    Declaration

    Swift

    public init()
  • Create a transformation that maps the points inside the modelBounds rectangle to the greatest possible area inside of the viewBounds rectangle. Examples:

    let b1 = CGRect(x: 0, y: 0, width: 400, height: 200)
    let b2 = CGRect(x: 0, y: 0, width: 200, height: 100)
    
    ModelToViewCoordinates(modelBounds: b1, viewBounds: b2)
    is the same as
    ModelToViewCoordinates(zoomScale: 0.5, viewOffset: CGPoint.zero)
    
    let b3 = CGRect(x: 0, y: 0, width: 200, height: 50)
    
    ModelToViewCoordinates(modelBounds: b1, viewBounds: b3)
    is the same as
    ModelToViewCoordinates(zoomScale: 0.25, viewOffset: CGPoint(x: 50, y: 0))
    

    Requires: modelBounds and viewBounds have width > 0 and height > 0.

    Declaration

    Swift

    public init(modelBounds: CGRect, viewBounds: CGRect)

    Parameters

    modelBounds

    A rectangle in model coordinates.

    viewBounds

    The rectangle in view coordinates that we wish to include all of the points in modelBounds.

  • TODO: Add specification

    Declaration

    Swift

    public func toView(modelPoint: CGPoint) -> CGPoint

    Parameters

    modelPoint

    The location to convert, in model coordinates.

    Return Value

    the modelPoint translated into view coordinates.

  • TODO: Add specification

    Declaration

    Swift

    public func fromView(viewPoint: CGPoint) -> CGPoint

    Parameters

    viewPoint

    The location to convert, in view coordinates.

    Return Value

    the viewPoint translated into model coordinates.

  • TODO: Add specification

    Declaration

    Swift

    public func scale(by amount: CGFloat) -> ModelToViewCoordinates

    Parameters

    by

    How much to change the zoomScale

    Return Value

    a new ModelToViewCoordinates with the same viewOffset but a scale of self.zoomScale * amount

  • TODO: Add specification

    Declaration

    Swift

    public func shift(by amount: CGPoint) -> ModelToViewCoordinates

    Parameters

    by

    How much to change the viewOffset

    Return Value

    a new ModelToViewCoordinates with the same zoomScale but an offset of (viewOffset.x + amount.x, viewOffset.y + amount.y)