# Regression2.py

from gpanel import *

samples = [[1, 2], [3, 3], [4, 5], [5, 6], [6, 5], [8, 7]]
a = 0.5
b = 3

def f(x):
    return a * x + b

def drawData():
    for sample in samples:
        pos(sample)
        fillCircle(0.07)
    
def drawLine():
    line(0, f(0), 10, f(10))
    
def showErrors():
    errorSum = 0
    setColor("red")
    for sample in samples:
        x = sample[0]
        y = sample[1]
        e = abs(y - f(x))
        errorSum += e
        line(x, y, x, f(x)) 
        text(x + 0.1, y, str(e))
    return errorSum  

makeGPanel(-1, 11, -1, 11, mousePressed = onMousePressed)
drawGrid(0, 10, 0, 10, "gray")
title("Data Points")
lineWidth(2)
drawData()
drawLine()
E = showErrors()
title("Error sum: " + str(E))
