We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Lee Byron's library Modification
Page Index Toggle Pages: 1
Lee Byron's library Modification (Read 1402 times)
Lee Byron's library Modification
May 31st, 2010, 4:56am
 
Hello,

I am very new to Processing (and Scripting / Java in general)...

Currently, I am trying to modify the Lee Byron's Mesh to make the points move. I managed to make them move, but now I would like to make the points that are closer to each other faster.

I also tried to add new points (saw the code in discourse) but it did not work)

I could use PVector to find distances between points and than have them move faster over time but I am not sure how to go about it. (its the last part in this code) . sorry if it looks messy- my code is divided into tabs and im not sure (yet) how to display this in discourse forums..

Here is the code:

(divided in 3 tabs):

MeshLibDemo:
____________________________________
import megamu.mesh.*;


void setup() {                  
 size(1100,700);
 // initialize points and calculate diagrams
 initMesh();
 smooth();
}


void draw() {
 background(200);
 drawMesh();
 initMesh();
 //movePoints();
 moveFaster();
}

void keyPressed() {
 // reset points and mesh when spacebar is pressed
 if(key==' ') {
   initPoints();
   initMesh();
 }
 
 // use keys '1'-'3' to toggle display
 if(key=='1') showVoronoi=!showVoronoi;
 if(key=='2') showDelaunay=!showDelaunay;
 if(key=='3') showHull=!showHull;
}

void mouseMoved() {

if(myRegions==null) return;
 points[0][0]=mouseX;
 points[0][1]=mouseY;
 initMesh();
}


//adding new points:
//void mousePressed() {
//  float [][] tmp=new float [1][2];
//  tmp [0][0]=mouseX;
//  tmp [0][1] = mouseY;
//  points [0] = tmp [0];
//  redraw();
//}[/color]

________________
Mesh Tab:

Voronoi myVoronoi;
Delaunay myDelaunay;
Hull myHull;

float[][] myEdges;
MPolygon myRegions[],myHullRegion;
int col[];

float startX,startY,endX,endY;
float[][] regionCoordinates;

boolean showVoronoi=true;
boolean showDelaunay=false;
boolean showHull=false;

void drawMesh(){
  if(myRegions==null) return;
 if(showVoronoi) {
   strokeWeight(1);
   stroke(0);
   for(int i=1; i<myRegions.length; i++) {
     fill(col[i]);
     regionCoordinates = myRegions[i].getCoords();
     myRegions[i].draw(this); // draw this shape
   }
  }
 
 // draw Voronoi as lines
 if(showDelaunay) {
   strokeWeight(2);
   stroke(255,0,0);
   for(int i=0; i<myEdges.length; i++) {
     startX = myEdges[i][0];
     startY = myEdges[i][1];
     endX = myEdges[i][2];
     endY = myEdges[i][3];
     line(startX, startY, endX, endY);
   }
 }
 
 // draw Hull in semi-transparent yellow
 if(showHull) {
   strokeWeight(1);
   stroke(0);
   fill(255,255,0, 150);
   myHullRegion.draw(this);
 }
}

void initMesh() {
 
 if(points==null) initPoints();
 int oldlength=0;
 if(myRegions!=null) oldlength=myRegions.length;

 myVoronoi = new Voronoi( points );
 myHull = new Hull( points );
 myDelaunay = new Delaunay( points );

 myRegions = myVoronoi.getRegions();
 myHullRegion = myHull.getRegion();
 myEdges = myDelaunay.getEdges();


 if(oldlength!=myRegions.length) {
   col=new int[myRegions.length];
   for(int i=0; i<myRegions.length; i++) {
     float prob=random(100);
     if(prob>60) col[i]=color(random(30,100));    
     else col[i]=color(random(200,255));  
   }
   col[0]=color(255,0,0);
 }
 
}


_______
particles tab:

float[][] points;
float[][] vel;
int searchDist;

PVector v1, v2;
//xPosition = points;
//yPosition = points;


void initPoints() {
   points = new float[100][2];
   vel = new float[100][2];

 for(int i=0; i<points.length; i++) {
   points[i][0] = random(width); // first point, x
   points[i][1] = random(height); // first point, y
 }
   for(int i=0; i<vel.length; i++) {
   vel[i][0] = random(-2,2); // first point, x
   vel[i][1] = random(-2,2); // first point, y
 }
}

void movePoints(){
    for(int i=0; i<points.length; i++) {
   points[i][0] +=vel[i][0]; // first point, x
   points[i][1] +=vel[i][1]; // first point, y
 }  
}

//move particles in smaller regions faster - over time- this is where i //get lost:
void moveFaster(PVector v1, PVector v2){
 PVector v1 = new PVector (random (100));
 PVector v2 = new PVector (random (100,200));
}
}
Re: Lee Byron's library Modification
Reply #1 - May 31st, 2010, 10:42am
 
I've downloaded the library (by downloading the demo at processingblogs) and looked at the source code a bit.  I added a drawPoints() method
Code:
void drawPoints() {
stroke(0);
fill(0);
for (int i = 0; i < points.length; ++i) {
ellipse(points[i][0], points[i][1], 5, 5);
}
}

and called it after drawMesh() so I could see the points and understand what was going on a little better.

So, each point gets its own region, a polygon, and you want the points with smaller polygons to move faster.  Assuming you can get the vertices of a point's polygon (I'm guessing this is what the getEdges() method is for?) then all you need is a formula to calculate the area of a polygon from the coordinates of its vertices, and then scale the velocity of the point by the area.

I don't know what that formula would be, but I think that's the general idea.


Re: Lee Byron's library Modification
Reply #2 - Jun 4th, 2010, 7:24am
 
thank you, will try this now, Smiley
Page Index Toggle Pages: 1