Scripts/TrigExplorer

From J Wiki
Jump to navigation Jump to search

Download the script here: File:Trigexplorer.ijs

NB. trigexplorer.ijs Trig Function Explorer
NB. See INFOTEXT at end of this file.

NB. Things we need...
require 'files isigraph numeric plot'

NB. Define this class (see end of script for invocation)
coclass  'trigExplorer'


NB. =========================================================
NB.
NB. 2005-02-14 1.00 KF Initial Release
NB. 2005-06-02 1.01 KF Fix formatting
NB.
NB. =========================================================
NB.
TRIGVER=: 1.01
TITLE=: 'Trig Explorer V',(4j2 ": TRIGVER)

NB. =========================================================
NB. Form definition
TRIGFORM=: 0 : 0
pc trigform closeok;
menupop "&Options";
menu reset "&Reset" "" "" "";
menusep;
menu clip "&Copy to Clipboard" "" "" "";
menu saveeps "Save as &EPS" "" "" "";
menu savepdf "Save as &PDF" "" "" "";
menu print "P&rint" "" "" "";
menusep;
menu about "&About" "" "" "";
menusep;
menu exit "E&xit" "" "" "";
menupopz;
xywh 0 0 260 260;cc plott isigraph;
xywh 276  70 160   8;cc eqninfo1 static;cn "";
xywh 276  80 160 120;cc trigtext static;cn "";
xywh 267   4   8   8;cc ax static;cn "";
xywh 274   4  12   8;cc spina  spin;
xywh 286   2 180  10;cc avalue trackbar tbs_autoticks tbs_both tbs_enableselrange tbs_top;
xywh 468   2  30  10;cc avalx  edit;
xywh 267  16   8   8;cc bx static;cn "";
xywh 274  16  12   8;cc spinb  spin;
xywh 286  14 180  10;cc bvalue trackbar tbs_autoticks tbs_both tbs_enableselrange tbs_top;
xywh 468  14  30  10;cc bvalx  edit;
xywh 267  28   8   8;cc cx static;cn "";
xywh 274  28  12   8;cc spinc  spin;
xywh 286  26 180  10;cc cvalue trackbar tbs_autoticks tbs_both tbs_enableselrange tbs_top;
xywh 468  26  30  10;cc cvalx  edit;
xywh 267  40   8   8;cc dx static;cn "";
xywh 274  40  12   8;cc spind  spin;
xywh 286  38 180  10;cc dvalue trackbar tbs_autoticks tbs_both tbs_enableselrange tbs_top;
xywh 468  38  30  10;cc dvalx  edit;
xywh 274  52  22   8;cc sin radiobutton;cn "Sin";
xywh 298  52  22   8;cc cos radiobutton group;cn "Cos";
xywh 320  52  22   8;cc tan radiobutton group;cn "Tan";
pas 0 0;pcenter;
rem form end;
)

NB. =========================================================
NB. Constructor
create=: 3 : 0
  wd TRIGFORM
  HWND=: wd'qhwndp'
  wd 'pn *',TITLE

  NB. Create plot object
  pl=: conew 'jzplot'
  PForm__pl=: 'trigform'
  PFormhwnd__pl=: HWND
  NB. connect to plot on form
  PId__pl=: 'plott'

  PShow__pl=: 0

  wd 'set trigtext *',TEXT1
  wd 'set ax *a'
  wd 'set bx *b'
  wd 'set cx *c'
  wd 'set dx *d'

  NB. trackbar data
  tsteps=: 0.1
  tcount=: 4
  titems=: 2 * tcount % tsteps
  thighv=: tcount
  tlowv=: - thighv
  tmid=: -: titems
  toffset=: - tmid
  Pi =: o.1

  reset_values ''

  do_trig ''

  wd 'pshow;'
)

NB. =========================================================
NB. Destructor
destroy=: 3 : 0
  wd 'pclose'
  codestroy ''
)

NB. =========================================================
NB. Formatters - formats sign based on value
plusf=: 3 : '((y<0){''+-''),'' '',": |y'
plusfx=: 3 : '((y<0){'' -''),": |y'


NB. =========================================================
NB. Reset values
reset_values=: 3 : 0
  NB. Initial values
  a=: 1.0
  b=: 1.0
  c=: 0
  d=: 0
  cur_func=: 1&o.
  cur_func_nam=: 'Sin'
)


NB. =========================================================
NB. Display Function
do_trig=: 3 : 0
  pd__pl 'reset'

  set_trackbars ''

  wd 'set eqninfo1 *Equation: y = ',(plusfx a),cur_func_nam,'(',(plusfx b),'x ',(plusf c),'Pi) ',(plusf d)

  NB. ploting...
  pd__pl 'textfont arial 36 bold italic'
  pd__pl 'textcolor darkslateblue'
  pd__pl 'textc 500 _10x Trig Explorer'
  pd__pl 'new 40x 20x -40x -70x'
  pd__pl 'xrange _8 8'
  pd__pl 'yrange _8 8'
  pd__pl 'ytic 0.5 1'
  pd__pl 'xtic 0.5 1'

  x=. steps _8 8 320
  pd__pl 'color blue,red'
  pd__pl 'type line;color blue'
  pd__pl x;cur_func x
  pd__pl 'type line;color red'
  pd__pl x; d + a * cur_func ((c*Pi) + b * x)

  pd__pl 'show'
)


