Example #1
0
    def test_intersections(self):
        c = Circle(Point(0, 0), 1)
        i = c.intersect(Line(0, 1, 0))
        assert len(i) == 2
        assert Point(1, 0) in i
        assert Point(-1, 0) in i

        c2 = Circle(Point(0, 2), 1)
        assert Point(0, 1) in c.intersect(c2)
Example #2
0
    def test_transform(self):
        p = RegularPolygon(Point(0, 0, 0), 1, 6, axis=Point(0, 0, 1))
        t = translation(1, 1, 0)

        assert t * p == RegularPolygon(Point(1, 1, 0),
                                       1,
                                       6,
                                       axis=Point(0, 0, 1))
        assert isinstance(t * p, RegularPolygon)
Example #3
0
 def test_area(self):
     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)
     assert len(cube.faces) == 6
     assert len(cube.vertices) == 8
     assert cube.area == 6
Example #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)]
Example #5
0
    def test_parallel(self):
        p = Point(0, 1)
        q = Point(1, 1)
        r = Point(0, 0)
        l = Line(p, q)
        m = l.parallel(through=r)

        assert m == Line(0, 1, 0)
        assert l.is_parallel(m)
Example #6
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)
Example #7
0
    def test_intersect(self):
        a = PointCollection([(0, 0), (0, 0)], homogenize=True)
        b = PointCollection([(2, 0), (4, 0)], homogenize=True)
        c = PointCollection([(2, 2), (4, 4)], homogenize=True)
        d = PointCollection([(0, 2), (0, 4)], homogenize=True)
        s1 = SegmentCollection(a, c)
        s2 = SegmentCollection(b, d)

        assert s1.intersect(s2) == PointCollection([(1, 1), (2, 2)], homogenize=True)
        assert s1.intersect(Line(Point(0, 0), Point(1, 1))).size == 0
Example #8
0
    def test_add(self):
        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)
        p = Point(1, 2, 3)

        assert cube + p == Cuboid(a + p, b + p, c + p, d + p)
        assert cube - p == Cuboid(a - p, b - p, c - p, d - p)
Example #9
0
    def test_contains(self):
        p1 = Point(1, 1, 0)
        p2 = Point(2, 1, 0)
        p3 = Point(3, 4, 0)
        p4 = Point(0, 2, 0)

        p = Plane(p1, p2, p3)
        l = Line(p1, p2)
        assert p.contains(p4)
        assert p.contains(l)
Example #10
0
    def test_transform(self):
        a = Point(0, 0, 0)
        b = Point(1, 0, 0)
        c = Point(0, 1, 0)
        d = Point(0, 0, 1)
        s = Simplex(a, b, c, d)
        x = Point(1, 1, 1)
        t = translation(x)

        assert t * s == Simplex(a + x, b + x, c + x, d + x)
Example #11
0
    def test_volume(self):
        a = Point(0, 0, 0)
        b = Point(1, 0, 0)
        c = Point(0, 1, 0)
        d = Point(0, 0, 1)
        s = Simplex(a, b, c, d)

        assert np.isclose(s.volume, 1 / 6)

        triangle = Simplex(a, b, c)
        assert np.isclose(triangle.volume, 1 / 2)
Example #12
0
    def test_init(self):
        a = Point(0, 0, 0)
        p = RegularPolygon(a, 1, 6, axis=Point(0, 0, 1))

        d = p.edges[0].length

        assert len(p.vertices) == 6
        assert np.isclose(dist(a, p.vertices[0]), 1)
        assert all(np.isclose(s.length, d) for s in p.edges[1:])
        assert np.allclose(p.angles, np.pi / 3)
        assert p.center == a
Example #13
0
    def test_transform(self):
        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)
        x = Point(1, 1, 1)
        t = translation(x)

        assert t * cube == Cuboid(a + x, b + x, c + x, d + x)
        assert isinstance(t * cube, Cuboid)
Example #14
0
    def test_getitem(self):
        a = Point(0, 0, 1)
        b = Point(2, 0, 1)
        c = Point(2, 2, 1)
        d = Point(0, 2, 1)
        r = Rectangle(a, b, c, d)

        assert r[0] == a
        assert r[1] == b
        assert r[2] == c
        assert r[3] == d
Example #15
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)
     ]
Example #16
0
    def test_centroid(self):
        a = Point(0, 0, 1)
        b = Point(2, 0, 1)
        c = Point(2, 2, 1)
        t = Triangle(a, b, c)

        s1, s2, s3 = t.edges
        l1 = s1.midpoint.join(c)
        l2 = s2.midpoint.join(a)

        assert t.centroid == (a + b + c) / 3
        assert t.centroid == l1.meet(l2)
