示例#1
0
    def test_affine(self):
        al = []
        for i in range(10):
            al.append(
                Affine(uniform(-10, 10), uniform(-10, 10), uniform(-10, 10),
                       uniform(-10, 10), uniform(-10, 10), uniform(-10, 10)))
        for A in al:
            for B in al:
                self.affine(A, B)

        o = Point(2, 4)
        v = Point(-1, 1) / sqrt(2)
        l = Line.from_origin_and_versor(o, v)

        R = Affine.reflection(v, o)
        for i in range(100):
            p = Point(randint(0, 100), randint(0, 100))
            self.assertAlmostEqual(Line.distance(p, l),
                                   Line.distance(p * R, l))
        self.assertTrue(Affine.are_near(R, R.inverse()))

        self.affine(R, R.inverse())
示例#2
0
    def test_affine(self):
        al = []
        for i in range(10):
            al.append(Affine(   uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10)))
        for A in al:
            for B in al:
                self.affine(A, B)

        o = Point(2, 4)
        v = Point(-1, 1)/sqrt(2)
        l = Line.from_origin_and_versor(o, v)
        
        R = Affine.reflection(v, o)
        for i in range(100):
            p = Point(randint(0, 100), randint(0, 100))
            self.assertAlmostEqual(Line.distance(p, l), Line.distance(p*R, l))
        self.assertTrue( Affine.are_near( R, R.inverse() ) )
        
        self.affine(R, R.inverse())
示例#3
0
    def test_line(self):

        l = Line(Point(), pi / 4)
        self.assertAlmostEqual(l.origin(), Point())
        self.assertAlmostEqual(l.versor(), Point(1, 1) / sqrt(2))
        self.assertAlmostEqual(l.angle(), pi / 4)

        k = Line.from_points(Point(), Point(2, 1))
        self.assertFalse(k.is_degenerate())
        self.assertFalse(Line().is_degenerate())
        self.assertAlmostEqual(l.point_at(sqrt(2)), Point(1, 1))
        self.assertAlmostEqual(k.point_at(43),
                               Point(k.value_at(43, 0), k.value_at(43, 1)))
        self.assertAlmostEqual(k.time_at(Point(4, 2)), sqrt(20))
        self.assertAlmostEqual(
            k.time_at_projection(Point(4, 2) + Point(2, -4)), sqrt(20))
        self.assertAlmostEqual(
            k.point_at(k.nearest_point(Point(4, 2) + Point(2, -4))),
            Point(4, 2))
        self.assertAlmostEqual(k.time_at_projection(Point(3, 3)),
                               -k.reverse().time_at_projection(Point(3, 3)))
        self.assertAlmostEqual(k.derivative().origin(), k.versor())
        self.assertAlmostEqual(k.normal(), k.versor().cw())

        roots = k.roots(3, 0)
        for root in roots:
            self.assertAlmostEqual(k.value_at(root, 0), 3)

        self.assertAlmostEqual(l.normal(), l.normal_and_dist()[0])
        self.assertAlmostEqual(Line.distance(Point(), l),
                               l.normal_and_dist()[1])

        self.assertAlmostEqual(Line.distance(Point(-1, 1), l), sqrt(2))
        self.assertTrue(Line.are_near(Point(0), l))
        self.assertFalse(Line.are_near(Point(1, 1), k))
        self.assertTrue(Line.are_near(Point(1, 1), k, 2))

        p = Line(Point(1, 1))
        p_orto = Line(Point(2, 3), pi / 2)
        p_para = Line(Point(2, 3))
        p_same = Line.from_points(Point(1, 1), Point(5, 1))

        self.assertTrue(Line.are_orthogonal(p, p_orto))
        self.assertFalse(Line.are_orthogonal(p, p_para))
        self.assertTrue(Line.are_parallel(p, p_para))
        self.assertFalse(Line.are_parallel(p, p_orto))
        self.assertTrue(Line.are_same(p, p_same))
        self.assertFalse(Line.are_same(p, p_para))

        self.assertTrue(
            Line.are_collinear(Point(1, 1), Point(2, 3), Point(4, 7)))
        self.assertAlmostEqual(Line.angle_between(p, p_orto), pi / 2)

        m = Line.from_normal_distance(Point(1, -1), 1)
        self.assertAlmostEqual(m.angle(), pi / 4)

        m = Line.from_LineSegment(LineSegment(Point(2, 2), Point(4, 4)))
        self.assertAlmostEqual(m.angle(), pi / 4)

        m = Line.from_Ray(Ray(Point(2, 3), 0.2))
        self.assertAlmostEqual(m.angle(), 0.2)
        self.assertAlmostEqual(m.origin(), Point(2, 3))

        self.assertIsInstance(m.portion(2, 4), Curve)
        self.assertAlmostEqual(m.portion(2, 4)(0), m.point_at(2))

        self.assertIsInstance(m.segment(1, 5), LineSegment)
        self.assertAlmostEqual(m.segment(1, 5)(1), m.point_at(5))

        self.assertAlmostEqual(m.ray(4).origin(), m.point_at(4))

        m.set_origin(Point())
        self.assertAlmostEqual(m.origin(), Point())

        m.set_angle(0.2)
        self.assertAlmostEqual(m.angle(), 0.2)

        m.set_versor(Point())
        self.assertTrue(m.is_degenerate())

        m.set_points(Point(2, 9), Point(1, 8))
        self.assertAlmostEqual(m.versor(),
                               Point.unit_vector(Point(1, 8) - Point(2, 9)))
示例#4
0
    def affine(self, A, B):
        c0, c1, c2, c3, c4, c5 = A[0], A[1], A[2], A[3], A[4], A[5]
        C = Affine(c0, c1, c2, c3, c4, c5)
        self.assertEqual(C, A)
        E = Affine.identity()
        self.assertEqual(C, C*E)
        self.assertEqual(E*B, B)
        self.assertEqual(E.det(), 1)

        self.assertAlmostEqual(A.det(), c0*c3-c1*c2)
        self.assertAlmostEqual(abs(A.det()), A.descrim2())
        self.assertAlmostEqual(abs(A.det())**0.5, A.descrim())
        #xor
        self.assertFalse( A.flips() ^ (A.det() < 0) )

        if A.is_singular():
            self.assertAlmostEqual(A.det(), 0)
        else:
            self.assertTrue( Affine.are_near (A*A.inverse(), E) )
            self.assertAlmostEqual(A.det(), 1/A.inverse().det())
        self.assertEqual( A.x_axis(), Point(c0, c1) )
        self.assertEqual( A.y_axis(), Point(c2, c3) )
        self.assertEqual( A.translation(), Point(c4, c5) )

        self.assertAlmostEqual(A.expansion_X(), A.x_axis().length())
        self.assertAlmostEqual(A.expansion_Y(), A.y_axis().length())
        
        if abs(A.expansion_X()) > 1e-7 and abs(A.expansion_Y()) > 1e-7:
            A.set_expansion_X(2)
            A.set_expansion_Y(3)
            self.assertAlmostEqual(A.expansion_X(), 2)
            self.assertAlmostEqual(A.expansion_Y(), 3)

        A.set_identity()

        self.assertTrue(A.is_identity())
        self.assertTrue(A.is_translation())
        self.assertFalse(A.is_nonzero_translation())
        self.assertTrue(A.is_scale())
        self.assertTrue(A.is_uniform_scale())
        self.assertFalse(A.is_nonzero_scale())
        self.assertFalse(A.is_nonzero_uniform_scale())
        self.assertTrue(A.is_rotation())
        self.assertFalse(A.is_nonzero_rotation())
        self.assertTrue(A.is_HShear())
        self.assertTrue(A.is_VShear())
        self.assertFalse(A.is_nonzero_HShear())
        self.assertFalse(A.is_nonzero_VShear())
        self.assertTrue(A.is_zoom())

        self.assertTrue(A.preserves_area() and A.preserves_angles() and A.preserves_distances())

        self.assertFalse( A.flips() )
        self.assertFalse( A.is_singular() )

        A.set_X_axis(Point(c0, c1))
        A.set_Y_axis(Point(c2, c3))

        self.assertEqual(A.without_translation(), A)

        A.set_translation(Point(c4, c5))
        self.assertEqual(C, A)

        self.assertAlmostEqual( (A*B).det(), A.det()*B.det() )

        self.assertEqual( A.translation(), Point()*A )
        self.assertEqual( Point(1, 1)*A, Point( c0+c2+c4, c1+c3+c5 ))

        l = Line(Point(1, 1), 2)
        self.assertEqual( (l.transformed(A)).origin(), l.origin()*A )
        self.assertTrue( Line.are_near( l.point_at(3)*A, l.transformed(A) ) )

        r = Ray(Point(2, 3), 4)
        self.assertEqual( (r.transformed(A)).origin(), r.origin()*A )
        self.assertTrue( Ray.are_near( r.point_at(3)*A, r.transformed(A) ) )
