Download code

Jump to: navigation, search

Back to Sierpinski_triangle_(JavaScript)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

sierpinski.js

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Sierpinski_triangle_(JavaScript)?oldid=11668
14 */
15 
16 var width = 80
17 var state
18 
19 function init_state() {
20   state = new Array(width+1)
21   state[width/2] = 1
22 }
23 
24 function print_state() {
25   for (var i=0; i<width; i++)
26     document.write(state[i] ? '@' : ' ')
27   document.write("<br/>\n")
28 }
29 
30 // the index has three bits: b0,b1,b2 = next neighbor, current cell, previous neighbor
31 
32 var transition = [0,1,1,1,1,1,1,0]
33 function gen() {
34   var n = state[0];
35   for (var i=0; i<width; i++) {
36     n = ((2*n) | state[i+1]) & 7
37     state[i] = transition[n]
38   }
39 }
40 function sierpinski(height) {
41   document.write("<"+"pre>\n")     // split tag for proper wiki formatting
42   init_state()
43   print_state()
44   for (var i=1; i<height; i++) {
45     gen()
46     print_state()
47   }
48   document.write("<"+"/pre>\n")
49 }
50 
51 sierpinski(32)


hijacker
hijacker
hijacker
hijacker