@Ignatz Although it was rushed, I found it to have few errors. I don’t know the cause because I didn’t look into it that much. It still works great for the most part. [Edit] I fixed the error it had, now I hope you think it is worth looking at.
Here is the first part:
--# Main
-- MyFactorCalc
displayMode(FULLSCREEN)
supportedOrientations(ANY)
function setup()
vara,varb,varc,var=readLocalData("vara",1),readLocalData("varb",2),readLocalData("varc",1),""
txt,bpos,cpos,counter="",readLocalData("bpos",true),readLocalData("cpos",true),readLocalData("counter",120)
arrows={upArrow={},downArrow={}}
for i=1,6 do table.insert(arrows.upArrow,Arrow{size=vec2(70,35)})
table.insert(arrows.downArrow,Arrow{size=vec2(70,35),angle=180}) end
factorButton=TextButton{text=string.upper("Factor"),pos=vec2(WIDTH/2,HEIGHT/8),
fill=color(255, 0, 186, 255),font="Baskerville-Bold",fontSize=80}
establishArrowFuncs()
end
function draw()
var=string.char(counter)
if cpos then varc=math.abs(varc) else varc=-math.abs(varc)end
if bpos then varb=math.abs(varb) else varb=-math.abs(varb)end
background(255, 255, 255, 255)
textMode(CENTER)
textAlign(CENTER)
fill(0, 45, 255, 255)
textWrapWidth(500)
fontSize(30)
text("Modify the variables, then click factor to factor the formula.",WIDTH/2,HEIGHT-100)
fontSize(50)
font("AmericanTypewriter-Bold")
fill(0)
local s=getString(vara,varb,varc,var)
text(s,WIDTH/2,HEIGHT*3/4)
textWrapWidth(-1)
text(txt,WIDTH/2,HEIGHT/4)
drawSelections()
for i,v in pairs(arrows) do
for n,m in ipairs(v) do
if m.counter and m.counter>1.2 then
if math.floor(m.counter*25)%2==0 then m.func(BEGAN) end
end
end
end
factorButton:draw()
end
function factor(a,b,c)
txt="Factoring..."
local signb,signc=sign(b),sign(c)
local mm=math.greatestCommonFactor(a,b)
local m=math.abs(math.greatestCommonFactor(mm,c))
a,b,c=a/m,b/m,c/m
local ac=a*c
local factors=math.findAllFactors(ac)
local sol
for i,v in ipairs(factors) do
for n,m in ipairs(factors) do
if i~=n or #factors==1 then
if m+v==b and m*v==ac then
sol=vec2(m,v)
elseif -m+v==b and -m*v==ac then
sol=vec2(-m,v)
elseif m-v==b and m*-v==ac then
sol=vec2(m,-v)
elseif -m-v==b and -m*-v==ac then
sol=vec2(-m,-v)
end
end
end
end
if sol then
local function func(a,b,sol,s)
local gcf=math.greatestCommonFactor
local aa=gcf(a,sol.x)
local bb=gcf(b,sol.y)
local cc=gcf(a,sol.y)
local dd=gcf(b,sol.x)
local ab=math.abs(aa)+math.abs(bb)
local cd=math.abs(cc)+math.abs(dd)
local soll=vec2(aa,bb)
if not s or s==1 then
if ab<cd and sign(cc)~=-1 or sign(aa)==-1 then soll=vec2(cc,dd) end elseif s==2 then
soll=vec2(cc,dd) end
return soll:unpack()
end
local tt
::place::
local aa,cc=func(a,c,vec2(sol.y,sol.x),tt)
local f=a/aa
local ff=sol.x/aa
if cc*ff~=c and ff==-1 then
ff=2
end
if sign(aa)==-1 then
aa=-aa
f=-f
ff=-ff
end
if not (aa*f==a and (cc*f)+(aa*ff)==b and cc*ff==c) then
tt=2
goto place
end
txt=getFactoredString(aa,cc,f,ff,m)
else txt="NOT FACTORABLE"
if m~=1 then
txt=tostring(m).."("..getString(a,b,c,var)..")"
end
end
end
function drawSelections()
local tab={
{txt="a",tag="math.floor(vara)"},{txt="+/-",tag="stringSign(varb)"},
{txt="b",tag="math.floor(math.abs(varb))"},{txt="+/-",tag="stringSign(varc)"},
{txt="c",tag="math.floor(math.abs(varc))"},{txt="var.",tag="var"}}
for i,v in ipairs(tab) do
pushStyle()
local a=color(255,0,0)
if i==2 or i==3 then a=color(0,0,255)
elseif i==4 or i==5 then a=color(67, 255, 0, 255)
elseif i==6 then a=color(0, 255, 223, 255) end
fill(0)
fontSize(30)
textMode(CENTER)
local pos=vec2(WIDTH*i/(#tab+1),HEIGHT/2+150)
text(tostring(v.txt),pos:unpack())
pos.y=HEIGHT/2-30
fill(a)
rectMode(CENTER)
rect(pos.x,pos.y,100,100)
fill(255)
font("Baskerville-SemiBold")
fontSize(55)
text(tostring(loadstring("return "..v.tag)()),pos:unpack())
arrows.upArrow[i].pos.x=pos.x
arrows.upArrow[i].pos.y=pos.y+90
arrows.upArrow[i]:draw()
arrows.downArrow[i].pos.x=pos.x
arrows.downArrow[i].pos.y=pos.y-90
arrows.downArrow[i]:draw()
popStyle()
end
end
function establishArrowFuncs()
local s=ENDED
arrows.upArrow[1].func=function(a)if a==s then vara=vara+1;saveLocalData("vara",vara) end end
arrows.downArrow[1].func=function(a)if a==s then vara=math.max(vara-1,1)saveLocalData("vara",vara) end end
arrows.upArrow[2].func=function(a)if a==s then bpos=not bpos;saveLocalData("bpos",bpos) end end
arrows.downArrow[2].func=function(a)if a==s then bpos=not bpos;saveLocalData("bpos",bpos) end end
arrows.upArrow[3].func=function(a)if a==s then varb=math.abs(varb)+1;saveLocalData("varb",varb) end end
arrows.downArrow[3].func=function(a)if a==s then varb=math.max(math.abs(varb)-1,0)saveLocalData("varb",varb) end end
arrows.upArrow[4].func=function(a)if a==s then cpos=not cpos;saveLocalData("cpos",cpos) end end
arrows.downArrow[4].func=function(a)if a==s then cpos=not cpos;saveLocalData("cpos",cpos) end end
arrows.upArrow[5].func=function(a)if a==s then varc=math.abs(varc)+1;saveLocalData("varc",varc) end end
arrows.downArrow[5].func=function(a)if a==s then varc=math.max(math.abs(varc)-1,0)saveLocalData("varc",varc) end end
arrows.downArrow[6].func=function(a)if a==s then
counter=counter+1
if counter>122 then counter=97 end
saveLocalData("counter",counter)
end
end
arrows.upArrow[6].func=function(a)if a==s then
counter=counter-1
if counter<97 then counter=122 end
saveLocalData("counter",counter)
end
end
end
function touched(t)
for i,v in pairs(arrows) do
for n,m in ipairs(v) do
if m:touched(t) then txt="" end
end
end
factorButton:touched(t)
if factorButton.state==ENDED then
factor(vara,varb,varc)
end
end
function orientationChanged(o)
if factorButton then factorButton.pos=vec2(WIDTH/2,HEIGHT/8) end
end