Need help with controlling images
import traer.physics.*;
final float NODE_SIZE = 10;
final float EDGE_LENGTH = 20;
final float EDGE_STRENGTH = 0.2;
final float SPACER_STRENGTH = 1000;
ParticleSystem physics;
float scale = 1;
float centroidX = 0;
float centroidY = 0;
int counter = 0;
String[] loadFilenames;
PImage[] loadPhotos;
int fileCount;
void setup()
{
size( 1280, 720 );
smooth();
strokeWeight( 2 );
ellipseMode( CENTER );
loadPhotos = new PImage[10000];
loadFilenames();
loadPhotos();
fileCount = loadFilenames.length;
physics = new ParticleSystem( 0, 0.1 );
initialize();
}
void draw()
{
physics.tick();
if ( physics.numberOfParticles() > 1 )
updateCentroid();
background( 255 );
fill( 0 );
text( "" + physics.numberOfParticles() + " PARTICLES\n" + (int)frameRate + " FPS", 10, 20 );
translate( width/2, height/2 );
scale( scale );
translate( -centroidX, -centroidY );
drawNetwork();
}
void loadFilenames() {
java.io.File folder = new java.io.File(dataPath(sketchPath + "/data"));
java.io.FilenameFilter imgFilter = new java.io.FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg")
|| name.toLowerCase().endsWith(".png")
|| name.toLowerCase().endsWith(".tif")
|| name.toLowerCase().endsWith(".tga");
}
};
loadFilenames = folder.list(imgFilter);
}
void loadPhotos() {
for (int i = fileCount; i < loadFilenames.length; i++) { // only load new images
loadPhotos[i] = loadImage(loadFilenames[i]);
}
}
void drawNetwork()
{
fill( 160 );
noStroke();
for ( int i = 0; i < physics.numberOfParticles(); ++i )
{
Particle v = physics.getParticle( i );
println(physics.getParticle(i));
ellipse( v.position().x(), v.position().y(), NODE_SIZE, NODE_SIZE );
imageMode(CENTER);
image(loadPhotos[i], v.position().x(), v.position().y(), width/50, height/50);
}
// draw edges
stroke( 0 );
beginShape( LINES );
for ( int i = 0; i < physics.numberOfSprings(); ++i )
{
Spring e = physics.getSpring( i );
Particle a = e.getOneEnd();
Particle b = e.getTheOtherEnd();
vertex( a.position().x(), a.position().y() );
vertex( b.position().x(), b.position().y() );
}
endShape();
}
void mousePressed()
{
addNode();
}
void updateCentroid()
{
float
xMax = Float.NEGATIVE_INFINITY,
xMin = Float.POSITIVE_INFINITY,
yMin = Float.POSITIVE_INFINITY,
yMax = Float.NEGATIVE_INFINITY;
for ( int i = 0; i < physics.numberOfParticles(); ++i )
{
Particle p = physics.getParticle( i );
xMax = max( xMax, p.position().x() );
xMin = min( xMin, p.position().x() );
yMin = min( yMin, p.position().y() );
yMax = max( yMax, p.position().y() );
}
float deltaX = xMax-xMin;
float deltaY = yMax-yMin;
centroidX = xMin + 0.5*deltaX;
centroidY = yMin +0.5*deltaY;
if ( deltaY > deltaX )
scale = height/(deltaY+50);
else
scale = width/(deltaX+50);
}
void addSpacersToNode( Particle p, Particle r )
{
for ( int i = 0; i < physics.numberOfParticles(); ++i )
{
Particle q = physics.getParticle( i );
if ( p != q && p != r )
physics.makeAttraction( p, q, -SPACER_STRENGTH, 20 );
}
}
void makeEdgeBetween( Particle a, Particle b )
{
physics.makeSpring( a, b, EDGE_STRENGTH, EDGE_STRENGTH, EDGE_LENGTH );
}
void initialize()
{
physics.clear();
physics.makeParticle();
}
void addNode()
{
Particle p = physics.makeParticle();
Particle q = physics.getParticle( (int)random( 0, physics.numberOfParticles()-1) );
while ( q == p )
q = physics.getParticle( (int)random( 0, physics.numberOfParticles()-1) );
addSpacersToNode( p, q );
makeEdgeBetween( p, q );
p.position().set( q.position().x() + random( -1, 1 ), q.position().y() + random( -1, 1 ), 0 );
}