Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // simple and fast Voronoi diagram with Hoff's algorithm
- float Rcone;
- int N = 70; // Defines N\
- Cell cell[] = new Cell[N];
- float t = 0;
- int B = 0; // Variable for color change
- /////////////////////////////////////////////////
- int numDataPoints = 100000; // Ammount of data points that can be collected
- int dataIndex = 1;
- String[] dataStrings = new String[numDataPoints]; // Save up to 10k values
- // loads a library needed to establish a connection to a serial device
- // in our case, the serial device is a Circuit Playground
- import processing.serial.*; // Imports the serial device
- // create a new array that will hold on to the sensor values
- // from the Circuit Playground
- float[] portValues = new float[8];
- // create a new serial connection
- Serial myPort; // Sets up the serial connection
- // Create a string that will hold onto all the sensor values
- // we will take this string and break it up into 8 parts in
- // other parts of the code
- String inString;
- //////////////////////////////////////////////
- void setup() // This sets up the sketch
- {
- size(700, 700, P3D); // Size of the background
- colorMode(HSB); // Defines the mode of the colors that are going to be used
- Rcone = dist(0, 0, width, height);
- ortho(-width / 2, width / 2, -height / 2, height / 2, 0, Rcone); // Sets up the width and the height
- float vm = 3;
- for (int i = 1; i < N; i++) {
- cell[i] = new Cell(
- random(width), random(height), // Makes it so that the width and the height of the cell change randomly...
- random(-vm, vm), random(-vm, vm),
- 255 * (0.4 * float(i) / N + 0.3 ),5
- ); //...
- }
- cell[0] = new Mover(width/2, height/2, 0, 0, B, 10); // Mover color
- //if(portValues[4] == 1){
- // B = 1;
- // //cell[0].changeHue(B);
- // println("button pressed");
- //} else {
- // B = 127;
- // //cell[0].changeHue(B);
- //}
- //////////////
- myPort = new Serial(this, "/dev/cu.usbmodem1421", 9600); // Adds port for Arduino
- for(int i = 0; i<8; i++)
- {
- portValues[i] = 0;
- }
- dataStrings[0] = "x,y,z,leftButton,rightButton";
- // Sets up use of x,y,z,leftButton, and rightButton
- }
- String buildDataString(float[] v) {
- String result = "";
- for(int i = 0; i<v.length-1; i++) {
- result += str(v[i]) + ",";
- }
- result += str(v[7]);
- return result;
- }
- ////////////////////////
- void draw() { // This is the part that shows up when it runs
- background(0.7); // Makes the background
- if(portValues[4] == 1){ // Defines what port value you are wanting to use
- B = 1; // Defines the use of the value B
- cell[0].changeHue(B);
- //println("button pressed");
- } else {
- B = 127;
- cell[0].changeHue(B); // Adjusts the color
- }
- for (int i = 0; i < N; i++) {
- cell[i].show(); // Shows the cell
- cell[i].update(); // Provides an update
- }
- t += 0.02;
- // this if statement makes sure that Processing is actually
- // reading data from the Circuit Playground BEFORE it runs the function
- // processSensorValues()
- if (inString != null) {
- portValues = processSensorValues(inString); // get data
- // manage data points
- dataIndex++;
- if(dataIndex > numDataPoints - 1) {
- dataIndex = 1;
- }
- dataStrings[dataIndex] = buildDataString(portValues);
- saveStrings("values.csv",dataStrings); // Saves data
- text(dataIndex,width-80,40); //Text shown
- }
- //println(inString);
- }
- //////////////////////////////////////////////////////////
- class Cell { // Defines the lass of the cell
- PVector r;
- PVector v;
- float hue;
- float ps;
- float eta = 0.02;
- float r0 = 60;
- float f0 = 0.002;
- float f1 = 0.002;
- Cell(float x0, float y0, float vx0, float vy0, float hue0, float ps0) {
- r = new PVector(x0,y0); // Vectors of which
- v = new PVector(vx0,vy0); // the cell is
- hue = hue0;
- ps = ps0;
- }
- void changeHue(float newHue) {
- hue = newHue; // Makes it so that the color of the main cell can be changed
- }
- void update() {
- r.add(v);
- PVector veta = new PVector(eta * v.x, eta * v.y);
- v.add(force());
- v.sub(veta);
- if (r.x < 0 || r.x > width) {
- v.x = -v.x;
- }
- if (r.y < 0 || r.y > height) {
- v.y = -v.y;
- }
- }
- PVector force(){ // Creates the new PVectors...
- PVector fsum = new PVector(0,0);
- PVector df = new PVector();
- for(int i = 1; i < N; i++){
- float d = distance(i);
- df.set(r.x-cell[i].r.x, r.y-cell[i].r.y);
- df.mult(f0*exp(-d/r0));
- fsum.add(df);
- }
- df.set(r.x-width/2, r.y-height/2);
- df.mult(-f1);
- fsum.add(df);
- float d = distance(0);
- df.set(r.x-cell[0].r.x, r.y-cell[0].r.y);
- df.mult(10*f0*exp(-d/r0));
- fsum.add(df);
- return fsum;
- } //...
- float distance(int i){
- //return r.dist(cell[i].r); // dist does Not work on OPP
- return sqrt( (r.x - cell[i].r.x)*(r.x - cell[i].r.x)+
- (r.y - cell[i].r.y)*(r.y - cell[i].r.y));
- }
- void show() { // What is shown
- pushMatrix();
- translate(r.x, r.y, 0);
- float n = 30;
- noStroke();
- //fill(hue,125 * v.magSq() + 100,255); // magSq does Not work on OPP
- fill(hue,125 * (v.x*v.x + v.y*v.y) + 100,255); // Changes the color of the sketch
- beginShape(TRIANGLE_FAN); // Beginning of the shape
- vertex(0, 0, 0);
- for (int i = 0; i <= n; i++) {
- float theta = i/(float)n * TWO_PI;
- vertex(Rcone * cos(theta), Rcone * sin(theta), -Rcone);
- }
- endShape(); // Ends the ellipse
- fill(0); // Makes collor of ellipse
- ellipse(0, 0, ps, ps); // Makes an ellipse
- popMatrix();
- }
- }
- float[] processSensorValues(String valString) { // Makes
- String[] temp = {"0", "0", "0", "0", "0", "0", "0", "0"}; // Code
- temp = split(valString,"\t"); // Show
- if(temp == null) { // Up
- for(int i = 0; i<8; i++) { // At
- temp[i] = "0"; // The
- }
- }
- float[] vals = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Bottom
- for(int i = 0; i<8; i++) // Of
- {
- if(temp != null) // The
- {
- vals[i] = float(temp[i]); // Screen
- }
- else // ...
- {
- vals[i] = 0; // ...
- }
- }
- return vals; // ...
- }
- // read new data from the Circuit Playground
- void serialEvent(Serial p) {
- inString = myPort.readStringUntil(10);
- }
- /////////////////////////////////////
- class Mover extends Cell{ // Defines the mover as the cell
- PVector df = new PVector(); // Sets up the depth, and each direction
- int x = width/2; // Controls the width and connects it to the x value
- int y = height/2; // Controls the height and connects it to the y value
- Mover(float x0, float y0, float vx0, float vy0, float hue0, float ps0) {
- super(x0,y0,vx0,vy0,hue0,ps0); // Sets up the direct and color of the main cell
- }
- void update() { // Updates the code
- r.add(v);
- df.set( noise(t)-0.5 , noise(t+100)-0.5 );
- df.mult(0.7);
- v.add(df);
- df.set(r.x-width/2, r.y-height/2);
- df.mult(-0.001);
- v.add(df);
- v.mult(0.97);
- if (r.x < 0 || r.x > width) { // If r.x is less than 0 or r.x is greater than the width, v.x = -v.x
- v.x = -v.x; // Previous line and v.x = -v.x
- }
- if (r.y < 0 || r.y > height) { // If r.y is less than 0 or r.y is greater than the height, v.y = -v.y
- v.y = -v.y; // Previous line and v.y = -v.y
- }
- // get the x value from the acceleromoter, use to move object horizontally
- float xa = map(portValues[1],-10,10,0,width); // Sets up use of the X axis
- // get the y value from the accelerometer, use to move object vertically
- float ya = map(portValues[0],-10,10,0,height); // Sets up use of the Y axis
- if(xa > x){x+=3;}
- else if( xa < x){x-=3;} // Allows you to move the cell around on the
- // x axis with the Arduino
- if(ya > y){ y += 3;}
- else if(ya < y){y -= 3;} // Allows you to move the cell around on the
- // y axis with the Arduino
- r.x =x; // X-axis
- r.y =y; // Y-axis
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement