# -*- coding: iso-8859-1 -*-
from __future__ import division # 1/2 == .5 (par defaut, 1/2 == 0)

from customtest import *





class TestGeolibLignes(CustomTest):

    def test_Segment(self):
        A = Point(4.5,  7.3)
        B = Point(4,  2.1)
        s = Segment(A,  B)
        self.assert_(isinstance(s.etiquette, Label_segment))
        self.assertAlmostEqual(s.longueur, math.sqrt((B.x - A.x)**2 + (B.y - A.y)**2))
        I = Milieu(s.point1,  s.point2)
        self.assertEqual(I.coordonnees,  ((A.x+B.x)/2, (A.y+B.y)/2))
        M = Barycentre((A,  1),  (B,  -2))
        N = Barycentre((A,  -2),  (B,  1))
        self.assert_(I in s)
        self.assert_(M not in s)
        self.assert_(N not in s)
        self.assert_(s.style("legende") == param.RIEN)

    def test_Demidroite(self):
        A = Point(4.5,  7.3)
        B = Point(4,  2.1)
        s = Demidroite(A,  B)
        self.assert_(isinstance(s.etiquette, Label_demidroite))
        self.assertRaises(AttributeError,  getattr,  s, "longueur")
        I = Milieu(s.origine,  s.point)
        self.assertEqual(I.coordonnees,  ((A.x+B.x)/2, (A.y+B.y)/2))
        M = Barycentre((A,  1),  (B,  -2))
        N = Barycentre((A,  -2),  (B,  1))
        self.assert_(I in s)
        self.assert_(M in s)
        self.assert_(N not in s)
        self.assert_(s.style("legende") == param.RIEN)

    def test_Droite(self):
        A = Point(4.5,  7.3)
        B = Point(4,  2.1)
        d = Droite(A,  B)
        self.assert_(isinstance(d.etiquette, Label_droite))
        self.assertRaises(AttributeError,  getattr,  d, "longueur")
        I = Milieu(d.point1,  d.point2)
        self.assertEqual(I.coordonnees,  ((A.x+B.x)/2, (A.y+B.y)/2))
        M = Barycentre((A,  1),  (B,  -2))
        N = Barycentre((A,  -2),  (B,  1))
        self.assert_(I in d)
        self.assert_(M in d)
        self.assert_(N in d)
        self.assert_(isinstance(d.equation,  tuple))
        self.assert_(d.style("legende") == param.RIEN)
        # Test du typage dynamique
        d = Droite("y=x+1")
        self.assert_(Point(0, 1) in d)
        d = Droite(Point(1, 2), Vecteur_libre(1, 1))
        self.assert_(Point(1, 2) in d)
        self.assert_(Point(2, 3) in d)
        d2 = Droite("y=-x+1")
        self.assert_(Point(0, 1) in d2)
        self.assert_(Point(1, 0) in d2)



    def test_Droite_vectorielle(self):
        v = Vecteur_libre(1,  7)
        A = Point(-2, 3)
        d = Droite_vectorielle(A,  v)
        self.assert_(d.vecteur is v and d.point is A)
        self.assertAlmostEqual(v.y/v.x,  -d.equation[0]/d.equation[1])
        B = rand_pt()
        d1 = Droite_vectorielle(B, v)
        self.assert_(d.parallele(d1))

    def test_Parallele(self):
        d0 = Droite_equation(2,  1,  7)
        A = Point(-2, 3)
        d = Parallele(d0,  A)
        self.assert_(d.parallele(d0))
        self.assert_(d.droite is d0 and d.point is A)
        self.assertAlmostEqual(d0.equation[:1],  d.equation[:1])

##    def test_Droite_rotation(self):
##        r = Rotation(Point(1.45, -2.59), math.pi/3)
##        C = Point(1.458, -5.255)
##        D = Point(3.478, -2.14788)
##        d = Droite(C, D)
##        # Dans ce qui suit, d1, d2 et d3 doivent correspondre à la même droite.
##        d1 = Droite_rotation(d,  r)
##        d2 = Droite(r(C), r(D))
##        d3 = r(d)
##        a, b, c = d1.equation
##        self.assertAlmostEqual(d1.equation_reduite, d2.equation_reduite)
##        self.assertAlmostEqual(d1.equation_reduite, d3.equation_reduite)
##        self.assertAlmostEqual(d1.equation_reduite, (-a/b, -c/b))
##        d = Droite_rotation(Droite_equation(1, -1, 1),  Rotation(Point(0, 0), math.pi/2))
##        a, b, c = d.equation
##        self.assertAlmostEqual(b/a,  1)
##        self.assertAlmostEqual(c/a,  1)


    def test_Mediatrice(self):
        A = Point(4.5,  7.3)
        B = Point(-4.147,  2.1)
        s = Segment(A,  B)
        d0 = Mediatrice(s)
        d1 = Mediatrice(A,  B)
        I = Milieu(A,  B)
        self.assert_(I in d0)
        self.assert_(I in d1)
        a,  b,  c = s.equation
        a0,  b0,  c0 = d0.equation
        self.assertAlmostEqual(a*a0 + b*b0,  0)
        self.assertAlmostEqual(d0.equation,  d1.equation)

    def test_Perpendiculaire(self):
        d = Droite_equation(-1, 2, 0)
        M = Point()
        d0 = Perpendiculaire(d,  M)
        a,  b,  c = d.equation
        a0,  b0,  c0 = d0.equation
        self.assert_(d.perpendiculaire(d0))
        self.assertAlmostEqual(a*a0 + b*b0,  0)
        self.assert_(M in d0)

    def test_Droite_equation(self):
        a = randint(50) - randint(50) + 0.1 # afin que a ne soit pas nul
        b = randint(50) - randint(50) + random()
        c = randint(50) - randint(50) + random()
        d, e, f = Droite_equation(a, b, c).equation
        self.assertAlmostEqual((e/d, f/d),  (b/a, c/a))
        self.assertEqual(Droite_equation(a, 0, 0).equation[1:],  (0, 0))
        self.assertEqual((Droite_equation(0, a, 0).equation[0], Droite_equation(0, a, 0).equation[2]),  (0, 0))
        self.assert_(not Droite_equation(0, 0, 0).existe)
        d = Droite_equation("y=-5/2x-3/2")
        self.assert_(Point(0, -1.5) in d)
        self.assert_(Point(-1, 1) in d)
        d = Droite_equation("x=2*10**2")
        self.assert_(Point(200, -1000) in d)
        self.assert_(Point(100, -1000) not in d)
        d = Droite_equation("2*x+2*y=1")
        self.assert_(Point(0.5, 0) in d)
        self.assert_(Point(1, -0.5) in d)

    def test_Bissectrice(self):
        A = Point(1, -5)
        B = Point(1.5, -5.3)
        C = Point(3, -4)
        d = Bissectrice(A,  B,  C)
        a, b, c = d.equation
        d,  e = (0.0870545184921, -1.03861105199)
        self.assertAlmostEqual(b/a, d)
        self.assertAlmostEqual(c/a, e)

    def test_Tangente(self):
        A = Point(4.75, -2.56887)
        O = Point(2.56874, -85.2541)
        M = Point(7.854, -552.444)
        c = Cercle(O, A)
        d = Tangente(c, A)
        self.assert_(A in d)
        self.assert_(M not in d)
        d1 = Tangente(c, M)
        self.assert_(M in d1)
        self.assert_(A not in d1)
        self.assert_(not  Tangente(c, O).existe)





if __name__ == '__main__':
    unittest.main()