NB. =========================================================
NB. set trackbars based on values a,b,c,d
set_trackbars=: 3 : 0
  wd 'set avalx *',(0j5": a)
  wd 'set bvalx *',(0j5": b)
  wd 'set cvalx *',(0j5": c)
  wd 'set dvalx *',(0j5": d)

  apos=: 0 >. titems <. tmid + a % tsteps
  bpos=: 0 >. titems <. tmid + b % tsteps
  cpos=: 0 >. titems <. tmid + c % tsteps
  dpos=: 0 >. titems <. tmid + d % tsteps

  wd 'set avalue 0 ', (4j0":apos) ,' ', (0":titems) ,' 1 1'
  wd 'set bvalue 0 ', (4j0":bpos) ,' ', (0":titems) ,' 1 1'
  wd 'set cvalue 0 ', (4j0":cpos) ,' ', (0":titems) ,' 1 1'
  wd 'set dvalue 0 ', (4j0":dpos) ,' ', (0":titems) ,' 1 1'
)


NB. =========================================================
NB. Exit buttons
trigform_close=: trigform_exit_button=: destroy


NB. =========================================================
NB. About message
trigform_about_button=: 3 : 0
  ver=. 'Trig Explorer V',(4j2 ": TRIGVER),INFOTEXT
  wdinfo 'Trig Explorer';ver
)


NB. =========================================================
NB. Slide controls
trigform_avalue_button=: 3 : 0
  apos=: ".avalue
  a=: tsteps * toffset + apos
  do_trig ''
)

trigform_bvalue_button=: 3 : 0
  bpos=: ".bvalue
  b=: tsteps * toffset + bpos
  do_trig ''
)


trigform_cvalue_button=: 3 : 0
  cpos=: ".cvalue
  c=: tsteps * toffset + cpos
  do_trig ''
)

trigform_dvalue_button=: 3 : 0
  dpos=: ".dvalue
  d=: tsteps * toffset + dpos
  do_trig ''
)


NB. =========================================================
NB. Spin controls
trigform_spina_button=: 3 : 0
  apos=: 0 >. titems <. (".avalue) + ".spina
  a=: tsteps * toffset + apos
  do_trig ''
)

trigform_spinb_button=: 3 : 0
  bpos=: 0 >. titems <. (".bvalue) + ".spinb
  b=: tsteps * toffset + bpos
  do_trig ''
)

trigform_spinc_button=: 3 : 0
  cpos=: 0 >. titems <. (".cvalue) + ".spinc
  c=: tsteps * toffset + cpos
  do_trig ''
)

trigform_spind_button=: 3 : 0
  dpos=: 0 >. titems <. (".dvalue) + ".spind
  d=: tsteps * toffset + dpos
  do_trig ''
)


NB. =========================================================
NB. Field entry
trigform_avalx_button=: 3 : 0
  a=: tlowv >. thighv <. 0".avalx
  do_trig ''
)

trigform_bvalx_button=: 3 : 0
  b=: tlowv >. thighv <. 0".bvalx
  do_trig ''
)

trigform_cvalx_button=: 3 : 0
  c=: tlowv >. thighv <. 0".cvalx
  do_trig ''
)

trigform_dvalx_button=: 3 : 0
  d=: tlowv >. thighv <. 0".dvalx
  do_trig ''
)


NB. =========================================================
NB. Radio controls
trigform_sin_button=: 3 : 0
  cur_func=: 1&o.
  cur_func_nam=: 'Sin'
  do_trig ''
)

trigform_cos_button=: 3 : 0
  cur_func=: 2&o.
  cur_func_nam=: 'Cos'
  do_trig ''
)

trigform_tan_button=: 3 : 0
  cur_func=: 3&o.
  cur_func_nam=: 'Tan'
  do_trig ''
)


NB. =========================================================
NB. Reset button
trigform_reset_button=: 3 : 0
  reset_values ''
  do_trig ''
)

NB. =========================================================
NB. Output formats
NB. do a refresh first (do_trig)
trigform_clip_button=: 3 : 0
  do_trig ''
  pd__pl 'clip'
)

trigform_saveeps_button=: 3 : 0
  do_trig ''
  pd__pl 'eps'
)

trigform_savepdf_button=: 3 : 0
  do_trig ''
  pd__pl 'pdf'
)

trigform_print_button=: 3 : 0
  do_trig ''
  pd__pl 'print'
)


NB. =========================================================
NB. Resize
trigform_plot_size=: 3 : 0
  isi_show__pl 0
)

NB. =========================================================
NB. Key bindings
trigform_f1_fkey=: trigform_about_button
trigform_f10_fkey=: trigform_saveeps_button
trigform_f11_fkey=: trigform_savepdf_button



NB. =========================================================
NB. Text block
TEXT1=: 0 : 0
Plot of sin, cos or tan (F)
 using the equation: y = aF(bx + c) + d
  Vary a for Amplitude
  Vary b for Frequecy
  Vary c for x origin (Phase)
  Vary d for y origin

The base function is plotted in blue and
 the modified function is plotted in red.

Move the slides to change the values of a,b,c and d.
Note:  c is multiplied by Pi to help with phase
Radio buttons select the function to plot.
)

NB. =========================================================
NB. INFOTEXT
INFOTEXT=: 0 : 0

A simple tool for exploring basic trig functions.

See: http://www.farnik.com

Copyright (c) 2005,2007 Kym Farnik (kym@farnik.com)

Permission is hereby granted, free of charge,
to any person obtaining a copy of this software
and associated documentation files (the "Software"),
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission
notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS.
)


NB. =========================================================
NB. Instantiate the class
cocurrent 'base'
'' conew 'trigExplorer'

NB. end.