import turtle
from math import cos,sin,pi

def dessine(n,d,legende):
    theta = n * pi / d
    turtle.setup(500, 500)
    turtle.speed("fast")
    turtle.up()
    turtle.goto(0,-200)
    turtle.down()
    turtle.title("Positionnement de l'angle") 
    turtle.bgcolor("white") 
    turtle.hideturtle()
    turtle.circle(200)
    turtle.up()
    turtle.goto(-200,0)
    turtle.down()
    turtle.goto(200,0)
    turtle.up()
    turtle.goto(0,-200)
    turtle.down()
    turtle.goto(0,200)
    turtle.up()
    turtle.goto(200*cos(theta),200*sin(theta))
    turtle.down()
    turtle.dot(10,"red")
    turtle.up()
    turtle.goto(220*cos(theta),220*sin(theta))
    turtle.down()
    turtle.write(legende)
    turtle.exitonclick()

def calculs(a,b):
    q = a // b
    r = a % b
    if q % 2 == 0:
        if r == 1:
            return (1,b)
        else:
            return (r,b)
    else:
        c = r - b
        if c == 1:
            return (1,b)
        elif c == -1:
            return (-1,b)
        else:
            return (c,b)

def affiche(n,d):
    if n == d:
        return "PI"
    elif n == 1:
        return "PI / {}".format(d)
    elif n == -1:
        return "-PI / {}".format(d)
    elif n == -d:
        return "-PI"
    elif n == 0:
        return "0"
    else:
        if n % d == 0:
            return "{}PI".format(n//d)
        else:
            return "{}PI / {}".format(n,d)

def mesure_principale(a,b):
    if b == 0:
        return "NULL (le dénominateur doit être différent de 0)"
    elif a == 0:
        return "0"
    return calculs(a,b)

print("Détermination de la mesure principale d'un angle s'écrivant aPI/b")
a = int(input("Valeur de 'a' : "))
b = int(input("Valeur de 'b' : "))

n,d = mesure_principale(a,b)
r = affiche(n,d)
print("La mesure principale de {}PI/{} est : {}".format(a,b,r))

dessine(n,d,r)