A Processing Quine

Hi There,

Earlier I made a Quine in Processing, that is code that prints it’s source code without any input.

It works, it prints to the canvas what I can see in the text editor.

I am wondering if anyone can see any further optimizations that I could be making?
My java/processing knowledge isn’t exactly vast so I’m keen for any input!

I’ve based my Processing sketch on this java example: Github Java Quine Example

My Code is as below:

//Quine
//Thomas Day 19/7/21
//Processing 3.5.4

import processing.svg.*;
PFont font;

char R = 82; char o = 111; char m = 109; char a = 97; char n = 110; char S = 83;
char dot = 46; char s = 115; char v = 118; char g = 103;

void setup() {
  size(891, 1260);
  noLoop();
  String RomanS = new StringBuilder().append(R).append(o).append(m).append(a).append(n).append(S).toString();
  font = createFont(RomanS, 12);
}

void draw() {
  String file = new StringBuilder().append(S).append(dot).append(s).append(v).append(g).toString();
  beginRecord(SVG, file);
  char quote = 34;
  String[] string = {
    "//Quine", 
    "//Thomas Day 19/7/21", 
    "//Processing 3.5.4", 
    "", 
    "import processing.svg.*;", 
    "PFont font;", 
    "", 
    "char R = 82; char o = 111; char m = 109; char a = 97; char n = 110; char S = 83;", 
    "char dot = 46; char s = 115; char v = 118; char g = 103;", 
    "", 
    "void setup() {", 
    "  size(594, 840);", 
    "  noLoop();", 
    "String RomanS = new StringBuilder().append(R).append(o).append(m).append(a).append(n).append(S).toString();", 
    "  font = createFont(RomanS, 12);", 
    "}", 
    "", 
    "void draw() {", 
    "  String file = new StringBuilder().append(S).append(dot).append(s).append(v).append(g).toString();", 
    "  beginRecord(SVG, file);", 
    "  char quote = 34;", 
    "  String[] string = {", 
    "  };", 
    "",
    "fill(0);",
    "  for (int i=0; i<22; i++)", 
    "    text(string[i], 50, i*font.getSize() + font.getSize() + i*2);", 
    "  for (int i=0; i<string.length; i++)", 
    "    text(quote + string[i] + quote + ',', 50, 22*font.getSize() + i*font.getSize() + font.getSize() + 22*2 + i*2);", 
    "  for (int i=22; i<string.length; i++)", 
    "    text(string[i], 50, string.length*font.getSize() + i*font.getSize() + font.getSize() + string.length*2 + i*2);", 
    "  endRecord();", 
    "}", 
  };

  fill(0);
  for (int i=0; i<22; i++) 
    text(string[i], 50, i*font.getSize() + font.getSize() + i*2);
  for (int i=0; i<string.length; i++) 
    text(quote + string[i] + quote + ',', 50+font.getSize(), 22*font.getSize() + i*font.getSize() + font.getSize() + 22*2 + i*2);
  for (int i=22; i<string.length; i++) 
    text(string[i], 50, string.length*font.getSize() + i*font.getSize() + font.getSize() + string.length*2 + i*2);
  endRecord();
}
2 Likes

Heh, nice one! :slight_smile:

I was half expecting some kind of self-modifying (or rather self-reading) code somehow. I guess the “without any input” part would make that tricky. I’ve no idea how to do that though, nor how to optimize it.

Wow very nice! :wink:

Next step is to have syntax color highlighting :grin:

Haha! :joy:
Reading the .pde file would definitely be cheating!

Here’s my approuch:

import processing.svg.*;

String[] code;

void setup() {
    size(400, 500);

    String path = sketchPath(getClass().getSimpleName() + ".pde");
    File sketch = new File(path);

    code = loadStrings(sketch);
}

void draw() {
    beginRecord(SVG, "output.svg");
    background(255);
    textAlign(LEFT, TOP);
    fill(0);

    int y = 0;
    for (String line : code) {
        text(line, 0, y);
        y += textAscent()+textDescent();
    }
    endRecord();
}

Have in mind that you need to save the sketch before running it and everytime you make changes to it.

2 Likes

That is a terrifying but brilliant idea.
I’m gonna let you figure that one out though! Too much for me! :rofl:

1 Like

Oh really? sorry i didn’t get that part :sweat_smile:

Yeah…
But your solution does look much neater with a file read!

I wonder how concise the code could get if you if you allowed inputs such as the source file?

We could have a (slightly cheaty) Processing Quine-lympics! :rofl:

Quine? Here’s mine:

That’s right. No code at all. When run, it generates itself. Do I win?

Ahah it doesn’t seem to work, from the Rosetta code quine page :

Empty programs producing no output are not allowed.

Because your program doesn’t even output “nothing”, it just doesn’t output anything :wink:

@TomD you should contribute to it and send your program!