// (C) 2009, Lubomir I. Ivanov
//
// NO WARRANTY IS GRANTED. THIS PLUG-IN IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
// WARRANTY OF ANY KIND. NO LIABILITY IS GRANTED, INCLUDING, BUT NOT LIMITED TO,
// ANY DIRECT OR INDIRECT,  SPECIAL,  INCIDENTAL OR CONSEQUENTIAL DAMAGE ARISING
// OUT OF  THE  USE  OR INABILITY  TO  USE  THIS PLUG-IN,  COMPUTER FAILTURE  OF
// MALFUNCTION INCLUDED.  THE USE OF THE SOURCE CODE,  EITHER  PARTIALLY  OR  IN
// TOTAL, IS ONLY GRANTED,  IF USED IN THE SENSE OF THE AUTHOR'S INTENTION,  AND
// USED WITH ACKNOWLEDGEMENT OF THE AUTHOR. FURTHERMORE IS THIS PLUG-IN A  THIRD
// PARTY CONTRIBUTION,  EVEN IF INCLUDED IN REAPER(TM),  COCKOS INCORPORATED  OR
// ITS AFFILIATES HAVE NOTHING TO DO WITH IT.  LAST BUT NOT LEAST, BY USING THIS
// PLUG-IN YOU RELINQUISH YOUR CLAIM TO SUE IT'S AUTHOR, AS WELL AS THE CLAIM TO
// ENTRUST SOMEBODY ELSE WITH DOING SO.
// 
// Released under GPL:
// <http://www.gnu.org/licenses/>.
//
// References:
// Uses code from 'gfxGionometer' by Michael Gruhn.

desc:Phase-Meter (Click to refresh)
slider1:0<0,1,1{Funky Drummer,Funky Worm,Space Funk}>Color Scheme
slider2:50<0,100,0.1>Precision (CPU usage)
slider3:1<1,5,0.001>Scale (x)

@init
// @ init variables
gfx_clear=-1;
off=500000;
off2=off*2;
nsamples=off;
rot=-45*0.017453292;
thstep=5;
thtotal=180/thstep;
pid180=-$pi/180;
sin45=sin(pid180*45);
cos45=cos(pid180*45);
sin135=sin(pid180*135);
cos135=cos(pid180*135);
phc=b=i=0;
phrate=srate/64;

@slider
// @ color scheme
slider1==0?(
  cr=0.5;cb=1;cg=1;
  crc=0.5;cgc=0.5;cbc=1;
):
slider1==1?(
  cr=1;cb=0.5;cg=1;
  crc=1;cgc=0.6;cbc=0.1;
):(  
  cr=0.5;cg=1;cb=0.5;
  crc=0.9;cgc=1;cbc=0.9;
);

// @ precision
prec=exp((100-slider2)*0.041);

// @ scale
vscale=slider3;

@sample

k>prec?(
  // @ angles
  angle=atan(spl0/spl1)-rot;
  radius=min(1.42,sqrt(spl0*spl0+spl1*spl1)*vscale);  
  (s1=sign(spl1))==-1?angle+=$pi;

  // @ store x,y values
  0[b]=cos(angle)*radius;
  off[b]=sin(angle)*radius;

  // @ phase cancelation (store colors)  
  (s0=sign(spl0))!=s1?phc-=1:phc+=1;
  (n+=1)>phrate?(ph=(1-(phc/n))*0.5;phc=n=0);
  off2[b]=0;
  ph>0.5?off2[b]=1; 

  // @ update
  b<nsamples?b+=1;
  k=0;
);
k+=1;

@gfx 400 200
// ----------------------
// @ clear plot
// ----------------------
(mouse_cap) ? (
  gfx_r=gfx_g=gfx_b=gfx_x=gfx_y=0;
  gfx_a=1;
  gfx_rectto(gfx_w,gfx_h);
  phc=ph=n=0;
);
// ----------------------
// @ get dimensions
// ----------------------
margin=25;
dim=min(gfx_w*0.5,gfx_h);
r=dim-margin;
rscale=r*0.7;

// ----------------------
// @ draw semi cicle
// ----------------------
newx=2*dim-margin;
newy=dim;
i=1;
gfx_a=1;
gfx_r=gfx_g=gfx_b=0.9;
loop(thtotal,
    thi=thstep*i;
    gfx_x=newx;gfx_y=newy;
    theta=pid180*thi;
    newx=dim+r*cos(theta);
    newy=dim+r*sin(theta);    
    gfx_lineto(newx,newy,1);
    i+=1;
);

// ----------------------
// @ draw grid
// ----------------------
// @ center
gfx_a=gfx_r=gfx_g=gfx_b=0.7;
gfx_x=gfx_y=dim;
gfx_lineto(dim,margin,1);
gfx_x=gfx_y=dim;
gfx_lineto(margin,dim,1);

// @ left
gfx_x=gfx_y=dim;
gridx1=dim+r*cos135;
gridy1=dim+r*sin135;
gfx_lineto(gridx1,gridy1,1);
gfx_x-=15;
gfx_y-=15;
gfx_a=gfx_r=gfx_g=gfx_b=1;
gfx_drawchar($'L');

// @ right
gfx_a=gfx_r=gfx_g=gfx_b=0.7;
gfx_x=gfx_y=dim;
gridx0=dim+r*cos45;
gridy0=dim+r*sin45;
gfx_lineto(gridx0,gridy0,1);
gfx_x+=7;
gfx_y-=15;
gfx_a=gfx_r=gfx_g=gfx_b=1;
gfx_drawchar($'R');

// ----------------------
// @ draw dots
// ----------------------
i = min(b,nsamples)-1;
while(
  gfx_a=1;  
  gfx_x=dim+0[i]*rscale;
  gfx_y=dim-off[i]*rscale;
  off2[i]==0?gfx_setpixel(cr,cg,cb):gfx_setpixel(crc,cgc,cbc);  
  (i-=1)>0;
);
b=0;

// ----------------------
// @ bottom
// ----------------------
gfx_r=gfx_g=gfx_b=0;gfx_a=1;
gfx_x=0;gfx_y=dim;
gfx_rectto(gfx_w,gfx_h+5);
gfx_x=margin;
gfx_y=dim;
gfx_r=gfx_g=gfx_b=gfx_a=0.9;
gfx_lineto(dim*2-margin,dim,1);
gfx_x=margin-20;gfx_y=dim-5;
gfx_drawchar($'-');
gfx_drawchar($'S');
gfx_x=dim*2-20;gfx_y=dim-5;
gfx_drawchar($'+');
gfx_drawchar($'S');
gfx_x=dim-2;gfx_y=margin-15;
gfx_drawchar($'C');

// ----------------------
// @ fade out
// ----------------------
gfx_r=gfx_g=gfx_b=0;gfx_a=0.05;
gfx_x=gfx_y=0;
gfx_rectto(gfx_w,gfx_h);

// ----------------------
// @ phase
// ----------------------
gfx_a=1;
gfx_r=gfx_g=gfx_b=0;
gfx_x=gfx_y=5;
gfx_rectto(dim-20,margin);
gfx_r=gfx_g=gfx_b=1;
gfx_x=gfx_y=5;
gfx_drawchar($'C');
gfx_drawchar($'-');
gfx_drawchar($'R');
gfx_drawchar($' ');
gfx_drawchar($'=');
gfx_drawchar($' ');
ph180=ph*180;
gfx_drawnumber(ph180,0);
