import numpy as np
import matplotlib.pyplot as plt


def create_distance_function(a, b, c):
    """ 0 = ax + by + c """

    def distance(x, y):
        """ returns tuple (d, pos)
            d is the distance
            If pos == -1 point is below the line,
            0 on the line and +1 if above the line
        """
        nom = a * x + b * y + c
        if nom == 0:
            pos = 0
        elif (nom < 0 and b < 0) or (nom > 0 and b > 0):
            pos = -1
        else:
            pos = 1
        return (np.absolute(nom) / np.sqrt(a ** 2 + b ** 2), pos)
# http://mathforum.org/library/drmath/view/55188.html
    return distance


points = [(3.5, 1.8), (1.1, 3.9)]
fig, ax = plt.subplots()
ax.set_xlabel("sweetness")
ax.set_ylabel("sourness")
ax.set_xlim([-1, 6])
ax.set_ylim([-1, 8])

X = np.arange(-0.5, 5, 0.1)
print(X)
colors = ["r", ""]  # for the samples

size = 10
for (index, (x, y)) in enumerate(points):
    if index == 0:
        ax.plot(x, y, "o",
                color="darkorange",
                markersize=size)
    else:
        ax.plot(x, y, "oy",
                markersize=size)
step = 0.05
for x in np.arange(0, 1 + step, step):
    slope = np.tan(np.arccos(x))
    dist4line1 = create_distance_function(slope, -1, 0)
    print("x: ", x, "slope: ", slope)
    Y = slope * X

    results = []
    for point in points:
        results.append(dist4line1(*point))
    print(slope, results)
    if (results[0][1] != results[1][1]):
        ax.plot(X, Y, "g-")
    else:
        ax.plot(X, Y, "r-")

plt.show()

###############################################################
###############################################################
###############################################################
###############################################################
# import numpy as np
# import matplotlib.pyplot as plt
#
# a = np.linspace(start=-5, stop=5,
#                 num=6, endpoint=True)
#
# print(a)
#
# print("Graphical Representation : \n",
#       np.absolute(a))
#
#
# plt.title("blue : with absolute\nred : without absolute")
# plt.plot(a, np.absolute(a))
#
# plt.plot(a, a, color='red')
# plt.show()