Code Sketch


Agent based genart
By: Lalit Pant
Category: Art
// code derived from http://www.generative-gestaltung.de/1/M_1_5_02_TOOL

cleari()
size(768, 480)
originTopLeft()
setBackground(black)

class Sketch {
    val numAgents = 2500
    val noiseScale = 200
    val noiseStrength = 10
    val overlayAlpha = 10
    val agentsAlpha = .9
    val strokeWidth = .3
    val agents = ArrayBuffer.empty[Agent]

    class Agent(surface: CanvasDraw) {
        import surface._
        var p, pOld: Vector2D = _
        var stepSize, angle = 0.0
        var isOutside = false

        p = Vector2D(randomDouble(cwidth), randomDouble(cheight))
        pOld = p
        stepSize = randomDouble(1, 5)

        def update1() {
            angle = perlin.noise(p.x / noiseScale, p.y / noiseScale) * noiseStrength

            p += Vector2D(math.cos(angle) * stepSize, math.sin(angle) * stepSize)

            if (p.x < -10) isOutside = true
            else if (p.x > cwidth + 10) isOutside = true
            else if (p.y < -10) isOutside = true
            else if (p.y > cheight + 10) isOutside = true

            if (isOutside) {
                p = Vector2D(random(cwidth), random(cheight))
                pOld = p
            }

            strokeWeight(strokeWidth * stepSize)
            line(pOld.x, pOld.y, p.x, p.y)

            pOld = p
            isOutside = false
        }
    }

    def setup(surface: CanvasDraw) {
        import surface._
        randomSeed(42)
        perlin.noiseSeed(42)
        repeat(numAgents) {
            agents.append(new Agent(surface))
        }
    }

    val palette = Seq(cm.hsla(200, 1, .7, agentsAlpha), cm.lightBlue.fadeOut(1 - agentsAlpha))
    val weights = Seq(5, 1)
    def drawLoop(surface: CanvasDraw) {
        import surface._
        fill(0, overlayAlpha)
        noStroke()
        rect(0, 0, cwidth, cheight)

        stroke(randomFrom(palette, weights))
        agents.foreach { agent =>
            agent.update1()
        }
    }
}

val sketch = new Sketch
canvasSketch(sketch)