I have been trying to add a restart button to this code but variables dont seem to work for me or maybe im doing it wrong. As of now i have tried to add:
void keyPressed() {
if(key == 'r') {
restart();
}
}
void restart() {
And then some of my variables
}
However i cannot figure it out, anyways here is my code
PImage img;
ArrayList<PVector> data;
float m = 1;
float b = 0;
void setup() {
img = loadImage("Cykelsti.jpg");
size(600, 600);
data = new ArrayList<PVector>();
}
void LinearRegression() {
float xsum = 0;
float ysum = 0;
for (int i = 0; i < data.size(); i++) {
PVector point = data.get(i);
xsum += point.x;
ysum += point.y;
}
float xmean = xsum / data.size();
float ymean = ysum / data.size();
float num = 0;
float den = 0;
for (int n = 0; n < data.size(); n++) {
PVector point = data.get(n);
float x = point.x;
float y = point.y;
num += (x - xmean) * (y - ymean);
den += (x - xmean) * (x - xmean);
}
m = num/den;
b = ymean - m * xmean;
}
void drawLine () {
float x1 = 0;
float y1 = m * x1 + b;
float x2 = width;
float y2 = m * x2 + b;
stroke(255, 0, 255);
line(x1, y1, x2, y2);
}
void mousePressed () {
PVector point = new PVector(mouseX, mouseY);
data.add(point);
}
void draw() {
background(51);
image(img, 0, 0, width, height);
for (int i = 0; i < data.size(); i++) {
PVector point = data.get(i);
fill(255, 0, 0);
stroke(255);
ellipse(point.x, point.y, 8, 8);
}
if (data.size() > 1) {
LinearRegression();
drawLine();
}
}