Ejemplo n.º 1
0
def test_delta_below_zero():
    """Test line align constraint with delta below zero.
    """
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=-5)
    lc.solve_for()
    assert round(abs(10.84 - point[0].value), 2) == 0
    assert round(abs(7.23 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(16.0 - point[0].value), 2) == 0
    assert round(abs(12.00 - point[1].value), 2) == 0
Ejemplo n.º 2
0
def test_delta():
    """Test line align constraint delta.
    """
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=5)
    lc.solve_for()
    assert round(abs(19.16 - point[0].value), 2) == 0
    assert round(abs(12.77 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(24.00 - point[0].value), 2) == 0
    assert round(abs(18.00 - point[1].value), 2) == 0
Ejemplo n.º 3
0
    def constraint(self, canvas, item, handle, glue_item):
        """Create connection line constraint between item's handle and the
        port."""
        line = canvas.project(glue_item, self.start, self.end)
        point = canvas.project(item, handle.pos)

        x, y = canvas.get_matrix_i2c(item).transform_point(*handle.pos)
        x, y = canvas.get_matrix_c2i(glue_item).transform_point(x, y)

        # keep message at the same distance from head or bottom of lifetime
        # line depending on situation
        height = self.end.y - self.start.y
        if y / height < 0.5:
            delta = y - self.start.y
            align = 0
        else:
            delta = y - self.end.y
            align = 1
        return LineAlignConstraint(line, point, align, delta)
Ejemplo n.º 4
0
def test_delta_below_zero():
    """Test line align constraint with delta below zero."""
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=-5)
    lc.solve_for()
    assert round(abs(10.84 - point[0].value), 2) == 0
    assert round(abs(7.23 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(16.0 - point[0].value), 2) == 0
    assert round(abs(12.00 - point[1].value), 2) == 0
Ejemplo n.º 5
0
def test_delta():
    """Test line align constraint delta."""
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=5)
    lc.solve_for()
    assert round(abs(19.16 - point[0].value), 2) == 0
    assert round(abs(12.77 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(24.00 - point[0].value), 2) == 0
    assert round(abs(18.00 - point[1].value), 2) == 0
Ejemplo n.º 6
0
    def test_delta_below_zero(self):
        """Test line align with delta below zero
        """
        line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
        point = (Variable(15), Variable(10))
        lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=-5)
        lc.solve_for()
        self.assertAlmostEqual(10.84, point[0].value, 2)
        self.assertAlmostEqual(7.23, point[1].value, 2)

        line[1][0].value = 40
        line[1][1].value =  30
        lc.solve_for()
        self.assertAlmostEqual(16.0, point[0].value, 2)
        self.assertAlmostEqual(12.00, point[1].value, 2)
Ejemplo n.º 7
0
    def test_delta(self):
        """Test line align delta
        """
        line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
        point = (Variable(15), Variable(10))
        lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=5)
        lc.solve_for()
        self.assertAlmostEqual(19.16, point[0].value, 2)
        self.assertAlmostEqual(12.77, point[1].value, 2)

        line[1][0].value = 40
        line[1][1].value =  30
        lc.solve_for()
        self.assertAlmostEqual(24.00, point[0].value, 2)
        self.assertAlmostEqual(18.00, point[1].value, 2)
Ejemplo n.º 8
0
    def constraint(self, item, handle, glue_item):
        """Create connection line constraint between item's handle and the
        port."""
        start = MatrixProjection(self.start, glue_item.matrix_i2c)
        end = MatrixProjection(self.end, glue_item.matrix_i2c)
        point = MatrixProjection(handle.pos, item.matrix_i2c)

        x, y = item.matrix_i2c.transform_point(*handle.pos)
        x, y = glue_item.matrix_i2c.inverse().transform_point(x, y)

        # keep message at the same distance from head or bottom of lifetime
        # line depending on situation
        height = self.end.y - self.start.y
        if y / height < 0.5:
            delta = y - self.start.y
            align = 0
        else:
            delta = y - self.end.y
            align = 1
        line = LineAlignConstraint((start, end), point, align, delta)
        return MultiConstraint(start, end, point, line)
Ejemplo n.º 9
0
    def constraint(
        self,
        horizontal=None,
        vertical=None,
        left_of=None,
        above=None,
        line=None,
        delta=0.0,
        align=None,
    ):
        """
        Utility (factory) method to create item's internal constraint
        between two positions or between a position and a line.

        Position is a tuple of coordinates, i.e. ``(2, 4)``.

        Line is a tuple of positions, i.e. ``((2, 3), (4, 2))``.

        This method shall not be used to create constraints between
        two different items.

        Created constraint is returned.

        :Parameters:
         horizontal=(p1, p2)
            Keep positions ``p1`` and ``p2`` aligned horizontally.
         vertical=(p1, p2)
            Keep positions ``p1`` and ``p2`` aligned vertically.
         left_of=(p1, p2)
            Keep position ``p1`` on the left side of position ``p2``.
         above=(p1, p2)
            Keep position ``p1`` above position ``p2``.
         line=(p, l)
            Keep position ``p`` on line ``l``.
        """
        cc = None  # created constraint
        if horizontal:
            p1, p2 = horizontal
            cc = EqualsConstraint(p1[1], p2[1], delta)
        elif vertical:
            p1, p2 = vertical
            cc = EqualsConstraint(p1[0], p2[0], delta)
        elif left_of:
            p1, p2 = left_of
            cc = LessThanConstraint(p1[0], p2[0], delta)
        elif above:
            p1, p2 = above
            cc = LessThanConstraint(p1[1], p2[1], delta)
        elif line:
            pos, l = line
            if align is None:
                cc = LineConstraint(line=l, point=pos)
            else:
                cc = LineAlignConstraint(line=l,
                                         point=pos,
                                         align=align,
                                         delta=delta)
        else:
            raise ValueError("Constraint incorrectly specified")
        assert cc is not None
        self._constraints.append(cc)
        return cc