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

from customtest import *







class TestGeolibCercles(CustomTest):

    def test_Arc_cercle(self):
        A = Point(-1.2561, 45.236)
        B = Point(251.2561, 41.256)
        O = Point(-42.25, 2.351)
        a = Arc_cercle(O, A, B)
        self.assert_(isinstance(a.etiquette, Label_arc_cercle))
        self.assert_(A in a)
        self.assert_(B not in a)
        self.assert_(O not in a)


    def test_Arc_points(self):
        A = Point(-1.2561, 45.236)
        B = Point(251.2561, 41.256)
        C = Point(-42.25, 2.351)
        a = Arc_points(A, B, C)
        self.assert_(A in a)
        self.assert_(B in a)
        self.assert_(C in a)
        self.assertAlmostEqual(a.longueur, 1208.25931027)
        self.assertAlmostEqual(a.centre.coordonnees, (122.51970100911581, -114.11725498641933))

    def test_Arc_oriente(self):
        A = Point(-1.2561, 45.236)
        B = Point(251.2561, 41.256)
        C = Point(-42.25, 2.351)
        a = Arc_oriente(A, B, C)
        self.assert_(A in a)
        self.assert_(B in a)
        self.assert_(C in a)
        self.assertAlmostEqual(a.longueur, 1208.25931027)
        self.assertAlmostEqual(a.centre.coordonnees, (122.51970100911581, -114.11725498641933))
        self.assert_(a.sens == "indirect")

    def test_Demicercle(self):
        A = Point(-1.2561, 45.236)
        B = Point(251.2561, 41.256)
        c = Demicercle(A, B)
        self.assertAlmostEqual(Milieu(A, B).coordonnees, c.centre.coordonnees)
        self.assertAlmostEqual(c.diametre, Segment(A, B).longueur)
        self.assertAlmostEqual(c.rayon, Segment(A, B).longueur/2)

    def test_Cercle_rayon(self):
        A = Point(-2.586, 7.541)
        c_1 = Cercle_rayon(A, -1)
        c0 = Cercle_rayon(A, 0)
        c2 = Cercle_rayon(A, 2)
        self.assert_(isinstance(c_1.etiquette, Label_cercle))
        self.assert_(not c_1.existe)
        self.assert_(c0.existe)
        self.assert_(c2.existe)
        self.assertAlmostEqual(A.coordonnees, c0.centre.coordonnees)
        self.assert_(A not in c_1)
        self.assert_(A in c0)
        self.assert_(A not in c2)
        self.assertEqual(c_1.rayon,  -1)
        self.assertEqual(c0.rayon,  0)
        self.assertEqual(c2.rayon,  2)
        k = random()
        B = Point(A.x + 2*math.sin(k),  A.y + 2*math.cos(k))
        self.assert_(B in c2)
 #        c = Cercle("x^2+y^2-2x+y-3=0")

    def test_Cercle(self):
        A = Point(2.78841, -5.25)
        O = Point(27.8841, -0.525)
        c = Cercle(O, A)
        self.assert_(c.centre is O)
        self.assert_(c.point is A)
        self.assertAlmostEqual(c.rayon, 25.5366262763)
       # Test du typage dynamique
        c = Cercle(Point(1, 1), 1)
        self.assert_(Point(1, 2) in c)
        c = Cercle(Point(1, 1), Point(0, 2), Point(-1, 1))
        self.assert_(Point(0, 0) in c)
        self.assertAlmostEqual(c.centre.coordonnees, (0, 1))

    def test_Cercle_diametre(self):
        A = Point(2.78841, -5.25)
        B = Point(27.8841, -0.525)
        c = Cercle_diametre(A, B)
        c1 = Cercle(A, B)
        self.assertAlmostEqual(c.diametre, c1.rayon)
        self.assertAlmostEqual(c.diametre, Segment(A, B).longueur)
        self.assertAlmostEqual(c.centre.coordonnees, Milieu(A, B).coordonnees)

    def test_Cercle_points(self):
        A = Point(2.78841, -5.25)
        B = Point(27.8841, -0.525)
        C = Point(-42.25, 2.351)
        c = Cercle_points(A, B, C)
        self.assert_(c.centre in Mediatrice(Segment(A, B)))
        self.assert_(c.centre in Mediatrice(Segment(A, C)))


    def test_Cercle_equation(self):
        c = Cercle_equation()
        self.assertAlmostEqual(c.centre.coordonnees, (0, 0))
        self.assert_(c.rayon == 1)
        c.a = -2
        c.b = 4
        c.c = -4
        self.assertAlmostEqual(c.centre.coordonnees, (1, -2))
        self.assert_(c.rayon == 3)
        self.assert_(c.existe)
        c.c = 6
        self.assert_(not c.existe)


    def test_Disque(self):
        A = Point(2.78841, -5.25)
        B = Point(27.8841, -0.525)
        C = Point(-42.25, 2.351)
        c = Cercle_points(A, B, C)
        d = Disque(c)
        self.assertAlmostEqual(d.centre.coordonnees, c.centre.coordonnees)
        self.assertAlmostEqual(d.rayon, c.rayon)
        self.assert_(A in d)
        self.assert_(B in d)
        self.assert_(C in d)
        self.assert_(d.centre in d)
        self.assert_(Point(-500, -500) not in d)


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