mathart

Lua + Cairo scripts that generate art, designs, and patterns
Log | Files | Refs | README | LICENSE

spiral.lua (1450B)


      1 --[[
      2   Spiral Art
      3   "spiral.lua"
      4   M. Yamanaka
      5   email: myamanaka@live.com
      6   website: csmyamanaka.com
      7   license: MIT (See included "LICENSE" file for details)
      8 ]]
      9 
     10 local ca = require("lgi").cairo
     11 require("csconv")
     12 
     13 function pebble(ctx, p, r, c)
     14   --[[
     15     draw a single pebble in canvs coordinate system
     16     arguments:
     17       ctx ... cairo context
     18       p   ... point in canvas coordinate system
     19       r   ... radius of the pebble
     20       c   ... colour
     21   ]]
     22   ctx:set_source_rgb(c[1], c[2], c[3])
     23   ctx:arc(p[1], p[2], r, 0, math.pi*2)
     24   ctx:fill()
     25 end
     26 
     27 function spiral()
     28   --[[
     29     The "main" function
     30     The polar coordinate system is used to generate a nice
     31     spiral pattern
     32   ]]
     33   
     34   local dim = {1920, 1080}
     35   --local img = ca.SvgSurface.create("spiral.svg", dim[1], dim[2])
     36   local fn = "spiral.svg"
     37   local ctx = ca.Context.create(ca.SvgSurface.create(fn, dim[1], dim[2]))
     38   
     39   local pts = {}
     40   local dTh = 5
     41   local dR = 45
     42   local sz = 11
     43   local N = 11
     44   
     45   --[[
     46     insert points into the list
     47   ]]
     48   for n = 0, N - 1 do
     49     for i = 1, sz do pts[n*sz + i] = {i*dR, (i - 1)*dTh + n*2*math.pi/N} end
     50   end
     51 
     52   --[[
     53     initialize the scene with a black background
     54   ]]
     55   ctx:set_source_rgb(0, 0, 0)
     56   ctx:rectangle(0, 0, dim[1], dim[2])
     57   ctx:fill()
     58 
     59   for i = 1, N*sz do
     60     local v = ((i - 1)%sz)
     61     pebble(ctx, xyCanv(polarXY(pts[i]), 1, {dim[1]/2, dim[2]/2}), v + 5, {0.9*v/sz + 0.1, 0.9*v/sz + 0.1, 0.9*v/sz + 0.1})
     62   end
     63 end
     64 
     65 spiral()

Generated using stagit (https://codemadness.org/stagit.html)