var x = []
var y = []
var num = 50;
var speedX = []
var speedY = []
function setup() {
createCanvas(1000, 1000);
for(var i=0;i<num;i++){
x[i] = random(0,width)
y[i] = random(0,height)
speedX[i] = random(-3,3)
speedY[i] = random(-3,3)
}
//noLoop();
}
function draw() {
background(220);
for(var i=0;i<num;i++){
x[i] = x[i] + speedX[i];
if(x[i] > width){
x[i] = x[i] - width
}
if(x[i] < 0){
x[i] = x[i] + width
}
if(y[i] > height){
y[i] = y[i] - height
}
if(y[i] < 0){
y[i] = y[i] + height
}
//var colour = color(random(0,255),random(0,255),random(0,255))
//fill(colour)
circle(x[i],y[i],10)
text(i,x[i],y[i])
}
for(var i=0;i<num-1;i++){
for(var j=0;j<i+1;j++){
var d = getDistance(x[i],y[i],x[j],y[j])
console.log('i ' + i + '+ j ' + j)
if(d < 180){
stroke("red")
line(x[i],y[i],x[j],y[j])
}
}
}
}
function getDistance(x1,y1,x2,y2){
//console.log('x1 y1 x2 y2 d='+sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
var x = []
var y = []
var num = 10;
var speedX = []
var speedY = []
function setup() {
createCanvas(1000, 1000);
for(var i=0;i<num;i++){
x[i] = random(0,width)
y[i] = random(0,height)
speedX[i] = random(-3, 3)
speedY[i] = random(-3, 2)
}
//noLoop();
}
function draw() {
background(220);
for(var i=0;i<num;i++){
x[i] = x[i] + speedX[i];
if(x[i] > width){
x[i] = x[i] - width
}
if(x[i] < 0){
x[i] = x[i] + width
}
if(y[i] > height){
y[i] = y[i] - height
}
if(y[i] < 0){
y[i] = y[i] + height
}
//var colour = color(random(0,255),random(0,255),random(0,255))
//fill(colour)
circle(x[i],y[i],1)
//text(i,x[i],y[i])
}
for(var i=0;i<num;i++){
for(var j=0;j<i+1;j++){
var d = dist(x[i],y[i],x[j],y[j])
//console.log('i ' + i + '+ j ' + j)
if(d < 500){
stroke("red")
line(x[i],y[i],x[j],y[j])
}
}
}
}
var grades = [
[2017, 68, 39],
[2018, 60, 52],
[2019, 34, 50],
[2018, 50, 51],
[2017, 70, 68],
[2019, 61, 50],
[2018, 71, 58],
[2018, 66, 70],
[2017, 69, 55],
[2018, 73, 56],
[2019, 82, 70],
[2017, 72, 54],
[2018, 62, 57],
[2019, 44, 6],
[2018, 73, 51],
[2017, 59, 25],
[2017, 59, 74],
[2018, 63, 57],
[2017, 70, 38],
[2017, 72, 75],
[2018, 77, 57],
[2017, 79, 67],
[2018, 70, 50],
[2018, 76, 68],
[2018, 55, 26],
[2017, 74, 69],
[2018, 54, 54],
[2018, 66, 63],
[2018, 72, 55],
[2017, 54, 50],
[2017, 79, 68],
[2018, 70, 73],
[2019, 55, 38],
[2018, 62, 5],
[2017, 59, 22],
[2017, 82, 60],
[2017, 64, 53],
[2017, 71, 56],
[2018, 90, 83],
[2018, 73, 73],
[2017, 70, 49],
[2017, 75, 71],
[2017, 46, 34],
[2017, 72, 53],
[2017, 66, 65],
[2017, 68, 63],
[2017, 82, 65],
[2017, 76, 77],
[2017, 72, 60],
[2017, 59, 65],
[2018, 71, 56],
[2018, 68, 65],
[2019, 49, 48],
[2018, 51, 55],
[2018, 55, 45],
[2019, 68, 46],
[2018, 69, 69],
[2019, 78, 54],
[2020, 59, 50],
[2019, 74, 46],
[2018, 73, 0],
[2018, 75, 48],
[2019, 67, 44],
[2018, 70, 60],
[2018, 78, 56],
[2018, 54, 50],
[2019, 65, 47],
[2018, 80, 63],
[2019, 83, 58],
[2018, 83, 75],
[2019, 51, 52],
[2018, 85, 67],
[2019, 78, 59],
[2019, 79, 52],
[2019, 60, 24],
[2019, 72, 54],
[2019, 79, 61],
[2018, 63, 0],
[2019, 52, 49],
[2018, 80, 57],
[2018, 73, 57],
[2019, 56, 8],
[2018, 60, 51],
[2019, 87, 50],
[2020, 65, 0],
[2018, 58, 74],
[2018, 56, 8],
[2019, 87, 60],
[2020, 69, 57],
[2020, 69, 60],
[2019, 91, 85],
[2019, 72, 60],
[2020, 63, 0],
[2020, 61, 60],
[2021, 74, 56],
[2020, 61, 54],
[2019, 66, 0],
[2019, 89, 71],
[2020, 78, 50],
[2021, 25, 33],
[2019, 75, 77],
[2019, 84, 57],
[2020, 54, 50],
[2022, 86, 0],
[2020, 66, 70],
[2020, 48, 31],
[2019, 74, 71],
[2019, 69, 16],
[2019, 71, 69],
[2021, 61, 4],
[2020, 45, 50],
[2020, 63, 4],
[2020, 73, 64],
[2020, 80, 65],
[2020, 47, 42],
[2020, 67, 39],
[2021, 85, 77],
[2020, 79, 50],
[2020, 60, 50],
[2021, 74, 57],
[2022, 45, 50],
[2020, 69, 20],
[2020, 71, 55],
[2021, 66, 58],
[2020, 71, 60],
[2020, 67, 55],
[2020, 75, 67],
[2020, 89, 69],
[2022, 57, 63],
[2021, 46, 59],
[2021, 73, 71],
[2021, 79, 63],
[2022, 41, 8],
[2021, 20, 0],
[2021, 55, 52],
[2021, 68, 62],
[2021, 50, 0],
[2022, 58, 50],
[2021, 56, 50],
[2022, 51, 50],
[2021, 69, 62],
[2023, 32, 48],
[2021, 68, 0],
[2021, 92, 64],
[2021, 50, 0],
[2021, 88, 69],
[2022, 51, 29],
[2021, 57, 46],
[2022, 38, 46],
[2021, 62, 11],
[2022, 75, 55],
[2023, 28, 23],
[2021, 52, 47],
[2022, 91, 83],
[2023, 51, 64],
[2023, 58, 69],
[2023, 67, 39],
[2022, 66, 59],
[2023, 55, 61],
[2023, 54, 53],
[2022, 55, 56],
[2022, 47, 0],
[2022, 54, 75],
[2023, 79, 61],
[2023, 31, 0],
[2023, 71, 72],
[2023, 66, 62],
[2022, 87, 50],
[2022, 51, 36],
[2023, 78, 73]
];
function setup() {
createCanvas(600, 600);
noLoop();
}
function draw() {
background(220);
var sumY = 0;
var sumX = 0;
var sumXY = 0;
var sumXsquared = 0;
var n = grades.length;
var scaleX = width/100;
var scaleY = height/100;
fill(0);
for (var i = 0; i < grades.length; i++)
{
var x = grades[i][1] * scaleX;
var y = height - grades[i][2] * scaleY;
circle(x, y, 5);
sumY += grades[i][2];
sumX += grades[i][1];
sumXY += (grades[i][1] * grades[i][2]);
sumXsquared += grades[i][1] * grades[i][1];
}
var a = (sumY * sumXsquared - sumX * sumXY) / (n*(sumXsquared) - (sumX * sumX));
var b = (n * sumXY - sumX * sumY) / (n*(sumXsquared) - (sumX * sumX));
console.log("a: " + a + " b: " + b);
// y = a + bx
var yAtZero = a;
var yAt100 = a + b * 100;
stroke(0,100,200);
line(0, height-(yAtZero*scaleY), 100 * scaleX, height-(yAt100*scaleY));
stroke(10, 200, 40);
var yAverage = sumY / n;
var RSS = 0;
var TSS = 0;
for (var i = 0; i < grades.length; i++)
{
var expectedY = a + b * grades[i][1];
line(grades[i][1]*scaleX, height-grades[i][2] * scaleY, grades[i][1]*scaleX, height - expectedY * scaleY);
console.log("for x : " + grades[i][1] + " our y value is : " + grades[i][2] + " but based on our equation, we expect: " + expectedY + " the diference is " + (grades[i][2] - expectedY));
RSS += (grades[i][2] - expectedY) * (grades[i][2] - expectedY);
TSS += (grades[i][2] - yAverage) * (grades[i][2] - yAverage);
}
console.log("RSS: " + RSS);
console.log("TSS: " + TSS);
console.log("RSS/TSS: " + (RSS/TSS));
console.log("Rsquared: " + (1 - (RSS/TSS)));
}