r/programming • • Dec 20 '10

A Christmas Tree in 1k of Javascript

http://www.romancortes.com/blog/how-i-did-the-1kb-christmas-tree/
1.1k Upvotes

221 comments sorted by

View all comments

13

u/abeliangrape Dec 20 '10

Can some explain to me how using cos(angle+11) for sin(angle) saves space?

32

u/cnk Dec 20 '10 edited Dec 20 '10

Look at the code:

M=Math
T=M.sin

Then he uses that trick for "cos" twice in the code:

T(R-11) //7 characters

The other way to write that is: M.cos(R) //8 characters

Assigning cos to a variable is 7 additional chars (C=M.cos), using it that way is C(a) 4 chars, he uses cos twice in the code: that would be 7+4+4 total: 15. Using T(a-11) twice is 14 chars.

Also: assigning to a variable uses an additional semicolon which isn't necessary using the sin approximation