exporting points to txt file. Says it's exporting but really isn't.
in
Processing with Other Languages
•
2 years ago
Hey all. Thanks a lot with my last question, I ended up getting that to work. A
nyway, have a new one now. I'm working on splicing a code to export points into the existing code i am working on, and it runs, and it looks right, and it tells me the points have been exported... but in the file folder for the code is a zero kb "exportedpoints.txt"
anyone offer any help?
Code:
Ball[] ballies = new Ball[75];
attractor[] attractor = new attractor[25];
//........................................................
ArrayList pointList; // arraylist to store the points in
PrintWriter OUTPUT; // an instantiation of the JAVA PrintWriter object.
// This variable reappears in our custom export function
//........................................................
// setup
//........................................................
void setup() {
size(800, 800);
background(255);
//........................................................
pointList = new ArrayList(); // instantiate an ArrayList
//........................................................
for (int i=0; i<ballies.length; i++) {
ballies[i] = new Ball(
random(width), random(height), random(0,2),random(0,0),25);
}
for (int p=0; p < attractor.length; p++) {
attractor[p] = new attractor( width/8 + random(width), height/8 + random(height) );
smooth();
}
//........................................................
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
ellipse(V.x,V.y, 8.0, 8.0);
}
//........................................................ x
}
void mousePressed() {
setup();
}
//........................................................
void keyPressed(){
switch(key){
case 's':
saveFrame("importedPoints.jpg");
break;
case 'x':
exportPoints2Text();
break;
}
}
void exportPoints2Text(){
OUTPUT = createWriter("exportedPoints.txt");
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
OUTPUT.println(V.x + "" + V.y + "," + V.z); // here we export the coordinates of the vector using String concatenation!
}
OUTPUT.flush();
OUTPUT.close();
println("points have been exported");
}
//........................................................
void draw() {
for (int i=0; i<ballies.length; i++) {
ballies[i].update();
}
for (int p=0; p < attractor.length; p++) {
attractor[p].update();
}
}
//........................................................
class Ball {
// ball fields
float x;
float y;
float speedX;
float speedY;
int normSize;
float angA;
float angV = 0;
boolean dead = false;
PVector a;
PVector v, p, standardV, offsetV;
// ball constructor
Ball(float _x, float _y, float _speedX, float _speedY, int _normSize) {
p = new PVector(_x, _y);
v = new PVector(_speedX, _speedY);
standardV = new PVector(_speedX, _speedY);
normSize = _normSize;
}
// ball methods
/*
void update() {
position();
checkCollisions();
drawBall();
}
*/
///////////////////////////////
void update() {
if (!dead) {
a = new PVector();
for (int i = 0; i < attractor.length; i++) {
if (dist(p.x, p.y, attractor[i].x, attractor[i].y) < 200) {
stroke(#ff0000, 2.5);
line(p.x, p.y, attractor[i].x, attractor[i].y);
a.add(attractor[i].getAccel(p.x,p.y));
}
}
angA = a.heading2D();
angV += angA/.25;
// render();
v = new PVector( standardV.x, standardV.y);
a.normalize();
a.mult(v.mag());
a.mult(0.9);
v.add(a);
checkCollisions();
p.add(v);
angV *= .9;
}
drawBall();
}
/////////////////////////////////////
void drawBall() {
stroke(0);
strokeWeight(.05);
//ellipse(p.x, p.y, 25, 25);
point (p.x, p.y);
}
void checkCollisions() {
if(p.x + v.x > width || p.x + v.x < 0) {
v.x *= -1;
standardV.x *= -1;
}
if(p.y + v.y > height || p.y + v.y < 0) {
v.y *= -1;
}
}
}
////////////////////////////////////////////////////////////////////
class attractor {
float x,y;
attractor(float _x, float _y ) {
x = _x;
y = _y;
}
void update() {
render();
}
void render() {
stroke(255,0,0);
}
PVector getAccel(float px, float py) {
PVector a = new PVector(0,0,0);
float d2 = sq(dist(px,py,x,y));
if (d2 > 10) {
a.x = G * (x-px) / d2;
a.y = G * (y-py) / d2;
}
return a;
}
}
1