Page 1 of 1
Posted: 01 Mar 2011, 00:09
by gthibert
Hi,
It's more a math question than an patching question, but here's the idea.
To control Ambisonic plugins, I want to get the angle (0 to 360) and the distance (0 to 1) of a ball in an XY pad, relative to the center.
Examples :
Ball at the middle = angle of 0, distance of 0.
Ball at upper left corner = angle of 135, distance of 1.
Ball at bottom right corner = angle of 315, distance of 1.
Anyone have an idea ?
- Guillaume
Posted: 01 Mar 2011, 08:30
by nay-seven
about the distance you can find an example in the addon named Morphusine
the match formula is
distance = /¯ [ (X1-X2)² + (Y1-Y2)²]
i will look if if i find about angle
Posted: 01 Mar 2011, 15:09
by gthibert
Thanks !
Posted: 02 Mar 2011, 19:44
by gthibert
I've found a solution. I don't totaly understand the maths (i've found the formula on the web), but it works.
Here is the script :
Code: Select all
var xIn : Tparameter;
var yIn : Tparameter;
var angleOut: Tparameter;
var distanceOut: Tparameter;
procedure init;
begin
xIn := CreateParam('x',ptDataField); SetIsOutput(xIn,false);
yIn := CreateParam('y',ptDataField); SetIsOutput(yIn,false);
angleOut := CreateParam('angle', ptDataField ); setIsInput(angleOut,false);
distanceOut := CreateParam('distance', ptDataField ); setIsInput(distanceOut,false);
end;
function sgn (a : real) : real;
begin
if a < 0 then sgn := -1
else sgn := 1;
end;
function atan2 (y, x : real) : real;
begin
if x > 0 then atan2 := arctan (y/x)
else if x < 0 then atan2 := arctan (y/x) + pi
else atan2 := pi/2 * sgn (y);
end;
Procedure Callback(n:integer);
var x:single;
var y:single;
var angle:single;
var distance:single;
begin
if (n=xIn) or (n=yIn) then begin
x := (getValue(xIn) - 0.5);
y := (abs(getValue(yIn) - 1) - 0.5);
//angle
angle := atan2(x,y) * 180 / pi;
if angle < 0 then angle:= angle + 360;
setValue(angleOut, round(angle));
//distance
distance := sqrt(x*x + y*y);
setValue (distanceOut, distance);
end;
end;
Posted: 02 Mar 2011, 20:08
by gthibert
Does anyone knows how I can reverse those formulas ?
Getting X-Y from angle and distance ?
Guillaume
Posted: 02 Mar 2011, 20:09
by nay-seven
cool, but how do you use it..?
don't have anything from the out of the script...
Posted: 02 Mar 2011, 20:18
by gthibert
If you connect the pos-X, pos-Y out of a XY Pad to the in of the script, it should work.
It works on my side...
Posted: 02 Mar 2011, 20:42
by nay-seven
strange , i see something in , nothing out...
Posted: 02 Mar 2011, 23:59
by senso
the script seems to be perfect!
to reverse the formula:
x = distance*cos(angle)
y = distance*sin(angle)
Posted: 03 Mar 2011, 00:42
by nay-seven
ok, forget it , was experimenting too much thing in the same time ..
works fine
Posted: 03 Mar 2011, 01:59
by gthibert
Thanks Senso,
It works !
The only thing, is that if you want to control angle with a value from 0 to 360 the formula should be :
x = distance * cos(angle/360*pi*2)
y = distance * sin(angle/360*pi*2)