cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

              <div class="rumor">
    <div class="dropdown">
        <select id="algo">
        <label>Number of People:</label>
        <input id="count" type="text" value="60" name="count"/>
        <select name="speed" id="speed">
            <option value="10">Slow</option>
            <option value="5">Medium</option>
            <option value="1">Fast</option>
        <input id="start" type="button" value="Start"/>    
    <div class="total">
      Total Messages : <span id="msg_count">0</span><span class="message"></span>
    <div class="cells">
              @import "compass/css3";

  position: absolute;
  z-index: 9999;
  border-top: 1px solid black;
  background-color: black;
  width: 100px;
  @include apply-origin(0,0);
  @include translate3d(0,0,0);
  @include border-radius(4px);
  @include box-shadow(#EEE 0 0 10px 5px);
  opacity: 0.4;
    content: "►";
    position: absolute;
    right: -5px;
    color: #000000;
    font-size: 10px;
    top: -6px

  font-family: "Helvetica";
    padding: 10px;
      color: grey;
      font-weight: bold;
      padding: 0 50px;

.dropdown {
  padding: 10px;
.grid table{
   position: normal;
  tr td{
    width: 60px;
    height: 60px;
    border: 1px solid grey;
    font-size: 10px;
    background: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAA3UlEQVR4Xu3asQ2DMBAF0LMIBVVGSEvPAs4GjMBoGSEjmAWoaRkhtaV/ucJSWqL4ohD9L53c3uOusUxQVdmTYRjOdtytovglWY3Lsjz29nWSnTFoad41sXykq1V1wKt5f4R4AORnQgABBBAQHQDpa4B1XefagL7vj71CquoLAOAKAMAJEEAAAQQQQAABBBBAAO8D/z0BAggggAACCCCg6zr1B/B9gIBbqanU8QA557ltW/EB+N8HpqZpBMB0yBX6ZHUMfrFjqw1IdkTxTwKwVZ+AZfzCHysphDC+o30CYFgMjsAPnYQAAAAASUVORK5CYII=) center center no-repeat;
      background-color: #B3D1B2;
      background-color: #F7D4BE;
      position: relative;
      width: 100%;
      height: 100%;
        position: absolute;
        padding: 3px;
          top: 0px;

          bottom: 0px;

          left: 0px; 
          color: blue;

          right: 0px;   
          color: green;
              class Arrow
  @draw = (ele1, ele2)->
    pt1 = getCenter(ele1)
    pt2 = getCenter(ele2)
    distance = getDistance(pt1, pt2)
    $arrow = $('<div class=\"arrow\"></div>')
    $arrow.css({left: pt1[0], top: pt1[1],width: Math.floor(distance)})
    rotation_trans = "rotate(#{Math.ceil(getSlope(pt1, pt2))}deg)"
    $arrow.css('-webkit-transform', rotation_trans)
    $arrow.css('-moz-transform', rotation_trans)            
    $arrow.css('transform', rotation_trans)            
  @delete = ($arrow)->
  getCenter = (ele)->
    offset = ele.offset()
    [offset.left + ele.width()/2, offset.top + ele.height()/2]
  getSlope = (pt1, pt2)->
    console.log [pt1[1]-pt2[1], pt2[0] - pt1[0]]
    r = Math.atan2(pt2[1]-pt1[1], pt2[0] - pt1[0])
    r *180 / Math.PI
  getDistance = (pt1, pt2)->
    Math.sqrt(Math.pow(pt1[0] - pt2[0],2) + Math.pow(pt1[1] - pt2[1],2))
class Grid
  ele = undefined
  [max_width, cell_count, cell_size] = [0, 0, 70]
  [rows, cols, cell_count] = [0, 0, 0]
  connections = {}

  @init = (selector, n)->
    [ele, cell_count] = [$(selector), n]
    max_width = ele.width()
  @connectCells = (cell1, cell2)->
    [$cell1, $cell2] = [getCell(cell1), getCell(cell2)]
    $cell1.addClass 'outgoing'
    $cell2.addClass 'incoming'
    connections["#{cell1}-#{cell2}"] = Arrow.draw($cell1, $cell2)
  @disconnectCells = (cell1, cell2)->
    [$cell1, $cell2] = [getCell(cell1), getCell(cell2)]
    $cell1.removeClass 'outgoing'
    $cell2.removeClass 'incoming'
  @clear = ->
    for own key, id of connections
  @setCellText = (cellNo, text)->

  addCells = ()->
    tags = "<table><tbody>"
    for x in [1..rows]
      tags += "<tr>"
      for y in [1..cols]
        cellNo = (x - 1)*cols + y    
        break if cellNo > cell_count
        tags += "<td><div class=\"container\">\
            <span class=\"top left\">#{cellNo}</span>\
            <span class=\"bottom right rumor-cnt\"></span>\
    tags += "</tbody></table>"

  calculateRowsCols = ()->
    cols = Math.floor(max_width / cell_size)
    rows = Math.ceil(cell_count / cols)

  getCell = (cellNo)->
    cellRow = Math.ceil(cellNo / cols)
    cellCol = (cellNo % cols)
    cellCol = cols if cellCol == 0 
    $cell = ele.find("table tr:nth-child(#{cellRow}) td:nth-child(#{cellCol})")

class Rumor
  state = {}
  total_messages = 0  
  timeout = undefined
  @delay = 50  
  init = (n)->
    Grid.init('.cells', n)
    for i in [1..n]
      state[i] = 1
  @start = (algo, n)->
    total_messages = 0    
    algoCaller = ->
      return if rumorSpread(state,n)
      next_messages = algo(state, n)
      $('.message').text("Person #{next_messages[0][0]} sends #{next_messages.length} message(s)  with #{next_messages[0][2]} rumor(s)") if next_messages[0] && next_messages[0][0]
      for [sender, receiver, message_cnt] in next_messages
        Grid.connectCells(sender, receiver)
        Grid.setCellText(receiver, state[receiver] + message_cnt)
        state[receiver] += message_cnt
        total_messages += 1
      timeout = setTimeout(algoCaller, Rumor.delay)
    setTimeout(algoCaller, Rumor.delay)
  rumorSpread = (state, n)->
    spread = true
    for own num, rumor_cnt of state
      if rumor_cnt < n
        spread = false
algos = {}
algos[1] =
      name: "All rumors in one message"
      func: (state, n)->
        this.max = 1 if !this.max || this.max > n
        this.max_num = 1 if !this.max_num || this.max_num > n
        messages = []
        if max_num == n
          for i in [1..n]
            continue if i == max_num  
            messages.push [max_num, i, max - state[i]]
          messages.push [max_num, max_num + 1, max]
        this.max += 1
        this.max_num += 1     

algos[2] =
      name: "Only one rumor in one message"
      func: (state, n)->
        messages = []
        this.itr = 1 if !this.itr || this.itr > n
        for i in [1..n]
          continue if i == this.itr
          messages.push [this.itr, i, 1]
        this.itr += 1
  for own index,algo of algos
    $("#algo").append("<option value=\"#{index}\">#{algo.name}</option>")
    Rumor.delay = parseInt($(this).val()) * 50                     
  Grid.init('.cells', parseInt($("#count").val()))
    Rumor.delay = parseInt($('#speed').val()) * 50
    Rumor.start(algos[$("#algo").val()].func, parseInt($("#count").val()))
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................