Skip to content

Instantly share code, notes, and snippets.

@vinc6
Last active January 18, 2024 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinc6/d2b55f3e64f1b1c2bcab9b87e051e72a to your computer and use it in GitHub Desktop.
Save vinc6/d2b55f3e64f1b1c2bcab9b87e051e72a to your computer and use it in GitHub Desktop.
--- coordinates cv generator
-- vinc 230124
-- in1-2: x-axis, y-axis
-- out1-4: magnitude in corresponding quadrants
function quadrants()
local x = math.min(math.max(input[1].volts, -5), 5)
local y = math.min(math.max(input[2].volts, -5), 5)
local h = math.sqrt(math.pow(x, 2)+math.pow(y, 2)) -- hypotenuse
local r = math.atan2(y, x) -- radian
local main_quadr = h*((math.pi/2)-math.abs(math.pi/4-r%(math.pi/2)))/(math.pi/2)
local adjacent_l = h*math.max(((math.pi/2)-math.abs(math.pi*3/4-r%(math.pi/2)))/(math.pi/2), 0)
local adjacent_r = h*math.max(((math.pi/4)-r%(math.pi/2))/(math.pi/2), 0)
if x > 0 and y > 0 then -- quadrant i
ch_adder, is_quadr = 0, true
elseif x < 0 and y > 0 then -- quadrant ii
ch_adder, is_quadr = 1, true
elseif x < 0 and y < 0 then -- quadrant iii
ch_adder, is_quadr = 2, true
elseif x > 0 and y < 0 then -- quadrant iv
ch_adder, is_quadr = 3, true
elseif x == 0 and y > 0 then -- positive y-axis
ch_adder, is_quadr = 0, false
elseif x < 0 and y == 0 then -- negative x-axis
ch_adder, is_quadr = 1, false
elseif x == 0 and y < 0 then -- negative y-axis
ch_adder, is_quadr = 2, false
elseif x > 0 and y == 0 then -- positive x-axis
ch_adder, is_quadr = 3, false
else -- origin
for ch = 1, 4 do
output[ch].volts = 0
end
end
output[(ch_adder+0)%4+1].volts = is_quadr and main_quadr or h*0.5
output[(ch_adder+1)%4+1].volts = is_quadr and adjacent_l or h*0.5
output[(ch_adder+2)%4+1].volts = 0
output[(ch_adder+3)%4+1].volts = is_quadr and adjacent_r or 0
end
function init()
for n = 1, 4 do
output[n].slew = 0.02
end
metro[1].event = quadrants
metro[1].time = 0.01
metro[1]:start()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment