aUCBLogo Demos and Tests / formulatest3
			
				
			
			setCaseIgnored false
be formulatest3 [singleshot false][FrameNr 0]
   ::formls=
   [   [
         pr a/((b+c)*f/g)
         show a/((b+c)*(f/g))
         pr (:A+1)/((B/(j-(g.iota)^alpha)+f)*g/i)
      ][   pr arctan (sin Delta+Omega)^(2*pi/epsilon^zeta)/gamma y
         [arctan a*b c/d]
      ]
   ]
ct   cs
(ss 0.5)
;   setUpdateGraph false
   WindowMode
   ht
   enableTextureFont
   disableTexture
   setLabelFont [Times]
   setLabelSize [100 100]*0.2
   setLabelAlign 1 1
   setPenSize [5 5]*(LabelSize).1/100
   rt 90
   PenUp
   (reRandom 0)
;untraceall
;trace primitives
;erract=[show this pause]
   x=-390
   y= 200
   load "lib/#.lg
   ::f1=Array count Procedures
   foreach Procedures ;[# Formula::drawBoxes Formula::init0 Formula::findy0 formulatest3]
   [   i=repcount
      ::f1.i=Formulas makeTreeFromBody Text ? [] ;makeTree ? 
         ;makeTreeFromBody Text "formulatest3 []
;stop
      ((f1.i)'init x y)
      seth 90
      f1.i'draw
      f1.i'drawBoxes
      y=f1.i'bottom
   ]
   home
;   Formula::drawParen 600
   updateGraph
   pr "Ok
   MouseScroll singleshot FrameNr
end
be MouseScroll singleshot FrameNr
   x=0
   y=FrameNr*10
   if singleshot
   [   setScreenRange -400 -300+y  400 300+y
      redraw
      updateGraph
      stop
   ]
   p0=[0 0 0]
   p=[0 0 0]
   x0=0
   y0=0
   mouseActive=false
   inmotion=false
   dispatchMessages
   OnMouseLeftDown 
   [   if not mouseActive
      [   p0=MousePos+p
         x0=p0.1+x
         y0=p0.2+y
         mouseActive=true
      ]
   ]
   OnMouseMotion
   [   if mouseActive and2 not inmotion
      [;   inmotion=true
         p=p0-MousePos
         x=p.1
         y=p.2
         setScreenRange -400 -300+y  400 300+y
         redraw
         updateGraph
      ;   inmotion=false
      ]
   ]
   OnMouseLeftUp
   [   mouseActive=false
      p0=p0-MousePos+p
      x=p.1
      y=p.2
   ]
   forever
   [   dispatchMessages
      waitMS 20
      if Key? [break]
   ]
   OnMouseLeftDown []
   OnMouseMotion []
   OnMouseLeftUp []   
   pr [End]
end
be Box x y w h
end
be Formulas lines dummy
   inherit Box 0 0 0 0
   local [f n fy y1 y0 bottom]
   f=Table 121
   fy=Table 121
   n=0
   lines=bf lines
   y1=0
;show lines
   while [lines != []]
   [   if (first lines) != []
      [   n=n+1
         f.n=Formula first lines lines
      ]
      lines=bf lines
   ]
      
   be init x_ y_
      s=(LabelSize).1
      Formulas::spacey=0.3*s
      Formulas::lineh=1*s
      fy.1=y_
      repeat n
      [   ifelse f.repcount != []
         [   local [xx yy]
            xx=LineTColumn (f.repcount)'lines
            yy=LineRow (f.repcount)'lines
            (show xx yy)
            xx=x_+8*xx
            ;xx=x_
            ((f.repcount)'init xx y_)
;            y_=y_-f.repcount'h-spacey
         ][;   y_=y_-lineh
         ]
      ]
      initxy x_ y_
      inity0 findy0
      repeat n
      [   ifelse f.repcount != []
         [   y_=y_-f.repcount'h-spacey
            xx=LineTColumn (f.repcount)'lines
            ((f.repcount)'init0 xx y_)
         ][   y_=y_-lineh
         ]
      ]
      bottom=f.n'y0-f.n'h-lineh
pr bottom
   end   
   be initwh oop_
      w=0
      h=0
      repeat n
      [   if f.repcount != []
         [   ((f.repcount)'initwh oop_)
            w=w+f.repcount'w
            h=h+f.repcount'h
         ]
      ]
   end
   
   be initxy x_ y_
      repeat n
      [   if f.repcount != []
         [   ((f.repcount)'initxy x_ y_)
         ]
      ]
   end
   be findy0
      y1=-100000
      ifelse f.1 != []
      [   repeat n
         [   y1=max y1 f.repcount'findy0
         ]
         output y1
      ][   output y
      ]
   end
   be inity0 ymin
      ifelse f.1 != []
      [   repeat n
         [   ((f.repcount)'inity0 ymin)
         ]
         y0=findy0
      ][   y0=y-ymin
      ]
      y=y-ymin
   end
   
   be init0 x_ y_
      if f.1 != []
      [   repeat n
         [   ((f.repcount)'init0 x_ y_)
         ]
      ]
      x=x+x_
      y=y+y_
      y0=f.n'y0
   end
   be draw x y
      repeat n
      [   if f.repcount != []
         [   f.repcount'draw
         ]
      ]
   end   
   be drawBoxes x y
      repeat n
      [   if f.repcount != []
         [   f.repcount'drawBoxes
         ]
      ]
   end   
end
be Formula expr lines
   inherit Box 0 0 0 0
   local [op_ oop f n opw y0 y1 lf paren pw]
   op_=[]
   f=Table 5
   paren=0
   pw=0
   x=0
   y=0
;   pr expr
   if List? expr
   [   ifelse ((Procedure? expr.1) or2 Primitive? expr.1)
      [   op_=pop "expr
         n=count expr
         ifelse op_=="--
         [   f.1=Formula expr.2 lines
            n=1
         ][
            ifelse op_=="=
            [   f.1=Formula first expr.1 lines
               f.2=Formula expr.2 lines
            ][
               repeat n
               [   f.repcount=Formula expr.repcount lines
               ;   expr=bf expr
               ]
            ]
         ]
      ][   n=count expr
         repeat n
         [   f.repcount=Formulas (List [] expr.repcount) lines
         ;   expr=bf expr
         ]
      ]
   ]
      
   be isop op_
      output op_=="* or2 op_=="+ or2 op_=="- or2 op_=="/
         or2 op_=="^ or2 op_==". or2 op_=="= or2 op_=="--
   end
   be ishorop op_
      output op_=="* or2 op_=="+ or2 op_=="- or2 op_=="=
   end
   
   be initwh oop_
      oop=oop_
      ifelse f.1 != []   
      [   repeat n
         [   ((f.repcount)'initwh op_)
         ]
         if ishorop op_
         [   w=opsizex.op_+(f.1'w)+f.2'w
            h=max f.1'h f.2'h
         ]
         if not isop op_
         [   op0size=LabelSize op_
            opw=op0size.1
            oph=op0size.2
            w=opw
            h=oph
            repeat n
            [   w=w+f.repcount'w+spacex
               h=max f.repcount'h h
            ]
         ]
         if (op_=="* and2 (oop=="+ or2 oop=="-))
         or2 (oop=="* and2 (op_=="+ or2 op_=="-))
         or2 (oop=="^ and2 (op_=="+ or2 op_=="-))
         or2 (oop==". and2 (op_=="+ or2 op_=="-))
         [   paren=1
 ignore [
            s=(LabelSize)
            setLabelSize [1 1]*h*1.2
            psize=LabelSize "(
            setLabelSize s
            pw=psize.1*1.4
]            
            pw=h*0.3
            w=w+pw*2
         ]
         if op_=="/
         [   w=(max f.1'w f.2'w)+opsizex.op_*2
            h=(f.1'h)+(f.2'h)+opsizey.op_*2
         ]
         if op_=="^
         [   w=(f.1'w)+(f.2'w)+opsizex.op_*2
            h=(f.1'h)+(f.2'h)+opsizey.op_*2
         ]
         if op_==".
         [   w=(f.1'w)+(f.2'w)+opsizex.op_*2
            h=(f.1'h)+(f.2'h)+opsizey.op_*2
         ]
         if op_=="--
         [   w=opsizex.op_+f.1'w
            h=f.1'h
         ]
      ][   local [s]
         ifelse symboltable.expr != []
         [   s=LabelSize symboltable.expr
         ][   s=LabelSize expr
         ]
         w=s.1
         h=s.2
      ]
   end
   
   be initxy x_ y_
      x=x_
      y=y_
      if f.1 != []
      [   ifelse isop op_
         [   if ishorop op_
            [   ((f.1)'initxy x+pw*paren y)
               ((f.2)'initxy x+pw*paren+opsizex.op_+f.1'w y)
            ]
            if op_=="/
            [   mw=(max f.1'w f.2'w)/2
               ((f.1)'initxy x+mw-(f.1'w)/2+opsizex.op_ y+opsizey.op_)
               ((f.2)'initxy f.1'x+f.1'w/2-(f.2'w)/2 f.1'findy0-opsizey.op_*2-(f.2'h)/2)
            ]
            if op_=="^
            [   ((f.1)'initxy x y)
               ((f.2)'initxy f.1'x+f.1'w f.1'findy0+f.1'h/2+f.2'h/2)
            ]
            if op_==".
            [   ((f.1)'initxy x y)
               ((f.2)'initxy f.1'x+f.1'w f.1'findy0-f.2'h/2)
            ]
            if op_=="--
            [   ((f.1)'initxy x+opsizex.op_+pw*paren y)
            ]
         ][   x1=x+opw+spacex   
            y1=y
            repeat n
            [   ((f.repcount)'initxy x1 y1)
               x1=f.repcount'x+f.repcount'w+spacex
            ]
         ]
      ]
   end
   
   be findy0
      y1=100000
      ifelse f.1 != []
      [   repeat n
         [   y1=min y1 f.repcount'findy0
         ]
         output y1
      ][   output y
      ]
   end
   
   be inity0 ymin
      ifelse f.1 != []
      [   repeat n
         [   ((f.repcount)'inity0 ymin)
         ]
         y0=findy0
      ][   y0=y-ymin
      ]
      y=y-ymin
   end
   
   be init0 x_ y_
      if f.1 != []
      [   repeat n
         [   ((f.repcount)'init0 x_ y_)
         ]
      ]
      x=x+x_
      y=y+y_
      y0=y0+y_
   end
   be init x_ y_
      s=(LabelSize).1
      Formula::opsizex=Table (List 
         "* 0.3*s
         "+ 0.6*s
         "- 0.6*s
         "/ 0.3*s
         "^ 0
         ". 0
         "= 0.6*s
         "-- 0.6*s)
      Formula::opsizey=Table (List 
         "* 0
         "+ 0
         "- 0
         "/ 0.4*s
         "^ 0
         ". 0
         "= 0
         "-- 0)
      Formula::opx=Table (List 
         "* 0.11*s
         "+ 0.06*s
         "- 0.06*s
         "/ 0
         "^ 0
         ". 0
         "= 0
         "-- 0.06*s)
      Formula::opy=Table (List 
         "* 0
         "+ 0
         "- 0
         "/ 0.2*s
         "^ 0
         ". 0
         "= 0
         "-- 0)
      Formula::parensizex=0.3*s
      Formula::pareny=0.18
      Formula::spacex=0.3*s
      Formula::spacey=0.3*s
      Formula::symboltable=Table (List
         "alpha "a
         "beta "b
         "chi "c
         "delta "d
         "epsilon "e
         "gamma "g
         "eta "h
         "iota "i
         "phi "j
         "kappa "k
         "lambda "l
         "mu "m
         "nu "n
         "omikron "o
         "pi   "p
         ":pi "p
         "theta "q
         "rho "r
         "sigma "s
         "tau "t
         "omega "w
         "xi "x
         "psi "y
         "zeta "z
         
         "Alpha "A
         "Beta "B
         "Chi "C
         "Delta "D
         "Epsilon "E
         "Gamma "G
         "Eta "H
         "Iota "I
         "Phi "J
         "Kappa "K
         "Lambda "L
         "Mu "M
         "Nu "N
         "Omikron "O
         "Pi   "P
         ":Pi "P
         "Theta "Q
         "Rho "R
         "Sigma "S
         "Tau "T
         "Omega "W
         "Xi "X
         "Psi "Y
         "Zeta "Z)
      initwh []
      initxy 0 0
      inity0 findy0
      init0 x_ y_
   end
   
   be draw
      ifelse f.1 != []
      [   repeat n
         [   f.repcount'draw
         ]
         if isop op_
         [   setXY x+(f.1'w)+opx.op_+pw*paren y+opy.op_
         ]
         local [lf]
         lf=LabelFont
         setLabelFont [Symbol]
         ifelse op_=="*   
         [   Label char 215
         ][   ifelse op_=="+   
            [   Label "+
            ][   ifelse op_=="-   
               [   Label "-
               ][   ifelse op_=="=   
                  [   Label "=
                  ][   ifelse op_=="/   
                     [   setXY x+opsizex.op_/2 y0+f.2'h+opy.op_ 
                        PenDown 
                        setXY x+w-opsizex.op_/2 y0+f.2'h+opy.op_ 
                        PenUp
                     ][   ifelse op_=="--
                        [   setX x
                           Label "-
                        ][   ifelse op_=="^ or2 op_==".
                           [
                           ][   setXY x y  
                              setLabelFont lf
                              Label op_
                           ]
                        ]
                     ]
                  ]
               ]
            ]
         ]
         if paren==1
         [   pu
            setXY x+pw/3 y0+h/2
            seth 0
            drawParen h
            setXY x+w-pw/5 y0+h/2
            seth 180
            drawParen h
            seth 90
         ]
         setLabelFont lf
      ][   setXY x y
         ifelse symboltable.expr != []
         [   lf=LabelFont
            setLabelFont [Symbol]
            Label symboltable.expr
            setLabelFont lf
         ][   Label expr
         ]
      ]
   end
   be drawParen h
      p0=Pos
      h0=Heading
      pd
      TessStart
      repeat 30
      [   forward h/60
         right 1
      ]
      left 1
      rt 60 fd 1.5*PenSize.1
      rt 120
      repeat Int 60*1
      [   forward h/60
         left 1
      ]
      right 1
      rt 120 fd 1.5*PenSize.1
      rt 60
      repeat 30
      [   forward h/60
         right 1
      ]
      TessEnd
      pu
   end
   
   be drawBoxes
      if f.1 != []
      [   repeat n
         [   f.1'drawBoxes
         ]
      ]
      setXY x y0
      setFC HSBA random 360 1 1 0.1
      setH 0
      fillRect [0 0] List w h
   end   
end