Tips for golfing in Processing

13

1

Does anyone have any tips for golfing in Processing? It is a graphical extension of java, and is somewhat difficult to golf.

TARDIS

Posted 2014-05-08T13:27:49.523

Reputation: 139

Nice question. I believe tips should be on Community Wiki. I flagged it for migration. – Level River St – 2014-05-08T14:30:35.660

4Downvote??? why? – TARDIS – 2014-05-08T14:41:53.893

Don't worry about the downvote... you'll lose all the rep you got from this post after it is marked as community wiki anyway... – user12205 – 2014-05-08T15:03:25.520

3

@ace: Reputation gained before a post is marked CW will be kept: http://meta.stackexchange.com/a/11741/229438

– ProgramFOX – 2014-05-08T15:27:40.863

Answers

7

  • If no animation is required, you can skip the void draw() and put everything into void setup().
  • Initialising the size of the canvas is sometimes not necessary - it will be initialised to (100, 100) by default.
  • If you need to use height and width in your code, it is usually shorter to use their numeric values instead. For example, with a canvas of size (100, 100), using 99 to replace height and width can save you 7 bytes.

user12205

Posted 2014-05-08T13:27:49.523

Reputation: 8 752

3

If you only run code in the setup method then you don't need write the method outline. For example you can write:

rect(10,10,90,90);

instead of

void setup {
    rect(10,10,90,90);
}

And as long as you don't use any other methods then everything will be put in the setup method before running

HEGX64

Posted 2014-05-08T13:27:49.523

Reputation: 313

This can be a bit buggy. I've noticed that if you try to define a function in static mode, Processing can get a bit confused: https://puu.sh/tpzP8.png

– quat – 2017-01-17T17:41:34.347

@quat as long as you don't define any other methods then everything will be put in the setup method. If you need to define methods then you'll need to explicitly name the setup method. – HEGX64 – 2017-01-19T06:06:24.727

2

Colours (R, G, B) notation

Hexadecimal colours

Processing is very flexible in colour format.

fill(255,255,0); //16 bytes

can be written using hexadecimal notation as

fill(#ffff00);   //14 bytes (2 bytes saved)

Grayscale

Here is a special usage for colours if all the Red, Green and the Blue values are the same (white):

fill(255,255,255); //18 bytes
fill(#ffffff);     //14 bytes
fill(255);         //10 bytes

All the three parameters can be shortened into one parameter containing the grayscale value: from 0 black to 255 white.

This can be extended for alpha as well:

fill(175,175,175,50); //translucent gray
fill(175,50);         //8 bytes shorter

Both mean the same colour, but the latter way is shorter by 8 bytes.

Alpha

Although obvious, it should be stated that the alpha parameter in specifying colours is optional since colours are defaulted to an alpha value of 255 (100% opaque).

Summary: Color formats

Use the shortest colour format to express your colour (remember to leave out unnecessary bits -depending on the context of the program- for example: alpha or grayscale)

R: Red G: green B: blue A: alpha g: grayscale

RRR,GGG,BBB
#RRGGBB
ggg
RRR,GGG,BBB,AAA
ggg,AAA

user41805

Posted 2014-05-08T13:27:49.523

Reputation: 16 320

2

Abbreviate constants

If you're ever using one of the all-caps keywords in Processing (such as DIFFERENCE or TRIANGLE_FAN), see if the number they correspond to is shorter.

For example, DIFFERENCE is just an int that's equal to 32. Instead of using DIFFERENCE here, I could write 32, saving 8 characters.

quat

Posted 2014-05-08T13:27:49.523

Reputation: 1 211

2

Shorter alternative to void keyPressed(){}

void draw(){}void keyPressed(){foo;} //36 bytes
void draw(){if(key>0)foo;}           //26 bytes

The void draw(){} is needed by default in order for key to be updated. If the user hasn't pressed a key since the start of the program, key is given a value of 0. By checking if it is more than 0 (ie the user has pressed a key), we have golfier code and save 10 bytes.

user41805

Posted 2014-05-08T13:27:49.523

Reputation: 16 320

2

White #FFFFFF

Related: Colour Notation

Instead of using this for white:

color(255)     //10 bytes

you can do this:

color(-1)      //9 bytes

and save 1 byte.

user41805

Posted 2014-05-08T13:27:49.523

Reputation: 16 320

1

Setting and committing pixels using set()

It's more efficient to set pixels via pixels[]:

pixels[0] = color(255)
updatePixels();

However, that requires updatePixels() and also using an index which depending on the scenario will require converting an x,y position to a pixel index. To keep things short, even though it's less CPU efficient (as each call updates the whole buffer), set() allows a pixel to set and committed to buffer straight away in one call.

set(0,0,color(255));

George Profenza

Posted 2014-05-08T13:27:49.523

Reputation: 191