Exporting Barnsley fern to SVG

Hello. I’m going to start by saying that I’m very new to coding. Sorry for my inexperience!

I’m trying to export a Barnsley fern code I found from Rosetta Code to an SVG. It needs to be SVG as it is for a vector art project that is not compatible with PDF files. I’ve tried using the methods found here but the problem seems to be the set line. Running the code as-is gives a “RuntimeException: No set() for PGraphicsSVG” error message.

I am admittedly out of my depth here, so I’m hoping that someone here will be able to help. I’ve pasted the code as I’ve tried to edit it:

import processing.svg.*;

void setup() {
  size(325, 725);
  background(219, 226, 233);
  noLoop();
  beginRecord(SVG, "Fern.svg");
}
 
float x = 0;
float y = 0;
 
void draw() {
 
  for (int i = 0; i < 155000000; i++) {
 
    float xt = 0;
    float yt = 0;
 
    float r = random(100);
 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt =  0.26*x + 0.24*y + 0.44;
    } else {
      xt =  0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
 
    x = xt;
    y = yt;
 
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    
    set(m, n, #009867);
    
  }
  endRecord();
}

And the original code from Rosetta Code, for sake of comparison:

import processing.svg.*;

void setup() {
  size(325, 725);
  background(219, 226, 233);
}
 
float x = 0;
float y = 0;
 
void draw() {
 
  for (int i = 0; i < 155000000; i++) {
 
    float xt = 0;
    float yt = 0;
 
    float r = random(100);
 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt =  0.26*x + 0.24*y + 0.44;
    } else {
      xt =  0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
 
    x = xt;
    y = yt;
 
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    
    set(m, n, #009867);
    
  }
  noLoop();
}

Thank you for any help you can provide!

1 Like

sorry,
it’s just that i think that

works on pixels ( of a image or PGraphics )

and that is NOT a vector thing at all, so not works for SVG

Many methods, particularly pixel-based methods, don’t make sense for SVG renderers. This includes: loadPixels, updatePixels, get, set, mask, filter, copy, blend, save, and image


BUT

// https://rosettacode.org/wiki/Barnsley_fern
// https://www.gnu.org/licenses/old-licenses/fdl-1.2.html

import processing.svg.*;

int imax = 15500; //155000000
String outfile = "data/Fern.svg";
float x = 0, y = 0;

void setup() {
  size(325, 725);
  background(219, 226, 233);
  noLoop();
  beginRecord(SVG, outfile);
}

void draw() { 
  for (int i = 0; i < imax; i++) { 
    float xt = 0;
    float yt = 0; 
    float r = random(100); 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt =  0.26*x + 0.24*y + 0.44;
    } else {
      xt =  0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
    x = xt;
    y = yt;
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    //    set(m, n, #009867);
    stroke(#009867);
    point(m, n);
  }
  endRecord();
  println("saved:"+outfile);
}


did give some result.

2 Likes

Thank you for your work. The SVG did get a result…but it was completely unrecognizable. The pixels didn’t render properly, as you said. PDF didn’t work either.

Since the vector didn’t work, how would I export this in a high resolution image, say, a BMP or PNG? Does Processing have the ability to do that? I know I’d need to change the size parameters but other than that I don’t know what I’d need to do.

yes, we know that SVG import and export is limited in several ways in that version.

for a export to a image file pls. experiment with
https://processing.org/reference/save_.html

oh yes, it is good style to have in the code the link, where you get it from!

If you are prepared to investigate contextfreeart as an alternative to processing, it is great at rendering fractals such as the Barnsley fern in high resolution. It is also possible to render as svg. PS there is a Barnsley fern in the gallery at https://contextfreeart.org

3 Likes

Thanks to both of you! Exporting as a BMP, then tracing in Inkscape to convert to a vector solved the problem.