美文网首页
week3 code1

week3 code1

作者: 提莫队长1234 | 来源:发表于2023-08-16 09:39 被阅读0次
    
    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])
          }
        }
      }
      
      
    }
    

    相关文章

      网友评论

          本文标题:week3 code1

          本文链接:https://www.haomeiwen.com/subject/iygopdtx.html