Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. // simple and fast Voronoi diagram with Hoff's algorithm
  2.  
  3. float Rcone;
  4. int N = 70; // Defines N\
  5. Cell cell[] = new Cell[N];
  6. float t = 0;
  7.  
  8. int B = 0; // Variable for color change
  9.  
  10. /////////////////////////////////////////////////
  11. int numDataPoints = 100000; // Ammount of data points that can be collected
  12. int dataIndex = 1;
  13. String[] dataStrings = new String[numDataPoints]; // Save up to 10k values
  14.  
  15. // loads a library needed to establish a connection to a serial device
  16. // in our case, the serial device is a Circuit Playground
  17. import processing.serial.*; // Imports the serial device
  18.  
  19. // create a new array that will hold on to the sensor values
  20. // from the Circuit Playground
  21. float[] portValues = new float[8];
  22.  
  23. // create a new serial connection
  24. Serial myPort; // Sets up the serial connection
  25.  
  26. // Create a string that will hold onto all the sensor values
  27. // we will take this string and break it up into 8 parts in
  28. // other parts of the code
  29. String inString;
  30.  
  31. //////////////////////////////////////////////
  32. void setup() // This sets up the sketch
  33. {
  34. size(700, 700, P3D); // Size of the background
  35.  
  36. colorMode(HSB); // Defines the mode of the colors that are going to be used
  37. Rcone = dist(0, 0, width, height);
  38. ortho(-width / 2, width / 2, -height / 2, height / 2, 0, Rcone); // Sets up the width and the height
  39. float vm = 3;
  40. for (int i = 1; i < N; i++) {
  41. cell[i] = new Cell(
  42. random(width), random(height), // Makes it so that the width and the height of the cell change randomly...
  43. random(-vm, vm), random(-vm, vm),
  44. 255 * (0.4 * float(i) / N + 0.3 ),5
  45. ); //...
  46. }
  47.  
  48. cell[0] = new Mover(width/2, height/2, 0, 0, B, 10); // Mover color
  49.  
  50. //if(portValues[4] == 1){
  51. // B = 1;
  52. // //cell[0].changeHue(B);
  53. // println("button pressed");
  54. //} else {
  55. // B = 127;
  56. // //cell[0].changeHue(B);
  57. //}
  58. //////////////
  59. myPort = new Serial(this, "/dev/cu.usbmodem1421", 9600); // Adds port for Arduino
  60. for(int i = 0; i<8; i++)
  61. {
  62. portValues[i] = 0;
  63. }
  64. dataStrings[0] = "x,y,z,leftButton,rightButton";
  65. // Sets up use of x,y,z,leftButton, and rightButton
  66. }
  67.  
  68. String buildDataString(float[] v) {
  69. String result = "";
  70. for(int i = 0; i<v.length-1; i++) {
  71. result += str(v[i]) + ",";
  72. }
  73. result += str(v[7]);
  74. return result;
  75. }
  76. ////////////////////////
  77. void draw() { // This is the part that shows up when it runs
  78. background(0.7); // Makes the background
  79.  
  80. if(portValues[4] == 1){ // Defines what port value you are wanting to use
  81. B = 1; // Defines the use of the value B
  82. cell[0].changeHue(B);
  83. //println("button pressed");
  84. } else {
  85. B = 127;
  86. cell[0].changeHue(B); // Adjusts the color
  87. }
  88.  
  89. for (int i = 0; i < N; i++) {
  90. cell[i].show(); // Shows the cell
  91. cell[i].update(); // Provides an update
  92. }
  93. t += 0.02;
  94.  
  95. // this if statement makes sure that Processing is actually
  96. // reading data from the Circuit Playground BEFORE it runs the function
  97. // processSensorValues()
  98. if (inString != null) {
  99. portValues = processSensorValues(inString); // get data
  100. // manage data points
  101. dataIndex++;
  102. if(dataIndex > numDataPoints - 1) {
  103. dataIndex = 1;
  104. }
  105. dataStrings[dataIndex] = buildDataString(portValues);
  106. saveStrings("values.csv",dataStrings); // Saves data
  107. text(dataIndex,width-80,40); //Text shown
  108. }
  109.  
  110.  
  111.  
  112. //println(inString);
  113. }
  114. //////////////////////////////////////////////////////////
  115. class Cell { // Defines the lass of the cell
  116. PVector r;
  117. PVector v;
  118. float hue;
  119. float ps;
  120. float eta = 0.02;
  121. float r0 = 60;
  122. float f0 = 0.002;
  123. float f1 = 0.002;
  124.  
  125. Cell(float x0, float y0, float vx0, float vy0, float hue0, float ps0) {
  126. r = new PVector(x0,y0); // Vectors of which
  127. v = new PVector(vx0,vy0); // the cell is
  128. hue = hue0;
  129. ps = ps0;
  130. }
  131.  
  132. void changeHue(float newHue) {
  133. hue = newHue; // Makes it so that the color of the main cell can be changed
  134. }
  135.  
  136. void update() {
  137. r.add(v);
  138. PVector veta = new PVector(eta * v.x, eta * v.y);
  139. v.add(force());
  140. v.sub(veta);
  141.  
  142. if (r.x < 0 || r.x > width) {
  143. v.x = -v.x;
  144. }
  145. if (r.y < 0 || r.y > height) {
  146. v.y = -v.y;
  147. }
  148. }
  149.  
  150. PVector force(){ // Creates the new PVectors...
  151. PVector fsum = new PVector(0,0);
  152. PVector df = new PVector();
  153. for(int i = 1; i < N; i++){
  154. float d = distance(i);
  155. df.set(r.x-cell[i].r.x, r.y-cell[i].r.y);
  156. df.mult(f0*exp(-d/r0));
  157. fsum.add(df);
  158. }
  159. df.set(r.x-width/2, r.y-height/2);
  160. df.mult(-f1);
  161. fsum.add(df);
  162. float d = distance(0);
  163. df.set(r.x-cell[0].r.x, r.y-cell[0].r.y);
  164. df.mult(10*f0*exp(-d/r0));
  165. fsum.add(df);
  166. return fsum;
  167. } //...
  168.  
  169. float distance(int i){
  170. //return r.dist(cell[i].r); // dist does Not work on OPP
  171. return sqrt( (r.x - cell[i].r.x)*(r.x - cell[i].r.x)+
  172. (r.y - cell[i].r.y)*(r.y - cell[i].r.y));
  173.  
  174. }
  175.  
  176. void show() { // What is shown
  177. pushMatrix();
  178. translate(r.x, r.y, 0);
  179. float n = 30;
  180. noStroke();
  181. //fill(hue,125 * v.magSq() + 100,255); // magSq does Not work on OPP
  182. fill(hue,125 * (v.x*v.x + v.y*v.y) + 100,255); // Changes the color of the sketch
  183. beginShape(TRIANGLE_FAN); // Beginning of the shape
  184. vertex(0, 0, 0);
  185. for (int i = 0; i <= n; i++) {
  186. float theta = i/(float)n * TWO_PI;
  187. vertex(Rcone * cos(theta), Rcone * sin(theta), -Rcone);
  188. }
  189. endShape(); // Ends the ellipse
  190. fill(0); // Makes collor of ellipse
  191. ellipse(0, 0, ps, ps); // Makes an ellipse
  192. popMatrix();
  193. }
  194. }
  195.  
  196.  
  197. float[] processSensorValues(String valString) { // Makes
  198.  
  199. String[] temp = {"0", "0", "0", "0", "0", "0", "0", "0"}; // Code
  200.  
  201. temp = split(valString,"\t"); // Show
  202.  
  203. if(temp == null) { // Up
  204. for(int i = 0; i<8; i++) { // At
  205. temp[i] = "0"; // The
  206. }
  207. }
  208.  
  209. float[] vals = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Bottom
  210. for(int i = 0; i<8; i++) // Of
  211. {
  212. if(temp != null) // The
  213. {
  214. vals[i] = float(temp[i]); // Screen
  215. }
  216.  
  217. else // ...
  218. {
  219. vals[i] = 0; // ...
  220. }
  221.  
  222. }
  223. return vals; // ...
  224. }
  225.  
  226. // read new data from the Circuit Playground
  227. void serialEvent(Serial p) {
  228. inString = myPort.readStringUntil(10);
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235. /////////////////////////////////////
  236. class Mover extends Cell{ // Defines the mover as the cell
  237. PVector df = new PVector(); // Sets up the depth, and each direction
  238.  
  239. int x = width/2; // Controls the width and connects it to the x value
  240. int y = height/2; // Controls the height and connects it to the y value
  241.  
  242. Mover(float x0, float y0, float vx0, float vy0, float hue0, float ps0) {
  243. super(x0,y0,vx0,vy0,hue0,ps0); // Sets up the direct and color of the main cell
  244. }
  245. void update() { // Updates the code
  246.  
  247. r.add(v);
  248. df.set( noise(t)-0.5 , noise(t+100)-0.5 );
  249. df.mult(0.7);
  250. v.add(df);
  251. df.set(r.x-width/2, r.y-height/2);
  252. df.mult(-0.001);
  253. v.add(df);
  254. v.mult(0.97);
  255. 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
  256. v.x = -v.x; // Previous line and v.x = -v.x
  257. }
  258. 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
  259. v.y = -v.y; // Previous line and v.y = -v.y
  260. }
  261.  
  262. // get the x value from the acceleromoter, use to move object horizontally
  263. float xa = map(portValues[1],-10,10,0,width); // Sets up use of the X axis
  264.  
  265. // get the y value from the accelerometer, use to move object vertically
  266. float ya = map(portValues[0],-10,10,0,height); // Sets up use of the Y axis
  267.  
  268.  
  269. if(xa > x){x+=3;}
  270. else if( xa < x){x-=3;} // Allows you to move the cell around on the
  271. // x axis with the Arduino
  272.  
  273. if(ya > y){ y += 3;}
  274. else if(ya < y){y -= 3;} // Allows you to move the cell around on the
  275. // y axis with the Arduino
  276.  
  277.  
  278. r.x =x; // X-axis
  279. r.y =y; // Y-axis
  280.  
  281. }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement