aUCBLogo Demos and Tests / formulatest2
setCaseIgnored false
be formulatest2
::formulas=
[
[a/((b+c)*f/g)]
[a/((b+c)*(f/g))]
[(:A+1)/((B/(j-(g.iota)^alpha)+f)*g/i)]
[arctan (sin Delta+Omega)^(2*pi/epsilon^zeta)/gamma y]
; [arctan a*b c/d]
]
ct cs
(ss 0.5)
setUpdateGraph false
ht
setLabelFont [Times]
disableTextureFont
setLabelSize [100 100]*0.5
setLabelAlign 1 1
setPenSize [5 5]*(LabelSize).1/100
rt 90
PenUp
(reRandom 0)
;untraceall
;trace primitives
;erract=[show this pause]
foreach formulas
[ ::f1=Formula makeTree ?
;stop
x=-390+250*(Int (#-1)/2)
y=-290+350*(mod #-1 2)
(f1'init x y)
seth 90
f1'draw
; f1'drawBoxes
]
home
; Formula::drawParen 600
updateGraph
end
be Box
local [x y w h]
end
be Formula expr
local [op_ oop f n x y w h opw y0 y1 lf paren pw]
op_=[]
f=Table 5
paren=0
pw=0
; pr expr
if (Procedure? expr.1) or2 Primitive? expr.1
[ op_=pop "expr
n=count expr
repeat n
[ f.repcount=Formula expr.repcount
; expr=bf expr
]
]
be isop op_
output op_=="* or2 op_=="+ or2 op_=="- or2 op_=="/
or2 op_=="^ or2 op_==".
end
be ishorop op_
output 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
]
][ 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)
]
][ 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.2*s
"+ 0.6*s
"- 0.6*s
"/ 0.3*s
"^ 0
". 0)
Formula::opsizey=Table (List
"* 0
"+ 0
"- 0
"/ 0.4*s
"^ 0
". 0)
Formula::opx=Table (List
"* 0.00*s
"+ 0.06*s
"- 0.06*s
"/ 0
"^ 0
". 0)
Formula::opy=Table (List
"* 0
"+ 0
"- 0
"/ 0.2*s
"^ 0
". 0)
Formula::parensizex=0.3*s
Formula::pareny=0.18
Formula::spacex=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_=="/
[ 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_=="^
[
][ 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