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:
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.
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.