             .attr("height", height)
 
         @bar = {domainDir:"y", domainAttr:"height", rangeDir:"x", rangeAttr:"width"}
-        #@bar = {domainDir:"x", domainAttr:"width", rangeDir:"y", rangeAttr:"height"}        
 
         @plotArea.selectAll("rect")
            .data(@data)

        @y = m*Math.sin(a)
        this

#==== Particles ====


class Sunlight
    
    colors: ["#ff0", "#00f"]
    sizes: [2, 3]
    w: Canvas.width
    h: Canvas.height
    O: -> new Vector 0, 0

    constructor: (@pos=@O(), @vel0=@O()) ->
        @init()
        @limit = 0
        @velocity = []  # Set of velocities indexed by state
        @setVelocities()  # Configured in subclass
        @d = 0
        @setState 0

    init: ->
        @xScale = d3.scale.linear() # sim units -> screen units
            .domain([-1, 1])
            .range([0, @w])

        @yScale = d3.scale.linear() # sim units -> screen units
            .domain([-1, 1])
            .range([@h, 0])

    setState: (@state) ->
        return if @state<0
        @vel = @velocity[@state]

        if @collision() and @state is 0
            @setState(1)
            count[@bin] += 1
        @d += @vel.mag()
        @pos.add @vel

    setCollision: (g, quant) ->
        @limit =  @yScale(g(@xScale.invert(@pos.x)))
        @bin = quant(@yScale.invert(@limit))
 
    collision: -> @pos.y > @limit


class Sun

    l: 180
    maxPhotons: 4000
    rate: 10
    cx: Canvas.width/2
    
    constructor: (@g, @q)->
        @photons = []

    emit: () ->
        unless @photons.length > @maxPhotons
            @photons.push(@emitPhoton()) for [0...@rate]
        @photons = @photons.filter (photon) => photon.visible()
        for photon in @photons
            photon.move()
            photon.draw()

    emitPhoton: ->
        pos = new Vector @cx + @l*(Math.random()-0.5), 1
        new Sunlight(pos, @g, @q)

class Simulation

    constructor: ->
        g = (u) -> ((u<0)*1.5 + (u>0)/2)*u + 0.375
        q = (y) -> Math.floor((y+0.875)/2*20) 
        @sun = new Sun g, q
        @h = new Histo
        
    start: () ->
