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

15

u/majoris Dec 20 '10 edited Dec 20 '10

Paraphrasing his code, here is how it looks with and without the trick:

M=Math;T=M.sin;T(a);T(a);T(a);T(a);T(a);T(a-11);T(a-11)
M=Math;T=M.sin;S=M.cos;T(a);T(a);T(a);T(a);T(a);S(a);S(a)

edit: who gave me upvotes without checking my solution!

6

u/cnk Dec 20 '10

in the 2nd line you don't need the "-11"s

1

u/abeliangrape Dec 20 '10

So it works when you end up using one of sin or cosine way less.

4

u/Douglas77 Dec 21 '10

11 = 3.5 * PI

(with an error of 0.0044, that's 1/4 degree)

EDIT: Misunderstood your question.