Advertisement
Guest User

Javascript Snowflake Fractal

a guest
Dec 27th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas_width = 500;
  2. var canvas_height = 500;
  3. var coor_width = 2;
  4. var coor_height = 2;
  5.  
  6. var context;
  7. var xs;
  8. var ys;
  9. var color = "rgb(255,0,0)";
  10.  
  11. function run(canvas_id, next_id, restart_id) {
  12.     var canvas = document.getElementById(canvas_id);
  13.     var next = document.getElementById(next_id);
  14.     var restart = document.getElementById(restart_id);
  15.  
  16.     xs = new Array();
  17.     ys = new Array();
  18.     context = canvas.getContext("2d");
  19.  
  20.     add_point(xs, ys, -.5, -.5);
  21.     add_point(xs, ys, .5, -.5);
  22.     add_point(xs, ys, .5, .5);
  23.     add_point(xs, ys, -.5, .5);
  24.  
  25.     draw_lines();
  26.  
  27.     next.onclick = next_iteration;
  28.     restart.onclick = restart_iters;
  29. }
  30.  
  31. function restart_iters() {
  32.     context.clearRect(0, 0, 500, 500);
  33.     xs = new Array();
  34.     ys = new Array();
  35.  
  36.     add_point(xs, ys, -.5, -.5);
  37.     add_point(xs, ys, .5, -.5);
  38.     add_point(xs, ys, .5, .5);
  39.     add_point(xs, ys, -.5, .5);
  40.  
  41.     draw_lines();
  42. }
  43.  
  44. function add_point(xs, ys, x, y) {
  45.     xs.push(x);
  46.     ys.push(y);
  47. }
  48.  
  49. function coor_to_canvas(x, y) {
  50.     var cx = Math.round(canvas_width / 2 + canvas_width * x / coor_width);
  51.     var cy = Math.round(canvas_height / 2 - canvas_height * y / coor_height);
  52.  
  53.     return [cx, cy];
  54. }
  55.  
  56. function draw_lines() {
  57.     context.fillColor = color;
  58.     context.clearRect(0, 0, 500, 500);
  59.  
  60.     context.beginPath();
  61.     var cx;
  62.     var cy;
  63.     var x;
  64.     var y;
  65.  
  66.     x = xs[0];
  67.     y = ys[0];
  68.  
  69.     var res = coor_to_canvas(x, y);
  70.     cx = res[0];
  71.     cy = res[1];
  72.  
  73.     context.moveTo(cx, cy);
  74.  
  75.     for (var i = 1; i < xs.length; i += 1) {
  76.     var x = xs[i];
  77.     var y = ys[i];
  78.  
  79.     res = coor_to_canvas(x, y);
  80.     cx = res[0];
  81.     cy = res[1];
  82.  
  83.     context.lineTo(cx, cy);
  84.     }
  85.  
  86.     x = xs[0];
  87.     y = ys[0];
  88.     res = coor_to_canvas(x, y);
  89.     cx = res[0];
  90.     cy = res[1];
  91.     context.lineTo(cx, cy);
  92.     context.closePath();
  93.     context.stroke();
  94. }
  95.  
  96.  
  97. function next_iteration() {
  98.     var x1;
  99.     var y1;
  100.     var x4;
  101.     var y4;
  102.  
  103.     var new_xs = new Array();
  104.     var new_ys = new Array();
  105.  
  106.     for (var i = 1; i <= xs.length; i += 1) {
  107.     var x1 = xs[(i - 1) % xs.length];
  108.     var y1 = ys[(i - 1) % xs.length];
  109.     var x4 = xs[i % xs.length];
  110.     var y4 = ys[i % xs.length];
  111.  
  112.     var L = Math.sqrt((x1 - x4) * (x1 - x4) + (y1 - y4) * (y1 - y4));
  113.  
  114.     var w = x4 - x1;
  115.     var h = y4 - y1;
  116.     var theta = Math.atan2(w, h);
  117.  
  118.     var hh = (L/3) * Math.tan(Math.PI / 6)
  119.     var LL = Math.sqrt(L * L / 9 + hh * hh);
  120.  
  121.     var x2 = x1 + LL * Math.sin(theta + Math.PI / 6);
  122.     var y2 = y1 + LL * Math.cos(theta + Math.PI / 6);
  123.     var x3 = x4 + LL * Math.sin(Math.PI + theta + Math.PI / 6);
  124.     var y3 = y4 + LL * Math.cos(Math.PI + theta + Math.PI / 6);
  125.  
  126.     add_point(new_xs, new_ys, x2, y2);
  127.     add_point(new_xs, new_ys, x3, y3);
  128.     add_point(new_xs, new_ys, x4, y4);
  129.     }
  130.  
  131.     xs = new_xs;
  132.     ys = new_ys;
  133.  
  134.     draw_lines();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement