13
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Nice question. I believe tips should be on Community Wiki. I flagged it for migration. \$\endgroup\$ – Level River St May 8 '14 at 14:30
  • 3
    \$\begingroup\$ Downvote??? why? \$\endgroup\$ – TARDIS May 8 '14 at 14:41
  • \$\begingroup\$ 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... \$\endgroup\$ – ace May 8 '14 at 15:03
  • 3
    \$\begingroup\$ @ace: Reputation gained before a post is marked CW will be kept: meta.stackexchange.com/a/11741/229438 \$\endgroup\$ – ProgramFOX May 8 '14 at 15:27
7
\$\begingroup\$
  • 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.
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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: puu.sh/tpzP8.png \$\endgroup\$ – quat Jan 17 '17 at 17:41
  • \$\begingroup\$ @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. \$\endgroup\$ – HEGX64 Jan 19 '17 at 6:06
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.