NB. supports graphics using the isigraph control
NB. built from project: ~Source\classes\graphics\isigraph\isigraph
NB. supports the isigraph control
NB.
NB. method:
NB.   gopen''      opens a graphics window
NB.   g...         fns to generate graphics
NB.   gshow''      show graphics
NB.
NB. points are plotted in a box with coordinates (0,0) at bottom left
NB. to (1000,1000) at top right.
NB.
NB. points should be given as a 2 element vector or 2 column matrix
NB. colors should be given as 3 numbers in range (0,255) for RGB,
NB. or as a single RGB number.
NB.
NB. see also isigutil.ijs for various supporting utilities.

script_z_ '~system\packages\graphics\bmp.ijs'
script_z_ '~system\main\gl2.ijs'
script_z_ '~system\classes\view\viewmat.ijs'

cocurrent 'z'


NB. main functions:
NB. gbitmap     bitmap viewer using isigraph control
NB. gbrush      set brush color
NB. gclear      clear graphics window (non-empty y. is color)
NB. gfit        fit data to graphics window
NB. gopen       open graphics window
NB. glines      display lines
NB. gpolygon    display polygon given vertices
NB. gpen        set pen color and style
NB. gscale      scale from range (_1,1) to (0,1000)
NB. gshow       show graph
NB.

NB. ========================================================
NB.*gbitmap v bitmap viewer using isigraph control
NB. y. is a boolean matrix
NB.
NB. Note max size is about 80 x 80 - after which the graphics buffer
NB. is full.
NB.
NB. This function is fast for small bitmaps. Use viewbmp in bmp.ijs
NB. to display larger bitmaps.
NB.
NB. e.g.  gshow gbitmap ?40 40$2 [gopen''
gbitmap=: 3 : 0
len=. *:rws=. #y.
ind=. (len$i.rws),.rws#i.-rws
scale=. 1000%rws
dat=. ,.<.scale*(,y.)#ind
dat=. dat,"1 ,~>.scale
glbrush_jgl2_ grgb 0 0 0
glrect_jgl2_ dat
)

NB. ========================================================
NB.*gbrush v set brush color
gbrush=: glbrush_jgl2_ @: grgb

NB. ========================================================
NB.*gclear v clear graphics window
gclear=: 3 : 0
glclear_jgl2_''
if. #y. do.
  gbrush y.
  glrect_jgl2_ 0 0 1000 1000
end.
)

NB. ========================================================
NB.*gfit v fit data to graphics window
NB.
NB. scales each column of data independently to range (0,1000)
gfit=: 3 : 0
min=. <./y.
max=. >./y.
(y.-"1 min)*"1 [ 1000%max-min
)

NB. ========================================================
NB.*glines v display line connecting points
NB.
NB. {color;width;style} glines points
NB.
NB. points should be a 2 column matrix of xy values
glines=: 3 : 0
gllines_jgl2_ y.
:
glpen_jgl2_ x.
gllines_jgl2_ y.
)

NB. ========================================================
NB.*gopen v open graphics window
NB. y. = controlname;parentname
NB.    if either empty, default to 'isigraph'
NB. e.g   gopen ''
NB.       gopen '';'J Graphics'
gopen=: 3 : 0
y=. 2{.}.0;y.
'c n'=. (<'isigraph') (I. y=<'') }y
if. (<c) e. <;._2 wd 'qp;' do.
  wd 'psel ',c,';ptop'
  glclear_jgl2_''
else.
  wd 'pc ',c,' closeok;pn *',n
  wd 'xywh 0 0 150 150;cc g0 isigraph rightmove bottommove'
  wd 'pas 0 0;pcenter;pmove 150 5 -1 -1'
  wd 'ptop;pshow;'
end.
)

NB. ========================================================
NB.*gpen v set pen color [;width,style]
NB. (default 1,0}
NB.
gpen=: 3 : 0
y=. boxopen y.
'c ws'=. 2{. y,(#y)}.0 0 0;1 0
glpen_jgl2_ ws
grgb c
)

NB. ========================================================
NB.*gpolygon v draw polygon given points
NB. {color} gpolygon points
gpolygon=: 3 : 0
glpolygon_jgl2_ y.
:
glbrush_jgl2_ grgb x.
glpolygon_jgl2_ y.
)

NB. ========================================================
NB.*grgb v set color values
grgb=: 3 : 0
clr=. y.
if. 1=#clr do. clr=. |. 256 256 256 #: {.clr end.
glrgb_jgl2_ clr
)

