week3 code1


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)));
  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容