I created this program to rotate a square on screen, but I haven't used Lua much, so I need some help optimizing the code:


Code:

local drawLine = zmg.drawLine
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local color = {makeColor("black"),makeColor("white")}

local LCD_SCREEN_WIDTH = 384
local LCD_SCREEN_HEIGHT = 216

local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27}
local points = { p1=0, p2=0, p3=0, p4=0, p5=0, p6=0, p7=0, p8=0 }
local turnval = { tv1=0, tv2=0, tv3=(math.pi/2), tv4=(math.pi/2), tv5=math.pi, tv6=math.pi, tv7=(math.pi+(math.pi/2)), tv8=(math.pi+(math.pi/2))}
local circle1 = { radiusx=40, radiusy=40, centerx=192, centery=108 }
local degrees = 0

   while keyMenuFast() ~= key.Exit do

   drawRectFill(0,0,LCD_SCREEN_WIDTH,LCD_SCREEN_HEIGHT,color[2])

   drawLine(points.p1,points.p2,points.p3,points.p4,color[1])
   drawLine(points.p3,points.p4,points.p5,points.p6,color[1])
   drawLine(points.p5,points.p6,points.p7,points.p8,color[1])
   drawLine(points.p7,points.p8,points.p1,points.p2,color[1])
      
   -- x values
   points.p1 = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval.tv1))
   points.p3 = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval.tv3))
   points.p5 = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval.tv5))
   points.p7 = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval.tv7))

   -- y values
   points.p2 = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval.tv2))
   points.p4 = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval.tv4))
   points.p6 = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval.tv6))
   points.p8 = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval.tv8))

   degrees = degrees + .1

   fastCopy()
end


I cant figure out how to use For Loops to generate the x and y values instead of creating all of them individually. Any other optimization tips would also be great.
I figured out how to get the For Loops working; I thought I was using an array instead of a list for some reason. Optimization tips for this would be great though.


Code:

local drawLine = zmg.drawLine
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local color = {makeColor("black"),makeColor("white")}

local LCD_SCREEN_WIDTH = 384
local LCD_SCREEN_HEIGHT = 216

local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27}
local points = { 0, 0, 0, 0, 0, 0, 0, 0 }
local turnval = { 0, 0, (math.pi/2), (math.pi/2), math.pi, math.pi, (math.pi+(math.pi/2)), (math.pi+(math.pi/2))}
local circle1 = { radiusx=40, radiusy=40, centerx=192, centery=108 }
local degrees = 0

while keyMenuFast() ~= key.Exit do
   
   -- draw everything
   drawRectFill(0,0,LCD_SCREEN_WIDTH,LCD_SCREEN_HEIGHT,color[2])
   drawLine(points[7],points[8],points[1],points[2],color[1])
   for i=1,5,2 do
   drawLine(points[i],points[i+1],points[i+2],points[i+3],color[1])
   end
      
   -- generate x values
   for i=1,7,2 do
   points[i] = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval[i]))
   end

   -- generate y values
   for i=2,8,2 do
   points[i] = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval[i]))
   end

   -- check keys
   if keyMenuFast() == key.Left then degrees = degrees + .1
      elseif keyMenuFast() == key.Right then degrees = degrees - .1
   end
   
   fastCopy()
end
Nice, that would have been the solution I was going to suggest. Smile As far as optimizations, you have actually written pretty tight code there. Oh, I see one. Convert:


Code:
   -- generate x values
   for i=1,7,2 do
   points[i] = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval[i]))
   end

   -- generate y values
   for i=2,8,2 do
   points[i] = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval[i]))
   end
to
Code:
   -- generate x&y values
   for i=1,7,2 do
   points[i] = circle1.centerx + (circle1.radiusx *  math.cos( degrees + turnval[i]))
   points[i+1] = circle1.centery + (circle1.radiusy * math.sin( degrees + turnval[i+1]))
   end
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement