# Draw from text file not working as expected

**URL:** <https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498>\
**Category:** Coding Questions\
**Created:** [February 18, 2019, 1:22am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498 "2019-02-18T01:22:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 18, 2019, 1:22am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/1 "2019-02-18T01:22:13Z")

</div>

Hi all,

I have a script which finds all the black pixels in an image along a spiral path and saves it to a text file.

```auto
float r = 0;
float theta = 0;
PImage input;
String[] output;

void setup() {
  size(300, 300);
  background(255);
  output = new String[1];
  output[0] = "";
  selectInput("select an image to process", "select");
}
void select(File selection){
  if (selection == null){
    exit();
  }
  else {
    input = loadImage(selection.getAbsolutePath());
    input.resize(width, height);
    input.filter(THRESHOLD, 0.5);
    input.loadPixels();

  while (z < 49950) {
  // Polar to Cartesian conversion
  float x = r * cos(theta);
  float y = r * sin(theta);

  
  int xx = int((x+width/2));
  int yy = int((y+height/2));
  if (z > 2000) {
  if(input.pixels[yy * width + xx] == color(0)) {
 
    output[0] += z + "\n";
    println(z);
 
  }
  }
  // Increment the angle
  theta += 0.01;
  // Increment the radius
  r += 0.003; //0.001 covers all pixels
  z = z + 1;
}
saveStrings("positions.txt", output);
println("done!");
}
  }

```

The second script should read the text file and recreate the image along a spiral path but it is not doing so.

```auto
 float r = 0;
float theta = 0;
int count = 0;

void setup() {
  size(300, 300);
  background(255);
  parseFile();
}

void parseFile() {
  // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    while ((line = reader.readLine()) != null) {
    float x = r * cos(theta);
    float y = r * sin(theta);
  // Adjust for center of window
    float xx = (x+width/2);
    float yy = (y+height/2);
    int giraffe = int(line);
    if (giraffe == count) {
    point(x, y);
    }
    // Increment the angle
    theta += 0.01;
    // Increment the radius
    r += 0.003;
    count = count +1;
    println(count);
    
    }
    reader.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

```

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [February 18, 2019, 3:59am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/2 "2019-02-18T03:59:11Z")

</div>

your code is a bit confusing for me.

there is no z variable in the first code.  
and in the second giraffe and count never match so no points will be drawn.  
xx and yy vars are never used

i’m not sure what the giraffe == count test is for truthfully nor why are you exporting to a text file then importing it. wouldn’t it be easier to process it all in one sketch?

if you add a z var to the first sketch and use the below code in place of the second you will get an output but… well have a look for yourself as i’m not sure if it is the output you are seeking.

```auto
float r = 0;
float theta = 0;
int count = 0;
ArrayList<float[]> points;

void setup() {
  size(300, 300);
  surface.setSize(300,300);
  background(255);
  fill(255, 0, 0);
points = new ArrayList<float[]>();
  parseFile();
}

void draw() {
  translate(width / 2, height / 2);
  for(int i = 0; i < points.size(); i++) {
    float[] p = points.get(i);
    point(p[0], p[1]);
  }
}

void parseFile() {
  // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    while ((line = reader.readLine()) != null) {
      float x = r * cos(theta);
      float y = r * sin(theta);
      // Adjust for center of window
      //you never use the xx and yy variables
      //float xx = (x+width/2);
      //float yy = (y+height/2);
      int giraffe = int(line);
      
      //you can see in the console they never match
      println("giraffe: " + giraffe + " count: " + count);
      
      points.add(new float[] {x,y});
      
      // Increment the angle
      theta += 0.01;
      // Increment the radius
      r += 0.003;
      count = count +1; 
    }
    reader.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

```

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [February 18, 2019, 4:48am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/3 "2019-02-18T04:48:31Z")

</div>

after having another look i actually think this is more what you were aiming for.

![paris](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1597b7b9cda07e967986e82ad838dd52abf8c788.png)

becomes

![parisSomething](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c2e4db51a1b671eac137181db52014c84b97ed72.png)

if you had an example of the desired output it would make it alot easier to help.

```auto
float r = 0;
float theta = 0;
PImage input;
String[] output;

void setup() {
  size(300, 300);
  surface.setSize(300, 300);
  background(255);
  output = new String[1];
  output[0] = "";
  selectInput("select an image to process", "select");
}

void select(File selection) {
  if (selection == null) {
    exit();
  } else {
    input = loadImage(selection.getAbsolutePath());
    input.resize(width, height);
    input.filter(THRESHOLD, 0.5);
    input.loadPixels();

    int z = 0;
    while (z < 49950) {
      // Polar to Cartesian conversion
      float x = r * cos(theta);
      float y = r * sin(theta);

      int xx = int((x+width/2));
      int yy = int((y+height/2));
      if (z > 2000) {
        if (input.pixels[yy * width + xx] == color(0)) {
          output[0] += x + "," + y + "\n";
        }
      }
      // Increment the angle
      theta += 0.01;
      // Increment the radius
      r += 0.003; //0.001 covers all pixels
      z = z + 1;
    }
    saveStrings("positions.txt", output);
    println("done!");
  }
}

```

```auto
ArrayList<PVector> points;
void setup() {
  size(300, 300);
  surface.setSize(300, 300);
  background(255);
  fill(255, 0, 0);
  points = new ArrayList<PVector>();
  parseFile();
}

void draw() {
  translate(width / 2, height / 2);
  fill(255, 255, 0);
  for (int i = 0; i < points.size(); i++) {
    PVector p = points.get(i);
    point(p.x, p.y);
  }
}

void parseFile() {
  // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    fill(255, 0, 0);
    while ((line = reader.readLine()) != null) {
      //println(line);
      String[] vals = split(line, ",");
      if (vals.length > 1) {
        points.add(new PVector(float(vals[0]), float(vals[1])));
      }
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 18, 2019, 5:37am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/4 "2019-02-18T05:37:25Z")

</div>

your while loop goes for unlimited z ( now called count )  
and reads lines,  
instead it should read one line, count z until is equal line,  
draw pix ( not at x,y better xx,yy )  
and read next line.

```auto
void parseFile() { // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    line = reader.readLine(); // read first line
    while ( line != null && z < 49950) {
      if (int(line) == z) {   
        float x = r * cos(theta);
        float y = r * sin(theta);
        // Adjust for center of window
        float xx = (x+width/2);
        float yy = (y+height/2);
        point(xx, yy);
        line = reader.readLine(); // next line
      }
      // Increment the angle
      theta += 0.01;
      // Increment the radius
      r += 0.003;
      z++;
      println(z);
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

```

![2019-02-18_13-02-16_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2da1a286d0f60103a04f049510ee1d5c75df2f20.png)

![2019-02-18_13-03-46_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4caba1618ed44654fbe843ab2b28963cf4d32082.png)

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 18, 2019, 4:30pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/5 "2019-02-18T16:30:32Z")

</div>

Thank you!! That’s 95% correct. The final drawing looks correct but the text file is formatted in a way that doesn’t look as it needs to be. The text file should just be points along the spiral path that were black in the source image, not x-y coordinates. The drawing file needs to increment along the same spiral path and read line by line the text file. If the step along the spiral equals the current count in the text file, then it can draw at that position.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 19, 2019, 4:29am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/7 "2019-02-19T04:29:26Z")

</div>

~~Awesome~~~~! ~~~~ Can ~~~~you~~~~ provide ~~~~the~~~~ whole ~~~~script~~~~? ~~~~ It ~~~~doesn~~~~ ’ ~~~~t~~~~ do ~~~~anything~~~~ when ~~~~I~~~~ run ~~~~it~~~~ as ~~~~you~~~~ pasted ~~~~.~~

i use one file and /\* \*/ to run

- step get
- step set

```auto
// https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498

float r = 0;
float theta = 0;
int z = 0;

void setup() {
  size(300, 300);
  background(255);
  parseFile();
}

void parseFile() { // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    line = reader.readLine(); // read first line
    while ( line != null && z < 49950) {
      if (int(line) == z) {   
        float x = r * cos(theta);
        float y = r * sin(theta);
        // Adjust for center of window
        float xx = (x+width/2);
        float yy = (y+height/2);
        point(xx, yy);
        line = reader.readLine(); // next line
      }
      // Increment the angle
      theta += 0.01;
      // Increment the radius
      r += 0.003;
      z++;
      println(z);
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

// slo mo version with show
/*
float r = 0;
 float theta = 0;
 PImage input; // from file explorer
 boolean gotit = false;
 String[] output;
 String infile= "data/moonwalker.jpg", outfile = "data/positions.txt";
 int z=0;
 color cr = color(200, 0, 0);
 color cg = color(0, 200, 0);
 color cu;
 
 void setup() {
 size(600, 300);
 background(255);
 output = new String[1];
 output[0] = "";
 selectInput("select an image to process", "select");
 }
 
 
 void select(File selection) {
 if (selection == null) {
 exit();
 } else {
 infile = selection.getAbsolutePath();
 println("load: "+infile);
 input = loadImage(infile);
 input.resize(width, height);
 input.filter(THRESHOLD, 0.5);
 gotit = true;
 }
 }
 
 
 void read_black() {
 input.loadPixels();
 //println("z "+z);
 if (z < 49950) {
 // Polar to Cartesian conversion
 float x = r * cos(theta);
 float y = r * sin(theta);
 int xx = int((x+width/2));
 int yy = int((y+height/2));
 int ppos = yy * width + xx;
 if (z > 2000) {
 if (input.pixels[ppos] == color(0)) { output[0] += z + "\n"; println("black "+z); cu = cr; }
 else cu = cg;
 input.pixels[ppos] = cu;
 input.updatePixels();
 }
 // Increment the angle
 theta += 0.01;
 // Increment the radius
 r += 0.003; //0.001 covers all pixels
 z = z + 1;
 } else {
 saveStrings(outfile, output);
 println("write "+outfile);
 exit();
 }
 }
 
 void draw() {
 if (gotit) { 
 image(input, 0, 0);
 read_black();
 }
 }
 */

```

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 19, 2019, 4:44am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/8 "2019-02-19T04:44:05Z")

</div>

Thanks! It is working now.

Script recreation of image from text file:  
 ![sim-output](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b1cf893458888fbad61fef55ecd66f0a3f05c3c8.png)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 19, 2019, 9:32am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/9 "2019-02-19T09:32:51Z")

</div>

> [@Unable to update pixels from text file containing pixel locations](https://discourse.processing.org/t/unable-to-update-pixels-from-text-file-containing-pixel-locations/2021/12):
>
> This is for a tractor that sprays pesticide in a pattern generated from an map image. The tractor reads the text file. Originally, it simply drove in a zig-zag pattern and if the number of wheel rotations equals the number in the text file, it would spray for a few seconds in that location. Unfortunately, uneven terrain and traction caused it to not maintain the correct heading zig zag path. (not straight lines or 180 degree turns as expected, with a compass sensor). Because of this, the current iteration of the tractor is tethered to a stake. As it orbits, the tether wraps around the stake and pulls the tractor in in an involute spiral. I needed to change the code for generating and testing the directions file to a spiral path instead of 1d pixel indices.

i think your math is still wrong!  
your last code goes by a const angle, that means a increasing speed / way as the R grows also,  
while you need constant way.

you should keep the spiral  
but find a const distance between 2 points on it.

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 19, 2019, 3:53pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/10 "2019-02-19T15:53:59Z")

</div>

Do you mean like equidistant points along a spiral? Looking for a processing compatible formula for that now. I found this clarkston scroll formula: `t = 2 * Pi * Sqrt(2 * s / a)` but I don’t understand how to implement it.

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 19, 2019, 5:45pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/11 "2019-02-19T17:45:24Z")

</div>

This is what I have so far but I do not understand how to integrate the S variable from the clarkston scroll formula:

```auto
float r = 0;
float t = 0;
int z = 0;
int s = 0;
PImage input;
String[] output;

void setup() {
  size(300, 300);
  background(255);
  output = new String[1];
  output[0] = "";
  selectInput("select an image to process", "select");
}
void select(File selection){
  if (selection == null){
    exit();
  }
  else {
    input = loadImage(selection.getAbsolutePath());
    input.resize(width, height);
    input.filter(THRESHOLD, 0.5);
    input.loadPixels();

  //while (z < 49950) {
  // Polar to Cartesian conversion
  float x = r * cos(t);
  float y = r * sin(t);

t = 2 * PI * sqrt(2 * s / r);
  
  int xx = int((x+width/2));
  int yy = int((y+height/2));
  //if (z > 2000) {
  if(input.pixels[yy * width + xx] == color(0)) {
 
    output[0] += z + "\n";
    println(s);
 
  }
  //}
  // Increment the angle
  t += 0.01;
  // Increment the radius
  r += 0.003; //0.001 covers all pixels
  z = z + 1;
}
saveStrings("positions.txt", output);
println("done!");
}
  //}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 20, 2019, 12:08pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/12 "2019-02-20T12:08:53Z")

</div>

i found something what looks good,  
imagine that as measuring points on the picture  
and later as rounds of the tractor wheel  
( what’s circumference is the distance of the points )

```auto
// equidistant spiral points
// https://stackoverflow.com/questions/13894715/draw-equidistant-points-on-a-spiral

void spiral() {
  float coils = 10, radius =200, rotation = 0.1;
  float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
  float awayStep = radius / thetaMax; // How far to step away from center for each side.
  float chord = 10; // distance between points to plot

  // For every side, step around and away from center. // start at the angle corresponding to a distance of chord // away from centre.
  for ( float theta = chord / awayStep; theta <= thetaMax; ) {
    float away = awayStep * theta; // How far away from center
    float around = theta + rotation; // How far around the center.
    float x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
    float y = height/2 + sin ( around ) * away;
    circle( x, y, 5 ); // show something
    theta += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  }
}

void setup() {
  size( 500, 500);
  background(200, 200, 0);
  stroke(200, 0, 0);
  spiral();
}

void draw() {
}

```

![2019-02-20_19-08-15_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/88600413b5346492c9f4ccd3cfa2743ceb9e5030.png)

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 20, 2019, 3:56pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/13 "2019-02-20T15:56:34Z")

</div>

I integrated your math into the picture-to-coordinates-file-generator sketch but it throws an error message and doesn’t save the text file.

```auto

PImage input;
String[] output;
int z = 0;

void setup() {
  size(300, 300);
  background(255);
  output = new String[1];
  output[0] = "";
  selectInput("select an image to process", "select");
}
void select(File selection){
  if (selection == null){
    exit();
  }
  else {
    input = loadImage(selection.getAbsolutePath());
    input.resize(width, height);
    input.filter(THRESHOLD, 0.5);
    input.loadPixels();
  
//void spiral() {
  float coils = 10, radius =200, rotation = 0.1;
  float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
  float awayStep = radius / thetaMax; // How far to step away from center for each side.
  float chord = 10; // distance between points to plot

  // For every side, step around and away from center. // start at the angle corresponding to a distance of chord // away from centre.
  for ( float theta = chord / awayStep; theta <= thetaMax; ) {
    float away = awayStep * theta; // How far away from center
    float around = theta + rotation; // How far around the center.
    float x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
    float y = height/2 + sin ( around ) * away;
    
    int xx = int(x);
    int yy = int(y);
    int loc = input.get(xx,yy);
    //
    if (loc == color(0)) {
    //if(input.pixels[yy * width + xx] == color(0)) {
      //if (input.pixels[yy + yy * width] == color(0)) {
    output[0] += away + "\n";
    println(away);
 }
    
    ///point( x, y); // show something
    theta += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  }

}
saveStrings("positions.txt", output);
println("done!");
}

```

recreator sketch:

```auto
 float coils = 10, radius =200, rotation = 0.1;
  float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
  float awayStep = radius / thetaMax; // How far to step away from center for each side.
  float chord = 10; // distance between points to plot
  int z = 0;

void setup() {
  size(300, 300);
  background(255);
  parseFile();
}

void parseFile() { // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    line = reader.readLine(); // read first line
    while ( line != null) {
      
      for ( float theta = chord / awayStep; theta <= thetaMax; ) {
    float away = awayStep * theta; // How far away from center
    float around = theta + rotation; // How far around the center.
    float x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
    float y = height/2 + sin ( around ) * away;
    
    int xx = int(x);
    int yy = int(y);
    
      
      if (int(line) == away) {   
        //float x = r * cos(theta);
        //float y = r * sin(theta);
        // Adjust for center of window
        //float xx = (x+width/2);
        //float yy = (y+height/2);
        point(xx, yy);
        line = reader.readLine(); // next line
      }
      // Increment the angle
      //theta += 0.01;
      // Increment the radius
     // r += 0.003;
      //z++;
      //println(z);
       theta += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  }
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 20, 2019, 11:26pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/14 "2019-02-20T23:26:04Z")

</div>

> [@socraticbot](#):
>
> I integrated your math into the picture-to-coordinates-file-generator sketch but it throws an error message

-a- that is not my math, i just found it and had a link to it in my processing version,  
so i think it is good style that you keep that link too.

-b- what error message on what line?

-c- why you change from saving “z” counter to file  
now to store “away” variable?? ( ok, might work too , but comparing floats is hmmm )

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 21, 2019, 3:23pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/15 "2019-02-21T15:23:15Z")

</div>

Thanks for your feedback.

It’s kinda working now. I am just wondering which values to change if I want to adjust the away step and the distance between wheel steps so that it may reproduce the image better.

![sim-output%2022119](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/24f3539dee3292a5705c4f5cba854fd6efda6cc0.png)

```auto
// https://stackoverflow.com/questions/13894715/draw-equidistant-points-on-a-spiral
// https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498

  float coils = 10, radius =200, rotation = 0.1;
  float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
  float awayStep = radius / thetaMax; // How far to step away from center for each side.
  float chord = 10; // distance between points to plot
  int z = 0;
  
  void setup() {
  size(300, 300);
  background(255);
  parseFile();
}

void parseFile() { // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    line = reader.readLine(); // read first line
    while ( line != null) {
      
      for ( float theta = chord / awayStep; theta <= thetaMax; ) {
    float away = awayStep * theta; // How far away from center
    float around = theta + rotation; // How far around the center.
    float x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
    float y = height/2 + sin ( around ) * away;
    
    int xx = int(x);
    int yy = int(y);
    
      
      if (int(line) == z) {   
        
        point(xx, yy);
        line = reader.readLine(); // next line
      }
     
      z++;
      println(z);
       theta += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  }
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}
type or paste code here

```

```auto
// equidistant spiral points
// https://stackoverflow.com/questions/13894715/draw-equidistant-points-on-a-spiral
// https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498

PImage input;
String[] output;
int z = 0;

void setup() {
  size(300, 300);
  background(255);
  output = new String[1];
  output[0] = "";
  selectInput("select an image to process", "select");
}
void select(File selection){
  if (selection == null){
    exit();
  }
  else {
    input = loadImage(selection.getAbsolutePath());
    input.resize(width, height);
    input.filter(THRESHOLD, 0.5);
    input.loadPixels();
  
//void spiral() {
  float coils = 10, radius =200, rotation = 0.1;
  float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
  float awayStep = radius / thetaMax; // How far to step away from center for each side.
  float chord = 10; // distance between points to plot

  // For every side, step around and away from center. // start at the angle corresponding to a distance of chord // away from centre.
  for ( float theta = chord / awayStep; theta <= thetaMax; ) {
    float away = awayStep * theta; // How far away from center
    float around = theta + rotation; // How far around the center.
    float x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
    float y = height/2 + sin ( around ) * away;
    
    int xx = int(x);
    int yy = int(y);
    int loc = input.get(xx,yy);
    //
    if (loc == color(0)) {
        output[0] += z + "\n";
    //output[0] += away + "\n";
    println(away);
 }
    
    ///point( x, y); // show something
    theta += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  z++;
}

}
saveStrings("positions.txt", output);
println("done!");
}

```

Also, the recreator sketch doesn’t stop running on the end of the text file for some reason.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 22, 2019, 9:00am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/16 "2019-02-22T09:00:46Z")

</div>

i try to make a better setup,  
combine both tools,  
allow both spiral types …

```auto
import processing.pdf.*; // make pdf print
boolean record;

// selectable old new spiral,
// combine create and recreate
// save create picture to PDF

//variables for file handling
PImage input; // from file explorer
boolean usefileexplorer=false; // if false expect path / file 
String[] output;
String pdffile= "data/getblack.pdf", infile= "data/moonwalk.jpg", outfile = "data/positions.txt";

// colors for progress show, see also pdf
color cr = color(200, 0, 0);
color cg = color(0, 200, 0);
color cu;

// main program flow
int mode = 0; // can use = 2 for recreate only
boolean diagp = false; //true; //false;
int z=0, zstart= 0, zstop = 5000; //49950;

void setup() {
  size(600, 600);
  //noSmooth();
  background(255);
  output = new String[1];
  output[0] = "";
  if ( mode == 0 ) {
    if ( usefileexplorer ) selectInput("select an image to process", "select");
    else select(null);
  }
}

void select(File selection) {
  if (selection == null) ; // use preconfig path/file
  else infile = selection.getAbsolutePath();
  println("load: "+infile);
  input = loadImage(infile);
  input.resize(width, height);
  input.filter(THRESHOLD, 0.5);
  input.loadPixels();
  mode = 1;
  beginRecord(PDF, pdffile); 
  image(input, 0, 0);
}

// _____________________________________________________ variables and calc for old spiral
float r = 0, rpp = 0.005;//0.003;
float theta = 0, thetapp = 0.01;
float x, y;
int pixpos;

int const_theta_spiral() {
  x = r * cos(theta) + width/2; // Polar to Cartesian conversion
  y = r * sin(theta) + height/2;
  theta += thetapp; // Increment the angle // for next loop
  r += rpp; // Increment the radius //0.001 covers all pixels
  return (int)y * width + (int)x; // just return the pix position in the image to check on
}

// ______________________________________________________ variables for new spiral
boolean use_const_dist = true;
float coils = 50, radius = 200, rotation = 0.1;
float thetaMax = coils * 2 * PI; // value of theta corresponding to end of last coil
float away, around, awayStep = radius / thetaMax; // How far to step away from center for each side.
float chord = 5; // distance between points to plot
float theta2 = chord / awayStep; // start value

int const_dist_spiral() { // For every side, step around and away from center. // start at the angle corresponding to a distance of chord // away from centre.
  // for ( float theta = chord / awayStep; theta <= thetaMax; ) { // WARNING limit disabled ONLY ZSTOP used
  away = awayStep * theta2; // How far away from center
  around = theta2 + rotation; // How far around the center.
  x = width/2 + cos ( around ) * away; // Convert 'around' and 'away' to X and Y.
  y = height/2 + sin ( around ) * away;
  theta2 += chord / away; // to a first approximation, the points are on a circle so the angle between them is chord/radius
  // }
  return (int)y * width + (int)x; // just return the pix position in the image to check on
}

void read_black() {
  if (z < zstop) {
    if ( use_const_dist ) pixpos = const_dist_spiral();
    else pixpos = const_theta_spiral();
    if ( x < 0 || x > width ) zstop = z; // the zstop thing can be a bad idea, actually we must stop when x and y out of canvas ( or picture )
    if ( y < 0 || y > height ) zstop = z;    
    if (z > zstart && z < zstop ) {     
      if (input.pixels[pixpos] == color(0)) { 
        output[0] += z + "\n";  
        if ( diagp ) println("black "+z+" pixpos "+pixpos+" x "+x+" y "+y);
        cu = cr;
      } else cu = cg;
      stroke(cu); fill(cu);
      circle(x,y,2);
    }
    z = z + 1;
  } else {
    saveStrings(outfile, output);
    println("write "+outfile);
    mode = 2; // exit();
  }
}

void draw() {
  if ( mode == 1 ) { 
    read_black();
  }
  if ( mode == 2 ) { // do some resets
    endRecord(); // write PDF file
    println("saved to "+pdffile);

    background(200, 200, 0);
    r = 0.0;
    theta = 0.0;
    theta2 = chord / awayStep; 
    z = 0;
    stroke(200, 0, 0);
    println("read "+outfile);
    mode = 3;
  }
  if ( mode == 3 ) {
    parseFile();
  }
}

void parseFile() { // Open the file from the createWriter() example
  BufferedReader reader = createReader("positions.txt");
  String line = null;
  try {
    line = reader.readLine(); // read first line
    if ( use_const_dist ) pixpos = const_dist_spiral();
    else pixpos = const_theta_spiral();
    while ( line != null && z < zstop) {
      if ( use_const_dist ) pixpos = const_dist_spiral();
      else pixpos = const_theta_spiral();
      if (int(line) == z) {   
        circle(x,y,2); //point(x, y);
        line = reader.readLine(); // next line
        if ( diagp ) println("z "+z+" x "+x+" y "+y);
      }
      z++; //println(z);
    }
    reader.close();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

```

 ![2019-02-22_15-58-19_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c8eb073ab0643b7bdf22b185dbbf1f2c8403afff.png)

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 22, 2019, 4:51pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/17 "2019-02-22T16:51:29Z")

</div>

It works except for the PDF which won’t open. Here’s the error message:

load: C:\Users\g9duf\Desktop\New folder\sim-input.jpg  
java.lang.reflect.InvocationTargetException  
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at processing.core.PApplet.selectCallback(PApplet.java:6712)  
at processing.core.PApplet.access$1(PApplet.java:6705)  
at processing.core.PApplet$4.run(PApplet.java:6616)  
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)  
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)  
at java.awt.EventQueue.access$500(EventQueue.java:97)  
at java.awt.EventQueue$3.run(EventQueue.java:709)  
at java.awt.EventQueue$3.run(EventQueue.java:703)  
at java.security.AccessController.doPrivileged(Native Method)  
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)  
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)  
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)  
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)  
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)  
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)  
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)  
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)  
Caused by: java.lang.RuntimeException: Problem saving the PDF file.  
at processing.pdf.PGraphicsPDF.beginDraw(Unknown Source)  
at processing.core.PApplet.beginRecord(PApplet.java:11141)  
at processing.core.PApplet.beginRecord(PApplet.java:11130)  
at printgencheck22219.select(printgencheck22219.java:65)  
… 21 more  
Caused by: com.lowagie.text.exceptions.IllegalPdfSyntaxException: Unbalanced begin/end text operators.  
at com.lowagie.text.pdf.PdfContentByte.beginText(Unknown Source)  
at com.lowagie.text.pdf.PdfDocument.initPage(Unknown Source)  
at com.lowagie.text.pdf.PdfDocument.open(Unknown Source)  
at com.lowagie.text.Document.open(Unknown Source)  
… 25 more  
write data/positions.txt  
ExceptionConverter  
ExceptionConverter  
ExceptionConverter  
ExceptionConverter  
ExceptionConverter  
Copied to the clipboard. Use shift-click to search the web instead.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 22, 2019, 5:27pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/18 "2019-02-22T17:27:54Z")

</div>

did you save that code first?

- and create a /data/ and copy your picture in it…
- and change data/moonwalk.jpg to data/\< your pic \>

and position.txt and getblack.pdf will be written into that path too.

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 22, 2019, 6:30pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/19 "2019-02-22T18:30:13Z")

</div>

I identified the issue. It doesn’t accept .png image files. Only jpegs.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 23, 2019, 2:15am UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/20 "2019-02-23T02:15:58Z")

</div>

> [@socraticbot](#):
>
> It doesn’t accept .png image files. Only jpegs.

you still talk about PDF file?  
or you mean for the picture to analyze?

-a- well i tested it with a .png and it works too  
-b- the code i show you use the default picture from many processing examples  
“moonwalk.jpg”  
( so i sure not need to provide that picture )  
-c- if you want try a code you best start to run it “as is”  
in the default environment ( processing IDE ( now 3.5.3 ))  
without any changes.  
later you can delete it or use one or many lines in you own project.  
but testing it with a ?bad? picture what you never used in your own project not makes sense.

---

<div class="post-metadata">

**Author:** ![socraticbot](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@socraticbot](https://discourse.processing.org/u/socraticbot)\
**Post date:** [February 23, 2019, 5:11pm UTC](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498/21 "2019-02-23T17:11:01Z")

</div>

Yes it seems it doesn’t like certain image files or their location as it won’t open some. That’s acceptable. Is there a way to put each of those “print” messages into the bottom corner of the window as it progresses? I’d like to display those without the console eventually.

[Next page](https://discourse.processing.org/t/draw-from-text-file-not-working-as-expected/8498.md?page=2)
