(C) 2007, Michael Gruhn.

desc:Goniometer
desc:Goniometer [LOSER]
//tags: analysis visualization
//author: LOSER

slider1:0<0,2,1{Dots,Lines,Rays}>Display

in_pin:left input
in_pin:right input
out_pin:none

@init
gfx_clear=-1;
off = 500000;
rot=-45*0.017453292;
DOTS = 0;
LINES = 1;
RAYS = 2;
MAXDRAWSPLS = 10000;
PHASEUPDATERATE = srate/8;
n = 0;
phC = 0;
ph = 0;
@slider

@block

@sample
s0 = sign(spl0);
s1 = sign(spl1);
angle = atan( spl0 / spl1 );
(s0 == 1 && s1 == -1) || (s0 == -1 && s1 == -1) ? angle += 3.141592654;
s0 == -1 && s1 == 1 ? angle += 6.283185307;
spl1 == 0 ? spl0 > 0 ? angle = 1.570796327 : angle = 4.71238898;
spl0 == 0 ? spl1 > 0 ? angle = 0 : angle = 3.141592654;
radius = sqrt( sqr(spl0)+sqr(spl1) ) ;
angle -= rot;
vert = 0[b] = cos(angle)*radius;
horiz = off[b] = sin(angle)*radius;
b<MAXDRAWSPLS ? b += 1;


s0 != s1 ? phC-=1:phC+=1;
(n+=1) > PHASEUPDATERATE ? (
  ph = (1-(phC/n))/2;
  phC = n = 0;
);

@gfx 450 450

size = min(gfx_w,gfx_h*.95)|0;

// override drawing functions to center
gxo = gfx_w*.5 - size*.5;
function gfx_lineto(x,y,aa) ( gfx_x+=gxo; gfx_lineto(x+gxo,y,aa); gfx_x-=gxo; );
function gfx_setpixel(r,g,b) ( gfx_x+=gxo; gfx_setpixel(r,g,b); gfx_x-=gxo; );
function gfx_drawnumber(y,x) ( gfx_x+=gxo; gfx_w<200||gfx_drawnumber(y,x); gfx_x-=gxo; );
function gfx_drawchar(x) (gfx_x+=gxo; gfx_w<200||gfx_drawchar(x); gfx_x-=gxo;);

sizeH = size/2;
sizeDSqr05 = sizeH * 0.70710681;
sizeQ = sizeH/2;
size3Q = 3*sizeQ;

(mouse_cap) ? (
  gfx_r=gfx_g=gfx_b=0; gfx_a=1;
  gfx_x=gfx_y=0;
  gfx_rectto(gfx_w,gfx_h);
);

gfx_r=gfx_g=gfx_b=0; gfx_a=0.025;
gfx_x=gfx_y=0;
gfx_rectto(gfx_w,gfx_h);

x = y = sizeH;

i = min(b,MAXDRAWSPLS);
while (
  slider1 == DOTS ? (
  gfx_a=1;
  gfx_x=sizeH+0[i]*sizeDSqr05;
  gfx_y=sizeH-off[i]*sizeDSqr05;
  gfx_setpixel(0.5,1,0);
  ):(
    slider1 == LINES ? (
      gfx_x=x; gfx_y=y;
      x=sizeH+0[i]*sizeDSqr05;
      y=sizeH-off[i]*sizeDSqr05;
    ):(
      gfx_x=sizeH+0[i]*sizeDSqr05;
      gfx_y=sizeH-off[i]*sizeDSqr05;
    );
      gfx_r=0.5; gfx_g=1; gfx_b=0; gfx_a=0.6;
  gfx_lineto(x,y,0);
  );
  (i-=1)>0;
);
b=0;

gfx_r=gfx_g=gfx_b=0.8; gfx_a=1;

gfx_x=sizeQ; gfx_y=sizeQ;
gfx_lineto(size3Q,size3Q,0);

gfx_x=sizeQ; gfx_y=size3Q;
gfx_lineto(size3Q,sizeQ,0);

gfx_x = gfx_y = sizeQ;
gfx_drawchar($'L');
gfx_x = size3Q; gfx_y = sizeQ;
gfx_drawchar($'R');
gfx_x = sizeH; gfx_y = 0;
gfx_drawchar($'M');
gfx_x = 0; gfx_y = sizeH;
gfx_drawchar($'+');
gfx_drawchar($'S');
gfx_x = size-20; gfx_y = sizeH;
gfx_drawchar($'-');
gfx_drawchar($'S');


gfx_r=gfx_g=gfx_b=0; gfx_a=1;
gfx_x=0+gxo; gfx_y=size;
gfx_rectto(size+1+gxo,size+20);
gfx_r=1; gfx_g=0; gfx_b=0; gfx_a=1;
gfx_x=1+ph*(size-1); gfx_y=size+1;
gfx_lineto(gfx_x,size+19,0);

gfx_r=gfx_g=gfx_b=0.8; gfx_a=1;
gfx_x = 0; gfx_y = size+10;
gfx_drawnumber(0,0);
gfx_x = sizeH-8; gfx_y = size+10;
gfx_drawnumber(90,0);
gfx_x = size-25; gfx_y = size+10;
gfx_drawnumber(180,0);

gfx_r=0.5; gfx_g=1; gfx_b=0; gfx_a=1;
gfx_x = 0; gfx_y = size;
gfx_drawnumber(ph*180,0);