Example #17
0
    def test_from_crossratio(self):
        a = Point(0, 1)
        b = Point(0, -1)
        c = Point(1.5, 0.5)
        d = Point(1.5, -0.5)
        e = Point(-1.5, 0.5)

        conic1 = Conic.from_points(a, b, c, d, e)
        cr = crossratio(a, b, c, d, e)
        conic2 = Conic.from_crossratio(cr, a, b, c, d)

        assert conic1 == conic2
Example #18
0
    def test_perpendicular(self):
        p = Point(1, 1, 0)
        q = Point(0, 0, 1)
        l = Line(p, q)
        m = l.perpendicular(p)

        assert is_perpendicular(l, m)

        r = Point(1, 2, 3)
        e = Plane(p, q, r)
        m = e.perpendicular(p)

        assert is_perpendicular(l, m)
Example #19
0
    def test_from_tangent(self):
        a = Point(-1.5, 0.5)
        b = Point(0, -1)
        c = Point(1.5, 0.5)
        d = Point(1.5, -0.5)
        l = Line(0, 1, -1)

        conic = Conic.from_tangent(l, a, b, c, d)

        assert conic.contains(a)
        assert conic.contains(b)
        assert conic.contains(c)
        assert conic.contains(d)
        assert conic.is_tangent(l)
Example #20
0
    def test_from_points(self):
        a = Point(-1, 0)
        b = Point(0, 3)
        c = Point(1, 2)
        d = Point(2, 1)
        e = Point(0, -1)

        conic = Conic.from_points(a, b, c, d, e)

        assert conic.contains(a)
        assert conic.contains(b)
        assert conic.contains(c)
        assert conic.contains(d)
        assert conic.contains(e)
    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)]
Example #22
0
def test_is_coplanar():
    p1 = Point(1, 1, 0)
    p2 = Point(2, 1, 0)
    p3 = Point(3, 4, 0)
    p4 = Point(0, 2, 0)

    assert is_coplanar(p1, p2, p3, p4)

    p1 = PointCollection([(1, 0), (1, 1)], homogenize=True)
    p2 = PointCollection([(2, 0), (2, 1)], homogenize=True)
    p3 = PointCollection([(3, 0), (3, 1)], homogenize=True)
    p4 = PointCollection([(4, 0), (4, 1)], homogenize=True)

    assert all(is_coplanar(p1, p2, p3, p4))
Example #23
0
    def test_from_points(self):
        a = Point(0, 1)
        b = Point(0, -1)
        c = Point(1.5, 0.5)
        d = Point(1.5, -0.5)
        e = Point(-1.5, 0.5)

        conic = Conic.from_points(a, b, c, d, e)

        assert conic.contains(a)
        assert conic.contains(b)
        assert conic.contains(c)
        assert conic.contains(d)
        assert conic.contains(e)
Example #24
0
def test_is_collinear():
    p1 = Point(1, 0)
    p2 = Point(2, 0)
    p3 = Point(3, 0)
    l = Line(p1, p2)
    assert l.contains(p3)
    assert is_collinear(p1, p2, p3)

    p1 = PointCollection([(1, 0), (1, 1)], homogenize=True)
    p2 = PointCollection([(2, 0), (2, 1)], homogenize=True)
    p3 = PointCollection([(3, 0), (3, 1)], homogenize=True)
    p4 = PointCollection([(4, 0), (4, 1)], homogenize=True)

    assert all(is_collinear(p1, p2, p3, p4))
Example #25
0
 def test_intersect(self):
     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)
     l = Line(Point(2, 0.5, 0.5), Point(-1, 0.5, 0.5))
     assert cube.intersect(l) == [Point(0, 0.5, 0.5), Point(1, 0.5, 0.5)]
Example #26
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))
Example #27
0
    def test_equal(self):
        points = np.random.rand(50, 3)

        p1 = Polygon(points)
        p2 = Polygon(*[Point(p) for p in points])

        assert p1 == p2
Example #28
0
    def test_s2(self):
        s2 = Sphere()

        assert s2.center == Point(0, 0, 0)
        assert np.isclose(s2.radius, 1)
        assert np.isclose(s2.volume, 4 / 3 * np.pi)
        assert np.isclose(s2.area, 4 * np.pi)
Example #29
0
    def test_perpendicular(self):
        p = Point(1, 1)
        l = Line(1, 1, 0)
        m = l.perpendicular(p)

        assert m == Line(-1, 1, 0)

        m = l.perpendicular(Point(0, 0))
        assert m == Line(-1, 1, 0)

        p = Point(1, 1, 0)
        q = Point(0, 0, 1)
        l = Line(p, q)
        m = l.perpendicular(p)

        assert is_perpendicular(l, m)
Example #30
0
    def test_equal(self):
        points = np.random.rand(60, 50, 3)

        p1 = PolygonCollection(points)
        p2 = PolygonCollection(
            [Polygon(*[Point(p) for p in poly]) for poly in points])

        assert p1 == p2