ShifmannFlock3D toxiclibs drawing a line between two points of imported txt strings
in
Contributed Library Questions
•
5 months ago
Hello,
I am dealing with "ShifmannFlock3D library toxiclibs" script.
I imported points:
- String points1[] = loadStrings("C:/Users/P3tr4s/Documents/PROGRAMMING/eclipse64/Plethora_agent_folow_curve_modify1/bin/data/bezier1.txt");
- String points1[] = loadStrings("C:/Users/P3tr4s/Documents/PROGRAMMING/eclipse64/Plethora_agent_folow_curve_modify1/bin/data/bezier1.txt");
txt files links:
Everything works fine.
But I want somehow get access separately to a list of points1 and points2 to draw a line between point1 and point2.
I know it this would be simple question for you. Please help me to draw a line between these two strings.
Here is the script:
- import processing.opengl.*;
- import toxi.processing.*;
- import toxi.geom.*;
- import toxi.geom.mesh.*;
- import toxi.math.*;
- import peasy.*;
- ArrayList<Vec3D> importPos = new ArrayList<Vec3D>();
- ArrayList<Vec3D> importPos2 = new ArrayList<Vec3D>();
- ArrayList<Vec3D> importPos3 = new ArrayList<Vec3D>();
- ArrayList<Vec3D> importPos4 = new ArrayList<Vec3D>();
- int DIM = 600;
- int NUM = 200;
- int NEIGHBOR_DIST = 50;
- int SEPARATION = 25;
- float BOID_SIZE = 5;
- Flock flock;
- Flock flock1;
- PeasyCam cam;
- ToxiclibsSupport gfx;
- void setup() {
- size(1280, 800, OPENGL);
- cam = new PeasyCam(this, 200, 500, 700, 400);
- flock = new Flock();
- smooth();
- String points1[] = loadStrings("C:/Users/P3tr4s/Documents/PROGRAMMING/eclipse64/Plethora_agent_folow_curve_modify1/bin/data/bezier1.txt");
- for (int i = 0; i < (points1.length); i++) {
- String[] txtCoord = split(points1[i], ",");
- float x = Float.parseFloat(txtCoord[0]);
- float y = Float.parseFloat(txtCoord[1]);
- float z = Float.parseFloat(txtCoord[2]);
- Vec3D pos = new Vec3D(x, -y, z+300);
- importPos.add(pos);
- }
- String points2[] = loadStrings("C:/Users/P3tr4s/Documents/PROGRAMMING/eclipse64/Plethora_agent_folow_curve_modify1/bin/data/bezier2.txt");
- for (int i = 0; i < (points2.length); i++) {
- String[] txtCoord = split(points2[i], ",");
- float x = Float.parseFloat(txtCoord[0]);
- float y = Float.parseFloat(txtCoord[1]);
- float z = Float.parseFloat(txtCoord[2]);
- Vec3D pos2 = new Vec3D(x, -y, z+300);
- importPos2.add(pos2);
- }
- //ARRAYLIST OF PARTICLES
- for (int i = 0; i < importPos.size(); i++) {
- Vec3D pos = importPos.get(i);
- flock.addBoid(new Boid(new Vec3D(pos.x, pos.y, pos.z), .12, 0.002, NEIGHBOR_DIST, SEPARATION));
- }
- for (int i = 0; i < importPos2.size(); i++) {
- Vec3D pos = importPos2.get(i);
- flock.addBoid(new Boid(new Vec3D(pos.x, pos.y, pos.z), .12, 0.002, NEIGHBOR_DIST, SEPARATION));
- }
- }
- void draw() {
- background(0);
- lights();
- translate(width/2, height/2, 0);
- noFill();
- stroke(255, 50);
- box(DIM*2);
- fill(255);
- noStroke();
- flock.run();
- }
- // Boid class
- // Methods for Separation, Cohesion, Alignment added
- class Boid {
- Vec3D loc;
- Vec3D vel;
- Vec3D acc;
- float maxforce;
- float maxspeed;
- float neighborDist;
- float desiredSeparation;
- Boid(Vec3D l, float ms, float mf, float nd, float sep) {
- loc=l;
- acc = new Vec3D();
- vel = Vec3D.randomVector();
- maxspeed = ms;
- maxforce = mf;
- neighborDist=nd*nd;
- desiredSeparation=sep;
- }
- void run(ArrayList boids) {
- flock(boids);
- update();
- borders();
- render();
- }
- // We accumulate a new acceleration each time based on three rules
- void flock(ArrayList boids) {
- Vec3D sep = separate(boids); // Separation
- Vec3D ali = align(boids); // Alignment
- Vec3D coh = cohesion(boids); // Cohesion
- // Arbitrarily weight these forces
- sep.scaleSelf(1.5);
- ali.scaleSelf(1.0);
- coh.scaleSelf(1.0);
- // Add the force vectors to acceleration
- acc.addSelf(sep);
- acc.addSelf(ali);
- acc.addSelf(coh);
- }
- // Method to update location
- void update() {
- // Update velocity
- vel.addSelf(acc);
- // Limit speed
- vel.limit(maxspeed);
- loc.addSelf(vel);
- // Reset accelertion to 0 each cycle
- acc.clear();
- }
- void seek(Vec3D target) {
- acc.addSelf(steer(target, false));
- }
- void arrive(Vec3D target) {
- acc.addSelf(steer(target, true));
- }
- // A method that calculates a steering vector towards a target
- // Takes a second argument, if true, it slows down as it approaches the target
- Vec3D steer(Vec3D target, boolean slowdown) {
- Vec3D steer; // The steering vector
- Vec3D desired = target.sub(loc); // A vector pointing from the location to the target
- float d = desired.magnitude(); // Distance from the target is the magnitude of the vector
- // If the distance is greater than 0, calc steering (otherwise return zero vector)
- if (d > 0) {
- // Normalize desired
- desired.normalize();
- // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
- if ((slowdown) && (d < 100.0f)) desired.scaleSelf(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary
- else desired.scaleSelf(maxspeed);
- // Steering = Desired minus Velocity
- steer = desired.sub(vel).limit(maxforce); // Limit to maximum steering force
- }
- else {
- steer = new Vec3D();
- }
- return steer;
- }
- void render() {
- stroke(255);
- strokeWeight(1);
- point(loc.x, loc.y, loc.z);
- }
- // Wraparound
- void borders() {
- if (loc.x < -DIM) loc.x = DIM;
- if (loc.y < -DIM) loc.y = DIM;
- if (loc.z < -DIM) loc.z = DIM;
- if (loc.x > DIM) loc.x = -DIM;
- if (loc.y > DIM) loc.y = -DIM;
- if (loc.z > DIM) loc.z = -DIM;
- }
- // Separation
- // Method checks for nearby boids and steers away
- Vec3D separate (ArrayList boids) {
- Vec3D steer = new Vec3D();
- int count = 0;
- // For every boid in the system, check if it's too close
- for (int i = boids.size()-1 ; i >= 0 ; i--) {
- Boid other = (Boid) boids.get(i);
- if (this != other) {
- float d = loc.distanceTo(other.loc);
- // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
- if (d < desiredSeparation) {
- // Calculate vector pointing away from neighbor
- Vec3D diff = loc.sub(other.loc);
- diff.normalizeTo(1.0/d);
- steer.addSelf(diff);
- count++;
- }
- }
- }
- // Average -- divide by how many
- if (count > 0) {
- steer.scaleSelf(1.0/count);
- }
- // As long as the vector is greater than 0
- if (steer.magSquared() > 0) {
- // Implement Reynolds: Steering = Desired - Velocity
- steer.normalizeTo(maxspeed);
- steer.subSelf(vel);
- steer.limit(maxforce);
- }
- return steer;
- }
- // Alignment
- // For every nearby boid in the system, calculate the average velocity
- Vec3D align (ArrayList boids) {
- Vec3D steer = new Vec3D();
- int count = 0;
- for (int i = boids.size()-1 ; i >= 0 ; i--) {
- Boid other = (Boid) boids.get(i);
- if (this != other) {
- if (loc.distanceToSquared(other.loc) < neighborDist) {
- steer.addSelf(other.vel);
- count++;
- }
- }
- }
- if (count > 0) {
- steer.scaleSelf(1.0/count);
- }
- // As long as the vector is greater than 0
- if (steer.magSquared() > 0) {
- // Implement Reynolds: Steering = Desired - Velocity
- steer.normalizeTo(maxspeed);
- steer.subSelf(vel);
- steer.limit(maxforce);
- }
- return steer;
- }
- // Cohesion
- // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
- Vec3D cohesion (ArrayList boids) {
- Vec3D sum = new Vec3D(); // Start with empty vector to accumulate all locations
- int count = 0;
- for (int i = boids.size()-1 ; i >= 0 ; i--) {
- Boid other = (Boid) boids.get(i);
- if (this != other) {
- if (loc.distanceToSquared(other.loc) < neighborDist) {
- sum.addSelf(other.loc); // Add location
- count++;
- }
- }
- }
- if (count > 0) {
- sum.scaleSelf(1.0/count);
- return steer(sum, false); // Steer towards the location
- }
- return sum;
- }
- }
- class Flock {
- ArrayList boids; // An arraylist for all the boids
- Flock() {
- boids = new ArrayList(); // Initialize the arraylist
- }
- void run() {
- for (int i = boids.size()-1 ; i >= 0 ; i--) {
- Boid b = (Boid) boids.get(i);
- b.run(boids); // Passing the entire list of boids to each boid individually
- }
- }
- void addBoid(Boid b) {
- boids.add(b);
- }
- }