# A Processing Quine

**URL:** https://discourse.processing.org/t/a-processing-quine/31332
**Category:** Coding Questions
**Created:** [July 19, 2021, 8:31pm UTC](https://discourse.processing.org/t/a-processing-quine/31332 "2021-07-19T20:31:40Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [July 19, 2021, 8:31pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/1 "2021-07-19T20:31:40Z")

</div>

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](https://gist.github.com/benjholla/9dd76ca9269e8c9af99a#file-quine-java-L5)

My Code is as below:

```auto
//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();
}

```

---

<div class="post-metadata">

### Author: ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)
#### Post date: [July 20, 2021, 1:00am UTC](https://discourse.processing.org/t/a-processing-quine/31332/2 "2021-07-20T01:00:08Z")

</div>

Heh, nice one! 🙂

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.

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [July 20, 2021, 12:39pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/3 "2021-07-20T12:39:19Z")

</div>

Wow very nice! 😉

Next step is to have syntax color highlighting 😁

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [July 20, 2021, 12:48pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/4 "2021-07-20T12:48:17Z")

</div>

Haha! 😂  
Reading the .pde file would definitely be cheating!

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [July 20, 2021, 12:50pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/5 "2021-07-20T12:50:40Z")

</div>

Here’s my approuch:

```auto
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.

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [July 20, 2021, 12:50pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/6 "2021-07-20T12:50:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [July 20, 2021, 12:51pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/7 "2021-07-20T12:51:52Z")

</div>

Oh really? sorry i didn’t get that part 😅

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [July 20, 2021, 12:55pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/8 "2021-07-20T12:55:06Z")

</div>

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! 🤣

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [July 21, 2021, 3:12am UTC](https://discourse.processing.org/t/a-processing-quine/31332/9 "2021-07-21T03:12:59Z")

</div>

Quine? Here’s mine:

```auto

```

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

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [July 21, 2021, 8:22am UTC](https://discourse.processing.org/t/a-processing-quine/31332/10 "2021-07-21T08:22:47Z")

</div>

Ahah it doesn’t seem to work, from the [Rosetta code quine page](https://rosettacode.org/wiki/Quine) :

> Empty programs producing no output are not allowed.

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

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 20, 2021, 11:30pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/11 "2021-09-20T23:30:48Z")

</div>

A slight modification of the example by @TomD to simplify things:

1. drop SVG, just use the screen and screenFrame() for output and use its default filenaming
2. use the built-in default font
3. drop all the chars except inline char(34) for `"`

TL;DR – a bunch of the code translated from the Java Quine example isn’t needed in Processing because it already does all those things for you.

```auto
// Quine2 by Jeremy Douglass 2021-09-20
// based on Quine by Thomas Day 19/7/21
// Processing 3.5.4 https://discourse.processing.org/t/a-processing-quine/31332
int off = 12;
int lh = 12;
void setup() {
  size(800, 600);
  noLoop();
}
void draw() {
  background(0);
  String[] code = {
    "// Quine2 by Jeremy Douglass 2021-09-20",
    "// based on Quine by Thomas Day 19/7/21",
    "// Processing 3.5.4 https://discourse.processing.org/t/a-processing-quine/31332",
    "int off = 12;",
    "int lh = 12;",
    "void setup() {",
    " size(800, 600);",
    " noLoop();",
    "}",
    "void draw() {",
    " background(0);",
    " String[] string = {",
    " };",
    " for (int i=0; i<off; i++) text(code[i], lh, lh*(i+1));",
    " for (int i=0; i<code.length; i++)",
    " text(char(34) + code[i] + char(34) + ',', lh*3, lh*(i+1+off));",
    " for (int i=lh; i<code.length; i++) text(code[i], lh, lh*(i+1+code.length));",
    " saveFrame();",
    "}", 
  };
  for (int i=0; i<off; i++) text(code[i], lh, lh*(i+1));
  for (int i=0; i<code.length; i++)
    text(char(34) + code[i] + char(34) + ',', lh*3, lh*(i+1+off));
  for (int i=off; i<code.length; i++) text(code[i], lh, lh*(i+1+code.length));
  saveFrame();
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 21, 2021, 12:07am UTC](https://discourse.processing.org/t/a-processing-quine/31332/12 "2021-09-21T00:07:04Z")

</div>

Followup: Here is a minified version of the same thing,

![screen-0001](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/ff0410c62314345095e66c02f3330fa8a310e6c8.png)

It removes comments and whitespace, makes loop indexes concrete, and reuses a global “int i” across all three loops to save a bit more space. It also takes advantage of non-linear writing to the screen – unlike the original example, the second for loop is actually the third visually so the index `i` can simply continue.

Note that not _all_ of the possible whitespace et cetera has been squeezed out – left in line breaks that make it easy to still see where the Quine trick happens. Still–pretty small.

```auto
int h=3,o=12,i=0;
void setup(){size(480,240);noLoop();}void draw(){background(0);
  String[] c = {
    "int h=3,o=12;i=0",
    "void setup(){size(480,240);noLoop();}void draw(){background(0);",
    " String[] c = {",
    " };",
    " for(;i<h;i++)text(c[i],o,o*(i+1));",
    " for(;i<9;i++)text(c[i],o,o*(i+10));",
    " for(i=0;i<9;i++)text(char(34)+c[i]+char(34)+',',o*3,o*(i+1+h));",
    " saveFrame();",
    "}",
  };
  for(;i<h;i++)text(c[i],o,o*(i+1));
  for(;i<9;i++)text(c[i],o,o*(i+10));
  for(i=0;i<9;i++)text(char(34)+c[i]+char(34)+',',o*3,o*(i+1+h));
  saveFrame();
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 21, 2021, 4:19pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/13 "2021-09-21T16:19:52Z")

</div>

One more quine, a short one-liner that just prints itself to the console.

Code:

```auto
String p="String p=%c%s%1$c;System.out.printf(p,34,p);";System.out.printf(p,34,p);

```

Output:

> String p=“String p=%c%s%1$c;System.out.printf(p,34,p);”;System.out.printf(p,34,p);

 ![Screen Shot 2021-09-21 at 9.15.24 AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b026dc373d88fbb1d3a7c67bcd448ccfa70ce67d.png)

Here `printf(p,34,p)` says "take the string p, and put 34 and p into its variables. Its variables `%c%s%1$c` say "a char, a string, and the first char again. ASCII 34 is the `"` character, so the String is placed inside itself, wrapped in quotes, completing the quine, with the character-based templating of quotes as a way to get around the problem of avoiding character-escaping (as things can’t be escaped in the inner and not in the outer - or vice-versa - without it failing to become a quine).

This example is more compact than the [Java quine it is based on](https://rosettacode.org/wiki/Quine#Java) because in Processing immediate mode you don’t need a wrapping class or even a main method that takes arguments. Interestingly, Processing doesn’t have a `printf` method, and its built-in `print` and `println` won’t work, so you still need to call `System.out.printf` to get compact templating. That isn’t very idiomatic to Processing, but it works.

**Edit** : both examples above added to [Rosetta Code : Quine : Processing](https://rosettacode.org/wiki/Quine#Processing).

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [September 28, 2021, 8:48pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/14 "2021-09-28T20:48:26Z")

</div>

Wow! Really happy that you ran with the idea of the quine!

The original purpose of my code was to be plotted to paper, hence the otherwise unnecessary code for SVG export and font changes.

But am impressed by how much you were able to reduce the amount of code!

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [October 4, 2021, 5:19pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/15 "2021-10-04T17:19:06Z")

</div>

Out of interest, are you using a tool to minify your code?  
I’ve been struggling to find one for java that doesn’t introduce bugs.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 5, 2021, 4:24am UTC](https://discourse.processing.org/t/a-processing-quine/31332/16 "2021-10-05T04:24:16Z")

</div>

I did not, I just did it manually. Also, it isn’t fully minified–I left some spaces and line breaks for readability.

The main tricks: remove optional spaces, drop for loop brackets when looping a single instruction, drop the loop variable declaration, use single letter variable names. That’s about it–nothing too fancy. The thing about Java is, it is pretty verbose even when optimally minified…

---

<div class="post-metadata">

### Author: ![TomD](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@TomD](https://discourse.processing.org/u/TomD)
#### Post date: [October 5, 2021, 1:29pm UTC](https://discourse.processing.org/t/a-processing-quine/31332/17 "2021-10-05T13:29:20Z")

</div>

Thanks! I suspected you’d done it manually.

The one online resource for it that I found has a nasty habit of dropping the + operator 🙃
