Exemple #1
0
def test_angle_bisectors():
    a = Point(0, 0)
    b = Point(1, 1)
    c = Point(1, 0)
    l = Line(a, b)
    m = Line(a, c)
    q, r = angle_bisectors(l, m)
    assert is_perpendicular(q, r)
    assert np.isclose(angle(l, q), angle(q, m))

    p1 = Point(0, 0, 0)
    p2 = Point(0, 1, 0)
    p3 = Point(1, 0, 0)
    l = Line(p1, p2)
    m = Line(p1, p3)
    q, r = angle_bisectors(l, m)
    assert is_perpendicular(q, r)
    assert np.isclose(angle(l, q), angle(q, m))

    p1 = PointCollection([(0, 0), (0, 0)], homogenize=True)
    p2 = PointCollection([(1, 1), (0, 1)], homogenize=True)
    p3 = PointCollection([(1, 0), (1, 1)], homogenize=True)
    l = LineCollection(p1, p2)
    m = LineCollection(p1, p3)
    q, r = angle_bisectors(l, m)
    assert all(is_perpendicular(q, r))
    assert np.allclose(angle(l, q), angle(q, m))
Exemple #2
0
    def test_perpendicular(self):
        p1 = PointCollection([(1, 1, 0), (1, 1, 5)], homogenize=True)
        p2 = PointCollection([(2, 1, 0), (2, 1, 5)], homogenize=True)
        p3 = PointCollection([(0, 0, 0), (0, 0, 5)], homogenize=True)
        l = LineCollection(p1, p2)
        m = l.perpendicular(p1)

        assert l.meet(m) == p1
        assert all(is_perpendicular(l, m))

        m = l.perpendicular(
            p3 + PointCollection([(1, 1, 0), (0, 0, 0)], homogenize=True)
        )

        assert all(is_perpendicular(l, m))

        e = PlaneCollection(l, p3)
        m = e.perpendicular(p1)

        assert e.meet(m) == p1
        assert all(is_perpendicular(l, m))

        m = e.perpendicular(p1 + PointCollection([m.direction[0], Point(0, 0, 0)]))

        assert e.meet(m) == p1
        assert all(is_perpendicular(l, m))
Exemple #3
0
    def test_intersect(self):
        q = QuadricCollection([Circle(Point(0, 1), 1), Circle(Point(0, 2), 2)])
        l = LineCollection(
            [Line(Point(-1, 1), Point(1, 1)),
             Line(Point(-1, 2), Point(1, 2))])

        assert q.intersect(l) == [
            PointCollection([Point(1, 1), Point(-2, 2)]),
            PointCollection([Point(-1, 1), Point(2, 2)]),
        ]

        q = QuadricCollection(
            [Sphere(Point(0, 0, 1), 1),
             Sphere(Point(0, 0, 2), 2)])
        l = LineCollection([
            Line(Point(-1, 0, 1), Point(1, 0, 1)),
            Line(Point(-1, 0, 2), Point(1, 0, 2)),
        ])
        m = Line(Point(-1, 0, 2), Point(1, 0, 2))

        assert q.intersect(l) == [
            PointCollection([Point(-1, 0, 1), Point(-2, 0, 2)]),
            PointCollection([Point(1, 0, 1), Point(2, 0, 2)]),
        ]
        assert q.intersect(m) == [
            PointCollection([Point(0, 0, 2), Point(-2, 0, 2)]),
            PointCollection([Point(0, 0, 2), Point(2, 0, 2)]),
        ]
Exemple #4
0
    def test_project(self):
        p1 = PointCollection([(1, 1, 0), (1, 1, 5)], homogenize=True)
        p2 = PointCollection([(2, 1, 0), (2, 1, 5)], homogenize=True)
        p3 = PointCollection([(0, 0, 0), (0, 0, 5)], homogenize=True)
        l = LineCollection(p1, p2)
        assert l.project(p3) == PointCollection([(0, 1, 0), (0, 1, 5)], homogenize=True)

        e = PlaneCollection([(0, 1, 0, -1), (0, 1, 0, -2)])
        assert e.project(p3) == PointCollection([(0, 1, 0), (0, 2, 5)], homogenize=True)
