         @y = m*Math.sin(a)
         this
 
-#==== Particles ====
-
 
 class Sunlight
     
-    colors: ["#ff0", "#00f"]
-    sizes: [2, 3]
+    colors: ["#ff0", "#ff0"]
+    sizes: [2, 5]
     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
+    constructor: (@pos=@O(), g, q) ->
+
+        # sim units -> screen units
+        @xScale = d3.scale.linear()
+            .domain([-1, 1])
+            .range([0, @w])
+        @yScale = d3.scale.linear()
+            .domain([-1, 1])
+            .range([@h, 0])
+
+        # dynamics
         @d = 0
+        @velocity = []
+        @velocity[0] = new Vector 0, 2
+        @velocity[1] = @O()
+        @setCollision(g, q)
         @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]

            .attr(@bar.rangeDir, (d,i) => (1-d.count/cmax)*width)
            .attr(@bar.rangeAttr, (d, i) => d.count/cmax*width)

        
class Canvas

    @width = 360

        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: () ->
