(Demo) 2D Wave




GeeXLab - 2D wave



Here is a simple demo that draws a cool 2D time-based wave. The wave is the superposition of several sine waves. I found the original code here.

You can find in init.lua, the function that computes the wave:

function update_curve_v2(curve, t)
  local step_angle = (2.0*PI) / curve.num_vertices
  local angle = 0
  local x = -curve.x_size/2.0
  local x_step = curve.x_size / curve.num_vertices
 
  for i=0, curve.num_vertices-1 do
    local y = math.sin(angle * curve.frequency)
    y = y + math.sin(angle * curve.frequency * 2.1 + t) * 4.5 
    y = y + math.sin(angle * curve.frequency * 1.72 + t*1.121) * 4.0
    y = y + math.sin(angle * curve.frequency * 2.221 + t*0.437) * 5.0
    y = y + math.sin(angle * curve.frequency * 3.1122 + t*4.269) * 2.5
    y = y * curve.amplitude
    angle = angle + step_angle
    gh_polyline.set_vertex_position(curve.line, i, x, y, 0, 1)
    x = x + x_step
  end
end

where curve is a structure defined as follows:

curve = {
  line = 0, -- polyline
  num_vertices = 1000,
  x_size = winW * 0.9,
  color = {r=91/255, g=200/255, b=1.0, a=1.0},
  amplitude = 10.0,
  frequency = 3.0,
}

 
In the demo, I added the possibility to tweak every parameter of the wave. Each coefficient can be changed.

I find this wave very relaxing, that’s why I decided to code this small demo. And if you need to draw a 2D curve, here is a ready-to-use template based on a polyline.


GeeXLab - 2D wave demo on macOS





Leave a Comment

Your email address will not be published. Required fields are marked *