第一次写lua

发布时间 2023-07-01 11:06:58作者: omori_Sakuya
成果

function disk(tx,ty,r) --圆的特征函数
return function(x,y)
return (x-tx)^2 + (y-tx)^2 <= r^2
end
end

function different(a ,b) --差集
return function(x,y)
return a(x,y) and (not b(x,y))
end
end

function translate(old,tx,ty) --平移
return function(x,y)
return old(x-tx,y-ty)
end
end

--

function plot(r,m,n)
io.write("P\n",m," ",n,"\n")
for i =1,n do
local y = (n-i*2)/n
for j=1,m do
local x = (m-j*2)/m
io.write( r(x,y) and "1" or "0")
end
io.write("\n")
end
end

c1 = disk(0,0,1)
plot(different(c1,translate(c1,0.1,0)),1000,1000)

 

过程中出现的bug1

/usr/local/lua-5.3.5/luac53: script.lua:3: 'end' expected (to close 'function' at line 2) near '='

return (x-tx)^2 + (y-tx)^2 <= r^2  正确
return (x-tx)^2 + (y-tx)^2 = r^2 错误


过程中出现的bug2

function disk(tx,ty,r) --圆的特征函数
return function(x,y)
return (x-tx)^2 + (y-yx)^2 <= r^2  --------- 拼写错误-------错误1
end
end

print(disk(5,1,5)(5,4))t  --------莫名奇妙的 t ------错误2


错误信息   

错误1

/usr/local/lua-5.3.5/lua53: script.lua:3: attempt to perform arithmetic on a nil value (global 'yx')
stack traceback:
script.lua:3: in function <script.lua:2>
script.lua:7: in main chunk
[C]: in ?

Exited with error status 1

错误2

/usr/local/lua-5.3.5/luac53: script.lua:7: syntax error near <eof>