13
1
Does anyone have any code-golf tips for golfing in Processing? It is a graphical extension of java, and is somewhat difficult to golf.
13
1
Does anyone have any code-golf tips for golfing in Processing? It is a graphical extension of java, and is somewhat difficult to golf.
7
void draw()
and put everything into void setup()
.(100, 100)
by default.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.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
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 – 8 years ago@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 – 8 years ago
2
(R, G, B)
notationProcessing 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)
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.
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).
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
2
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.
2
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.
2
#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.
1
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));
Nice question. I believe tips should be on Community Wiki. I flagged it for migration. – Level River St – 11 years ago
4Downvote??? why? – TARDIS – 11 years ago
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 – 11 years ago
3
@ace: Reputation gained before a post is marked CW will be kept: http://meta.stackexchange.com/a/11741/229438
– ProgramFOX – 11 years ago