Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # lib.coffee
  2.  
  3. functions =
  4.  
  5.   draw_axis: (axis, svg) ->
  6.   #
  7.   # Draw a hive plot axis in the svg area.
  8.  
  9.     a_rad       = axis.angle * (Math.PI / 180.0)
  10.     r_beg       = axis.r_beg
  11.     r_end       = r_beg + axis.r_len
  12.  
  13.     [ height, width ] = [ svg.attr('height'), svg.attr('width') ]
  14.     [ x_ctr,  y_ctr ] = [ height * 0.5,       width * 0.5       ]
  15.     [ x_mul,  y_mul ] = [ Math.sin(a_rad),    Math.cos(a_rad)   ]
  16.  
  17.     axis.x_beg  = x_ctr + x_mul * r_beg
  18.     axis.x_end  = x_ctr + x_mul * r_end
  19.     axis.x_len  = axis.x_end - axis.x_beg
  20.     axis.x_mul  = x_mul
  21.  
  22.     axis.y_beg  = y_ctr + y_mul * r_beg
  23.     axis.y_end  = y_ctr + y_mul * r_end
  24.     axis.y_len  = axis.y_end - axis.y_beg
  25.     axis.y_mul  = y_mul
  26.  
  27.     a_func  = () -> "rotate(#{ -axis.angle } " +
  28.                     "#{ axis.x_beg } #{ axis.y_beg })"
  29.  
  30.     axis_attrs =
  31.       class:      'axis_rect'
  32.       fill:       axis.color
  33.       height:     axis.r_len
  34.       transform:  a_func()
  35.       stroke:     axis.color
  36.       width:      2
  37.       x:          axis.x_beg
  38.       y:          axis.y_beg
  39.  
  40.     svg.append('rect')
  41.       .call(set_attrs, axis_attrs)
  42.       .append('title').text(axis.title)
  43.  
  44.  
  45.   edge_dir: (axes, edge) ->
  46.   #
  47.   # Find the edge direction, given the axes hash and an edge
  48.  
  49.     edge_dir_h( axes[ edge[0] ].angle,
  50.                 axes[ edge[2] ].angle )
  51.  
  52.  
  53.   edge_dir_h: (a_beg, a_end) ->
  54.   #
  55.   # Find the edge direction, given a pair (beginning, end) of angles.
  56.  
  57.     diff  = a_end - a_beg
  58.     abs   = Math.abs(diff)
  59.     tmp   = if diff < 0 then -1 else 1
  60.     if abs < 180 then tmp else -tmp
  61.  
  62.  
  63.   edge_path: (edge, axes, cp_off) ->
  64.   #
  65.   # Create an SVG path (between the relevant axes) for this edge.
  66.  
  67.     edge_dir    = edge[5]
  68.  
  69.     end_points  = (end_key) ->
  70.     #
  71.     # Calculate the edge's intersection and control points (ip, cp),
  72.     # based on end_key.  cp is on the normal to the axis, near ip.
  73.  
  74.       if end_key == 'from' then [ dir, ndx ] = [  edge_dir, 0 ]
  75.       else                      [ dir, ndx ] = [ -edge_dir, 2 ]
  76.  
  77.       name    = edge[ndx]
  78.       start   = edge[ndx+1]
  79.       axis    = axes[name]
  80.       x_ip    = axis.x_beg + start * axis.x_len
  81.       y_ip    = axis.y_beg + start * axis.y_len
  82.       x_off   = cp_off * axis.y_mul
  83.       y_off   = cp_off * axis.x_mul
  84.       x_cp    = x_ip + x_off * dir
  85.       y_cp    = y_ip - y_off * dir
  86.       [ [ x_ip, y_ip ], [ x_cp, y_cp ] ]
  87.  
  88.     from  = end_points('from')
  89.     to    = end_points('to').reverse()
  90.     out   = from.concat(to)
  91.  
  92.  
  93.   set_attrs: (object, hash) ->
  94.   #
  95.   # Set an object's attributes, based on a hash.
  96.  
  97.     object.attr(key, val) for key, val of hash
  98.  
  99.  
  100. # Make functions global.
  101.  
  102. window[key] = val for key, val of functions