示例#5
0
    def test_line(self):

        l = Line(Point(), pi/4)
        self.assertAlmostEqual( l.origin(), Point() )
        self.assertAlmostEqual( l.versor(), Point(1, 1)/sqrt(2) )
        self.assertAlmostEqual( l.angle(), pi/4 )

        k = Line.from_points(Point(), Point(2, 1))
        self.assertFalse(k.is_degenerate())
        self.assertFalse(Line().is_degenerate())
        self.assertAlmostEqual( l.point_at(sqrt(2)), Point(1,1) )
        self.assertAlmostEqual(
            k.point_at(43),
            Point(k.value_at(43, 0), k.value_at(43, 1)))
        self.assertAlmostEqual(k.time_at(Point(4, 2)), sqrt(20))
        self.assertAlmostEqual(
            k.time_at_projection(Point(4, 2) + Point(2, -4)),
            sqrt(20))
        self.assertAlmostEqual(
            k.point_at(k.nearest_point(Point(4, 2) + Point(2, -4))),
            Point(4,2))
        self.assertAlmostEqual(
            k.time_at_projection(Point(3, 3)),
            -k.reverse().time_at_projection(Point(3, 3)))
        self.assertAlmostEqual( k.derivative().origin(), k.versor())
        self.assertAlmostEqual(k.normal(), k.versor().cw())

        roots = k.roots( 3, 0 )
        for root in roots:
            self.assertAlmostEqual( k.value_at(root, 0), 3)

        self.assertAlmostEqual(l.normal(), l.normal_and_dist()[0])
        self.assertAlmostEqual(Line.distance(Point(), l), l.normal_and_dist()[1])

        self.assertAlmostEqual(Line.distance(Point(-1, 1), l), sqrt(2))
        self.assertTrue(Line.are_near(Point(0), l))
        self.assertFalse(Line.are_near(Point(1, 1), k))
        self.assertTrue(Line.are_near(Point(1, 1), k, 2))

        p = Line(Point(1, 1))
        p_orto = Line(Point(2, 3), pi/2)
        p_para = Line(Point(2, 3))
        p_same = Line.from_points(Point(1, 1), Point(5, 1))

        self.assertTrue(Line.are_orthogonal(p, p_orto))
        self.assertFalse(Line.are_orthogonal(p, p_para))
        self.assertTrue(Line.are_parallel(p, p_para))
        self.assertFalse(Line.are_parallel(p, p_orto))
        self.assertTrue(Line.are_same(p, p_same))
        self.assertFalse(Line.are_same(p, p_para))

        self.assertTrue(Line.are_collinear(
            Point(1,1),
            Point(2, 3),
            Point(4, 7)))
        self.assertAlmostEqual(Line.angle_between(p, p_orto), pi/2)

        m = Line.from_normal_distance(Point(1, -1), 1)
        self.assertAlmostEqual(m.angle(), pi/4)

        m = Line.from_LineSegment( LineSegment( Point(2, 2), Point(4, 4) ) )
        self.assertAlmostEqual(m.angle(), pi/4)

        m = Line.from_Ray( Ray(Point(2, 3), 0.2) )
        self.assertAlmostEqual(m.angle(), 0.2)
        self.assertAlmostEqual(m.origin(), Point(2, 3))

        self.assertIsInstance(m.portion(2, 4), Curve)
        self.assertAlmostEqual(m.portion(2, 4)(0), m.point_at(2))

        self.assertIsInstance(m.segment(1, 5), LineSegment)
        self.assertAlmostEqual(m.segment(1, 5)(1), m.point_at(5))

        self.assertAlmostEqual(m.ray(4).origin(), m.point_at(4))

        m.set_origin(Point())
        self.assertAlmostEqual(m.origin(), Point())

        m.set_angle(0.2)
        self.assertAlmostEqual(m.angle(), 0.2)

        m.set_versor(Point())
        self.assertTrue(m.is_degenerate())

        m.set_points(Point(2, 9), Point(1, 8))
        self.assertAlmostEqual(m.versor(), Point.unit_vector(Point(1, 8) - Point(2, 9)))
示例#6
0
    def affine(self, A, B):
        c0, c1, c2, c3, c4, c5 = A[0], A[1], A[2], A[3], A[4], A[5]
        C = Affine(c0, c1, c2, c3, c4, c5)
        self.assertEqual(C, A)
        E = Affine.identity()
        self.assertEqual(C, C * E)
        self.assertEqual(E * B, B)
        self.assertEqual(E.det(), 1)

        self.assertAlmostEqual(A.det(), c0 * c3 - c1 * c2)
        self.assertAlmostEqual(abs(A.det()), A.descrim2())
        self.assertAlmostEqual(abs(A.det())**0.5, A.descrim())
        #xor
        self.assertFalse(A.flips() ^ (A.det() < 0))

        if A.is_singular():
            self.assertAlmostEqual(A.det(), 0)
        else:
            self.assertTrue(Affine.are_near(A * A.inverse(), E))
            self.assertAlmostEqual(A.det(), 1 / A.inverse().det())
        self.assertEqual(A.x_axis(), Point(c0, c1))
        self.assertEqual(A.y_axis(), Point(c2, c3))
        self.assertEqual(A.translation(), Point(c4, c5))

        self.assertAlmostEqual(A.expansion_X(), A.x_axis().length())
        self.assertAlmostEqual(A.expansion_Y(), A.y_axis().length())

        if abs(A.expansion_X()) > 1e-7 and abs(A.expansion_Y()) > 1e-7:
            A.set_expansion_X(2)
            A.set_expansion_Y(3)
            self.assertAlmostEqual(A.expansion_X(), 2)
            self.assertAlmostEqual(A.expansion_Y(), 3)

        A.set_identity()

        self.assertTrue(A.is_identity())
        self.assertTrue(A.is_translation())
        self.assertFalse(A.is_nonzero_translation())
        self.assertTrue(A.is_scale())
        self.assertTrue(A.is_uniform_scale())
        self.assertFalse(A.is_nonzero_scale())
        self.assertFalse(A.is_nonzero_uniform_scale())
        self.assertTrue(A.is_rotation())
        self.assertFalse(A.is_nonzero_rotation())
        self.assertTrue(A.is_HShear())
        self.assertTrue(A.is_VShear())
        self.assertFalse(A.is_nonzero_HShear())
        self.assertFalse(A.is_nonzero_VShear())
        self.assertTrue(A.is_zoom())

        self.assertTrue(A.preserves_area() and A.preserves_angles()
                        and A.preserves_distances())

        self.assertFalse(A.flips())
        self.assertFalse(A.is_singular())

        A.set_X_axis(Point(c0, c1))
        A.set_Y_axis(Point(c2, c3))

        self.assertEqual(A.without_translation(), A)

        A.set_translation(Point(c4, c5))
        self.assertEqual(C, A)

        self.assertAlmostEqual((A * B).det(), A.det() * B.det())

        self.assertEqual(A.translation(), Point() * A)
        self.assertEqual(Point(1, 1) * A, Point(c0 + c2 + c4, c1 + c3 + c5))

        l = Line(Point(1, 1), 2)
        self.assertEqual((l.transformed(A)).origin(), l.origin() * A)
        self.assertTrue(Line.are_near(l.point_at(3) * A, l.transformed(A)))

        r = Ray(Point(2, 3), 4)
        self.assertEqual((r.transformed(A)).origin(), r.origin() * A)
        self.assertTrue(Ray.are_near(r.point_at(3) * A, r.transformed(A)))