Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add spring

  • Loading branch information...
commit 5c6b49f53ef2a17418e5f6548dec56fd67b3ed27 1 parent 115a1ab
Ryohei Suzuki authored January 30, 2012
1  .gitignore
... ...
@@ -1 +0,0 @@
1  
-.DS_Store
8  falling_snow/falling_snow.pde
@@ -46,9 +46,9 @@ void draw() {
46 46
   for(int y=0; y<pg.height; y++) {
47 47
     for(int x=0; x<pg.width; x++) {
48 48
       pg.pixels[x+y*pg.width] = color(mr[x][y], mg[x][y], mb[x][y], 191);
49  
-      mr[x][y] /= 1.05;
50  
-      mg[x][y] /= 1.05;
51  
-      mb[x][y] /= 1.05;
  49
+      mr[x][y] = int(sqrt(mr[x][y]))*2;
  50
+      mg[x][y] = int(sqrt(mg[x][y]))*2;
  51
+      mb[x][y] = int(sqrt(mb[x][y]))*2;
52 52
     }
53 53
   }
54 54
   pg.updatePixels();
@@ -86,7 +86,7 @@ class Particle {
86 86
     this.r_ratio = random(0.5, 1);
87 87
     this.g_ratio = random(0.5, 1);
88 88
     this.b_ratio = random(0.5, 1);
89  
-    this.ratio = random(0.2, 0.8);
  89
+    this.ratio = random(0.5, 1.0);
90 90
   }
91 91
   
92 92
   public void drawMetaball(int x, int y) {
131  simple_spring/applet/index.html
... ...
@@ -0,0 +1,131 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3
+  <head>
  4
+    <!-- charset must remain utf-8 to be handled properly by Processing -->
  5
+    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6
+
  7
+    <title>simple_spring : Built with Processing</title>
  8
+
  9
+    <style type="text/css">
  10
+      /* <![CDATA[ */
  11
+	
  12
+	body {
  13
+  	  margin: 60px 0px 0px 55px;
  14
+	  font-family: verdana, geneva, arial, helvetica, sans-serif; 
  15
+	  font-size: 11px; 
  16
+	  background-color: #ddddcc; 
  17
+	  text-decoration: none; 
  18
+	  font-weight: normal; 
  19
+	  line-height: normal; 
  20
+	}
  21
+		 
  22
+	a          { color: #3399cc; }
  23
+	a:link     { color: #3399cc; text-decoration: underline; }
  24
+	a:visited  { color: #3399cc; text-decoration: underline; }
  25
+	a:active   { color: #3399cc; text-decoration: underline; }
  26
+	a:hover    { color: #3399cc; text-decoration: underline; }
  27
+
  28
+      /* ]]> */
  29
+    </style>    
  30
+  </head>
  31
+
  32
+  <body>
  33
+    <div id="content">
  34
+      <div id="simple_spring_container">
  35
+
  36
+	<!-- This version plays nicer with older browsers, 
  37
+	     but requires JavaScript to be enabled. 
  38
+	     http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
  39
+	     http://download.oracle.com/javase/tutorial/deployment/deploymentInDepth/ -->
  40
+	<script type="text/javascript"
  41
+		src="http://www.java.com/js/deployJava.js"></script>
  42
+	<script type="text/javascript">
  43
+	  /* <![CDATA[ */
  44
+
  45
+	  var attributes = { 
  46
+            code: 'simple_spring.class',
  47
+            archive: 'simple_spring.jar',
  48
+            width: 400, 
  49
+            height: 400,
  50
+          };
  51
+          var parameters = { 
  52
+            image: 'loading.gif',
  53
+            centerimage: 'true',
  54
+          };
  55
+          var version = '1.5';
  56
+          deployJava.runApplet(attributes, parameters, version);
  57
+
  58
+          /* ]]> */
  59
+        </script>
  60
+        
  61
+	<noscript> <div>
  62
+	  <!--[if !IE]> -->
  63
+	  <object classid="java:simple_spring.class" 
  64
+            	  type="application/x-java-applet"
  65
+            	  archive="simple_spring.jar"
  66
+            	  width="400" height="400"
  67
+            	  standby="Loading Processing software..." >
  68
+            
  69
+	    <param name="archive" value="simple_spring.jar" />
  70
+	    
  71
+	    <param name="mayscript" value="true" />
  72
+	    <param name="scriptable" value="true" />
  73
+	    
  74
+	    <param name="image" value="loading.gif" />
  75
+	    <param name="boxmessage" value="Loading Processing software..." />
  76
+	    <param name="boxbgcolor" value="#FFFFFF" />
  77
+	  <!--<![endif]-->
  78
+
  79
+	    <!-- For more instructions on deployment, 
  80
+		 or to update the CAB file listed here, see:
  81
+		 http://java.sun.com/javase/6/webnotes/family-clsid.html
  82
+		 http://java.sun.com/javase/6/webnotes/install/jre/autodownload.html -->
  83
+	    <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
  84
+		    codebase="http://java.sun.com/update/1.6.0/jinstall-6u20-windows-i586.cab"
  85
+		    width="400" height="400"
  86
+		    standby="Loading Processing software..."  >
  87
+	      
  88
+	      <param name="code" value="simple_spring" />
  89
+	      <param name="archive" value="simple_spring.jar" />
  90
+	      
  91
+	      <param name="mayscript" value="true" />
  92
+	      <param name="scriptable" value="true" />
  93
+	      
  94
+	      <param name="image" value="loading.gif" />
  95
+	      <param name="boxmessage" value="Loading Processing software..." />
  96
+	      <param name="boxbgcolor" value="#FFFFFF" />
  97
+	      
  98
+	      <p>
  99
+		<strong>
  100
+		  This browser does not have a Java Plug-in.
  101
+		  <br />
  102
+		  <a href="http://www.java.com/getjava" title="Download Java Plug-in">
  103
+		    Get the latest Java Plug-in here.
  104
+		  </a>
  105
+		</strong>
  106
+	      </p>
  107
+	      
  108
+	    </object>
  109
+	    
  110
+	  <!--[if !IE]> -->
  111
+	  </object>
  112
+	  <!--<![endif]-->
  113
+
  114
+	</div> </noscript>
  115
+
  116
+      </div>
  117
+      
  118
+      <p>
  119
+	
  120
+      </p>
  121
+      
  122
+      <p>
  123
+	Source code: <a href="simple_spring.pde">simple_spring</a> 
  124
+      </p>
  125
+      
  126
+      <p>
  127
+	Built with <a href="http://processing.org" title="Processing.org">Processing</a>
  128
+      </p>
  129
+    </div>
  130
+  </body>
  131
+</html>
BIN  simple_spring/applet/loading.gif
BIN  simple_spring/applet/simple_spring.jar
Binary file not shown
122  simple_spring/applet/simple_spring.java
... ...
@@ -0,0 +1,122 @@
  1
+import processing.core.*; 
  2
+import processing.xml.*; 
  3
+
  4
+import java.applet.*; 
  5
+import java.awt.Dimension; 
  6
+import java.awt.Frame; 
  7
+import java.awt.event.MouseEvent; 
  8
+import java.awt.event.KeyEvent; 
  9
+import java.awt.event.FocusEvent; 
  10
+import java.awt.Image; 
  11
+import java.io.*; 
  12
+import java.net.*; 
  13
+import java.text.*; 
  14
+import java.util.*; 
  15
+import java.util.zip.*; 
  16
+import java.util.regex.*; 
  17
+
  18
+public class simple_spring extends PApplet {
  19
+
  20
+Particle[] particles;
  21
+int N=5;
  22
+float K=0.002f;
  23
+float pathprob=0.5f;
  24
+
  25
+public void setup() {
  26
+   size(400, 400, JAVA2D);
  27
+
  28
+   particles = new Particle[N];
  29
+   for(int i=0; i<N; i++) {
  30
+     float x = random(0, width+1);
  31
+     float y = random(0, height+1);
  32
+     particles[i] = new Particle(x, y);
  33
+   }
  34
+   for(int i=0; i<N; i++) {
  35
+     for(int j=i+1; j<N; j++) {
  36
+       if(random(0, 1) < pathprob) {
  37
+         particles[i].addConnection(j);
  38
+         particles[j].addConnection(i); 
  39
+       }
  40
+     }  
  41
+   }
  42
+   
  43
+   ellipseMode(CENTER);
  44
+   strokeWeight(1);
  45
+   smooth();
  46
+}
  47
+
  48
+public void draw() {
  49
+  background(191);
  50
+  for(int i=0; i<N; i++) {
  51
+    // draw line
  52
+    for(int j=0; j<particles[i].connects; j++) {
  53
+      int k = particles[i].connections[j];
  54
+      if(i<k) {
  55
+        line(PApplet.parseInt(particles[i].pos.x), PApplet.parseInt(particles[i].pos.y), PApplet.parseInt(particles[k].pos.x), PApplet.parseInt(particles[k].pos.y));
  56
+      }
  57
+    }
  58
+    
  59
+    // draw particle
  60
+    ellipse(PApplet.parseInt(particles[i].pos.x), PApplet.parseInt(particles[i].pos.y), 20, 20);
  61
+  }
  62
+  
  63
+  // simulation
  64
+  for(int i=0; i<N; i++) {
  65
+    particles[i].simulate();
  66
+  }
  67
+  
  68
+  // apply
  69
+  for(int i=0; i<N; i++) {
  70
+    particles[i].apply(); 
  71
+  }
  72
+}
  73
+
  74
+
  75
+class Particle {
  76
+   int[] connections;
  77
+   int connects;
  78
+   PVector pos;
  79
+   PVector newpos;
  80
+   PVector vel;
  81
+   PVector acc;
  82
+   
  83
+   public Particle(float x, float y) {
  84
+     this.pos = new PVector(x, y);
  85
+     this.vel = new PVector(0, 0);
  86
+     this.acc = new PVector(0, 0);
  87
+     this.newpos = new PVector(0, 0);
  88
+     this.connections = new int[N];
  89
+     this.connects=0;
  90
+   }
  91
+   
  92
+   public void simulate() {
  93
+     this.newpos.x = this.pos.x;
  94
+     this.newpos.y = this.pos.y;
  95
+     this.acc.x = 0;
  96
+     this.acc.y = 0;
  97
+     for(int i=0; i<connects; i++) {
  98
+       PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  99
+                                 particles[this.connections[i]].pos.y - this.pos.y);
  100
+       float r = particles[this.connections[i]].pos.dist(this.pos);
  101
+       vec.normalize();
  102
+       vec.mult(r);
  103
+       vec.mult(K);
  104
+       this.acc.add(vec);
  105
+     }
  106
+     this.vel.add(this.acc);
  107
+     this.newpos.add(this.vel);
  108
+   }
  109
+   
  110
+   public void apply() {
  111
+     this.pos.x = this.newpos.x;
  112
+     this.pos.y = this.newpos.y;
  113
+   }
  114
+   
  115
+   public void addConnection(int number) {
  116
+     this.connections[this.connects++] = number; 
  117
+   }
  118
+}
  119
+  static public void main(String args[]) {
  120
+    PApplet.main(new String[] { "--bgcolor=#FFFFFF", "simple_spring" });
  121
+  }
  122
+}
99  simple_spring/applet/simple_spring.pde
... ...
@@ -0,0 +1,99 @@
  1
+Particle[] particles;
  2
+int N=5;
  3
+float K=0.002;
  4
+float pathprob=0.5;
  5
+
  6
+void setup() {
  7
+   size(400, 400, JAVA2D);
  8
+
  9
+   particles = new Particle[N];
  10
+   for(int i=0; i<N; i++) {
  11
+     float x = random(0, width+1);
  12
+     float y = random(0, height+1);
  13
+     particles[i] = new Particle(x, y);
  14
+   }
  15
+   for(int i=0; i<N; i++) {
  16
+     for(int j=i+1; j<N; j++) {
  17
+       if(random(0, 1) < pathprob) {
  18
+         particles[i].addConnection(j);
  19
+         particles[j].addConnection(i); 
  20
+       }
  21
+     }  
  22
+   }
  23
+   
  24
+   ellipseMode(CENTER);
  25
+   strokeWeight(1);
  26
+   smooth();
  27
+}
  28
+
  29
+void draw() {
  30
+  background(191);
  31
+  for(int i=0; i<N; i++) {
  32
+    // draw line
  33
+    for(int j=0; j<particles[i].connects; j++) {
  34
+      int k = particles[i].connections[j];
  35
+      if(i<k) {
  36
+        line(int(particles[i].pos.x), int(particles[i].pos.y), int(particles[k].pos.x), int(particles[k].pos.y));
  37
+      }
  38
+    }
  39
+    
  40
+    // draw particle
  41
+    ellipse(int(particles[i].pos.x), int(particles[i].pos.y), 20, 20);
  42
+  }
  43
+  
  44
+  // simulation
  45
+  for(int i=0; i<N; i++) {
  46
+    particles[i].simulate();
  47
+  }
  48
+  
  49
+  // apply
  50
+  for(int i=0; i<N; i++) {
  51
+    particles[i].apply(); 
  52
+  }
  53
+}
  54
+
  55
+
  56
+class Particle {
  57
+   int[] connections;
  58
+   int connects;
  59
+   PVector pos;
  60
+   PVector newpos;
  61
+   PVector vel;
  62
+   PVector acc;
  63
+   
  64
+   public Particle(float x, float y) {
  65
+     this.pos = new PVector(x, y);
  66
+     this.vel = new PVector(0, 0);
  67
+     this.acc = new PVector(0, 0);
  68
+     this.newpos = new PVector(0, 0);
  69
+     this.connections = new int[N];
  70
+     this.connects=0;
  71
+   }
  72
+   
  73
+   public void simulate() {
  74
+     this.newpos.x = this.pos.x;
  75
+     this.newpos.y = this.pos.y;
  76
+     this.acc.x = 0;
  77
+     this.acc.y = 0;
  78
+     for(int i=0; i<connects; i++) {
  79
+       PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  80
+                                 particles[this.connections[i]].pos.y - this.pos.y);
  81
+       float r = particles[this.connections[i]].pos.dist(this.pos);
  82
+       vec.normalize();
  83
+       vec.mult(r);
  84
+       vec.mult(K);
  85
+       this.acc.add(vec);
  86
+     }
  87
+     this.vel.add(this.acc);
  88
+     this.newpos.add(this.vel);
  89
+   }
  90
+   
  91
+   public void apply() {
  92
+     this.pos.x = this.newpos.x;
  93
+     this.pos.y = this.newpos.y;
  94
+   }
  95
+   
  96
+   public void addConnection(int number) {
  97
+     this.connections[this.connects++] = number; 
  98
+   }
  99
+}
99  simple_spring/simple_spring.pde
... ...
@@ -0,0 +1,99 @@
  1
+Particle[] particles;
  2
+int N=10;
  3
+float K=0.002;
  4
+float pathprob=0.5;
  5
+
  6
+void setup() {
  7
+   size(400, 400, JAVA2D);
  8
+
  9
+   particles = new Particle[N];
  10
+   for(int i=0; i<N; i++) {
  11
+     float x = random(0, width+1);
  12
+     float y = random(0, height+1);
  13
+     particles[i] = new Particle(x, y);
  14
+   }
  15
+   for(int i=0; i<N; i++) {
  16
+     for(int j=i+1; j<N; j++) {
  17
+       if(random(0, 1) < pathprob) {
  18
+         particles[i].addConnection(j);
  19
+         particles[j].addConnection(i); 
  20
+       }
  21
+     }  
  22
+   }
  23
+   
  24
+   ellipseMode(CENTER);
  25
+   strokeWeight(1);
  26
+   smooth();
  27
+}
  28
+
  29
+void draw() {
  30
+  background(191);
  31
+  for(int i=0; i<N; i++) {
  32
+    // draw line
  33
+    for(int j=0; j<particles[i].connects; j++) {
  34
+      int k = particles[i].connections[j];
  35
+      if(i<k) {
  36
+        line(int(particles[i].pos.x), int(particles[i].pos.y), int(particles[k].pos.x), int(particles[k].pos.y));
  37
+      }
  38
+    }
  39
+    
  40
+    // draw particle
  41
+    ellipse(int(particles[i].pos.x), int(particles[i].pos.y), 20, 20);
  42
+  }
  43
+  
  44
+  // simulation
  45
+  for(int i=0; i<N; i++) {
  46
+    particles[i].simulate();
  47
+  }
  48
+  
  49
+  // apply
  50
+  for(int i=0; i<N; i++) {
  51
+    particles[i].apply(); 
  52
+  }
  53
+}
  54
+
  55
+
  56
+class Particle {
  57
+   int[] connections;
  58
+   int connects;
  59
+   PVector pos;
  60
+   PVector newpos;
  61
+   PVector vel;
  62
+   PVector acc;
  63
+   
  64
+   public Particle(float x, float y) {
  65
+     this.pos = new PVector(x, y);
  66
+     this.vel = new PVector(0, 0);
  67
+     this.acc = new PVector(0, 0);
  68
+     this.newpos = new PVector(0, 0);
  69
+     this.connections = new int[N];
  70
+     this.connects=0;
  71
+   }
  72
+   
  73
+   public void simulate() {
  74
+     this.newpos.x = this.pos.x;
  75
+     this.newpos.y = this.pos.y;
  76
+     this.acc.x = 0;
  77
+     this.acc.y = 0;
  78
+     for(int i=0; i<connects; i++) {
  79
+       PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  80
+                                 particles[this.connections[i]].pos.y - this.pos.y);
  81
+       float r = particles[this.connections[i]].pos.dist(this.pos);
  82
+       vec.normalize();
  83
+       vec.mult(r);
  84
+       vec.mult(K);
  85
+       this.acc.add(vec);
  86
+     }
  87
+     this.vel.add(this.acc);
  88
+     this.newpos.add(this.vel);
  89
+   }
  90
+   
  91
+   public void apply() {
  92
+     this.pos.x = this.newpos.x;
  93
+     this.pos.y = this.newpos.y;
  94
+   }
  95
+   
  96
+   public void addConnection(int number) {
  97
+     this.connections[this.connects++] = number; 
  98
+   }
  99
+}
131  spring_metaball/applet/index.html
... ...
@@ -0,0 +1,131 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3
+  <head>
  4
+    <!-- charset must remain utf-8 to be handled properly by Processing -->
  5
+    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6
+
  7
+    <title>spring_metaball : Built with Processing</title>
  8
+
  9
+    <style type="text/css">
  10
+      /* <![CDATA[ */
  11
+	
  12
+	body {
  13
+  	  margin: 60px 0px 0px 55px;
  14
+	  font-family: verdana, geneva, arial, helvetica, sans-serif; 
  15
+	  font-size: 11px; 
  16
+	  background-color: #ddddcc; 
  17
+	  text-decoration: none; 
  18
+	  font-weight: normal; 
  19
+	  line-height: normal; 
  20
+	}
  21
+		 
  22
+	a          { color: #3399cc; }
  23
+	a:link     { color: #3399cc; text-decoration: underline; }
  24
+	a:visited  { color: #3399cc; text-decoration: underline; }
  25
+	a:active   { color: #3399cc; text-decoration: underline; }
  26
+	a:hover    { color: #3399cc; text-decoration: underline; }
  27
+
  28
+      /* ]]> */
  29
+    </style>    
  30
+  </head>
  31
+
  32
+  <body>
  33
+    <div id="content">
  34
+      <div id="spring_metaball_container">
  35
+
  36
+	<!-- This version plays nicer with older browsers, 
  37
+	     but requires JavaScript to be enabled. 
  38
+	     http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
  39
+	     http://download.oracle.com/javase/tutorial/deployment/deploymentInDepth/ -->
  40
+	<script type="text/javascript"
  41
+		src="http://www.java.com/js/deployJava.js"></script>
  42
+	<script type="text/javascript">
  43
+	  /* <![CDATA[ */
  44
+
  45
+	  var attributes = { 
  46
+            code: 'spring_metaball.class',
  47
+            archive: 'spring_metaball.jar',
  48
+            width: 600, 
  49
+            height: 600,
  50
+          };
  51
+          var parameters = { 
  52
+            image: 'loading.gif',
  53
+            centerimage: 'true',
  54
+          };
  55
+          var version = '1.5';
  56
+          deployJava.runApplet(attributes, parameters, version);
  57
+
  58
+          /* ]]> */
  59
+        </script>
  60
+        
  61
+	<noscript> <div>
  62
+	  <!--[if !IE]> -->
  63
+	  <object classid="java:spring_metaball.class" 
  64
+            	  type="application/x-java-applet"
  65
+            	  archive="spring_metaball.jar"
  66
+            	  width="600" height="600"
  67
+            	  standby="Loading Processing software..." >
  68
+            
  69
+	    <param name="archive" value="spring_metaball.jar" />
  70
+	    
  71
+	    <param name="mayscript" value="true" />
  72
+	    <param name="scriptable" value="true" />
  73
+	    
  74
+	    <param name="image" value="loading.gif" />
  75
+	    <param name="boxmessage" value="Loading Processing software..." />
  76
+	    <param name="boxbgcolor" value="#FFFFFF" />
  77
+	  <!--<![endif]-->
  78
+
  79
+	    <!-- For more instructions on deployment, 
  80
+		 or to update the CAB file listed here, see:
  81
+		 http://java.sun.com/javase/6/webnotes/family-clsid.html
  82
+		 http://java.sun.com/javase/6/webnotes/install/jre/autodownload.html -->
  83
+	    <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
  84
+		    codebase="http://java.sun.com/update/1.6.0/jinstall-6u20-windows-i586.cab"
  85
+		    width="600" height="600"
  86
+		    standby="Loading Processing software..."  >
  87
+	      
  88
+	      <param name="code" value="spring_metaball" />
  89
+	      <param name="archive" value="spring_metaball.jar" />
  90
+	      
  91
+	      <param name="mayscript" value="true" />
  92
+	      <param name="scriptable" value="true" />
  93
+	      
  94
+	      <param name="image" value="loading.gif" />
  95
+	      <param name="boxmessage" value="Loading Processing software..." />
  96
+	      <param name="boxbgcolor" value="#FFFFFF" />
  97
+	      
  98
+	      <p>
  99
+		<strong>
  100
+		  This browser does not have a Java Plug-in.
  101
+		  <br />
  102
+		  <a href="http://www.java.com/getjava" title="Download Java Plug-in">
  103
+		    Get the latest Java Plug-in here.
  104
+		  </a>
  105
+		</strong>
  106
+	      </p>
  107
+	      
  108
+	    </object>
  109
+	    
  110
+	  <!--[if !IE]> -->
  111
+	  </object>
  112
+	  <!--<![endif]-->
  113
+
  114
+	</div> </noscript>
  115
+
  116
+      </div>
  117
+      
  118
+      <p>
  119
+	
  120
+      </p>
  121
+      
  122
+      <p>
  123
+	Source code: <a href="spring_metaball.pde">spring_metaball</a> 
  124
+      </p>
  125
+      
  126
+      <p>
  127
+	Built with <a href="http://processing.org" title="Processing.org">Processing</a>
  128
+      </p>
  129
+    </div>
  130
+  </body>
  131
+</html>
BIN  spring_metaball/applet/loading.gif
BIN  spring_metaball/applet/spring_metaball.jar
Binary file not shown
170  spring_metaball/applet/spring_metaball.java
... ...
@@ -0,0 +1,170 @@
  1
+import processing.core.*; 
  2
+import processing.xml.*; 
  3
+
  4
+import java.applet.*; 
  5
+import java.awt.Dimension; 
  6
+import java.awt.Frame; 
  7
+import java.awt.event.MouseEvent; 
  8
+import java.awt.event.KeyEvent; 
  9
+import java.awt.event.FocusEvent; 
  10
+import java.awt.Image; 
  11
+import java.io.*; 
  12
+import java.net.*; 
  13
+import java.text.*; 
  14
+import java.util.*; 
  15
+import java.util.zip.*; 
  16
+import java.util.regex.*; 
  17
+
  18
+public class spring_metaball extends PApplet {
  19
+
  20
+Particle[] particles;
  21
+int N=20;
  22
+float K=0.002f;
  23
+float pathprob=0.5f;
  24
+
  25
+// metaball variables
  26
+PGraphics pg;
  27
+int mr[][], mg[][], mb[][];
  28
+int meta_const = 10000;
  29
+float divisor = 1.5f;
  30
+int shut_r = 80;
  31
+
  32
+float simplify = 2.0f;
  33
+int far_threshold = 500;
  34
+
  35
+public void keyPressed() {
  36
+  setup();
  37
+}
  38
+
  39
+public void setup() {
  40
+  size(600, 600, P2D);
  41
+
  42
+  particles = new Particle[N];
  43
+  for(int i=0; i<N; i++) {
  44
+    float x = random(0, (width+1)/simplify);
  45
+    float y = random(0, (height+1)/simplify);
  46
+    particles[i] = new Particle(x, y);
  47
+  }
  48
+  for(int i=0; i<N; i++) {
  49
+    for(int j=i+1; j<N; j++) {
  50
+      if(random(0, 1) < pathprob) {
  51
+        particles[i].addConnection(j);
  52
+        particles[j].addConnection(i); 
  53
+      }
  54
+    }  
  55
+  }
  56
+ 
  57
+  pg = createGraphics((int)(width/simplify), (int)(height/simplify), P2D);
  58
+  mr = new int[(int)(width/simplify)][(int)(height/simplify)];
  59
+  mg = new int[(int)(width/simplify)][(int)(height/simplify)];
  60
+  mb = new int[(int)(width/simplify)][(int)(height/simplify)];
  61
+}
  62
+
  63
+public void draw() {
  64
+  // draw metaballs
  65
+  for(int i=0; i<N; i++) {
  66
+    Particle p = this.particles[i];
  67
+    int px = (int)p.pos.x;
  68
+    int py = (int)p.pos.y;
  69
+    for(int y=(py-shut_r >= 0) ? py-shut_r : 0; y<py+shut_r && y<pg.height; y++) {
  70
+      for(int x=(px-shut_r >= 0) ? px-shut_r : 0; x<px+shut_r && x<pg.width; x++) {
  71
+        p.drawMetaball(x, y);
  72
+      }
  73
+    }
  74
+  }
  75
+  pg.beginDraw();
  76
+  pg.loadPixels();
  77
+  for(int y=0; y<pg.height; y++) {
  78
+    for(int x=0; x<pg.width; x++) {
  79
+      pg.pixels[x+y*pg.width] = color(mr[x][y], mg[x][y], mb[x][y], 191);
  80
+//      mr[x][y] = int(sqrt(mr[x][y]))*4;
  81
+//      mg[x][y] = int(sqrt(mg[x][y]))*4;
  82
+//      mb[x][y] = int(sqrt(mb[x][y]))*4;
  83
+      mr[x][y] /= divisor;
  84
+      mg[x][y] /= divisor;
  85
+      mb[x][y] /= divisor;
  86
+    }
  87
+  }
  88
+  pg.updatePixels();
  89
+  pg.endDraw();
  90
+  
  91
+  image(pg, 0, 0, width, height);
  92
+  
  93
+  // simulation
  94
+  for(int i=0; i<N; i++) {
  95
+    particles[i].simulate();
  96
+  }
  97
+  
  98
+  // apply
  99
+  for(int i=0; i<N; i++) {
  100
+    particles[i].apply(); 
  101
+  }
  102
+}
  103
+
  104
+
  105
+class Particle {
  106
+  int[] connections;
  107
+  int connects;
  108
+  PVector pos;
  109
+  PVector newpos;
  110
+  PVector vel;
  111
+  PVector acc;
  112
+   
  113
+  float ratio = 1, r_ratio = 1, g_ratio = 1, b_ratio = 1;
  114
+   
  115
+  public Particle(float x, float y) {
  116
+    this.pos = new PVector(x, y);
  117
+    this.vel = new PVector(0, 0);
  118
+    this.acc = new PVector(0, 0);
  119
+    this.newpos = new PVector(0, 0);
  120
+    this.connections = new int[N];
  121
+    this.connects=0;
  122
+    
  123
+    this.r_ratio = random(0.5f, 1);
  124
+    this.g_ratio = random(0.5f, 1);
  125
+    this.b_ratio = random(0.5f, 1);
  126
+    this.ratio = 1;
  127
+//    this.ratio = random(0.5, 1.0);
  128
+  }
  129
+   
  130
+  public void simulate() {
  131
+    this.newpos.x = this.pos.x;
  132
+    this.newpos.y = this.pos.y;
  133
+    this.acc.x = 0;
  134
+    this.acc.y = 0;
  135
+    for(int i=0; i<connects; i++) {
  136
+      PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  137
+                                particles[this.connections[i]].pos.y - this.pos.y);
  138
+      float r = particles[this.connections[i]].pos.dist(this.pos);
  139
+      vec.normalize();
  140
+      vec.mult(r);
  141
+      vec.mult(K);
  142
+      this.acc.add(vec);
  143
+      }
  144
+    this.vel.add(this.acc);
  145
+    this.newpos.add(this.vel);
  146
+    
  147
+    this.ratio = this.acc.mag()/500*255;
  148
+  }
  149
+   
  150
+  public void apply() {
  151
+    this.pos.x = this.newpos.x;
  152
+    this.pos.y = this.newpos.y;
  153
+  }
  154
+   
  155
+  public void addConnection(int number) {
  156
+    this.connections[this.connects++] = number; 
  157
+  }
  158
+   
  159
+  public void drawMetaball(int x, int y) {
  160
+    float p = meta_const/((x-pos.x)*(x-pos.x) + (y-pos.y)*(y-pos.y) + 1);
  161
+    mr[x][y] += p * r_ratio * ratio;
  162
+    mg[x][y] += p * g_ratio * ratio;
  163
+    mb[x][y] += p * b_ratio * ratio;
  164
+  }
  165
+}
  166
+
  167
+  static public void main(String args[]) {
  168
+    PApplet.main(new String[] { "--bgcolor=#FFFFFF", "spring_metaball" });
  169
+  }
  170
+}
147  spring_metaball/applet/spring_metaball.pde
... ...
@@ -0,0 +1,147 @@
  1
+Particle[] particles;
  2
+int N=20;
  3
+float K=0.002;
  4
+float pathprob=0.5;
  5
+
  6
+// metaball variables
  7
+PGraphics pg;
  8
+int mr[][], mg[][], mb[][];
  9
+int meta_const = 10000;
  10
+float divisor = 1.5;
  11
+int shut_r = 80;
  12
+
  13
+float simplify = 2.0;
  14
+int far_threshold = 500;
  15
+
  16
+void keyPressed() {
  17
+  setup();
  18
+}
  19
+
  20
+void setup() {
  21
+  size(600, 600, P2D);
  22
+
  23
+  particles = new Particle[N];
  24
+  for(int i=0; i<N; i++) {
  25
+    float x = random(0, (width+1)/simplify);
  26
+    float y = random(0, (height+1)/simplify);
  27
+    particles[i] = new Particle(x, y);
  28
+  }
  29
+  for(int i=0; i<N; i++) {
  30
+    for(int j=i+1; j<N; j++) {
  31
+      if(random(0, 1) < pathprob) {
  32
+        particles[i].addConnection(j);
  33
+        particles[j].addConnection(i); 
  34
+      }
  35
+    }  
  36
+  }
  37
+ 
  38
+  pg = createGraphics((int)(width/simplify), (int)(height/simplify), P2D);
  39
+  mr = new int[(int)(width/simplify)][(int)(height/simplify)];
  40
+  mg = new int[(int)(width/simplify)][(int)(height/simplify)];
  41
+  mb = new int[(int)(width/simplify)][(int)(height/simplify)];
  42
+}
  43
+
  44
+void draw() {
  45
+  // draw metaballs
  46
+  for(int i=0; i<N; i++) {
  47
+    Particle p = this.particles[i];
  48
+    int px = (int)p.pos.x;
  49
+    int py = (int)p.pos.y;
  50
+    for(int y=(py-shut_r >= 0) ? py-shut_r : 0; y<py+shut_r && y<pg.height; y++) {
  51
+      for(int x=(px-shut_r >= 0) ? px-shut_r : 0; x<px+shut_r && x<pg.width; x++) {
  52
+        p.drawMetaball(x, y);
  53
+      }
  54
+    }
  55
+  }
  56
+  pg.beginDraw();
  57
+  pg.loadPixels();
  58
+  for(int y=0; y<pg.height; y++) {
  59
+    for(int x=0; x<pg.width; x++) {
  60
+      pg.pixels[x+y*pg.width] = color(mr[x][y], mg[x][y], mb[x][y], 191);
  61
+//      mr[x][y] = int(sqrt(mr[x][y]))*4;
  62
+//      mg[x][y] = int(sqrt(mg[x][y]))*4;
  63
+//      mb[x][y] = int(sqrt(mb[x][y]))*4;
  64
+      mr[x][y] /= divisor;
  65
+      mg[x][y] /= divisor;
  66
+      mb[x][y] /= divisor;
  67
+    }
  68
+  }
  69
+  pg.updatePixels();
  70
+  pg.endDraw();
  71
+  
  72
+  image(pg, 0, 0, width, height);
  73
+  
  74
+  // simulation
  75
+  for(int i=0; i<N; i++) {
  76
+    particles[i].simulate();
  77
+  }
  78
+  
  79
+  // apply
  80
+  for(int i=0; i<N; i++) {
  81
+    particles[i].apply(); 
  82
+  }
  83
+}
  84
+
  85
+
  86
+class Particle {
  87
+  int[] connections;
  88
+  int connects;
  89
+  PVector pos;
  90
+  PVector newpos;
  91
+  PVector vel;
  92
+  PVector acc;
  93
+   
  94
+  float ratio = 1, r_ratio = 1, g_ratio = 1, b_ratio = 1;
  95
+   
  96
+  public Particle(float x, float y) {
  97
+    this.pos = new PVector(x, y);
  98
+    this.vel = new PVector(0, 0);
  99
+    this.acc = new PVector(0, 0);
  100
+    this.newpos = new PVector(0, 0);
  101
+    this.connections = new int[N];
  102
+    this.connects=0;
  103
+    
  104
+    this.r_ratio = random(0.5, 1);
  105
+    this.g_ratio = random(0.5, 1);
  106
+    this.b_ratio = random(0.5, 1);
  107
+    this.ratio = 1;
  108
+//    this.ratio = random(0.5, 1.0);
  109
+  }
  110
+   
  111
+  public void simulate() {
  112
+    this.newpos.x = this.pos.x;
  113
+    this.newpos.y = this.pos.y;
  114
+    this.acc.x = 0;
  115
+    this.acc.y = 0;
  116
+    for(int i=0; i<connects; i++) {
  117
+      PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  118
+                                particles[this.connections[i]].pos.y - this.pos.y);
  119
+      float r = particles[this.connections[i]].pos.dist(this.pos);
  120
+      vec.normalize();
  121
+      vec.mult(r);
  122
+      vec.mult(K);
  123
+      this.acc.add(vec);
  124
+      }
  125
+    this.vel.add(this.acc);
  126
+    this.newpos.add(this.vel);
  127
+    
  128
+    this.ratio = this.acc.mag()/500*255;
  129
+  }
  130
+   
  131
+  public void apply() {
  132
+    this.pos.x = this.newpos.x;
  133
+    this.pos.y = this.newpos.y;
  134
+  }
  135
+   
  136
+  public void addConnection(int number) {
  137
+    this.connections[this.connects++] = number; 
  138
+  }
  139
+   
  140
+  public void drawMetaball(int x, int y) {
  141
+    float p = meta_const/((x-pos.x)*(x-pos.x) + (y-pos.y)*(y-pos.y) + 1);
  142
+    mr[x][y] += p * r_ratio * ratio;
  143
+    mg[x][y] += p * g_ratio * ratio;
  144
+    mb[x][y] += p * b_ratio * ratio;
  145
+  }
  146
+}
  147
+
147  spring_metaball/spring_metaball.pde
... ...
@@ -0,0 +1,147 @@
  1
+Particle[] particles;
  2
+int N=20;
  3
+float K=0.002;
  4
+float pathprob=0.5;
  5
+
  6
+// metaball variables
  7
+PGraphics pg;
  8
+int mr[][], mg[][], mb[][];
  9
+int meta_const = 10000;
  10
+float divisor = 1.5;
  11
+int shut_r = 80;
  12
+
  13
+float simplify = 2.0;
  14
+int far_threshold = 500;
  15
+
  16
+void keyPressed() {
  17
+  setup();
  18
+}
  19
+
  20
+void setup() {
  21
+  size(600, 600, P2D);
  22
+
  23
+  particles = new Particle[N];
  24
+  for(int i=0; i<N; i++) {
  25
+    float x = random(0, (width+1)/simplify);
  26
+    float y = random(0, (height+1)/simplify);
  27
+    particles[i] = new Particle(x, y);
  28
+  }
  29
+  for(int i=0; i<N; i++) {
  30
+    for(int j=i+1; j<N; j++) {
  31
+      if(random(0, 1) < pathprob) {
  32
+        particles[i].addConnection(j);
  33
+        particles[j].addConnection(i); 
  34
+      }
  35
+    }  
  36
+  }
  37
+ 
  38
+  pg = createGraphics((int)(width/simplify), (int)(height/simplify), P2D);
  39
+  mr = new int[(int)(width/simplify)][(int)(height/simplify)];
  40
+  mg = new int[(int)(width/simplify)][(int)(height/simplify)];
  41
+  mb = new int[(int)(width/simplify)][(int)(height/simplify)];
  42
+}
  43
+
  44
+void draw() {
  45
+  // draw metaballs
  46
+  for(int i=0; i<N; i++) {
  47
+    Particle p = this.particles[i];
  48
+    int px = (int)p.pos.x;
  49
+    int py = (int)p.pos.y;
  50
+    for(int y=(py-shut_r >= 0) ? py-shut_r : 0; y<py+shut_r && y<pg.height; y++) {
  51
+      for(int x=(px-shut_r >= 0) ? px-shut_r : 0; x<px+shut_r && x<pg.width; x++) {
  52
+        p.drawMetaball(x, y);
  53
+      }
  54
+    }
  55
+  }
  56
+  pg.beginDraw();
  57
+  pg.loadPixels();
  58
+  for(int y=0; y<pg.height; y++) {
  59
+    for(int x=0; x<pg.width; x++) {
  60
+      pg.pixels[x+y*pg.width] = color(mr[x][y], mg[x][y], mb[x][y], 191);
  61
+//      mr[x][y] = int(sqrt(mr[x][y]))*4;
  62
+//      mg[x][y] = int(sqrt(mg[x][y]))*4;
  63
+//      mb[x][y] = int(sqrt(mb[x][y]))*4;
  64
+      mr[x][y] /= divisor;
  65
+      mg[x][y] /= divisor;
  66
+      mb[x][y] /= divisor;
  67
+    }
  68
+  }
  69
+  pg.updatePixels();
  70
+  pg.endDraw();
  71
+  
  72
+  image(pg, 0, 0, width, height);
  73
+  
  74
+  // simulation
  75
+  for(int i=0; i<N; i++) {
  76
+    particles[i].simulate();
  77
+  }
  78
+  
  79
+  // apply
  80
+  for(int i=0; i<N; i++) {
  81
+    particles[i].apply(); 
  82
+  }
  83
+}
  84
+
  85
+
  86
+class Particle {
  87
+  int[] connections;
  88
+  int connects;
  89
+  PVector pos;
  90
+  PVector newpos;
  91
+  PVector vel;
  92
+  PVector acc;
  93
+   
  94
+  float ratio = 1, r_ratio = 1, g_ratio = 1, b_ratio = 1;
  95
+   
  96
+  public Particle(float x, float y) {
  97
+    this.pos = new PVector(x, y);
  98
+    this.vel = new PVector(0, 0);
  99
+    this.acc = new PVector(0, 0);
  100
+    this.newpos = new PVector(0, 0);
  101
+    this.connections = new int[N];
  102
+    this.connects=0;
  103
+    
  104
+    this.r_ratio = random(0.5, 1);
  105
+    this.g_ratio = random(0.5, 1);
  106
+    this.b_ratio = random(0.5, 1);
  107
+    this.ratio = 1;
  108
+//    this.ratio = random(0.5, 1.0);
  109
+  }
  110
+   
  111
+  public void simulate() {
  112
+    this.newpos.x = this.pos.x;
  113
+    this.newpos.y = this.pos.y;
  114
+    this.acc.x = 0;
  115
+    this.acc.y = 0;
  116
+    for(int i=0; i<connects; i++) {
  117
+      PVector vec = new PVector(particles[this.connections[i]].pos.x - this.pos.x, 
  118
+                                particles[this.connections[i]].pos.y - this.pos.y);
  119
+      float r = particles[this.connections[i]].pos.dist(this.pos);
  120
+      vec.normalize();
  121
+      vec.mult(r);
  122
+      vec.mult(K);
  123
+      this.acc.add(vec);
  124
+      }
  125
+    this.vel.add(this.acc);
  126
+    this.newpos.add(this.vel);
  127
+    
  128
+    this.ratio = this.acc.mag()/500*255;
  129
+  }
  130
+   
  131
+  public void apply() {
  132
+    this.pos.x = this.newpos.x;
  133
+    this.pos.y = this.newpos.y;
  134
+  }
  135
+   
  136
+  public void addConnection(int number) {
  137
+    this.connections[this.connects++] = number; 
  138
+  }
  139
+   
  140
+  public void drawMetaball(int x, int y) {
  141
+    float p = meta_const/((x-pos.x)*(x-pos.x) + (y-pos.y)*(y-pos.y) + 1);
  142
+    mr[x][y] += p * r_ratio * ratio;
  143
+    mg[x][y] += p * g_ratio * ratio;
  144
+    mb[x][y] += p * b_ratio * ratio;
  145
+  }
  146
+}
  147
+

0 notes on commit 5c6b49f

Please sign in to comment.
Something went wrong with that request. Please try again.