Skip to content
Back to top
View this page

# Variables and Position

The most basic class for a solvable value is `Variable`. It acts a lot like a `float`, which makes it easy to work with.

Next to that there’s Position, which is a coordinate `(x, y)` defined by two variables.

To support connections between variables, a `MatrixProjection` class is available. It translates a position to a common coordinate space, based on `Item.matrix_i2c`. Normally, it’s only `Ports` that deal with item-to-common translation of positions.

class gaphas.solver.Variable(value: SupportsFloat = 0.0, strength: int = 20)

  Representation of a variable in the constraint solver.

  Each Variable has a `value` and a `strength`. In a constraint the weakest variables are changed.

  You can even do some calculating with it. The Variable always represents a float variable.

  The `variable` decorator can be used to easily define variables in classes.

  add_handler(handler: Callable[[Variable, float], None]) → None

    Add a handler, to be invoked when the value changes.

  remove_handler(handler: Callable[[Variable, float], None]) → None

    Remove a handler.

  notify(old: float) → None

    Notify all handlers.

  property strength: int

    Strength.

  dirty() → None

    Mark the variable dirty in all attached constraints.

    Variables are marked dirty also during constraints solving to solve all dependent constraints, i.e. two equals constraints between 3 variables.

Variables can have different strengths. The higher the number, the stronger the variable. Variables can be `VERY_WEAK` (0), up to `REQUIRED` (100). Other constants are `WEAK` (10) `NORMAL` (20) `STRONG` (30), and `VERY_STRONG` (40).

gaphas.solver.variable(strength=20, varname=None)

  Easy-to-use drop Variable descriptor.

      >>> class A(object):
      ...     x = variable(varname='_v_x')
      ...     y = variable(STRONG)
      ...     def __init__(self):
      ...         self.x = 12
      >>> a = A()
      >>> a.x
      Variable(12, 20)
      >>> a._v_x
      Variable(12, 20)
      >>> a.x = 3
      >>> a.x
      Variable(3, 20)
      >>> a.y
      Variable(0, 30)

class gaphas.position.Position(x, y, strength=20)

  A point constructed of two Variable’s.

      >>> vp = Position(3, 5)
      >>> vp.x, vp.y
      (Variable(3, 20), Variable(5, 20))
      >>> vp.pos
      (Variable(3, 20), Variable(5, 20))
      >>> vp[0], vp[1]
      (Variable(3, 20), Variable(5, 20))

class gaphas.position.MatrixProjection(pos: Position, matrix: Matrix)