Exemple #5
0
    def test_components(self):
        l = Line(1, 2, 3)
        m = Line(4, 5, 6)
        g = Line(3, 2, 1)
        h = Line(6, 5, 4)

        q = QuadricCollection([Conic.from_lines(l, m), Conic.from_lines(g, h)])
        assert q.components == [LineCollection([m, g]), LineCollection([l, h])]

        e = Plane(1, 2, 3, 4)
        f = Plane(4, 3, 2, 1)
        g = Plane(5, 6, 7, 8)
        h = Plane(8, 7, 6, 5)

        q = QuadricCollection([Quadric.from_planes(e, f), Quadric.from_planes(g, h)])
        assert q.components == [PlaneCollection([e, g]), PlaneCollection([f, h])]
Exemple #6
0
    def test_meet(self):
        # three planes
        a = PlaneCollection([Plane(1, 0, 0, 0), Plane(1, 0, 0, -1)])
        b = PlaneCollection([Plane(0, 1, 0, 0), Plane(0, 1, 0, -1)])
        c = PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)])
        assert meet(a, b, c) == PointCollection([Point(0, 0, 0), Point(1, 1, 1)])

        # two planes
        l = a.meet(b)
        m = LineCollection(
            [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 1, 0), Point(1, 1, 1))]
        )
        assert l == m

        # two lines in 2D
        a = LineCollection([Line(0, 1, 0), Line(0, 1, -1)])
        b = LineCollection([Line(1, 0, 0), Line(1, 0, -1)])
        assert a.meet(b) == PointCollection([Point(0, 0), Point(1, 1)])

        # two lines in 3D
        a = LineCollection(
            [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 0, 0), Point(1, 0, 1))]
        )
        b = LineCollection(
            [Line(Point(0, 0, 0), Point(0, 1, 0)), Line(Point(1, 0, 0), Point(1, 1, 0))]
        )
        assert a.meet(b) == PointCollection([Point(0, 0, 0), Point(1, 0, 0)])

        # plane and line
        a = LineCollection(
            [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 0, 0), Point(1, 0, 1))]
        )
        b = PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)])
        assert a.meet(b) == PointCollection([Point(0, 0, 0), Point(1, 0, 1)])
Exemple #7
0
def test_angle():
    a = Point(0, 0)
    b = Point(1, 1)
    c = Point(1, 0)

    assert np.isclose(angle(a, b, c), np.pi / 4)
    assert np.isclose(angle(a, c, b), -np.pi / 4)

    e1 = Plane(1, 0, 0, 0)
    e2 = Plane(0, 0, 1, 0)

    assert np.isclose(abs(angle(e1, e2)), np.pi / 2)

    p1 = Point(0, 0, 0)
    p2 = Point(0, 1, 0)
    p3 = Point(1, 0, 0)

    assert np.isclose(abs(angle(p1, p2, p3)), np.pi / 2)

    l = Line(p1, p2)
    m = Line(p1, p3)

    assert np.isclose(abs(angle(l, m)), np.pi / 2)

    p1 = PointCollection([(0, 0), (0, 0)], homogenize=True)
    p2 = PointCollection([(1, 1), (0, 1)], homogenize=True)
    p3 = PointCollection([(1, 0), (1, 1)], homogenize=True)

    assert np.allclose(angle(p1, p2, p3), np.pi / 4)

    l = LineCollection(p1, p2)
    m = LineCollection(p1, p3)

    assert np.allclose(angle(l, m), np.pi / 4)

    e1 = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)])
    e2 = PlaneCollection([(1, 1, 1, 0), (0, 1, 0, 0)])

    assert np.allclose(angle(e1, e2), [np.arccos(1 / np.sqrt(3)), np.pi / 2])
Exemple #8
0
def test_is_perpendicular():
    l = Line(0, 1, 0)
    m = Line(1, 0, 0)
    assert is_perpendicular(l, m)

    p1 = Point(0, 0, 0)
    p2 = Point(0, 1, 0)
    p3 = Point(1, 0, 0)
    l = Line(p1, p2)
    m = Line(p1, p3)
    assert is_perpendicular(l, m)

    e1 = Plane(p1, p2, p3)
    e2 = Plane(p1, p2, Point(0, 0, 1))
    assert is_perpendicular(e1, e2)

    l = LineCollection([(0, 1, 0), (0, 1, 0)])
    m = LineCollection([(1, 0, 0), (1, -1, 0)])
    assert list(is_perpendicular(l, m)) == [True, False]

    e1 = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)])
    e2 = PlaneCollection([(0, 1, 0, 0), (0, 0, 1, 0)])
    assert all(is_perpendicular(e1, e2))
