Does anyone have any code-golf tips for golfing in Processing? It is a graphical extension of java, and is somewhat difficult to golf.
- \$\begingroup\$ Nice question. I believe tips should be on Community Wiki. I flagged it for migration. \$\endgroup\$Level River St– Level River St2014-05-08 14:30:35 +00:00Commented May 8, 2014 at 14:30
- 4\$\begingroup\$ Downvote??? why? \$\endgroup\$TARDIS– TARDIS2014-05-08 14:41:53 +00:00Commented May 8, 2014 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\$user12205– user122052014-05-08 15:03:25 +00:00Commented May 8, 2014 at 15:03
- 4\$\begingroup\$ @ace: Reputation gained before a post is marked CW will be kept: meta.stackexchange.com/a/11741/229438 \$\endgroup\$user3094403– user30944032014-05-08 15:27:40 +00:00Commented May 8, 2014 at 15:27
7 Answers
- If no animation is required, you can skip the
void draw()and put everything intovoid 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
heightandwidthin your code, it is usually shorter to use their numeric values instead. For example, with a canvas of size(100, 100), using99to replaceheightandwidthcan save you 7 bytes.
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
- \$\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– quat2017-01-17 17:41:34 +00:00Commented Jan 17, 2017 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– HEGX642017-01-19 06:06:24 +00:00Commented Jan 19, 2017 at 6:06
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.
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 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.
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.
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));