Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: cypressx1 on July 22, 2011, 07:11:59 am

Title: How can l make it ?
Post by: cypressx1 on July 22, 2011, 07:11:59 am
Code:(lua)
Code: [Select]
----------block1-------------
block1=class()
bw,bh=80,70
cc=20 ff=25     t1="start" t2="stop"
tp=0.02
st=1
n=1/2.5

-----st must =ff or ff≥st+1------
function block1:init()
end
function block1:set()
wbx,wby=platform.window:width(),platform.window:height()
bx=wbx/2-bw/2
by=wby/2-bh/2
end

function block1:setc1()
block1:set()
c1x,c1y,c1w,c1h=bx,by+bh/2-cc/2,bw,cc
end
function block1:setc2()
block1:set()
c2x,c2y,c2w,c2h=bx+bw/2-cc/2,by,cc,bh
end

function block1:paint(gc)
gc:setColorRGB(150,200,100)
block1:set()
gc:fillRect(bx,by,bw,bh)
-------c1------
gc:setColorRGB(0,0,0)
block1:setc1()
gc:fillRect(c1x,c1y,c1w,c1h)
-----c2-------
gc:setColorRGB(0,0,0)
block1:setc2()
gc:fillRect(c2x,c2y,c2w,c2h)
end
function block1:timer()
platform.window:invalidate()
end
--------mouse------
Mouse=class()
mx=0
my=0
function Mouse:init()
end
function Mouse:paint(gc)
gc:setFont("sansserif","i",10)
gc:drawString("MouseLocation : ( "..mx.." , "..my.." )",60,0,"top")
end
function on.mouseMove(x,y)
mx=x
my=y
end
function Mouse:send()
--Ox=mx
end
function Mouse:timer()
platform.window:invalidate()
end
--------------------------
--[[
function ST()
if B1x<140 or B1x>171
then st=1
elseif B1x>140 and B1x<171
then st=1
end end
--]]
-----------Steps----------
function step1()
  if B1x==c2x-ff  --and B2y==c1y
   then t1="start"
end
if B1x==c2x+ff
   then t1="stop"
return step2()
   end
--ST()
B1x=B1x+st
 B2y=B1y-(ff^2-(B1x-B2x)^2)^0.5
 --B2y=math.eval("round("..B2y..",0)")
end
 
function step2()
  if B1x==c2x+ff --and B2y==c1y
   then t2="start"
end
if B1x==c2x-ff
   then t2="stop" B1x=c2x-ff
--timer.stop()
return step1() 
 end
--ST()
B1x=B1x-st
 B2y=B1y+(ff^2-(B1x-B2x)^2)^0.5
--B2y=math.eval("round("..B2y..",0)")
end






B1=class()
block1:setc1()
 block1:setc2()
B1x,B1y,B2x,B2y=c2x-ff,c1y,c2x,c1y
function B1:paint(gc)
step1()
gc:fillArc(B1x,B1y,cc,cc,0,360)
gc:fillArc(B2x,B2y,cc,cc,0,360)
end
B2=class()
block1:setc1()
 block1:setc2()
function B2:paint(gc)
step2()
gc:fillArc(B1x,B1y,cc,cc,0,360)
gc:fillArc(B2x,B2y,cc,cc,0,360)
end



-----------------------
function on.paint(gc)
block1():paint(gc)
Mouse():paint(gc)
gc:setColorRGB(255,255,255)
if t1=="start" then
B1():paint(gc)
end
if t2=="start" then
B2():paint(gc)
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","i",10)
gc:drawString(t1.." "..t2,0,0,"top")
gc:drawString("( "..B1x.." , "..B1y.." )",100,199)
gc:drawString(" ( "..B2x.." , "..B2y.." )",100,212)
d=((B1x-B2x)^2+(B1y-B2y)^2)^0.5
d=math.eval("iPart("..d..")")
gc:drawString(d..st,100,180)
-------o-------
--Mouse:send()
--l=math.eval("linsolve("..B1y.."=k*"..B1x.."+b and "..B2y.." =k*"..B2x.."+b,{k,b})")
Ox=(B2x*(1+n)-B1x)/n
Oy=(B2y*(1+n)-B1y)/n
gc:fillArc(Ox,Oy,cc,cc,0,360)
end


function on.timer()
timer.start(tp)
block1():timer()
Mouse():timer()
platform.window:invalidate()
end
on.timer()

l'm leaning the lua. l want to make a prgm to draw the oval. But l don't know how to draw the ball's locus on the screen.in addition, l also want to make the vertical ball move more smoothly? Please give me some ideas :)
Title: Re: How can l make it ?
Post by: cypressx1 on July 23, 2011, 08:37:19 am
No one ?
Title: Re: How can l make it ?
Post by: calcdude84se on July 23, 2011, 11:35:39 am
It's only been one day since your post. Give everyone a bit of time ;)
I myself do not know Lua, especially not w/r/t the Nspire, otherwise I would help :/
Title: Re: How can l make it ?
Post by: Jim Bauwens on July 23, 2011, 02:07:17 pm
Hi, I'm still looking at your code, because its so big and I can't test it right now.
But here already a suggestio:
Use Lua native functions instead of calling them through math.eval() (if they are available). This will improve the speed.

I'm still looking at your code, and will post an update as soon as I test it :)
Title: Re: How can l make it ?
Post by: Munchor on July 24, 2011, 09:24:26 am
Quote
l'm leaning the lua. l want to make a prgm to draw the oval.

I may be wrong, but can't that be done with ellipse?
Title: Re: How can l make it ?
Post by: cypressx1 on July 26, 2011, 08:08:52 am
ok,thanks! by the way, l have another question, can we use two different timer.start() at one time? for example,  l start the  timer with timer.start(XX)  "XX will change", and l also want the clock function which l programmed work well ?
Title: Re: How can l make it ?
Post by: Jim Bauwens on July 26, 2011, 03:30:21 pm
timer.start will change the time (invalidate the other one) between the ticks. The best would be to make it go faster and check yourself if its time to do something.