Lua问题与cos?

时间:2014-10-24 22:23:15

标签: lua

我已经在论坛上重写了我以前的代码以获得更好的东西,这是针对TI-Nspire计算器的,它将解决三角形的所有缺失边和角度,然后绘制三角形使用所提供的度量或侧面/角度。我有解决方案查找器的问题,当我将数据输入到A,B,C,a,b,c的字段时,它不会计算任何缺失的一面或者区域/周长。我可以点击输入,它不会崩溃或解决任何问题。如果我填写所有字段,我得到我的if语句错误,在此之前我的错误^ 1.Heres代码

platform.apilevel = '2.3'     
A,B,C,a,b,c,Perimeter,Area=1,2,3,4,5,6,7,8
values = {0,0,0,0,0,0,0,0} 
selection=1

function on.arrowKey(key)
if key=="up" then
    selection=selection-(selection>1 and 1 or -7)
elseif key=="down" then
    selection=selection%8+1     
end
platform.window:invalidate()
end

function on.paint(gc)
gc:setColorRGB(0,0,0)
gc:fillRect(0,0,318,212)
gc:setFont("sansserif", "r", 12)
gc:setColorRGB(0,200,0)
gc:drawString("Welcome Right Triangle Solver V:1",37,-2)
gc:setColorRGB(200,0,0)
gc:setFont("sansserif","i",6)
gc:drawString("Created by: ApprenticeCoder And Ldstudios Of tifreakware.net",30,16)
gc:setFont("sansserif","b",12)
gc:drawString("Only Click",16,160)
gc:drawString("Enter Once!",10,180)
gc:drawRect(6,160,98,44)
gc:drawRect(5,32,100,125)
gc:drawRect(151,32,165,178)
gc:drawRect(151,45,165,165)  
gc:setFont("sansserif","i",6)
gc:drawString("Section For Drawing Triangle",173,32)
gc:setColorRGB(200,0,0)

for i,v in ipairs({"A","B","C","a","b","c","Perimeter","Area"}) do
    gc:setFont("serif","r",12)
    if selection==i then
        gc:setFont("serif","b",12)
    end
    gc:drawString(v..": "..values[i],10,15+15*i) 
end
end

function on.charIn(char) 
if char:find("%d") then 
    values[selection]=tonumber(values[selection]..char)
    platform.window:invalidate()
end
end

function on.backspaceKey()
values[selection]=string.sub(tostring(values[selection]),0,-2)
if values[selection]:find("%d") then
    values[selection]=tonumber(values[selection])
else
    values[selection]=0
end
platform.window:invalidate()
end
function on.enterKey()
if values[A]==0 and values[B]~=0 and values[C]~=0 and values[a]~=0 then
values[A]=math.sqrt(values[B]^2+values[C]^2-((2*values[B]*values[C])*math.cos(values[a])))
elseif values[B]==0 and values[A]~=0 and values[C]~=0 and values[b]~=0 then
values[B]=math.sqrt(values[A]^2+values[C]^2-((2*values[A]*values[C])*math.cos(values[b])))
elseif values[C]~=0 and values[A]~=0 and values[B]~=0 and values[c]~=0 then
values[C]=math.sqrt(values[A]^2+values[B]^2-((2*values[A]*values[B])*math.cos(values[c])))
end
if values[a]==0 and values[A]~=0 and values[B]~=0 and values[C]~=0 then
values[a]=(math.cos^-1)((values[B]^2+values[C]^2-values[A]^2)/(2*values[B]*values[C]))
elseif values[b]==0 and values[A]~=0 and values[B]~=0 and values[C]~=0 then
values[b]=(math.cos^-1)((values[A]^2+values[C]^2-values[B]^2)/(2*values[A]*values[C]))
elseif values[c]==0 and values[A]~=0 and values[B]~=0 and values[C]~=0 then
values[c]=(math.cos^-1)((values[A]^2+values[B]^2-values[C]^2)/(2*values[A]*values[B]))
end
if values[Perimeter]==0 and values[A]~=0 and values[B]~=0 and values[C]~= 0 then
values[Perimeter]=values[A]+values[B]+values[C]
end
if values[Area]==0 and values[A]~=0 and values[B]~=0 and values[C]~=0 then
s=((values[A]+values[B]+values[C])/2)
values[Area]=math.sqrt(s*(s-values[A])(s-values[B])(s-values[C]))
end

platform.window:invalidate()
end

1 个答案:

答案 0 :(得分:0)

你似乎在使用余弦定律。在这种情况下,您需要math.acos而不是math.cos^-1