Exemplo n.º 1
0
    def test_intersect(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)
        r = Rectangle(a, b, c, d)
        s = Segment(a, c)
        assert r.intersect(s) == [a, c]

        foo = Rectangle(
            Point(148.06094049635456, 10.151151779987144, 60.522099063951394),
            Point(129.78569335065157, -42.129870038015355, 60.54878245579997),
            Point(85.91668756014471, -26.79517716452499, 60.41723371984577),
            Point(104.19193470584759, 25.485844653477507, 60.390550327997126),
        )
        bar = Segment(
            Point(-38.9592826559563, -6.703132040294841, 64.78693707404751),
            Point(133.01711836447913, -6.633886165038485, 54.310634812542006),
        )

        assert len(foo.intersect(bar)) == 0

        p1 = Point(0, 0)
        p2 = Point(100, 0)
        p3 = Point(100, 100)
        p4 = Point(0, 100)
        square = Polygon(p1, p2, p3, p4)
        line1 = Line(p3, p4)
        line2 = Line(Point(100, 100 - 1e-8), Point(0, 100 + 1e-8))

        assert square.intersect(line1) == [p3, p4]
        assert len(square.intersect(line2)) == 2
Exemplo n.º 2
0
 def test_edges(self):
     a = Point(0, 0)
     b = Point(0, 2)
     c = Point(2, 2)
     d = Point(2, 0)
     r = Rectangle(a, b, c, d)
     assert r.edges == [Segment(a, b), Segment(b, c), Segment(c, d), Segment(d, a)]
Exemplo n.º 3
0
    def test_transformation(self):
        p = Point(0, 0)
        q = Point(2, 2)
        s = Segment(p, q)

        r = rotation(np.pi / 2)
        assert r * s == Segment(p, Point(-2, 2))
        assert r.apply(s)._line == r.apply(s._line)
Exemplo n.º 4
0
    def test_intersect(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)
        s1 = Segment(a, c)
        s2 = Segment(b, d)

        assert s1.intersect(s2) == [Point(1, 1)]
Exemplo n.º 5
0
    def test_equal(self):
        p = Point(0, 0)
        q = Point(2, 1)
        s = Segment(p, q)

        assert s == Segment(q, p)
        assert s == s
        assert s == Segment([(0, 0), (2, 1)], homogenize=True)
        assert s != Segment([(0, 0), (1, 2)], homogenize=True)
Exemplo n.º 6
0
    def test_midpoint(self):
        p = Point(0, 0)
        q = Point(2, 2)
        s = Segment(p, q)
        assert s.midpoint == Point(1, 1)

        p = Point(0, 0, 0)
        q = Point(0, 2, 0)
        s = Segment(p, q)
        assert s.midpoint == Point(0, 1, 0)
Exemplo n.º 7
0
    def test_getitem(self):
        p = Point(0, 0)
        q = Point(2, 2)
        s = Segment(p, q)

        assert s[0] == p
        assert s[1] == q
Exemplo n.º 8
0
 def test_intersect(self):
     a = Point(0, 0)
     b = Point(0, 2)
     c = Point(2, 2)
     d = Point(2, 0)
     r = Rectangle(a, b, c, d)
     s = Segment(a, c)
     assert r.intersect(s) == [a, c]
Exemplo n.º 9
0
    def test_contains(self):
        # both points finite
        p = Point(0, 0)
        q = Point(2, 1)
        s = Segment(p, q)
        assert s.contains(p)
        assert s.contains(q)
        assert s.contains(0.5 * (p + q))
        assert not s.contains(p - q)
        assert not s.contains(Point([2, 1, 0]))

        # first point at infinity
        p = Point([-2, -1, 0])
        q = Point(2, 1)
        s = Segment(p, q)
        assert s.contains(p)
        assert s.contains(q)
        assert s.contains(0.5 * q)
        assert not s.contains(2 * q)

        # second point at infinity
        p = Point(0, 0)
        q = Point([2, 1, 0])
        s = Segment(p, q)
        assert s.contains(p)
        assert s.contains(q)
        assert s.contains(Point(2, 1))
        assert not s.contains(Point(-2, -1))

        # both points  at infinity
        p = Point([-2, 1, 0])
        q = Point([2, 1, 0])
        s = Segment(p, q)
        assert s.contains(p)
        assert s.contains(q)
        assert s.contains(0.5 * (p + q))
        assert not s.contains(p - q)
        assert not s.contains(Point(0, 0))
Exemplo n.º 10
0
def test_dist():
    p = Point(0, 0)
    q = Point(1, 0)
    assert np.isclose(dist(p, q), 1)

    p = Point(1000000, 0)
    q = Point(1000001, 0)
    assert np.isclose(dist(p, q), 1)

    p1 = Point(1j, 0, 0, 2j)
    p2 = Point(0, 2j, 0, 0)
    assert np.isclose(dist(p1, p2), 3)

    p1 = Point(1, 0, 0)
    p2 = Point([1, 0, 0, 0])
    assert dist(p1, p2) == dist(p2, p1) == np.inf

    p1 = Point(0, 0, 0)
    p2 = Point(1, 0, 0)
    assert np.isclose(dist(p1, p2), 1)

    e = Plane(1, 0, 0, 0)
    assert np.isclose(dist(e, p2), 1)

    p = Point(1, 2, 0)
    l = Line(Point(0, 0, 0), Point(3, 0, 0))
    assert np.isclose(dist(p, l), 2)

    l = Line(p2, Point(1, 1, 0))
    assert np.isclose(dist(l, e), 1)
    assert np.isclose(dist(l, p1), 1)

    p = Point(0, 0)
    poly = Rectangle(Point(-1, 1), Point(1, 1), Point(1, 2), Point(-1, 2))
    assert np.isclose(dist(p, poly), 1)

    p = Point(0, 0, 0)
    poly = Rectangle(Point(-1, -1, 1), Point(1, -1, 1), Point(1, 1, 1),
                     Point(-1, 1, 1))
    assert np.isclose(dist(p, poly), 1)
    assert np.isclose(dist(Point(-1, -1, 0), poly), 1)
    assert np.isclose(dist(Point(-4, 0, -3), poly), 5)

    a = Point(0, 0, 0)
    b = Point(1, 0, 0)
    c = Point(0, 1, 0)
    d = Point(0, 0, 1)
    cube = Cuboid(a, b, c, d)
    # TODO: speed this up
    assert np.isclose(dist(p, cube), 0)
    assert np.isclose(dist(Point(-1, 0, 0), cube), 1)
    assert np.isclose(dist(Point(0.5, 0.5, 2), cube), 1)

    p = PointCollection([(1, 0, 1), (1, 1, 0)], homogenize=True)
    e = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)])
    s = Segment(Point(1, 0, 1), Point(1, 2, 1))
    # TODO: speed this up
    assert np.allclose(dist(e, p), 1)
    assert np.allclose(dist(p, cube), 0)
    assert np.allclose(dist(p, poly), [0, 1])
    assert np.allclose(dist(p, s), [0, 1])