Exemple #9
0
    def test_intersection(self):
        c = Circle(Point(0, 2), 2)
        l = Line(Point(-1, 2), Point(1, 2))

        assert c.contains(Point(0, 0))
        assert c.intersect(l) == [Point(-2, 2), Point(2, 2)]
        assert c.intersect(l - Point(0, 2)) == [Point(0, 0)]

        l = LineCollection(
            [Line(Point(-1, 2), Point(1, 2)),
             Line(Point(0, 2), Point(0, 0))])
        assert c.intersect(l) == [
            PointCollection([Point(-2, 2), Point(0, 0)]),
            PointCollection([Point(2, 2), Point(0, 4)])
        ]
Exemple #10
0
    def test_intersect(self):
        s = Sphere(Point(0, 0, 2), 2)
        l = Line(Point(-1, 0, 2), Point(1, 0, 2))

        assert s.contains(Point(0, 0, 0))
        assert s.intersect(l) == [Point(-2, 0, 2), Point(2, 0, 2)]
        assert s.intersect(l - Point(0, 0, 2)) == [Point(0, 0, 0)]

        l = LineCollection([Line(Point(-1, 0, 2), Point(1, 0, 2)), Line(Point(0, 0, 0), Point(0, 0, 2))])
        assert s.intersect(l) == [PointCollection([Point(-2, 0, 2), Point(0, 0, 0)]),
                                  PointCollection([Point(2, 0, 2), Point(0, 0, 4)])]

        s = Sphere(Point(0, 0, 0, 2), 2)
        l = Line(Point(-1, 0, 0, 2), Point(1, 0, 0, 2))

        assert s.contains(Point(0, 0, 0, 0))
        assert s.intersect(l) == [Point(2, 0, 0, 2), Point(-2, 0, 0, 2)]
Exemple #11
0
    def test_intersect(self):
        c = Circle(Point(0, 2), 2)
        l = Line(Point(-1, 2), Point(1, 2))

        assert c.contains(Point(0, 0))
        assert c.intersect(l) == [Point(-2, 2), Point(2, 2)]
        assert c.intersect(l - Point(0, 2)) == [Point(0, 0)]

        l = LineCollection(
            [Line(Point(-1, 2), Point(1, 2)),
             Line(Point(0, 2), Point(0, 0))])
        assert c.intersect(l) == [
            PointCollection([Point(-2, 2), Point(0, 0)]),
            PointCollection([Point(2, 2), Point(0, 4)]),
        ]

        c1 = Circle(Point(0, 0), 5)
        c2 = Circle(Point(8, 0), 5)
        intersections = c1.intersect(c2)
        assert Point(4, 3) in intersections
        assert Point(4, -3) in intersections
Exemple #12
0
    def test_join(self):
        # 2 points
        a = PointCollection([Point(0, 0), Point(0, 1)])
        b = PointCollection([Point(1, 0), Point(1, 1)])

        assert a.join(b) == LineCollection([Line(0, 1, 0), Line(0, 1, -1)])

        # 3 points
        a = PointCollection([Point(0, 0, 0), Point(0, 0, 1)])
        b = PointCollection([Point(1, 0, 0), Point(1, 0, 1)])
        c = PointCollection([Point(0, 1, 0), Point(0, 1, 1)])

        assert join(a, b, c) == PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)])

        # two lines
        l = a.join(b)
        m = a.join(c)
        assert join(l, m) == PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)])

        # point and line
        assert join(a, b.join(c)) == PlaneCollection(
            [Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)]
        )
Exemple #13
0
    def test_intersect(self):
        c = Circle(Point(0, 2), 2)
        l = Line(Point(-1, 2), Point(1, 2))

        assert c.contains(Point(0, 0))
        assert c.intersect(l) == [Point(-2, 2), Point(2, 2)]
        assert c.intersect(l - Point(0, 2)) == [Point(0, 0)]

        l = LineCollection([Line(Point(-1, 2), Point(1, 2)), Line(Point(0, 2), Point(0, 0))])
        assert c.intersect(l) == [PointCollection([Point(-2, 2), Point(0, 0)]),
                                  PointCollection([Point(2, 2), Point(0, 4)])]

        c1 = Circle(Point(0, 0), 5)
        c2 = Circle(Point(8, 0), 5)
        intersections = c1.intersect(c2)
        assert Point(4, 3) in intersections
        assert Point(4, -3) in intersections

        c1 = Circle(Point(0, 0))
        c2 = Circle(Point(0.5, 0))
        i = c1.intersect(c2)
        assert len(i) == 4
        assert len([p for p in i if p.isreal]) == 2