import matplotlib.pyplot as plt
plt.rc('text', usetex=True)


def nombres_espaces_regulierement(deb, fin, pas):
    """Fonction pour éviter d'utiliser la bibliothèque numpy et sa
    fonction linspace.

    La différence étant qu'ici on renseigne le pas et non le nombre
    de points souhaités.

    """
    liste_nbres = []
    x = deb
    for i in range(int(1 + (fin - deb) / pas)):
        liste_nbres.append(x)
        x += pas
    return liste_nbres


def produit_scalaire(u, v):
    """Fonction qui ... tout est dans le titre, non ?"""
    return sum([a * b for a, b in zip(u, v)])


def vecteur(A, B):
    """Fonction de calcul des coords d'un vecteur

    En entréé deux tuples, on renvoie un tuple

    """
    return tuple([B[i]-A[i] for i in range(len(A))])


A = (-1, -1)
B = (1, 1)

# la figure et la sousfigure ('axes' pour matplotlib)
f = plt.figure(figsize=(10, 10))
ax = f.add_subplot(111, aspect='equal')

lim = 2.5
pas = .08
ax.set_xlim([-lim, lim])
ax.set_ylim([-lim, lim])
ax.axis("off")
X = Y = nombres_espaces_regulierement(-lim, lim, pas)

# on boucle sur les points choisis du plan 
for x in X:
    for y in Y:
        M = (x, y)
        p = produit_scalaire(vecteur(M, A), vecteur(M, B)) 
        if  p > 0:
            couleur = "green"
            alpha = .5
        elif p < 0:
            couleur = "red"
            alpha = 1
        else:
            couleur = "yellow"
            alpha = 1
        plt.scatter(x, y, color=couleur, alpha=alpha)

for point in [A, B]:
    plt.scatter(*point, color="black")

# Étiquettes des points A et B
ax.text(A[0], A[1]-.3,
        s=r"\Huge\boldmath$A$",
        horizontalalignment='center')
ax.text(B[0], B[1]+.1,
        s=r"\Huge\boldmath$B$",
        horizontalalignment='center')
# Légende
ax.text(0, -2.8,
        s=r"\Huge\textbf{Signe de }\boldmath$\overrightarrow{MA}\cdot\overrightarrow{MB}$",
        horizontalalignment='center'
)

plt.show()