NB. ========================================================
NB.*gscale v scale from (-1,1) to (0,1000)
gscale=: 500&* @ >:

NB. ========================================================
NB.*gshow v {parent} gshow graph
gshow=: 3 : 0
'isigraph' gshow y.
:
wd 'psel ',x.,';ptop;'
glshow_jgl2_''
)

NB. isigraph utilities
NB.
NB. use with isigraph.ijs
NB.
NB. points are a 2 element vector or 2 column matrix of x,y coordinates
NB. from (-1,-1) at bottom left, to (1,1) at top right.
NB.
NB. cile         x. cile values of y.
NB. fit01        fit values into range 0 1
NB. fit11        fit values into range _1 1
NB. fitrect01    fit rect values into range 0 1
NB. fitrect11    fit rect values into range _1 1
NB. grayscale    grayscale with values from 0 to 1
NB. hue          colors with values from 0 to 1
NB. hueRGB       variant of hue specifically for RGB colors
NB. polygon      vertices of regular polygon
NB. rotate       rotate points by angle
NB.
NB. HUES         standard color set

HUES=: 255*|."1#:7|3^i.7      NB. standard color set

NB. =========================================================
NB.*cile v x. cile values of y.
NB. example:
NB.    3 cile i.12
cile=: $@] $ ((* <.@:% #@]) /:@/:@,)

NB.*grayscale v generate grayscale
NB. example:
NB.   grayscale (i.%<:) 5
grayscale=: 3&#"0 @ >.@ (255&*)

NB. =========================================================
NB.*fit01 v fit data to range 0 1
NB. form: [anisotropic] fit data
NB.
NB. if left argument is 1, scales columns independently,
NB. otherwise applies same factors to all data,
fit01=: 3 : 0
0 fit01 y.
:
dat=. y.
s=. $dat
if. x.=0 do. dat=. ,dat end.
min=. <./dat
max=. >./dat
s $ ,(dat -"1 min) %"1 max-min
)

NB. =========================================================
NB.*fit11 v fit data to range _1 1
fit11=: <: @: +: @: fit01

NB. =========================================================
NB.*fitrect01 v fit rectangle data to 0 1
NB. form: [anisotropic] fitrect01 data
NB.
NB. if left argument is 1, applies same factors to both columns
NB. otherwise, scales columns independently.
fitrect01=: 0&$: : (4 : 0)
s=. $y.
'x y w h'=. |: _4[\ ,y.
rx=. #x
d=. x. fit01 (x,x+w) ,. y,y+h
'x xw'=. (2,rx)${."1 d
'y yh'=. (2,rx)${:"1 d
s $ , x,.y,.(xw-x),.yh-y
)

NB. =========================================================
NB.*fitrect11 v fit rectangle data to _1 1
fitrect11=: 3 : 0
s=. $y.
'x y w h'=. |: _4[\ ,y.
rx=. #x
d=. <: +: x. fit01 (x,x+w) ,. y,y+h
'x xw'=. (2,rx)${."1 d
'y yh'=. (2,rx)${:"1 d
s $ , x,.y,.(xw-x),.yh-y
)

NB. ========================================================
NB.*hue v generate color from color set
NB. x. is color set
NB. y. is values from 0 to 1, selecting color
hue=: 4 : 0
y=. y.*<:#x.
b=. x. {~ <.y
t=. x. {~ >.y
k=. y-<.y
(t*k)+b*-.k
)

NB. ========================================================
NB.*hueRGB v generate color from RGB color se
NB. x. is RGB color set (default HUES)
NB. y. is values from 0 to 1, selecting color
hueRGB=: (HUES&$:) : (4 : 0)
<. 0.5 + x. hue y.
)

NB. ========================================================
NB.*polygon v vertices of a regular polygon
NB. y. = number of points
NB. x. = scale factor on angle 2p1%y.  (default 1)
NB.
NB. e.g.   polygon 5 = pentagon
NB.      2 polygon 5 = 5 pointed star
polygon=: 1&$: : (4 : '|: clean 2 1 o./ (2p1*x.%y.)*i.>:y.')

NB. ========================================================
NB.*rotate v rotate angle by given amount
NB.
NB. e.g. 0.5p1 rotate points = rotate clockwise a quarter turn
rotate=: 4 : 0
rot=. 2 2$1 1 _1 1*0 1 1 0{2 1 o. x.
rot +/ . * "2 1 y.
)
