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

from customtest import *

import geolib

class TestGeolibAngles(CustomTest):

    def test_Secteur_angulaire(self):
        u = Vecteur_libre(5.458, -2.546)
        v = Vecteur_libre(-5.75, 12.6)
        P = Point(2.54, -5.68)
        a = Secteur_angulaire(P, u, v)
        self.assert_(isinstance(a.etiquette, Label_angle))
        self.assertAlmostEqual(a.val, 2.43538435941)
        self.assertAlmostEqual(a.degre, 139.537245287)

    def test_Angle_oriente(self):
        A = Point(250.54, 612.78)
        B = Point(115.54, 168.24)
        C = Point(412.78, -254.23)
        a = Angle_oriente(A, B, C)
        self.assertAlmostEqual(a.rad, -2.2336365048)

    def test_Angle(self):
        A = Point(250.54, 612.78)
        B = Point(115.54, 168.24)
        C = Point(412.78, -254.23)
        a = Angle(A, B, C)
        self.assertAlmostEqual(a.rad, +2.2336365048)

    def test_Angle_libre(self):
        x = random()
        a = Angle_libre(x)
        self.assertAlmostEqual(a.deg, x*180/math.pi)
        self.assertAlmostEqual(a.grad, x*200/math.pi)
        self.assertAlmostEqual(a.rad, x)
        self.assertAlmostEqual(a.val, x)
        self.assertAlmostEqual(math.sin(a.val), math.sin(x))
        self.assertAlmostEqual(math.cos(a.val), math.cos(x))
        self.assertAlmostEqual(math.tan(a.val), math.tan(x))
        y = x*180/math.pi
        a = Angle_libre(y, u"°")
        self.assertAlmostEqual(a.deg, y)
        self.assertAlmostEqual(a.grad, x*200/math.pi)
        self.assertAlmostEqual(a.rad, x)
        self.assertNotAlmostEqual(a.val, a.deg)
        sin = univ.sin
        cos = univ.cos
        tan = univ.tan
        self.assertAlmostEqual(sin(a), math.sin(x))
        self.assertAlmostEqual(cos(a), math.cos(x))
        self.assertAlmostEqual(tan(a), math.tan(x))
        a.unite = "g"
        self.assertNotAlmostEqual(a.val, a.grad)
        b = Angle_libre(u"45°")
        self.assertAlmostEqual(b.rad, math.pi/4)
##        print "%% FEUILLE %%"
##        f = geolib.Feuille()
##        print f, type(f)
##        p = Point(40, 20)
##        print p,  type(p), id(p)
##        f.objets.A = p
##        print f.objets.A, type(f.objets.A), id(f.objets.A)
##        v = Variable("A.x+5")
##        print v, type(v)
##        f.objets.k = v
##        f.objets.c = Angle_libre(f.objets.k, "d")
##        self.assertAlmostEqual(b.rad, c.rad)
        f = geolib.Feuille()
        f.objets.A = geolib.Point(40, 20)
        f.objets.k = geolib.Variable("A.x+5")
        f.objets.c = geolib.Angle_libre(f.objets.k, "d")
        f.objets.d = geolib.Angle_libre("A.x+5", "d")
        self.assert_(f.objets.c.rad is not None)
        self.assertAlmostEqual(b.rad, f.objets.c.rad)
        self.assertAlmostEqual(f.objets.d.rad, f.objets.c.rad)


    def test_Angle_vectoriel(self):
        u = Vecteur_libre(5.458, -2.546)
        v = Vecteur_libre(-5.75, 12.6)
        a = Angle_vectoriel(u, v)
        self.assertAlmostEqual(a.val, 2.43538435941)



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