//snake shape float xpos, ypos; // Starting position of shape float xspeed = 12; // Speed of the shape float yspeed = 11.4; // Speed of the shape float xdirection = 1; // Left or Right float ydirection = 1; // Top to Bottom //1st cluster float xth1,yth1; //x and y theta float xth_vel1,yth_vel1; //x and y velocity //2nd cluster float xth2,yth2; //x,y theta float xth_vel2,yth_vel2; //x,y velocity //3rd cluster float xth3,yth3; //x,y theta float xth_vel3,yth_vel3; //x,y velocity //4th cluster float xth4,yth4; //x,y theta float xth_vel4,yth_vel4; //x,y velocity //5th cluster float xth5,yth5; //x,y theta float xth_vel5,yth_vel5; //x,y velocity void setup() { size(800, 600); background(5); framerate(30); smooth(); // Set the starting position of the snake shape to center xpos = width/2; ypos = height/2; //1st cluster xth1 = yth1 = 0.0; //theta xth_vel1 = 0.01; //x vel yth_vel1 = 0.0132; //y vel //2nd cluster xth2 = yth2 = 0.0; //theta xth_vel2 = 0.03; //x vel yth_vel2 = 0.0332; //y vel //3rd cluster xth3 = yth3 = 0.0; //theta xth_vel3 = 0.02; //x vel yth_vel3 = 0.0232; //y vel //4th cluster xth4 = yth4 = 0.0; //theta xth_vel4 = 0.02; //x vel yth_vel4 = 0.0232; //y vel //5th cluster xth5 = yth5 = 0.0; //theta xth_vel5 = 0.01; //x vel yth_vel5 = 0.0132; //y vel } void draw() { //alpha fade fill(5,20); noStroke(); rect(0,0,width,height); // Update the position of the snake shape xpos = xpos + ( xspeed * random(.2,xdirection)); ypos = ypos + ( yspeed * random(.3,ydirection) ); //1st cluster float xx = random(150,180); //set starting x float yy = random(130,150); //set starting y xx += random(50.0,90.0) * cos(xth1); //determine motion of x xth1 += xth_vel1; //set velocity x yy += random(50.0,90.0) * sin(yth1); //determine motion of y yth1 += yth_vel1; //set velocity y //2nd cluster float xxx = 600; float yyy = 160; xxx += random(60.0,100.0) * sin(xth2); xth2 += xth_vel2; yyy += random(60.0,90.0) * cos(yth2); yth2 += yth_vel2; //3rd cluster float xxxx = random(530,560); float yyyy = random(480,510); xxxx += random(80.0,115.0) * sin(xth3); xth3 += xth_vel3; yyyy += random(70.0,105.0) * sin(yth3); yth3 += yth_vel3; //4th cluster float xxxxx = 230; float yyyyy = 400; xxxxx += random(60.0,95.0) * cos(xth4); xth4 += xth_vel4; yyyyy += random(60.0,100.0) * cos(yth4); yth4 += yth_vel4; //5th cluster float x6 = 400; float y6 = random(220,260); x6 += random(60.0,75.0) * sin(xth5); xth5 += xth_vel5; y6 += random(60.0,70.0) * cos(yth5); yth5 += yth_vel5; // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 if (xpos > width-20 || xpos < 18) { xspeed *= -1; } if (ypos > height-20 || ypos < 18) { yspeed *= -1; } // if it goes out of the boundaries of the screen , reset snake shape to center if ( xpos < -20 || xpos > width + 20) { xpos = ypos = width/2; } if ( ypos < -20 || ypos > height + 20) { ypos = xpos = height/2; } //1st cluster parameters, if distance of position of snake shape to 1st cluster <35 //than reverse direction float d = dist(xpos,ypos,xx,yy); if ( d <35.0) { xspeed *= -1; println("d +"); } //2nd cluster " " " float dd = dist(xpos,ypos,xxx,yyy); if ( dd < 35.0) { xspeed *= -1; println("dd +"); } //3rd cluster " " " " float ddd = dist(xpos,ypos,xxxx,yyyy); if ( ddd < 35.0) { xspeed *= -1; println("ddd +"); } //4th cluster "" "" " float dddd = dist(xpos,ypos,xxxxx,yyyyy); if ( dddd < 35.0) { xspeed *= -1; println("dddd +"); } //5th cluster "" "" " float ddddd = dist(xpos,ypos,x6,y6); if ( ddddd < 35.0) { xspeed *= -1; println("ddddd +"); } ellipseMode(CENTER); float alpha = mouseX/3.5; //determine fill colors float alpha1 = mouseY/3; //determine fill colors stroke(255,150); // draw snake shape fill(29,alpha,alpha1,230); ellipse(xpos,ypos,20,20); //draw clusters with different fills fill(alpha,alpha1,0,230); //fill for 1,3,5 ellipse(xx,yy,20,20); //1st ellipse(xxxx,yyyy,20,20); //3rd ellipse(x6,y6,20,20); //5th fill (alpha/3,alpha1/2,0,230); //fill for 2and 4 ellipse(xxx,yyy,20,20); //2nd ellipse(xxxxx,yyyyy,20,20); //4th }