Compass Learning Technologies
ABN: 57 953 712 517
Home

Home TI-Nspire Authoring Lua Scripting HQ Lua and JavaScript TI-Nspire Lua Javascript Examples

Home TI-Nspire Authoring Lua Scripting HQ Lua and JavaScript TI-Nspire Lua Javascript Examples

	
	platform.apilevel = '1.0' 

-- Copyright (C) 2013, Texas Instruments Incorporated 
-- All rights reserved 
-- Steve Arnold 2011-08-12 

local screen = platform.window 
local w = screen:width() 
local h = screen:height() 
local testmode = 0 

local xstep 
local ystep 
local fontSize 
local size 
local slider 
local reset_button 
local properties 
local props 
local tolerance 
local str 
local str0 = "" 
local sw 
local par1 
local par2 
local perp1 
local perp2 
local perp3 
local parnum 
local perpnum 
local diag 
local sides1 
local sides2 
local sides3 
local sides4 
local sides5 
local name = "?" 
local choose 
local quad 
local kite 
local square 
local parallelogram 
local trapezium 
local rectangle 
local rhombus 
local answer 
local dropX 
local dropY 
local dropWidth 
local dropHeight 
local types 
local correct = 0 
local score = 0 
local total = 0 
local record = {} 


local quadlist = {"Kite", "Trapezoid", "Parallelogram", "Rectangle", "Square", "Rhombus"} 
local placeList = {"Random", "Kite", "Trapezoid", "Parallelogram", "Rectangle", "Square", "Rhombus"} 

local Objects 
local actions = {"Kite", "Trapezoid", "Parallelogram", "Rectangle", "Square", "Rhombus", "Line of Symmetry", "Bisect Diagonals"} 
local grids = {"Dotted", "Lines", "None"} 
local gridscaleList = {1, 2, 3, 4, 5} 

local gridObjects 
local actionObjects 
local gridScaleObjects 
local placeObjects 
local qtype = "None" 
local unit = 1 
local space = math.floor(w/(unit*20)) 
local gridtype = "dot" 
local feedback = 1 
local placement = "Random" 
local questions = 1 
local amove = var.recall("amove") or 1 
local bmove = var.recall("bmove") or 1 
local cmove = var.recall("cmove") or 1 
local dmove = var.recall("dmove") or 1 
local pageCount = 1 
local str9 
local str10 
--local tabObjects 
local tabNumber = 0 
local current = 0 

 color = { 
  red         = {255, 0, 0}, 
  orange      = {255, 165, 0}, 
  yellow      = {255, 255, 0}, 
  green       = {0, 255, 0}, 
  blue        = {0, 0, 255}, 
  white       = {255, 255, 255}, 
  black       = {0, 0, 0}, 
  paleblue    = {0, 175, 235}, 
  navy        = {20, 20, 138}, 
        maroon      = {170, 50, 50}, 
        gray        = {120, 120, 120}, 
        lightgray   = {240, 240, 240}, 
        bluesteel   = {0, 175, 235}, 
        salmon1      = {235, 230, 230}, 
        salmon = {220, 220, 250}, 
        niceblue = {0, 175, 235} 
 } 



timer.start(0.4) 

function on.timer() 
    cursor.show() 

    screen:invalidate() 
end 




function grid(unit, form, gc) 

    w = screen:width() 
    h = screen:height() 

    if unit < 10 then 
    gc:setPen("thin", "smooth") 
    gc:setColorRGB(200, 200, 200) 

    if form == "dot" then    
    for k = 1, math.floor(w/space + 0.5) + 1  do 
    for m = 1, math.floor(h/space + 0.5) do 
        gc:drawArc((k - 1)*space, (m - 1)*space, 0.0025*w, 0.0025*w, 0, 360) 
     --   Circle((k - 1)*space, (m - 1)*space, 0.0025*w, 0.0025*w, {200, 200, 200}, ""):paint(gc) 
    end 
    end 
    else 
    gc:setPen("thin", "dotted") 
    gc:setColorRGB(200, 200, 200) 
    for k = 1, math.floor(w/space + 0.5) + 1  do 
    for m = 1, math.floor(h/space + 0.5) do 
        gc:drawLine(0.4*w, (m-1)*space, w, (m-1)*space) 
        gc:drawLine((k-1)*space, 0, (k-1)*space, h) 
    end 
    end 
    end 
    end 

end 


------------------------- 
-- The DropDown menu 

dropDown = class() 

function dropDown:init(x, y, width, height, color, label, contents, selected) 
    self.x = x 
    self.y = y 
    self.width = width 
    self.height= height 
    self.color = color 
    self.label = label 
    self.contents = contents 
    self.selected = false 

end 

  
function dropDown:contains(x, y) 

    local sw = self.width 
    local sh = self.height 
    return x >= self.x and x <= self.x + sw and 
        y >= self.y and y <= self.y + sh 
end 


function dropDown:paint(gc) 

    local x = self.x 
    local y = self.y 
    local width = self.width 
    local height = self.height 
    local label = self.label 
    local contents = self.contents 


 dropButton1 = Button(x, y, width, height, color.niceblue, label, false) 
 dropButton1:paint(gc) 
    

    local fontSize = math.floor(height/5) 
     local fontSize = fontSize >= 7 and fontSize or 7 
     local fontSize = fontSize <= 24 and fontSize or 24 
     gc:setFont("sansserif","b",fontSize) 
 
 gc:setColorRGB(unpack(color.black)) 
 gc:setPen("medium", "smooth")   
 if self.selected == true then 
     gc:drawRect(x + 0.09*width, y + height, 0.8*width, (#contents)*h/10) 

 end 
 
 gc:setColorRGB(unpack(color.white)) 
 local sw = gc:getStringWidth(label) 
    local sh = gc:getStringHeight(label) 
 --   gc:drawString(label, 1.1*x, y + height/2, "middle") 
    
    local vertices = {x + 0.875*width, y + 0.35*height, 
                        x + 0.925*width, y + 0.35*height, 
                        x + 0.9*width, y + 0.65*height, 
                        x + 0.875*width, y + 0.35*height} 
    gc:setPen("medium", "smooth") 
    gc:fillPolygon(vertices) 

end 




 --------------------------------------------------------------------------- 

-- The Button 


Button = class() 

function round(input) 
    return math.floor(input + 0.5) 
end 


function Button:init(x, y, width, height, color, label, selected) 
    self.x = x 
    self.y = y 
    self.width = width 
    self.height= height 
    self.color = color 
    self.label = label 
    self.selected = false 
end 


function Button:contains(x, y) 

local sw = self.width or self.height 
local sh = self.height or self.width 
return x >= self.x and x <= self.x + sw and 
       y >= self.y and y <= self.y + sh 
end 



function Button:paint(gc) 

    local x = self.x 
    local y = self.y 
    local width = self.width 
    local height = self.height 
    local label = self.label 
    
    
    gc:setColorRGB(unpack(self.color))    
    gc:setPen("thin", "smooth") 
  
     local curve = 0.25*height 
       
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    
    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 

        gc:setColorRGB(unpack(color.black)) 
    gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y)) 
    gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height)) 
    gc:drawLine(round(x + width - curve), round(y + height), round(x + curve), round(y + height)) 
    gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve)) 

    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 


    local fontSize = math.floor(height/5) 
    local fontSize = fontSize 
     local fontSize = fontSize >= 7 and fontSize or 7 
     local fontSize = fontSize <= 24 and fontSize or 24 
 gc:setFont("sansserif","b",fontSize) 
 
 if self.selected then 
 gc:setColorRGB(unpack(color.red)) 
 
 else 
 gc:setColorRGB(unpack(color.white)) 
 end 
 
 local sw = gc:getStringWidth(label) 
    local sh = gc:getStringHeight(label) 
    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle") 
 
end 




 --------------------------------------------------------------------------- 


checkBox = class() 

function checkBox:init(x, y, width, label, selected) 

    self.x  = x 
    self.y  = y 
    self.color = color.white 
    self.selected = selected 
    self.label = label 
    self.width = width 
    self.height = width 
    self.textcolor = {50, 50, 50} 

end 

  
  
function checkBox:contains(x, y) 

    local sw = self.width 
    local sh = self.height 
    return x >= self.x and x <= self.x + sw and 
        y >= self.y and y <= self.y + sh 

end 

  
  
function checkBox:paint(gc) 
    
    gc:setPen("thin","dotted") 
    gc:setColorRGB(unpack(self.color)) 
    gc:fillRect(self.x, self.y, self.width, self.height) 
        gc:setColorRGB(150, 150, 150) 
        gc:drawRect(self.x, self.y, self.width, self.height) 

    gc:setColorRGB(unpack(self.textcolor))    

    fontSize = math.floor(w/70+0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 
        local str = self.label or "P" 
        local sw = gc:getStringWidth(str) 
        local sh = gc:getStringHeight(str) 
        gc:drawString(str, 1.075*self.x, self.y + sh/2, "middle") 

    if self.selected then 
    gc:setColorRGB(unpack(color.niceblue)) 
    gc:fillRect(self.x, self.y, self.width, self.height) 
    
  
    gc:setColorRGB(unpack(color.niceblue)) 
    gc:drawString(str, 1.075*self.x, self.y + sh/2, "middle") 

        gc:setPen("medium","smooth") 
        gc:setColorRGB(0, 0, 0) 
        gc:drawRect(self.x, self.y, self.width, self.height) 
    end 
end 



 --------------------------------------------------------------------------- 


Rectangle = class() 

function Rectangle:init(x, y, width, height, label) 

    self.x  = x 
    self.y  = y 
    self.width = width or 20 
    self.height = height or 20 
    self.color = {255, 255, 255} 
    self.selected = false 
    self.label = label 
    self.textcolor = {50, 50, 50} 

end 

  
  
function Rectangle:contains(x, y) 

    local sw = self.width 
    local sh = self.height 
    return x >= self.x and x <= self.x + sw and 
        y >= self.y and y <= self.y + sh 

end 

  
  
function Rectangle:paint(gc) 
    
    gc:setPen("thin","smooth") 
    gc:setColorRGB(unpack(self.color)) 
    gc:fillRect(self.x, self.y, self.width, self.height) 
        gc:setColorRGB(250, 250, 250) 
        gc:drawRect(self.x, self.y, self.width, self.height) 

    gc:setColorRGB(unpack(self.textcolor))    

    fontSize = math.floor(w/70+0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 
        local str = self.label or "P" 
        local sw = gc:getStringWidth(str) 
        local sh = gc:getStringHeight(str) 
        gc:drawString(str, self.x + 0.1*self.width, self.y + sh, "middle") 

    if self.selected then 
    gc:setColorRGB(240, 240, 250) 
    gc:fillRect(self.x, self.y, self.width, self.height) 
        gc:setColorRGB(unpack(self.textcolor))    
    gc:setColorRGB(unpack(color.red)) 
    fontSize = math.floor(w/70+0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 
        local str = self.label 
        local sw = gc:getStringWidth(str) 
        local sh = gc:getStringHeight(str) 
        gc:drawString(str, self.x + 0.1*self.width, self.y + sh, "middle") 

        gc:setPen("thin","smooth") 
        gc:setColorRGB(0, 0, 0) 
        gc:drawRect(self.x, self.y, self.width, self.height) 
    end 
end 



 --------------------------------------------------------------------------- 

Circle = class() 

function Circle:init(x, y, width, height, color, label) 

    self.x = x 
    self.y = y 
    self.width = width * 2 
    self.height = height * 2 
    self.radius = height 
    self.color = color 
    self.selected = false 
    self.label = label 

end 

function Circle:contains(x, y) 

    local r = self.radius 
    local d = math.sqrt((self.x - x)^2 + (self.y - y)^2) 
    return d <= 4*r 

end 

function Circle:paint(gc) 

    local cx = self.x - self.radius 
    local cy = self.y - self.radius 
    local diameter = 2*self.radius 
    local label = self.label 

    gc:setPen("thin","smooth") 
    gc:setColorRGB(unpack(self.color)) 
    gc:fillArc(cx, cy, diameter, diameter, 0, 360) 
    gc:setColorRGB(unpack(color.black)) 
    gc:drawArc(cx, cy, diameter, diameter, 0, 360) 

    fontSize = math.floor(w/70+0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 
        local str = self.label or "P" 
        local sw = gc:getStringWidth(str) 
        local sh = gc:getStringHeight(str) 
        if str == "A" or str == "D" then 
        gc:drawString(str, self.x - sw, self.y - sh, "middle") 
        else 
        gc:drawString(str, self.x + sw/2, self.y - sh, "middle") 
        end 

    if self.selected then
        gc:setPen("medium","smooth")
    gc:setColorRGB(unpack(self.color))
    gc:fillArc(cx - diameter/4, cy - diameter/4, 2*diameter, 2*diameter, 0, 360)
        gc:setColorRGB(0, 0, 0)
        gc:drawArc(cx - diameter/4, cy - diameter/4, 2*diameter, 2*diameter, 0, 360)
    end
end 


------------------------- 

function test() 
    
    name = "?" 
    
    if parallel(A, B, C, D) ~= 0 then 
    par1 = 1 
    else 
    par1 = 0 
    end 

    if parallel(A, D, B, C) ~= 0 then 
    par2 = 1 
    else 
    par2 = 0 
    end 

    if perpendicular(A, B, C, B) ~= 0 then 
    perp1 = 1 
    else 
    perp1 = 0 
    end 

    if perpendicular(A, D, A, B) ~= 0 then 
    perp2 = 1 
    else 
    perp2 = 0 
    end 

    if perpendicular(C, B, C, D) ~= 0 then 
    perp3 = 1 
    else 
    perp3 = 0 
    end 

    if perpendicular(A, D, C, D) ~= 0 then 
    perp4 = 1 
    else 
    perp4 = 0 
    end 

   if perpendicular(A, C, B, D) ~= 0 then 
    diag = 1 
    else 
    diag = 0 
    end 
    
    if sides(A, B, C, D) ~= 0 then 
        sides1 = 1 
    else 
        sides1 = 0 
    end 

    if sides(A, D, B, C) ~= 0 then 
        sides2 = 1 
    else 
        sides2 = 0 
    end 

    if sides(A, B, A, D) ~= 0 then 
        sides3 = 1 
    else 
        sides3 = 0 
    end 

   if sides(B, C, C, D) ~= 0 then 
        sides4 = 1 
    else 
        sides4 = 0 
    end 

   if sides(A, B, B, C) ~= 0 then 
        sides6 = 1 
    else 
        sides6 = 0 
    end 

   if sides(A, D, D, C) ~= 0 then 
        sides7 = 1 
    else 
        sides7 = 0 
    end 

   if sides(A, C, B, D) ~= 0 then 
        sides5 = 1 
    else 
        sides5 = 0 
    end 


    parnum = par1 + par2 
    perpnum = perp1 + perp2 + perp3 + perp4 
    sidesopp = sides1 + sides2 
    sidesadj = sides3 + sides4 + sides6 + sides7 

end 


function parallel(p1, p2, p3, p4) 

    if math.abs(p2.x - p1.x) < tolerance and math.abs(p4.x - p3.x) < tolerance then 
            return 1 

    elseif math.abs(p2.x - p1.x) > 0 and math.abs(p4.x - p3.x) > 0 then 
        slope1 = (p2.y - p1.y)/(p2.x - p1.x) 
        slope2 = (p4.y - p3.y)/(p4.x - p3.x) 
        
        if math.abs(slope1 - slope2) < tolerance then 
            return 1 
        else 
            return 0 
        end 
    else 
        return 0 
    end 
end 

function perpendicular(p1, p2, p3, p4) 

    if math.abs(p2.x - p1.x) > tolerance and math.abs(p4.x - p3.x) > tolerance then 
        local slope1 = (p2.y - p1.y)/(p2.x - p1.x) 
        local slope2 = (p4.y - p3.y)/(p4.x - p3.x) 
        
        if math.abs(slope1*slope2 + 1) < tolerance then 
            return 1 
        else 
            return 0 
        end 
    else 
        if math.abs(p2.x - p1.x) < tolerance and math.abs(p4.y - p3.y) < tolerance then 
            return 1 
        elseif math.abs(p4.x - p3.x) < tolerance and math.abs(p2.y - p1.y) < tolerance then 
            return 1 
        else 
            return 0 
        end 
    end 
end 

function sides(p1, p2, p3, p4) 

    local dist1 = math.sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2) 
    local dist2 = math.sqrt((p3.x - p4.x)^2 + (p3.y - p4.y)^2) 
    
    if math.abs(dist1 - dist2) < tolerance then 
    return 1 
    else 
    return 0 
    end 

end 

------------------------- 


function on.resize() 
    w = screen:width() 
    h = screen:height() 

    gridObjects = {} 
    actionObjects = {} 
    gridScaleObjects = {} 
    placeObjects = {} 

    dropX = 0.01*w 
    dropY = 0.055*h 
    dropWidth = 0.3825*w 
    dropHeight = 0.075*h 

    size = 0.01*w 
    tolerance = 0.025 

    Objects = {} 
    properties = {} 
    props = 0 
    parnum = 0 
    perpnum = 0 
    diag = 0 
    par1 = 0 
    par2 = 0 
    perp1 = 0 
    perp2 = 0 
    perp3 =0 
    perp4 =0 
    diag = 0 
    sides1 = 0 
    sides2 = 0 
    sides3 = 0 
    sides4 = 0 
    sides5 = 0 
    
    kite = 0 
    square = 0 
    parallelogram = 0 
    trapezium = 0 
    rectangle = 0 
    rhombus = 0 
    answer = "?" 
    str0 = "" 

    types = 0 
    qtype = "None" 
    unit = 1 
    space = math.floor(w/(unit*20)) 
    gridtype = "dot" 
    feedback = 1 
    placement = placeList[math.random(1, 7)] 
    questions = 5 
    amove = 1 
    bmove = 1 
    cmove = 1 
    dmove = 1 
    testmode = 0    
    
    if placement == "Random" then 
        placement = placeList[math.random(1, 7)] 
    end 

    str01 = "How many steps are" 
    str02 = "needed to form a " 
    str9 = pageCount..") This is a " 
    if answer == name then 
    str10 = answer 
    end 
    
     space = math.floor(w/(unit*20)) 

     ax = space*math.floor((50+math.random(10, 25))*w/(space*100) + 0.5) 
     bx = space*math.floor((50+math.random(25, 40))*w/(space*100) + 0.5) 
     cx = space*math.floor((50+math.random(25, 40))*w/(space*100) + 0.5) 
     dx = space*math.floor((50+math.random(10, 25))*w/(space*100) + 0.5) 

     ay = space*math.floor(math.random(10, 40)*h/(space*100) + 0.5) 
     by = space*math.floor(math.random(10, 40)*h/(space*100) + 0.5) 
     cy = space*math.floor(math.random(40, 80)*h/(space*100) + 0.5) 
     dy = space*math.floor(math.random(40, 80)*h/(space*100) + 0.5) 

    local scale = math.random(5, 30)/10 
    local scale2 = math.random(20, 30)/10 

    if placement == "Square" then 
    -- Square 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle(bx, by, size, size, {250, 50, 50}, "B") 
    C = Circle(bx + ay - by, by + bx - ax, size, size, {250, 50, 50}, "C") 
    D = Circle(ax + ay - by, ay + bx - ax, size, size, {250, 50, 50}, "D") 
    elseif placement == "Rectangle" then 
    -- Rectangle 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle(bx, by, size, size, {250, 50, 50}, "B") 
    C = Circle(bx + scale*(ay - by), by + scale*(bx - ax), size, size, {250, 50, 50}, "C") 
    D = Circle(ax + scale*(ay - by), ay + scale*(bx - ax), size, size, {250, 50, 50}, "D") 
    elseif placement == "Parallelogram" then 
    -- Parallelogram 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle(bx, by, size, size, {250, 50, 50}, "B") 
    C = Circle(cx, cy, size, size, {250, 50, 50}, "C") 
    D = Circle(ax + (cx - bx), ay + (cy - by), size, size, {250, 50, 50}, "D") 
    elseif placement == "Trapezoid" then 
    -- Trapezoid 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle(bx, by, size, size, {250, 50, 50}, "B") 
    C = Circle(cx, cy, size, size, {250, 50, 50}, "C") 
    D = Circle(ax + scale2*(cx - bx), ay + scale2*(cy - by), size, size, {250, 50, 50}, "D") 
    elseif placement == "Kite" then 
    -- Kite 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle((ax + cx)/2, ay - math.abs(math.ceil(by/2)), size, size, {250, 50, 50}, "B") 
    C = Circle(cx, ay, size, size, {250, 50, 50}, "C") 
    D = Circle((ax + cx)/2, ay + scale2*math.abs(math.ceil(by/2)), size, size, {250, 50, 50}, "D") 
    elseif placement == "Rhombus" then 
    -- Rhombus 
    A = Circle(ax, ay, size, size, {250, 50, 50}, "A") 
    B = Circle(bx, by, size, size, {250, 50, 50}, "B") 
    C = Circle(bx + ay - by, by + bx - ax, size, size, {250, 50, 50}, "C") 
    D = Circle(ax + ay - by, ay + bx - ax, size, size, {250, 50, 50}, "D") 
    else 
    -- Random 
    A = Circle(ax, ay, size, size, {150, 150, 150}, "A") 
    B = Circle(bx, by, size, size, {150, 150, 150}, "B") 
    C = Circle(cx, cy, size, size, {150, 150, 150}, "C") 
    D = Circle(dx, dy, size, size, {150, 150, 150}, "D") 
    end 
    
    if amove == 1 then 
        checkA = checkBox(0.7*w, 0.85*h, 0.05*h, "A", true) 
        A.color = color.niceblue 
    else 
        checkA = checkBox(0.7*w, 0.85*h, 0.05*h, "A", false) 
        A.color = {200, 200, 200} 
    end 
    if bmove == 1 then 
        checkB = checkBox(0.85*w, 0.85*h, 0.05*h, "B", true) 
        B.color = color.niceblue 
    else 
        checkB = checkBox(0.85*w, 0.85*h, 0.05*h, "B", false) 
        B.color = {200, 200, 200} 
    end 
    if cmove == 1 then 
        checkC = checkBox(0.7*w, 0.925*h, 0.05*h, "C", true) 
        C.color = color.niceblue 
    else 
        checkC = checkBox(0.7*w, 0.925*h, 0.05*h, "C", false) 
        C.color = {200, 200, 200} 
    end 
    if dmove == 1 then 
        checkD = checkBox(0.85*w, 0.925*h, 0.05*h, "D", true) 
        D.color = color.niceblue 
    else 
        checkD = checkBox(0.85*w, 0.925*h, 0.05*h, "D", false) 
        D.color = {200, 200, 200} 
    end 
    
    if feedback == 1 and testmode == 0 then 
        feedB = checkBox(0.7*w, 0.7*h, 0.05*h, "FeedBack?", true) 
    else 
        feedB = checkBox(0.7*w, 0.7*h, 0.05*h, "FeedBack?", false) 
    end 
    
    gridMenu = dropDown(dropX, dropY + 0.2*h, dropWidth, dropHeight, {0, 0, 0}, "Grid: "..gridtype, grids, false) 
    
 for k = 1, #grids do 
     gridObjects[k] = Rectangle(1.035*dropX, dropY + 0.2*h + dropHeight + (k - 1)*h/10, 0.795*dropWidth, h/10, grids[k]) 
 end 

    gridScale = dropDown(dropX, dropY + 0.3*h, dropWidth, dropHeight, {0, 0, 0}, "Grid Scale: "..unit, gridscaleList, false) 
    
 for k = 1, #gridscaleList do 
     gridScaleObjects[k] = Rectangle(1.035*dropX, dropY + 0.3*h + dropHeight + (k - 1)*h/10, 0.795*dropWidth, h/10, gridscaleList[k]) 
 end 

    
    actionMenu = dropDown(dropX, dropY, dropWidth, dropHeight, {0, 0, 0}, "Question Type:", actions, false) 
    
 for k = 1, #actions do 
     actionObjects[k] = Rectangle(1.035*dropX, dropY + dropHeight + (k - 1)*h/10, 0.795*dropWidth, h/10, actions[k]) 
 end 

    placeMenu = dropDown(dropX, dropY + 0.1*h, dropWidth, dropHeight, {0, 0, 0}, "Initial: "..placement, placeList, false) 
    
 for k = 1, #placeList do 
     placeObjects[k] = Rectangle(1.035*dropX, dropY + 0.1*h + dropHeight + (k - 1)*h/10, 0.795*dropWidth, h/10, placeList[k]) 
 end 

    dropDownMenu = dropDown(dropX, dropY, dropWidth, dropHeight, color.blue, "Choose...", quadlist, false) 
    
 for k = 1, #quadlist do 
     Objects[k] = Rectangle(dropX + 0.1*dropWidth, dropY + dropHeight + (k - 1)*h/10, 0.775*dropWidth, h/10, quadlist[k]) 
 end 

    reset_button = Button(0.4*w - 0.21*w, 0.9*h, w/5, 0.075*h, color.green, "Next ▶", false) 
    resetall_button = Button(w - 0.21*w, 0.9*h, w/5, 0.075*h, color.black, "Start Over", false) 


end 


function reset() 
    tabNumber = 0 
    qtype = "None" 
    unit = 1 
    space = math.floor(w/(unit*20)) 
    gridtype = "dot" 
    feedback = 1 
    placement = placeList[math.random(1, 7)] 
    questions = 5 
    amove = 1 
    bmove = 1 
    cmove = 1 
    dmove = 1 
    feedback = 1 
    testmode = 0 

    pageCount = 1 
     score = 0 
     total = 0 
     
     var.store("pagecount", 1) 
     var.store("score", 0) 
     var.store("total", 0) 
     
     record = {} 
    on.resize() 
    
end 


------------------------- 

function on.mouseDown(x,y) 

    if A:contains(x, y) then 
        if amove == 1 then 
            A.selected = true 
            var.store("amove", 1) 
        else 
            A.selected = false 
            var.store("amove", 0) 
        end 
    end 
    if B:contains(x, y) then 
        if bmove == 1  then 
            B.selected = true 
            var.store("bmove", 1) 
        else 
            B.selected = false 
            var.store("bmove", 0) 
        end 
    end 
    if C:contains(x, y) then 
        if cmove == 1  then 
            C.selected = true 
            var.store("cmove", 1) 
        else 
            C.selected = false 
            var.store("cmove", 0) 
        end 
    end 
    if D:contains(x, y) then 
        if dmove == 1  then 
            D.selected = true 
            var.store("dmove", 1) 
        else 
            D.selected = false 
            var.store("dmove", 0) 
        end 
    end 
    if resetall_button:contains(x, y) then 
        reset() 
        resetall_button.selected = true 
         
    end 
    if reset_button:contains(x, y) then 
     
                 if reset_button.color == color.black then 
                    reset() 
                else 

       
        if pageCount <= (questions) then 
            
            if pageCount == (questions) then 
                   reset_button.label = "end" 
                   reset_button.color = color.black 
                   reset_button.selected = false                  
            end 
           if correct > 0  then 
                score = score + 1 
            end 
                total = total + 1 
                table.insert(record, name..":"..answer) 

            if pageCount < (questions) then 
                pageCount = pageCount + 1 
                on.escapeKey() 
            end 
            if pageCount >= (questions) then 
                   reset_button.label = "end" 
                   pageCount = (questions) 
            end 
            reset_button.selected = false 
        end 
        end 
            
    elseif dropDownMenu:contains(x, y) then 
        
        if dropDownMenu.selected then 
            dropDownMenu.selected = false 
        else 
            dropDownMenu.selected = true 
            
        end 
    end 
              for k = 1, #Objects do 
                if Objects[k]:contains(x, y) == true then 
                    Objects[k].selected = true 
                else 
                    Objects[k].selected = false 
                end 
          end 
 
        var.store("score", score) 
        var.store("total", total) 
        var.store("record", record) 
        var.store("pagecount", pageCount) 

            screen:invalidate() 
end 

function on.mouseUp(x, y) 

    if A.selected == true then 
        if x >= 0.4*w and x <= w then 
            A.x = space*math.floor(x/space + 0.5) 
            A.y = space*math.floor(y/space + 0.5) 
            var.store("ax", A.x) 
            var.store("ay", A.y) 
        end 
     elseif B.selected == true then 
     if x >= 0.4*w and x <= w then 
            B.x = space*math.floor(x/space + 0.5) 
            B.y = space*math.floor(y/space + 0.5) 
            var.store("bx", B.x) 
            var.store("by", B.y) 
        end 
     elseif C.selected == true then 
     if x >= 0.4*w and x <= w then 
            C.x = space*math.floor(x/space + 0.5) 
            C.y = space*math.floor(y/space + 0.5) 
            var.store("cx", C.x) 
            var.store("cy", C.y) 
        end 
     elseif D.selected == true then 
     if x >= 0.4*w and x <= w then 
            D.x = space*math.floor(x/space + 0.5) 
            D.y = space*math.floor(y/space + 0.5) 
            var.store("dx", D.x) 
            var.store("dy", D.y) 
        end 
     end 


    A.selected = false 
    B.selected = false 
    C.selected = false 
    D.selected = false 
    
    test() 
   if parnum == 1 then 
       name = "Trapezoid" 
       trapezium = 1 
       types = types + 1 
       else 
       trapezium = 0 

   end 

   if diag == 1 and sides5 == 0 and sidesopp == 0 and sidesadj == 2 then 
       name = "Kite" 
       kite = 1 
       types = types + 1 
       else 
       kite = 0      
   end 

   if parnum == 2 and kite == 0  then 
       name = "Parallelogram" 
       parallelogram = 1 
       types = types + 1 
       else 
       parallelogram = 0 
   end 

   if parnum == 2 and sides5 > 0 then 
       name = "Rectangle" 
       rectangle = 1 
       types = types + 1 
   else 
       rectangle = 0 
   end 

    if sides5 > 0 and diag > 0 and sidesopp > 0 and sidesadj > 0 then 
        name = "Rhombus" 
        rhombus = 1 
        types = types + 1 
        else 
        rhombus = 0 
    end 

    if sides5 > 0 and diag > 0 and perpnum > 0 and sidesopp > 0 and sidesadj > 0 then 
        name = "Square" 
        square = 1 
        types = types + 1 
        else 
        square = 0 
    end 

--    qtype = var.recall("qtype") or "None" 
    
                  if qtype == "Kite" then 
                            name = "Kite" 
                             if kite == 1 then 
                             answer = "Kite" 
                                correct = 1 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                 else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                   -- end 
                    elseif qtype == "Trapezoid" then 
                            name = "Trapezoid" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if trapezium == 1 then 
                                correct = 1 
                                answer = "Trapezoid" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Parallelogram" then 
                            name = "Parallelogram" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if parallelogram == 1 then 
                                correct = 1 
                                answer = "Parallelogram" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end                    
                    elseif qtype == "Rectangle" then 
                            name = "Rectangle" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if rectangle == 1 then 
                                correct = 1 
                                answer = "Rectangle" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Square" then 
                            name = "Square" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if square == 1 then 
                                correct = 1 
                                answer = "Square" 
                                str9 = "Correct!" 
                                str0 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Rhombus" then 
                            name = "Rhombus" 
                            str0 = "How many steps to form a "..answer.."?" 
                             if rhombus == 1 then 
                                correct = 1 
                                answer = "Rhombus" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                   -- end 
                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                    end 


          for k = 1, #Objects do 
                if Objects[k]:contains(x, y) == true then 
                    Objects[k].selected = false 
                    Objects[k].color = color.white 
                    answer = quadlist[k] 

                    if answer == "Kite" and kite == 1 then 
                            name = "Kite" 
                            correct = 1 
                    end 
                    if answer == "Trapezoid" and trapezium == 1 then 
                            name = "Trapezoid" 
                            correct = 1 
                    end 
                    if answer == "Parallelogram" and parallelogram == 1 then 
                            name = "Parallelogram" 
                            correct = 1 
                    end                    
                    if answer == "Rectangle" and rectangle == 1 then 
                            name = "Rectangle" 
                            correct = 1 
                    end 
                    if answer == "Square" and square == 1 then 
                            name = "Square" 
                            correct = 1 
                    end 
                    if answer == "Rhombus" and rhombus == 1 then 
                            name = "Rhombus" 
                            correct = 1 
                    end 
                     dropDownMenu.selected = false 
                end 
            end 
            
        if reset_button.selected == true then 
                   reset_button.selected = false 
        end 

        if resetall_button.selected == true then 
                   reset() 
                   resetall_button.selected = false 
                   
        end 

     var.store("correct", correct) 

    screen:invalidate() 
end 

function on.mouseMove(x, y) 

    if A.selected == true then 
        if x >= 0.4*w and x <= w then 
            A.x = x 
            A.y = y 
        end 
     elseif B.selected == true then 
     if x >= 0.4*w and x <= w then 
            B.x = x 
            B.y = y 
        end 
     elseif C.selected == true then 
     if x >= 0.4*w and x <= w then 
            C.x = x 
            C.y = y 
        end 
     elseif D.selected == true then 
     if x >= 0.4*w and x <= w then 
            D.x = x 
            D.y = y 
        end 
     end 

          
          for k = 1, #Objects do 
                if Objects[k]:contains(x, y) == true then 
                    Objects[k].selected = true 
                else 
                    Objects[k].selected = false 
                end 
          end 

    screen:invalidate() 
end 

function on.escapeKey() 
    on.resize() 
    unit = 1 
    gridtype = "dot" 
    
    current = 0 
    screen:invalidate() 
end 



function on.tabKey() 

    tabObjects = {dropDownMenu, A, B, C, D} 
       
    tabNumber = (tabNumber) % (#tabObjects) 
    tabNumber = (tabNumber + 1) 
    
     if tabObjects[tabNumber].selected == false then 
     dropDownMenu.selected = false 
         A.selected = false 
         B.selected = false 
         C.selected = false 
         D.selected = false 
        tabObjects[tabNumber].selected = true 
    else 
        tabObjects[tabNumber].selected = false 
    end 
       screen:invalidate() 

end 


function on.backtabKey() 

    tabObjects = {dropDownMenu, A, B, C, D} 
       
    tabNumber = (tabNumber) % (#tabObjects) 
    if tabNumber > 1 then 
    tabNumber = (tabNumber - 1) 
    
     if tabObjects[tabNumber].selected == false then 
     dropDownMenu.selected = false 
         A.selected = false 
         B.selected = false 
         C.selected = false 
         D.selected = false 
        tabObjects[tabNumber].selected = true 
    else 
        tabObjects[tabNumber].selected = false 
    end 
    end 
       screen:invalidate() 

end 



function on.arrowKey(key) 

    tabObjects = {dropDownMenu, A, B, C, D} 
     
     
     for k = #tabObjects - 4, #tabObjects do 

    if tabObjects[k].label == "A" and amove == 1 then 
         
         if A.selected == true then 
         
             if key == "up" then A.y = space*math.floor((A.y - space)/space + 0.5) end 
             if key == "down" then A.y = space*math.floor((A.y + space)/space + 0.5) end 
             if key == "left" then A.x = space*math.floor((A.x - space)/space + 0.5) end 
             if key == "right" then A.x = space*math.floor((A.x + space)/space + 0.5) end 
         end 
         
     elseif tabObjects[k].label == "B" and bmove == 1 then 
         
        if B.selected == true then 
         
             if key == "up" then B.y = space*math.floor((B.y - space)/space + 0.5) end 
             if key == "down" then B.y = space*math.floor((B.y + space)/space + 0.5) end 
             if key == "left" then B.x = space*math.floor((B.x - space)/space + 0.5) end 
             if key == "right" then B.x = space*math.floor((B.x + space)/space + 0.5) end 
         end 
    
     elseif tabObjects[k].label == "C" and cmove == 1 then 
         
        if C.selected == true then 
         
             if key == "up" then C.y = space*math.floor((C.y - space)/space + 0.5) end 
             if key == "down" then C.y = space*math.floor((C.y + space)/space + 0.5) end 
             if key == "left" then C.x = space*math.floor((C.x - space)/space + 0.5) end 
             if key == "right" then C.x = space*math.floor((C.x + space)/space + 0.5) end 
         end 
    
     elseif tabObjects[k].label == "D" and dmove == 1 then 
         
        if D.selected == true then 
         
             if key == "up" then D.y = space*math.floor((D.y - space)/space + 0.5) end 
             if key == "down" then D.y = space*math.floor((D.y + space)/space + 0.5) end 
             if key == "left" then D.x = space*math.floor((D.x - space)/space + 0.5) end 
             if key == "right" then D.x = space*math.floor((D.x + space)/space + 0.5) end 
         end 
       end 
     end 
        

        if dropDownMenu.selected == true then 

             if key == "up" and current > 1 then 
                 for k = 1, #Objects do 
                     Objects[k].selected = false 
                 end 
                 
                 current = current - 1 
                 Objects[current].selected = true 
             end 
             
             if key == "down" and current < #Objects then 
                 for k = 1, #Objects do 
                     Objects[k].selected = false 
                 end 
                 current = current + 1 
                 Objects[current].selected = true 
             end 
         else 
             current = 0 
        end 
    screen:invalidate() 

end 

function on.charIn(ch) 

    if ch == "s" or ch == "S" then 
        on.escapeKey() 
        on.resize() 
        if testmode == 0 then 
        reset() 
        end 
    elseif ch == "a" or ch == "A" then 
        if amove == 1 then 
        if A.selected == false then 
            A.selected = true 
        else 
            A.selected = false 
        end 
        end 
    elseif ch == "b" or ch == "B" then 
        if bmove == 1 then 
        if B.selected == false then 
            B.selected = true 
        else 
            B.selected = false 
        end 
        end 
    elseif ch == "c" or ch == "C" then 
        if cmove == 1 then 
         if C.selected == false then 
            C.selected = true 
        else 
            C.selected = false 
        end 
        end 
    elseif ch == "d" or ch == "D" then 
        if dmove == 1 then 
        if D.selected == false then 
           D.selected = true 
        else 
           D.selected = false 
        end 
        end 
        
    end 
end 


function on.enterKey() 
    
    if dropDownMenu.selected == true or qtype == "Random" then 
                 
                  if qtype == "Kite" then 
                            name = "Kite" 
                             if kite == 1 then 
                             answer = "Kite" 
                                correct = 1 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                 else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                   -- end 
                    elseif qtype == "Trapezoid" then 
                            name = "Trapezoid" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if trapezium == 1 then 
                                correct = 1 
                                answer = "Trapezoid" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Parallelogram" then 
                            name = "Parallelogram" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if parallelogram == 1 then 
                                correct = 1 
                                answer = "Parallelogram" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end                    
                    elseif qtype == "Rectangle" then 
                            name = "Rectangle" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if rectangle == 1 then 
                                correct = 1 
                                answer = "Rectangle" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Square" then 
                            name = "Square" 
                            str0 = "How many steps to form a "..answer.."?" 
                            if square == 1 then 
                                correct = 1 
                                answer = "Square" 
                                str9 = "Correct!" 
                                str0 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                    --end 
                    elseif qtype == "Rhombus" then 
                            name = "Rhombus" 
                            str0 = "How many steps to form a "..answer.."?" 
                             if rhombus == 1 then 
                                correct = 1 
                                answer = "Rhombus" 
                                str9 = "Correct!" 
                                str10 = "This is a "..answer 
                                                    else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                            end 
                   -- end 
                     else 
                            correct = 0 
                            answer = "?" 
                            str9 = "" 
                            str10 = "This is a "..name 

                    end 

          for k = 1, #Objects do 
                if Objects[k].selected == true then 
                    Objects[k].selected = false 
                    Objects[k].color = color.white 
                    answer = quadlist[k] 

                    if answer == "Kite" and kite == 1 then 
                            name = "Kite" 
                            correct = 1 
                    end 
                    if answer == "Trapezoid" and trapezium == 1 then 
                            name = "Trapezoid" 
                            correct = 1 
                    end 
                    if answer == "Parallelogram" and parallelogram == 1 then 
                            name = "Parallelogram" 
                            correct = 1 
                    end                    
                    if answer == "Rectangle" and rectangle == 1 then 
                            name = "Rectangle" 
                            correct = 1 
                    end 
                    if answer == "Square" and square == 1 then 
                            name = "Square" 
                            correct = 1 
                    end 
                    if answer == "Rhombus" and rhombus == 1 then 
                            name = "Rhombus" 
                            correct = 1 
                    end 
                    dropDownMenu.selected = false 
                end 
            end 
        elseif reset_button.color == color.black then 
        
            reset() 
         
        else 
        
        questions = 5 
       
        if pageCount <= (questions) then 
            
            if pageCount == (questions) then 
                   reset_button.label = "end" 
                   reset_button.color = color.black 
                   reset_button.selected = false                  
            end 
           if correct > 0  then 
                score = score + 1 
            end 
                total = total + 1 
                table.insert(record, name..":"..answer) 

            if pageCount < (questions) then 
                pageCount = pageCount + 1 
                on.escapeKey() 
            end 
            if pageCount >= (questions) then 
                   reset_button.label = "end" 
                   pageCount = (questions) 
            end 
            reset_button.selected = false 
        end 
          
        end 
        var.store("score", score) 
        var.store("total", total) 
        var.store("record", record) 
        var.store("pagecount", pageCount) 

        screen:invalidate() 

end 



function on.paint(gc) 
    w = screen:width() 
    h = screen:height() 
 
    gc:setPen("thin", "smooth") 
    gc:setColorRGB(200, 200, 200)   
    gc:fillRect(0, 0, w, h) 
    gc:setColorRGB(20, 20, 20)    
    gc:drawRect(0, 0, w, h) 

    gc:setColorRGB(254, 254, 255)    
    gc:fillRect(w*0.4, 0, w, h) 
    gc:setColorRGB(50, 50, 30)    
    gc:drawRect(w*0.4, 0, w, h) 

    grid(unit, gridtype, gc) 
     gc:setColorRGB(20, 20, 20)    
    gc:drawRect(0, 0, w, h) 
    
    gc:setPen("thin", "dotted") 
    for m = 1, questions do  
    gc:setColorRGB(250, 250, 250)    
    gc:fillRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
    gc:setColorRGB(100, 100, 100)    
    gc:drawRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
    end 

 if total <= 5 then 
    for m = 1, total do    
        gc:setColorRGB(200, 200, 200)  
        gc:fillRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
        gc:setColorRGB(150, 150, 150)  
        gc:drawRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
    end 
 
    if testmode == 0 and feedback == 1 then 

    for m = 1, score do    
        gc:setColorRGB(200, 250, 200)  
        gc:fillRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
        gc:setColorRGB(150, 150, 150)  
        gc:drawRect(0.4*(m-1)*w/questions, 0, 0.4*w/questions, 0.05*h) 
    end 
    end 
   end 

 
    fontSize = math.floor(w/55 + 0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "r", fontSize) 
     
    gc:setColorRGB(unpack(color.white))    
    gc:fillPolygon({A.x, A.y, B.x, B.y, C.x, C.y, D.x, D.y, A.x, A.y}) 


    if amove ~= 0 then 
        A.color = color.niceblue 
        else 
        A.color = {150, 150, 150} 
    end 
    if  bmove ~= 0 then 
        B.color = color.niceblue 
        else 
        B.color = {150, 150, 150} 
    end 
    if  cmove ~= 0 then 
        C.color = color.niceblue 
        else 
        C.color = {150, 150, 150} 
    end 
    if  dmove ~= 0 then 
        D.color = color.niceblue 
        else 
        D.color = {150, 150, 150} 
    end 
    

    A:paint(gc) 
    B:paint(gc) 
    C:paint(gc) 
    D:paint(gc) 

    gc:setPen("thin", "smooth") 

    gc:setColorRGB(40, 40, 40)    
    gc:drawPolyLine({A.x, A.y, B.x, B.y, C.x, C.y, D.x, D.y, A.x, A.y}) 

    gc:setPen("thin", "dotted") 
    gc:drawLine(A.x, A.y, C.x, C.y) 
    gc:drawLine(B.x, B.y, D.x, D.y) 

    
    gc:setFont("sansserif", "r", fontSize) 
    gc:setColorRGB(unpack({60, 60, 60})) 
    
    test() 
    
    if testmode == 0 and feedback == 1 then 
    if parnum > 0 then 
        str1 = "Opposite sides parallel ("..parnum..")" 
      --  properties[#properties + 1] = str1 
        sw1 = gc:getStringWidth(str1) 
        gc:drawString(str1, 0.015*w, 0.35*h, "middle") 
    end  

    if perpnum > 0 then 
        str2 = "Adjacent sides perpendicular ("..perpnum..")" 
      --  properties[#properties + 1] = str2 
        sw2 = gc:getStringWidth(str2) 
        gc:drawString(str2, 0.015*w, 0.4*h, "middle") 
    end  


    if sidesopp > 0 then 
        str5 = "Opposite sides equal ("..sidesopp..")" 
    --    properties[#properties + 1] = str5 
        sw5 = gc:getStringWidth(str5) 
        gc:drawString(str5, 0.015*w, 0.45*h, "middle") 
    end  

    if sidesadj > 0 then 
        str6 = "Adjacent sides equal ("..sidesadj..")" 
    --    properties[#properties + 1] = str6 
        sw6 = gc:getStringWidth(str6) 
        gc:drawString(str6, 0.015*w, 0.5*h, "middle") 
    end  

    if diag > 0 then 
        str3 = "Diagonals perpendicular" 
      --  properties[#properties + 1] = str3 
        sw3 = gc:getStringWidth(str3) 
        gc:drawString(str3, 0.015*w, 0.55*h, "middle") 
    end  


    if sides5 > 0 then 
        str4 = "Diagonals equal" 
    --    properties[#properties + 1] = str4 
        sw4 = gc:getStringWidth(str4) 
        gc:drawString(str4, 0.015*w, 0.6*h, "middle") 
    end  
    
    
    end 
    
    if parnum == 1 then 
       name = "Trapezoid" 
       trapezium = 1 
       types = types + 1 
       else 
       trapezium = 0 

   end 

   if diag == 1 and sides5 == 0 and sidesopp == 0 and sides6 == 1 and sides7 == 1 then 
       name = "Kite" 
       kite = 1 
       types = types + 1 
       else 
       kite = 0      
   end 

   if parnum == 2 and kite == 0  then 
       name = "Parallelogram" 
       parallelogram = 1 
       types = types + 1 
       else 
       parallelogram = 0 
   end 

   if parnum == 2 and sides5 > 0 then 
       name = "Rectangle" 
       rectangle = 1 
       types = types + 1 
       else 
       rectangle = 0 
   end 

    
    if sides5 > 0 and diag > 0 and sidesopp > 0 and sidesadj > 0 then 
        name = "Rhombus" 
        rhombus = 1 
        types = types + 1 
        else 
        rhombus = 0 
    end 
    if sides5 > 0 and diag > 0 and perpnum > 0 and sidesopp > 0 and sidesadj > 0 then 
        name = "Square" 
        square = 1 
        types = types + 1 
        else 
        square = 0 
    end 

                   if answer == "Kite" and kite == 1 then 
                            name = "Kite" 
                            correct = 1 
                    end 
                    if answer == "Trapezoid" and trapezium == 1 then 
                            name = "Trapezoid" 
                            correct = 1 
                    end 
                    if answer == "Parallelogram" and parallelogram == 1 then 
                            name = "Parallelogram" 
                            correct = 1 
                    end                    
                    if answer == "Rectangle" and rectangle == 1 then 
                            name = "Rectangle" 
                            correct = 1 
                    end 
                    if answer == "Square" and square == 1 then 
                            name = "Square" 
                            correct = 1 
                    end 
                    if answer == "Rhombus" and rhombus == 1 then 
                            name = "Rhombus" 
                            correct = 1 
                    end 
                    if answer == "None of these" then 
                            name = "None" 
                            correct = 1 
                    end 


    fontSize = math.floor(w/55 + 0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 
            
    if correct == 0 and answer ~= "?" then 
        str9 = pageCount..") ".."This is not a" 
        str10 = answer 
    elseif correct == 1 then 
        str9 = pageCount..") ".."This is a" 
        str10 = answer 
    elseif qtype == "None" then 
         str9 = "Build a quadrilateral then" 
         str10 = "check using the menu above" 
    else 
        str9 = pageCount..") ".."This is" 
        str10 = "?" 
         str9 = "Build a "..qtype 
         str10 = "then press the Next button" 
   
    
    end 
    
    gc:setColorRGB(unpack(color.black)) 
    
    if testmode == 0 and feedback == 1 then 
    local sw9 = gc:getStringWidth(str9) 
    gc:drawString(str9, 0.015*w, 0.7*h, "middle") 
    local sw10 = gc:getStringWidth(str10) 
    gc:drawString(str10, 0.015*w, 0.77*h, "middle") 
    end 

    if testmode == 0 then 
    resetall_button:paint(gc) 
    end 
    reset_button:paint(gc) 
    
    
    fontSize = math.floor(w/50 + 0.5) 
    fontSize = fontSize > 6 and fontSize or 7 
    gc:setFont("sansserif", "b", fontSize) 

    if qtype ~= "None" then 
        gc:setColorRGB(unpack(color.black))   
    local sw0 = gc:getStringWidth(str01) 
    gc:drawString(str01, 0.015*w, 0.1*h, "middle") 
 
    local sw1 = gc:getStringWidth(str02) 
    gc:drawString(str02, 0.015*w, 0.175*h, "middle") 
    local sw2 = gc:getStringWidth(qtype.."?") 
    gc:drawString(qtype.."?", 0.015*w, 0.25*h, "middle") 
  
    gc:setColorRGB(unpack(color.gray)) 
    local str0001 = qtype.." Explorer" 
    local sw0001 = gc:getStringWidth(str0001) 
    gc:drawString(str0001, 0.41*w, 0.05*h, "middle") 
-- 
    else 

    gc:setColorRGB(unpack(color.gray)) 
    local str0001 = score.."/"..total..") ".."Quadrilaterals Explorer" 
    local sw0001 = gc:getStringWidth(str0001) 
    gc:drawString(str0001, 0.41*w, 0.05*h, "middle") 
--[[ 
    local str0002 = "Explorer" 
    local sw0002 = gc:getStringWidth(str0002) 
    gc:drawString(str0002, 0.025*w, 0.125*h, "middle") 
--]]    
    if answer ~= "?" then 
        dropDownMenu.label = answer 
    end 
    
    dropDownMenu:paint(gc) 
    for k = 1, #quadlist do 
    if dropDownMenu.selected == true then 
        Objects[k]:paint(gc) 
 end 
    end 
    
    end 

    
end 



	platform.apilevel = '1.0' 

-- Copyright (C) 2011, Texas Instruments Incorporated
-- All rights reserved
-- Steve Arnold 2011-08-12

    number = (var.recall("n") or 1)
	Color = {
		red         = {255, 0, 0},
		orange      = {255, 165, 0},
		yellow      = {255, 255, 0},
		green       = {0, 255, 0},
		realblue        = {0, 0, 255},
		white       = {255, 255, 255},
		black       = {0, 0, 0},
		paleblue    = {0, 175, 235},
		navy        = {20, 20, 138},
        maroon      = {170, 50, 50},
        gray        = {120, 120, 120},
        lightgray   = {240, 240, 240},
        bluesteel   = {0, 175, 235},
        salmon1      = {235, 230, 230},
        salmon = {220, 220, 250},
        blue = {0, 155, 235}
	}
   

local screen = platform.window
local W = screen:width()
local H = screen:height()
local num = 1


function on.resize()

        
    H=platform.window:height() 
    W=platform.window:width()
    
    width = 2
    height = 1
    num = 1

    vslider = Slider(0.05*W, 0.8*H, 0.05*W, 0.1*H, 0.05*W, 0.1*H, 0.035*H, 0, 1, 9, 1, "tens", Color.blue)
    hslider = Slider(0.15*W, 0.95*H, 0.15*W, 0.95*H, 0.75*W, 0.95*H, 0.035*H, 1, 1, 9, 1, "units", Color.blue)

    mainbutton = Button(W - width*W/10, H - height*H/10, width*W/10, height*H/10, Color.blue, "RESET", false)
    
    
end   	



--------------------------------------------------------------------------- 
-- The Button 


Button = class() 

function round(input) 
    return math.floor(input + 0.5) 
end 


function Button:init(x, y, width, height, color, label, selected) 
    self.x = x 
    self.y = y 
    self.width = width 
    self.height= height 
    self.color = color 
    self.label = label 
    self.selected = false 
end 


function Button:contains(x, y) 

local sw = self.width or self.height 
local sh = self.height or self.width 
return x >= self.x and x <= self.x + sw and 
       y >= self.y and y <= self.y + sh 
end 



function Button:paint(gc) 

    local x = self.x 
    local y = self.y 
    local width = self.width 
    local height = self.height 
    local label = self.label 
    
    
    gc:setColorRGB(unpack(self.color))    
    gc:setPen("thin", "smooth") 
   
  
 local curve = 0.25*height 
       
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 

    gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y)) 
    gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height)) 
    gc:drawLine(round(x + width - curve), round(y + height), round(x + curve), round(y + height)) 
    gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve)) 

    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 


--    local fontSize = math.floor(height/5) 
--    local fontSize = fontSize 
--     local fontSize = fontSize >= 7 and fontSize or 7 
--     local fontSize = fontSize <= 24 and fontSize or 24 
-- gc:setFont("sansserif","b",fontSize) 
 
 if self.selected then 
 gc:setColorRGB(unpack(Color.red)) 
 
 else 
 gc:setColorRGB(unpack(Color.white)) 
 end 
 
 local sw = gc:getStringWidth(label) 
    local sh = gc:getStringHeight(label) 
    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle") 
 
end 




 --------------------------------------------------------------------------- 

-- Sliders 

Slider = class() 

function Slider:init(sx, sy, x, y, ex, ey, radius, height, startvalue, range, units, label, color) 

    self.sx = sx 
    self.sy = sy 
    self.x = x 
    self.y = y 
    self.ex = ex 
    self.ey = ey 
    self.radius = radius 
    self.width = self.radius/4 
    self.height = height 
    self.startvalue = startvalue 
    self.range = range 
    self.units = units 
    self.label = label 
    self.color = color 
    self.selected = false 
    self.value = Xval 
    self.orient = (ex - sx == 0) and 0 or 1 -- Vertical default 

    if self.orient == 1 then 
         self.step = (self.ex - self.sx)/(self.range) 
    else 
         self.step = (self.sy - self.ey)/(self.range) 
    end 


    self.textorient = 1        
    
    if self.orient == 0 and sx < 0.1*W then 
        self.textorient = -1 
     end 

    if self.orient == 1 and H - sy < 0.1*H then 
        self.textorient = -1 
    end 

end 

function Slider:contains(x, y) 

    local r = self.radius 
    local d = math.sqrt((self.x - x)^2 + (self.y - y)^2) 
    return d <= r 
    
end 

function Slider:paint(gc) 
    
    local sx = self.sx 
    local sy = self.sy 
    local x = self.x 
    local y = self.y 
    local ex = self.ex 
    local ey = self.ey 
    local width = self.width 
    local height = self.height 
    local radius = self.radius 
    local orient = self.orient 
    local text = self.textorient 

    
    if orient == 0 then 
    

     self.value = math.floor(self.units*(self.range*(sy - y)/(sy - ey) + self.startvalue) + 0.5)/self.units 
 
    for k = self.startvalue, self.startvalue + self.range, 2 do 
        local  fontSize = math.floor(W/100+0.5) 
     local fontSize = fontSize > 6 and fontSize or 7 
       gc:setFont("sansserif", "r", fontSize) 
        local sw = gc:getStringWidth(k) 
        if text == 1 then 
            gc:drawString("-", sx - width*1.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle") 
        else 
            gc:drawString("-", sx + width*0.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle") 
        
        end 
        
    gc:setPen("thin", "smooth") 
    gc:setColorRGB(250, 250, 250)    
    gc:fillPolygon({sx - width/2, sy, sx - width/2, ey, 
                    sx + width/2, ey, sx + width/2, sy, 
                    sx - width/2, sy 
                    }) 
    gc:fillArc(sx - width/2, ey - width/2, width, width, 0, 180) 
                    
    gc:setColorRGB(unpack(self.color)) 
    gc:fillPolygon({sx - width/2, sy, sx - width/2, y, 
                    sx + width/2, y, sx + width/2, sy, 
                    sx - width/2, sy 
                    }) 
    gc:fillArc(sx - width/2, sy - width/2, width, width, 180, 180) 
                    
        gc:setColorRGB(unpack(Color.gray)) 
    gc:drawLine(sx - width/2, sy, sx - width/2, ey) 
    gc:drawLine(sx + width*0.5, sy, sx + width*0.5, ey) 
    gc:drawArc(sx - width/2, sy - width/2, width, width, 180, 180) 
    gc:drawArc(sx - width/2, ey - width/2, width, width, 0, 180) 
 

    end 
   
    
    if self.selected then 
    	gc:setPen("medium", "smooth") 
    	gc:setColorRGB(unpack(self.color))    
    	gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360) 
    	gc:setColorRGB(0, 0, 0)    
    	gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)    
    else
        gc:setColorRGB(unpack(self.color))    
    	gc:fillArc(x - radius/2, y - width, radius, radius, 0, 360) 
    	gc:setColorRGB(80, 80, 80)    
    	gc:drawArc(x - radius/2, y - width, radius, radius, 0, 360) 

    end 
    

    else 
    
         gc:setColorRGB(unpack(Color.gray)) 
   
    self.value = math.floor(self.units*(self.range*(sx - x)/(sx - ex) + self.startvalue) + 0.5)/self.units 

    for k = self.startvalue, self.startvalue + self.range, 2 do 
        local  fontSize = math.floor(W/100) 
     local fontSize = fontSize > 6 and fontSize or 7 
        gc:setFont("sansserif", "r", fontSize) 
        local sw = gc:getStringWidth("|") 
        if text == 1 then 
        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy + width*1.5, "middle") 
        else 
        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy - width*1.2, "middle") 
        
        end 
        gc:setPen("thin", "smooth") 
    gc:setColorRGB(250, 250, 250)    
    gc:fillPolygon({sx, sy - width/2, ex, ey - width/2, 
                    ex, ey + width/2, sx, sy + width/2, 
                    sx, sy - width/2 
                    }) 
    gc:fillArc(ex - width/2, ey - width/2, width, width*1, -90, 180) 
                    
    gc:setColorRGB(unpack(self.color)) 
    gc:fillPolygon({sx, sy - width/2, x, sy - width/2, 
                    x, sy + width/2, sx, sy + width/2, 
                    sx, sy - width/2 
                    }) 
    gc:fillArc(sx - width*0.5, sy - width/2, width, width, 90, 180) 
                    
	gc:setColorRGB(unpack(Color.gray)) 
    gc:drawLine(sx, sy - width/2, ex, ey - width/2) 
    gc:drawLine(sx, sy + width*0.5, ex, ey + width*0.5) 
    gc:drawArc(sx - width/2, sy - width/2, width, width*1, 90, 180) 
    gc:drawArc(ex - width/2, ey - width/2, width, width*1, -90, 180) 
 

    if self.selected then 
   		gc:setPen("medium", "smooth") 
    	gc:setColorRGB(unpack(self.color))    
    	gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360) 
    	gc:setColorRGB(unpack(Color.black))    
    	gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)    
    else
    	gc:setColorRGB(unpack(self.color))    
    	gc:fillArc(x - radius, y - radius/2, radius, radius, 0, 360) 
    	gc:setColorRGB(unpack(Color.black))    
    	gc:drawArc(x - radius, y - radius/2, radius, radius, 0, 360) 

    end 
    
    end 
   
    
    end 
    
    return self.value 

end 




--------------------------------------------------------------------------- 

-- SECTION 3: Mouse controls

function on.mouseDown(x,y)

    if hslider:contains(x, y) then
	    hslider.selected = true
	    screen:invalidate()
	end

    if vslider:contains(x, y) then
	    vslider.selected = true
	    screen:invalidate()
	end


    if mainbutton:contains(x, y) then
	    mainbutton.selected = true
	    screen:invalidate()
	end

    screen:invalidate()

end

function on.mouseUp(x,y)

    if math.abs(hslider.y - y) < 0.05*H then
        if hslider.x < x and x < 0.9*W and x > 0.1*W then
            on.arrowRight()
        elseif hslider.x > x then
            on.arrowLeft()
        end        
    end

    if math.abs(vslider.x - x) < 0.05*W then
        if vslider.y < y and y > 0.2*H and y < 0.8*H then
            on.arrowDown()
        elseif vslider.y > y then
            on.arrowUp()
        end        
    end

	if hslider.selected then
        hslider.selected = false
	    screen:invalidate()	    
	end

	if vslider.selected then
        vslider.selected = false
	    screen:invalidate()	    
	end


    if mainbutton.selected then
        on.resize()
	    mainbutton.selected = false
	    screen:invalidate()
	end

		screen:invalidate()
end

function on.mouseMove(x,y)


	if vslider.selected == true  then
	
	  if y >= vslider.ey and y <= vslider.sy then
	       vslider.y = y
    	  num = 10*(10 - vslider.value) + hslider.value

	  end
	  
      screen:invalidate()

	end

	if hslider.selected == true  then
	
	  if x >= hslider.sx and x <= hslider.ex then
	       hslider.x = x
    	  num = 10*(10 - vslider.value) + hslider.value

	  end
      screen:invalidate()

	end

    screen:invalidate()
end


---------------------------------------------------------------------------


function on.escapeKey() 
    on.resize()
    screen:invalidate()
end


function on.tabKey() 
    
   -- choose = choose + 1 % 3 + 1
    screen:invalidate()
               
end

function on.arrowUp()


	if vslider.y >= vslider.ey + vslider.step and vslider.y <= vslider.sy then
	      vslider.y = vslider.y - vslider.step
    	  num = 10*(10 - vslider.value + 1) - hslider.value
	  end
    screen:invalidate()
end
    
function on.arrowDown() 
	  if vslider.y >= vslider.ey and vslider.y <= vslider.sy - vslider.step then
	      vslider.y = vslider.y + vslider.step
    	  num = 10*(10 - vslider.value + 1) + hslider.value
	  end
    screen:invalidate()
end

function on.arrowLeft()
	  if hslider.x >= hslider.sx + hslider.step and hslider.x <= hslider.ex then
	       hslider.x = hslider.x - hslider.step
    	  num = 10*(10 - vslider.value) - hslider.value	+ 1  
	  end
    screen:invalidate()

end

function on.arrowRight() 
	  if hslider.x >= hslider.sx and hslider.x <= hslider.ex - hslider.step then
	       hslider.x = hslider.x + hslider.step
    	  num = 10*(10 - vslider.value) + hslider.value	+ 1 
	  end
    screen:invalidate()

end

   
function on.paint(gc)	
   H = platform.window:height()
   W = platform.window:width()
    
    
        local fontSize = math.floor(W/32)
   	    local fontSize = math.floor(fontSize)
	    local fontSize = fontSize > 6 and fontSize or 7
        gc:setFont("sansserif", "r", fontSize)

    gc:setColorRGB(240, 240, 240)
    gc:fillRect(0, 0, W, H)

    h=platform.window:height() 
    w=platform.window:width() 	
		
	local table = {}
	
		
	for k = 1, 10 do
	for m = 1, 10 do


		    gc:setColorRGB(185, 211, 238)

			table[k] = k + 10*(m - 1)             
    		strwidth = gc:getStringWidth(table[k])
    		strheight = gc:getStringHeight(table[k])

        if table[k] % num == 0 then
            gc:setColorRGB(20, 20, 138)
            gc:drawString(table[k], 0.9*w*k/(11) + w/20 ,0.9*h*m/(11) + strheight/3)
               else
    		gc:drawString(table[k], 0.9*w*k/(11) + w/20 ,0.9*h*m/(11) + strheight/3) 
    		end
		end
      end
    
      
    hslider:paint(gc)
    vslider:paint(gc)
    mainbutton:paint(gc)
    
       local fontSize = math.floor(W/42)
   	    local fontSize = math.floor(fontSize)
	    local fontSize = fontSize > 6 and fontSize or 7
        gc:setFont("sansserif", "r", fontSize)
 		    gc:setColorRGB(unpack(Color.navy))
 
    gc:drawString("units ▶", W*0.015, 0.95*H, "middle")
    gc:drawString("tens", W*0.025, 0.035*H, "middle")
      
      
      
end


	platform.apilevel = "2.0" 
-- by Jeff VanArnhem 

 H = platform.window:height() 
 W = platform.window:width() 

 local Xadjust, Yadjust = 150, 100 
 local mouseX, MouseY = 0, 0 
 local spaced = W/12 
 local bottom = 3*H/4 


 Color = { 
  red         = {255, 0, 0}, 
  orange      = {255, 165, 0}, 
  yellow      = {255, 255, 0}, 
  green       = {0, 255, 0}, 
  realblue    = {0, 0, 255}, 
  white       = {255, 255, 255}, 
  black       = {0, 0, 0}, 
  paleblue    = {0, 175, 235}, 
  navy        = {20, 20, 138}, 
        maroon      = {170, 50, 50}, 
        gray        = {120, 120, 120}, 
        lightgray   = {240, 240, 240}, 
        bluesteel   = {0, 175, 235}, 
        salmon1      = {235, 230, 230}, 
        salmon = {220, 220, 250}, 
        blue = {0, 155, 235} 
 } 
   
    buttoncolors = { Color.red, Color.maroon, Color.orange, Color.yellow, 
                     Color.green, Color.paleblue, Color.bluesteel,                                   
                    Color.blue, Color.realblue, Color.navy, 
                    Color.salmon1, Color.lightgray, Color.gray, Color.black } 


function on.resize() 
 H = platform.window:height() 
 W = platform.window:width() 
 Longest = math.floor(W/3.5) 
 Radius = W/10 
 resetbutton = Button(0.025*W, 0.025*H, W/5, H/10, buttoncolors[2], "reset", false) 

 message = "drag from the center, click elsewhere to rotate" 
 setup() 
end 

function setup() 
 Xadjust, Yadjust = 150, 100 
 mouseX, MouseY = 0, 0 
 spaced = W/12 
 bottom = 3*H/4 
 Pieces = {Triangle(Longest, 0.5*spaced, bottom, math.pi/4), Triangle(Longest, spaced*2.5, bottom, math.pi/4), 
    Triangle(Longest/2*math.sqrt(2), spaced*4.5, bottom, math.pi/4), Triangle(Longest/2, spaced*6, bottom, math.pi/4), 
    Triangle(Longest/2, spaced*7, bottom, math.pi/4), Quad("p", spaced*8.5, bottom-10, math.pi/4), Quad("s", spaced*10, bottom, 0)} 
 turnR = {} 
 turnR[1] = {math.cos(math.pi/4), -1*math.sin(math.pi/4)} 
 turnR[2] = {math.sin(math.pi/4), math.cos(math.pi/4)} 
 turnL = {} 
 turnL[1] = {math.cos(-math.pi/4), -1*math.sin(-math.pi/4)} 
 turnL[2] = {math.sin(-math.pi/4), math.cos(-math.pi/4)} 
 flip = {} 
 flip[1] = {-1, 0} 
 flip[2] = {0, 1} 
end 

function on.paint(gc) 
 for _, pie in pairs(Pieces) do 
  pie:paint(gc) 
 end 
 gc:setColorRGB(142, 142, 142) 
 local sw = gc:getStringWidth(message) 
 gc:drawString(message,W - 1.05*sw,0.05*H, "middle") 
 resetbutton:paint(gc) 
end 

function on.tabKey() 
 if TrackedObject ~= nil then 
  TrackedObject.matrx = addScalar(TrackedObject.matrx, -1*Xadjust, 0) 
  TrackedObject.matrx = multiplyMatrices(flip, TrackedObject.matrx) 
  TrackedObject.matrx = addScalar(TrackedObject.matrx, Xadjust, 0) 
  offset() 
  platform.window:invalidate() 
  
 end 
end 

function on.escapeKey() 
    on.resize() 
end 


function on.charIn(char) 
 if TrackedObject ~= nil then 
  if char == '+' then 
   TrackedObject.matrx = addScalar(TrackedObject.matrx, -1*Xadjust, -1*Yadjust) 
   TrackedObject.matrx = multiplyMatrices(turnR, TrackedObject.matrx) 
   TrackedObject.matrx = addScalar(TrackedObject.matrx, Xadjust, Yadjust) 
   offset() 
  elseif char == '-' then 
   TrackedObject.matrx = addScalar(TrackedObject.matrx, -1*Xadjust, -1*Yadjust) 
   TrackedObject.matrx = multiplyMatrices(turnL, TrackedObject.matrx) 
   TrackedObject.matrx = addScalar(TrackedObject.matrx, Xadjust, Yadjust) 
   offset() 
  end 
  platform.window:invalidate() 
 end 
end 

function on.mouseDown (x,y) 
 mouseX, mouseY = x, y 

    if resetbutton:contains(x, y) then 
     resetbutton.selected = true 
     platform.window:invalidate() 
 end 

    for i = 1, #Pieces do 
  local piece = Pieces[i] 
  if piece:contains(x,y) then 
  
   TrackedObject = piece 
   piece.selected = true 
   
   if (piece.x - x)^2 + (piece.y - y)^2 > piece.radius^2 then 
      on.charIn("+") 
      platform.window:invalidate() 
            else 
   cursor.set("drag grab") 
   offset() 
   platform.window:invalidate() 
   end 
  end 
 end 
end 

function on.mouseUp(x,y) 
 mouseX, mouseY = x, y 


    if resetbutton.selected then 
        on.resize() 
     resetbutton.selected = false 
     platform.window:invalidate() 
 end 


    if TrackedObject ~= nil then 
  TrackedObject.selected = false 
 end 
 TrackedObject = nil 
 platform.window:invalidate() 
end 

function on.mouseMove(x,y) 
 mouseX, mouseY = x, y 
 Xadjust, Yadjust = x, y 
 if TrackedObject ~= nil then 
     TrackedObject.x = x - TrackedObject.offsetx 
     TrackedObject.y = y - TrackedObject.offsety 
  TrackedObject.matrx[1][1] = x + deltaX1 
  TrackedObject.matrx[2][1] = y + deltaY1 
  TrackedObject.matrx[1][2] = x + deltaX2 
  TrackedObject.matrx[2][2] = y + deltaY2 
  TrackedObject.matrx[1][3] = x + deltaX3 
  TrackedObject.matrx[2][3] = y + deltaY3 
  if TrackedObject.numsides == 4 then 
   TrackedObject.matrx[1][4] = x + deltaX4 
   TrackedObject.matrx[2][4] = y + deltaY4 
  end 
  platform.window:invalidate() 
 else 
  for i = 1, #Pieces do 
   local piece = Pieces[i] 
   if piece:contains(x,y) then 
    cursor.set("hand open") 
    break 
   else 
    cursor.set("default") 
   end 
   platform.window:invalidate() 
  end 
 end 
end 

function offset () 

 deltaX1 = TrackedObject.matrx[1][1] - mouseX 
 deltaY1 = TrackedObject.matrx[2][1] - mouseY 
 deltaX2 = TrackedObject.matrx[1][2] - mouseX 
 deltaY2 = TrackedObject.matrx[2][2] - mouseY 
 deltaX3 = TrackedObject.matrx[1][3] - mouseX 
 deltaY3 = TrackedObject.matrx[2][3] - mouseY 
 if TrackedObject.numsides == 4 then 
  deltaX4 = TrackedObject.matrx[1][4] - mouseX 
  deltaY4 = TrackedObject.matrx[2][4] - mouseY 
 end 
end 

function multiplyMatrices (a, b) 
 --int 'a' is 2x2 transformation matrix ,'b' is 2xn polygon matrix 
    c = {} 
 c[1] = {0,0,0,0} 
 c[2] = {0,0,0,0} 
    for i=1,2 do 
        for j=1,#b[1] do 
            for k=1, 2 do 
                c[i][j] = c[i][j] + a[i][k] * b[k][j] 
            end 
        end 
    end 
    return c 
end 

function addScalar (a, scalarx, scalary) 
 -- allow me to temporarily tranform coordinate system so mouse is origin 
 -- necessary since reflection is over y-axis and rotation is about origin 
 c = {} 
 c[1]={0,0,0,0} 
 c[2]={0,0,0,0} 
 for j = 1, #a[1] do 
  c[1][j] = a[1][j] + scalarx 
 end 
 for i = 1, #a[1] do 
  c[2][i] = a[2][i] + scalary 
 end 
 return c 
end 

Triangle = class() 

function Triangle:init(size, x, y, theta) 
 self.size = size 
 self.sside = size/math.sqrt(2) 
 self.theta = theta 
 self.comptheta = theta - math.pi/2 
 self.matrx = {} 
 self.matrx[1] = {x, x+self.sside*math.cos(self.theta), x+self.sside*math.cos(self.comptheta)} 
 self.matrx[2] = {y, y+self.sside*math.sin(self.theta), y+self.sside*math.sin(self.comptheta)} 
 self.selected = false 
 self.numsides = 3 
 self.offsetx = self.size/8 
 self.x = x + self.offsetx 
 self.offsety =  - self.size/5 
 self.y = y + self.offsety 
 self.radius = self.size/(2*math.sqrt(2)) 
 
end 

function Triangle:contains(x, y) 
 local test1 = false 
 local test2 = false 
 local test3 = false 
 if (self.matrx[2][3] - self.matrx[2][1] > ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(self.matrx[1][3]-self.matrx[1][1])) then 
  test1 = (y - self.matrx[2][1] > ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 else 
  test1 = (y - self.matrx[2][1] < ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 end 
 if (self.matrx[2][2] - self.matrx[2][1] > ((self.matrx[2][3]-self.matrx[2][1])/(self.matrx[1][3]-self.matrx[1][1]))*(self.matrx[1][2]-self.matrx[1][1])) then 
  test2 = (y - self.matrx[2][1] > ((self.matrx[2][3]-self.matrx[2][1])/(self.matrx[1][3]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 else 
  test2 = (y - self.matrx[2][1] < ((self.matrx[2][3]-self.matrx[2][1])/(self.matrx[1][3]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 end 
 if (self.matrx[2][1] - self.matrx[2][3] > ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(self.matrx[1][1]-self.matrx[1][3])) then 
  test3 = (y - self.matrx[2][3] > ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 else 
  test3 = (y - self.matrx[2][3] < ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 end 
 return test1 and test2 and test3 
end 

function Triangle:paint(gc) 
 if self.size == Longest then 
  gc:setColorRGB(126, 192, 238) 
 elseif self.size == Longest/2*math.sqrt(2) then 
  gc:setColorRGB(255, 106, 106) 
 else 
  gc:setColorRGB(152, 251, 152) 
 end 
 local coord = {self.matrx[1][1],self.matrx[2][1], self.matrx[1][2],self.matrx[2][2],self.matrx[1][3],self.matrx[2][3],self.matrx[1][1],self.matrx[2][1]} 
 gc:fillPolygon(coord) 
 if self.selected then 
  gc:setPen("thin","smooth") 
  gc:setColorRGB(0, 0, 0) 
  gc:drawPolyLine(coord) 
 end 
 
-- gc:setColorRGB(0, 0, 0) 
-- gc:drawArc(self.x, self.y, self.radius, self.radius, 0, 360) 
end 

Quad = class() 

function Quad:init(kind, x, y, theta) 
 self.theta = theta+math.pi/4 
 self.comptheta = theta - math.pi/2 
 self.long = Longest/2 
 self.short = self.long/2*math.sqrt(2) 
 self.side = Longest/2/math.sqrt(2) 
 self.kind = kind 
 self.matrx = {} 
 if kind == "p" then 
  self.matrx[1] = {x, x+self.long*math.cos(self.theta), x+self.short*math.cos(self.theta-math.pi/4), x+self.short*math.cos(self.theta-math.pi/4*3)} 
  self.matrx[2] = {y, y+self.long*math.sin(self.theta), y+self.short*math.sin(self.theta-math.pi/4), y+self.short*math.sin(self.theta-math.pi/4*3)} 
 self.x = x 
 self.y = y 
 self.offsetx = 0 
 self.offsety = 0 
 self.radius = self.side*0.75 

 else 
  self.matrx[1] = {x, x+self.side*math.cos(self.theta), x+self.side*math.sqrt(2)*math.cos(self.theta-math.pi/4), x+self.side*math.cos(self.theta-math.pi/2)} 
  self.matrx[2] = {y, y+self.side*math.sin(self.theta), y+self.side*math.sqrt(2)*math.sin(self.theta-math.pi/4), y+self.side*math.sin(self.theta-math.pi/2)} 
 self.offsetx = self.side/3.75 
 self.offsety = -self.side/2.2 
 self.x = x + self.offsetx 
 self.y = y + self.offsety 
 self.radius = self.side*0.9 

 end 
 self.selected = false 
 self.numsides = 4 
end 

function Quad:contains(x, y) 
 local test1 = false 
 local test2 = false 
 local test3 = false 
 local test4 = false 
 -- side1-2 
 if (self.matrx[2][3] - self.matrx[2][1] > ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(self.matrx[1][3]-self.matrx[1][1])) then 
  test1 = (y - self.matrx[2][1] > ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 else 
  test1 = (y - self.matrx[2][1] < ((self.matrx[2][2]-self.matrx[2][1])/(self.matrx[1][2]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 end 
 -- side 1-4 
 if (self.matrx[2][2] - self.matrx[2][1] > ((self.matrx[2][4]-self.matrx[2][1])/(self.matrx[1][4]-self.matrx[1][1]))*(self.matrx[1][2]-self.matrx[1][1])) then 
  test2 = (y - self.matrx[2][1] > ((self.matrx[2][4]-self.matrx[2][1])/(self.matrx[1][4]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 else 
  test2 = (y - self.matrx[2][1] < ((self.matrx[2][4]-self.matrx[2][1])/(self.matrx[1][4]-self.matrx[1][1]))*(x-self.matrx[1][1])) 
 end 
 -- side 2-3 
 if (self.matrx[2][1] - self.matrx[2][3] > ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(self.matrx[1][1]-self.matrx[1][3])) then 
  test3 = (y - self.matrx[2][3] > ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 else 
  test3 = (y - self.matrx[2][3] < ((self.matrx[2][2]-self.matrx[2][3])/(self.matrx[1][2]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 end 
 -- side 3-4 
 if (self.matrx[2][1] - self.matrx[2][3] > ((self.matrx[2][4]-self.matrx[2][3])/(self.matrx[1][4]-self.matrx[1][3]))*(self.matrx[1][1]-self.matrx[1][3])) then 
  test4 = (y - self.matrx[2][3] > ((self.matrx[2][4]-self.matrx[2][3])/(self.matrx[1][4]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 else 
  test4 = (y - self.matrx[2][3] < ((self.matrx[2][4]-self.matrx[2][3])/(self.matrx[1][4]-self.matrx[1][3]))*(x-self.matrx[1][3])) 
 end 
 return test1 and test2 and test3 and test4 
end 

function Quad:paint(gc) 
 if self.kind == "p" then 
  gc:setColorRGB(255,165,0) 
 else 
  gc:setColorRGB(186,85,211) 
 end 
 local coord = {self.matrx[1][1],self.matrx[2][1], self.matrx[1][2],self.matrx[2][2],self.matrx[1][3],self.matrx[2][3],self.matrx[1][4], self.matrx[2][4], self.matrx[1][1],self.matrx[2][1]} 
 gc:fillPolygon(coord) 
 if self.selected then 
  gc:setPen("thin","smooth") 
  gc:setColorRGB(0, 0, 0) 
  gc:drawPolyLine(coord) 
 end 
-- gc:setColorRGB(0, 0, 0) 
-- gc:drawArc(self.x, self.y, self.radius, self.radius, 0, 360) 

end 

 --------------------------------------------------------------------------- 
-- The Button 


Button = class() 

function round(input) 
    return math.floor(input + 0.5) 
end 


function Button:init(x, y, width, height, color, label, selected) 
    self.x = x 
    self.y = y 
    self.width = width 
    self.height= height 
    self.color = color 
    self.label = label 
    self.selected = false 
end 


function Button:contains(x, y) 

local sw = self.width or self.height 
local sh = self.height or self.width 
return x >= self.x and x <= self.x + sw and 
       y >= self.y and y <= self.y + sh 
end 



function Button:paint(gc) 

    local x = self.x 
    local y = self.y 
    local width = self.width 
    local height = self.height 
    local label = self.label 
    
    
    gc:setColorRGB(unpack(self.color))    
    gc:setPen("thin", "smooth") 
   
  
 local curve = 0.25*height 
       
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 

    gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y)) 
    gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height)) 
    gc:drawLine(round(x + width - curve), round(y + height), round(x + curve), round(y + height)) 
    gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve)) 

    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 


--    local fontSize = math.floor(height/5) 
--    local fontSize = fontSize 
--     local fontSize = fontSize >= 7 and fontSize or 7 
--     local fontSize = fontSize <= 24 and fontSize or 24 
-- gc:setFont("sansserif","b",fontSize) 
 
 if self.selected then 
 gc:setColorRGB(unpack(Color.red)) 
 
 else 
 gc:setColorRGB(unpack(Color.white)) 
 end 
 
 local sw = gc:getStringWidth(label) 
    local sh = gc:getStringHeight(label) 
    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle") 
 
end 



platform.apilevel = '1.0' 

	--Timer Number
	    local sqrRoot = 0
	    local isFirstTime = "false"
	    local scale = 1
	    local number = 1

	--number = (var.recall("n") or 0)

	-------------------------------------------------------------------------------------
		Color = {
			red         = {255, 0, 0},
			orange      = {255, 165, 0},
			yellow      = {255, 255, 0},
			green       = {0, 255, 0},
			realblue        = {0, 0, 255},
			white       = {255, 255, 255},
			black       = {0, 0, 0},
			paleblue    = {0, 175, 235},
			navy        = {20, 20, 138},
	        maroon      = {170, 50, 50},
	        gray        = {120, 120, 120},
	        lightgray   = {240, 240, 240},
	        bluesteel   = {0, 175, 235},
	        salmon1      = {235, 230, 230},
	        salmon = {220, 220, 250},
	        blue = {0, 155, 235}
		}


	    local screen = platform.window
	    local W = screen:width()
	    local H = screen:height()

	 timer.start(4)

	function on.timer()
	    H=screen:height() 
	    W=screen:width()    

	   screen:invalidate()
	end


	   	function on.resize()
	   	    h=screen:height() 
	   	    w=screen:width()
	   	    H=screen:height() 
	   	    W=screen:width()

	        number = 1
	   	    scale = 1

	    vslider = Slider(0.05*W, 0.8*H, 0.05*W, 0.45*H, 0.05*W, 0.1*H, 0.05*W, 0, 0, 2, 0.1, "scale", Color.blue)
	    hslider = Slider(0.15*W, 0.925*H, 0.15*W, 0.925*H, 0.7*W, 0.925*H, 0.05*W, 1, 1, 49, 5, "number", Color.blue)

	    resetbutton = Button(0.8*W, 0.9*H, W/5, H/10, Color.blue, "reset", false)

	        vslider.value = 1
	        hslider.value = 1


	end   	


	--------------------------------------------------------------------------- 
	-- The Button


	Button = class()

	function round(input)
	    return math.floor(input + 0.5)
	end


	function Button:init(x, y, width, height, color, label, selected)
	    self.x = x
	    self.y = y
	    self.width = width
	    self.height= height
	    self.color = color
	    self.label = label
	    self.selected = false
	end


	function Button:contains(x, y)

	local sw = self.width or self.height
	local sh = self.height or self.width
	return x >= self.x and x <= self.x + sw and
	       y >= self.y and y <= self.y + sh
	end


	function Button:paint(gc)

	    local x = self.x
	    local y = self.y
	    local width = self.width
	    local height = self.height
	    local label = self.label


	    gc:setColorRGB(unpack(self.color))    
	    gc:setPen("thin", "smooth")


	 local curve = 0.25*height

	    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height))
	    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve))
	    gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
	    gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
	    gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
	    gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
	    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height))
	    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve))
	    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
	    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
	    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
	    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
	    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height))
	    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve))
	    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height))
	    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve))

	    gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y))
	    gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height))
	    gc:drawLine(round(x + width - curve), round(y + height), round(x + curve), round(y + height))
	    gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve))

	    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
	    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
	    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
	    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)

	    local fontSize = math.floor(height/5)
	    local fontSize = fontSize
	    	local fontSize = fontSize >= 7 and fontSize or 7
	    	local fontSize = fontSize <= 24 and fontSize or 24
		gc:setFont("sansserif","b",fontSize)

		if self.selected then
		gc:setColorRGB(unpack(Color.red))

		else
		gc:setColorRGB(unpack(Color.white))
		end

		local sw = gc:getStringWidth(label)
	    local sh = gc:getStringHeight(label)
	    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle")

	end



	 --------------------------------------------------------------------------- 

	-- Correctly formatting text for algebra



	 --------------------------------------------------------------------------- 

	-- Sliders

	Slider = class()

	function Slider:init(sx, sy, x, y, ex, ey, radius, height, startvalue, range, units, label, color)

	    self.sx = sx
	    self.sy = sy
	    self.x = x
	    self.y = y
	    self.ex = ex
	    self.ey = ey
	    self.radius = radius
	    self.width = self.radius/4
	    self.height = height
	    self.startvalue = startvalue
	    self.range = range
	    self.units = units
	    self.label = label
	    self.color = color
	    self.selected = false
	    self.value = Xval
	    self.orient = (ex - sx == 0) and 0 or 1 -- Vertical default

	    if self.orient == 1 then
	         self.step = (self.ex - self.sx)/(self.range)
	    else
	         self.step = (self.sy - self.ey)/(self.range)
	    end


	    self.textorient = 1        

	    if self.orient == 0 and sx < 0.1*W then
	        self.textorient = -1
	     end

	    if self.orient == 1 and H - sy < 0.1*H then
	        self.textorient = -1
	    end

	end

	function Slider:contains(x, y)

	    local r = self.radius
	    local d = math.sqrt((self.x - x)^2 + (self.y - y)^2)
	    return d <= r

	end

	function Slider:paint(gc)

	    local sx = self.sx
	    local sy = self.sy
	    local x = self.x
	    local y = self.y
	    local ex = self.ex
	    local ey = self.ey
	    local width = self.width
	    local height = self.height
	    local radius = self.radius
	    local orient = self.orient
	    local text = self.textorient


	    if orient == 0 then


	     self.value = math.floor(100*self.units*(self.range*(sy - y)/(sy - ey) + self.startvalue) + 0.5)/(self.units*100)
	         gc:setColorRGB(unpack(Color.gray))
	        local  fontSize = math.floor(W/60+0.5)
		    local fontSize = fontSize > 6 and fontSize or 7
		    gc:setFont("sansserif", "b", fontSize)
		    local sw = gc:getStringWidth(self.label.."="..self.value)

		    if text == 1 then
	     	    gc:drawString(self.label.." = "..self.value, sx - 1.3*sw, y, "middle")
	        else
	 	        gc:drawString(self.label.." = "..self.value, sx + 0.275*sw, y, "middle")
	        end

	        gc:setColorRGB(unpack(Color.gray))

	    for k = self.startvalue, self.startvalue + self.range, 10*self.units do
	        local  fontSize = math.floor(W/100+0.5)
		    local fontSize = fontSize > 6 and fontSize or 7
	       gc:setFont("sansserif", "r", fontSize)
	        local sw = gc:getStringWidth(k)
	        if text == 1 then
	            gc:drawString("-", sx - width*1.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle")
	        else
	            gc:drawString("-", sx + width*0.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle")

	        end

	    gc:setPen("thin", "smooth")
	    gc:setColorRGB(250, 250, 250)    
	    gc:fillPolygon({sx - width/2, sy, sx - width/2, ey,
	                    sx + width/2, ey, sx + width/2, sy,
	                    sx - width/2, sy
	                    })
	    gc:fillArc(sx - width/2, ey - width/2, width, width, 0, 180)

	    gc:setColorRGB(unpack(self.color))
	    gc:fillPolygon({sx - width/2, sy, sx - width/2, y,
	                    sx + width/2, y, sx + width/2, sy,
	                    sx - width/2, sy
	                    })
	    gc:fillArc(sx - width/2, sy - width/2, width, width, 180, 180)

	        gc:setColorRGB(unpack(Color.gray))
	    gc:drawLine(sx - width/2, sy, sx - width/2, ey)
	    gc:drawLine(sx + width*0.5, sy, sx + width*0.5, ey)
	    gc:drawArc(sx - width/2, sy - width/2, width, width, 180, 180)
	    gc:drawArc(sx - width/2, ey - width/2, width, width, 0, 180)


	    end


	    if self.selected then
	    gc:setPen("medium", "smooth")
	    gc:setColorRGB(unpack(self.color))    
	    gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)
	    gc:setColorRGB(0, 0, 0)    
	    gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)   
		else
		gc:setColorRGB(unpack(self.color))    
	    gc:fillArc(x - radius/2, y - width, radius, radius, 0, 360)
	    gc:setColorRGB(80, 80, 80)    
	    gc:drawArc(x - radius/2, y - width, radius, radius, 0, 360)

	    end


	    else

	         gc:setColorRGB(unpack(Color.gray))

	    self.value = math.floor((self.range*(sx - x)/(sx - ex) + self.startvalue) + 0.5)
	        local  fontSize = math.floor(W/60 + 0.5)
		    local fontSize = fontSize > 6 and fontSize or 7
	        gc:setFont("sansserif", "r", fontSize)
		    local str = self.label.." = "..self.value
		    local sw = gc:getStringWidth(str)
		    local sh = gc:getStringHeight(str)
		    if text == 1 then	    
		    gc:drawString(str, (sx + ex)/2 - sw/2, y + sh, "middle")
		    else
		    gc:drawString(str, (sx + ex)/2 - sw/2, y - sh, "middle")	    
		    end

	    for k = self.startvalue, self.startvalue + self.range, self.units do
	        local  fontSize = math.floor(W/100)
		    local fontSize = fontSize > 6 and fontSize or 7
	        gc:setFont("sansserif", "r", fontSize)
	        local sw = gc:getStringWidth("|")
	        if text == 1 then
	        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy + width*1.5, "middle")
	        else
	        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy - width*1.2, "middle")

	        end
	        gc:setPen("thin", "smooth")
	    gc:setColorRGB(250, 250, 250)    
	    gc:fillPolygon({sx, sy - width/2, ex, ey - width/2,
	                    ex, ey + width/2, sx, sy + width/2,
	                    sx, sy - width/2
	                    })
	    gc:fillArc(ex - width/2, ey - width/2, width, width*1, -90, 180)

	    gc:setColorRGB(unpack(self.color))
	    gc:fillPolygon({sx, sy - width/2, x, sy - width/2,
	                    x, sy + width/2, sx, sy + width/2,
	                    sx, sy - width/2
	                    })
	    gc:fillArc(sx - width*0.5, sy - width/2, width, width, 90, 180)

	        gc:setColorRGB(unpack(Color.gray))
	    gc:drawLine(sx, sy - width/2, ex, ey - width/2)
	    gc:drawLine(sx, sy + width*0.5, ex, ey + width*0.5)
	    gc:drawArc(sx - width/2, sy - width/2, width, width*1, 90, 180)
	    gc:drawArc(ex - width/2, ey - width/2, width, width*1, -90, 180)


	     if self.selected then
	    gc:setPen("medium", "smooth")
	    gc:setColorRGB(unpack(self.color))    
	    gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)
	    gc:setColorRGB(unpack(Color.black))    
	    gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)    
		else
			    gc:setColorRGB(unpack(self.color))    
	    gc:fillArc(x - width, y - radius/2, radius, radius, 0, 360)
	    gc:setColorRGB(unpack(Color.black))    
	    gc:drawArc(x - width, y - radius/2, radius, radius, 0, 360)

	    end

	    end


	    end

	    return self.value

	end



	function on.mouseDown(x,y)

	    if hslider:contains(x, y) then
		    hslider.selected = true
		    screen:invalidate()
		end

	    if vslider:contains(x, y) then
		    vslider.selected = true
		    screen:invalidate()
		end

	    if resetbutton:contains(x, y) then
		    resetbutton.selected = true
		    screen:invalidate()
		end


	    screen:invalidate()

	end

	function on.mouseUp(x,y)

	    if math.abs(hslider.y - y) < 0.05*h then
	        if hslider.x < x and x < 0.9*w and x > 0.2*w then
	            on.arrowRight()
	        elseif hslider.x > x then
	            on.arrowLeft()
	        end        
	    end

	    if math.abs(vslider.x - x) < 0.05*w then
	        if vslider.y < y and y > 0.2*h and y < 0.9*h and not resetbutton:contains(x, y) then
	            on.arrowDown()
	        elseif vslider.y > y then
	            on.arrowUp()
	        end        
	    end

		if hslider.selected then
	        hslider.selected = false
		    screen:invalidate()	    
		end

		if vslider.selected then
	        vslider.selected = false
		    screen:invalidate()	    
		end


	    if resetbutton.selected then
	        on.resize(gc)
		    resetbutton.selected = false
		    screen:invalidate()
		end

			screen:invalidate()
	end

	function on.mouseMove(x,y)


		if vslider.selected == true  then

		  if y >= vslider.ey and y <= vslider.sy then
		       vslider.y = y
		       scale = vslider.value
		  end

	      screen:invalidate()

		end

		if hslider.selected == true  then

		  if x >= hslider.sx and x <= hslider.ex then
		       hslider.x = x
		       number = hslider.value
		  end
	      screen:invalidate()

		end

	    screen:invalidate()
	end


	-------------------------------------------------------------------------------------

	-- Grab keyboard entry to build the variable answer.


	function on.arrowUp()
	    if scale < 2 then
	    scale = scale + 0.1
	    vslider.y = vslider.y - vslider.step/10
	    end
	    screen:invalidate()
	end

	function on.arrowDown() 
	    if scale > 0 then
	    scale = scale - 0.1
	    vslider.y = vslider.y + vslider.step/10
	    end
	    screen:invalidate()
	end

	function on.arrowLeft()
	   if number > 1 then
	        number = number - 1
	     hslider.x = hslider.x - hslider.step
	    end
	    screen:invalidate()

	end

	function on.arrowRight() 
	    if number < 49 then
	        number = number + 1
	   hslider.x = hslider.x + hslider.step
	   end
	    screen:invalidate()

	end



	-------------------------------------------------------------------------------------





	function on.escapeKey()

	         on.resize()
	--    shw = (shw + 1) % 4
	    platform.window:invalidate()

	end




	--***DRAWBASELINE() FUNCTION
	function drawBaseLine(x,y,length,height, gc)
		gc:setColorRGB(20,20,138)
		gc:setPen("thin","smooth")
		gc:drawLine(x,y,length,height)
	    gap=(w/2-10*scale)/5*scale
		for i=1, 20 do
			gc:drawLine(i*(gap)+(w/2),h/2-divderHeight/2,i*(gap)+(w/2),h/2+divderHeight-divderHeight/2)
		end
	end

	--***DRAWCIRCLE() FUNCTION
	function drawCircles(x,y,width,height,startAngle,finishAngle, gc)
		gc:setColorRGB(158,5,8)
		gc:setPen("thin","dashed")
		gc:drawArc(x,y,width,height,startAngle,finishAngle)
	end

	--***DRAWRADIUSLINE() FUNCTION
	function drawRadiusLine(x,y,radius,height, gc)
		gc:setColorRGB(165,42,42)
		gc:setPen("thin","smooth")
		gc:drawLine(x,y,radius,height)	
	end



	function on.paint(gc)
	   	    h=screen:height() 
	   	    w=screen:width()
	   	    H=screen:height() 
	   	    W=screen:width()

	        gc:setColorRGB(240, 240, 240)
	        gc:fillRect(0,0,w,h)

		xPos = w/2
		yPos = h/2
		gap=(w/2-10*scale)/5*scale

		divderHeight=h/60

		sqrRoot = math.sqrt(number)

		sqrRootLineLength = xPos + gap*math.sqrt(number)	


		--***[SYNTAX: drawArc(x, y, width, height, start angle, finish angle)]
		gc:setColorRGB(20,20,138)
		gc:setPen("thin","dashed")
	    turn_angle = 0
	    turn_angle1 = math.sqrt(1/2)

	    if number > 0 and gap > 0 then

	        gc:drawArc(xPos - gap*math.sqrt(2*number * 2)/2, 
	                   yPos - gap*math.sqrt(2*number * 2)/2, 
	                   2*gap*math.sqrt(number), 2*gap*math.sqrt(number),
	                   0,360)

	   if number >= 2 then
	          gc:drawPolyLine({xPos, yPos, 
	                        xPos+gap*math.sqrt(1), yPos, 
	                        xPos+gap*math.sqrt(1), yPos-gap*math.sqrt(1), 
	                        xPos, yPos}) 
	    end

		for k = 3, number do

	 		turn_angle = turn_angle + math.atan2(1,math.sqrt(k - 2))
		    turn_angle1 = turn_angle + math.atan2(1,math.sqrt(k - 1))

	        gc:drawPolyLine({xPos, yPos, 
			                xPos + gap*math.sqrt(k - 1)*math.cos(turn_angle),
			                yPos - gap*math.sqrt(k - 1)*math.sin(turn_angle),
			                xPos + gap*math.sqrt(k)*math.cos(turn_angle1),
			                yPos - gap*math.sqrt(k)*math.sin(turn_angle1),
			                xPos,yPos})	


		    end
	    end	


		--***BASE LINE : drawBaseLine(x,y,length,height)
		drawBaseLine(xPos,yPos,w-5,yPos, gc)

		--***RADIUS LINE
		drawRadiusLine(xPos,yPos,xPos + gap*math.sqrt(number),yPos, gc)

	    local fontSize = math.floor(H/20 + 0.5)
	    	local fontSize = fontSize >= 7 and fontSize or 7
	    	local fontSize = fontSize <= 24 and fontSize or 24
		gc:setFont("sansserif","b",fontSize)

		if number == 0 then
			gc:setFont("sansserif", "b", fontSize)
			local sw = gc:getStringWidth("Square Root of "..number.." = √(0² + (√0)²)")
	    gc:drawString("Square Root of "..number.." = √(0² + (√0)²)", w - sw - w/20, 0.1*h)
		else
			gc:setFont("sansserif", "b", fontSize)
			local sw = gc:getStringWidth("Square Root of "..number.." = √(1² + (√"..(number - 1)..")²)")
	    gc:drawString("Square Root of "..number.." = √(1² + (√"..(number - 1)..")²)", w - sw - w/20, 0.1*h)
		end
			gc:setFont("sansserif", "b", fontSize)
			local sw = gc:getStringWidth("√("..number..") ▸ "..sqrRoot)
		gc:drawString("√("..number..") ▸ "..sqrRoot, w - sw - h/20, 0.2*h)

		hslider:paint(gc)
	    vslider:paint(gc)
	    resetbutton:paint(gc)


	end
	
		platform.apilevel = '1.0' 

		-- Steve Arnold November 2012 


		function reset() 

		    Hvar = 3 
		    Vvar = 10 

		    update() 
		    build() 

		    platform.window:invalidate() 

		end 
		------------------------------------------------------------------------------------- 
		    Color = { 
		  red         = {255, 0, 0}, 
		  orange      = {255, 165, 0}, 
		  yellow      = {255, 255, 0}, 
		  green       = {0, 255, 0}, 
		  realblue        = {0, 0, 255}, 
		  white       = {255, 255, 255}, 
		  black       = {0, 0, 0}, 
		  paleblue    = {0, 175, 235}, 
		  navy        = {20, 20, 138}, 
		        maroon      = {170, 50, 50}, 
		        gray        = {120, 120, 120}, 
		        lightgray   = {240, 240, 240}, 
		        bluesteel   = {0, 175, 235}, 
		        salmon1      = {235, 230, 230}, 
		        salmon = {220, 220, 250}, 
		        blue = {0, 155, 235} 
		 } 

		     local grablist = { "build", "move"} 
		    local screen = platform.window 
		    local TrackedObject = nil 
		    local TrackOffsetx = 0 
		    local TrackOffsety = 0 


		    local W = screen:width() 
		    local H = screen:height() 
		    local w = screen:width() 
		    local h = screen:height() 

		 timer.start(4) 

		function on.timer() 
		    H=screen:height() 
		    W=screen:width()    
		    h=screen:height() 
		    w=screen:width()    

		   screen:invalidate() 
		end 

		    local Hvar = 3 
		    local Vvar = 5 
		    local Vertex = {} 
		    local Radius = H/3 
		    local Grab = 1 
		-- ** wide is the size of your circles and triangles 
		    local Wide = H/15 
		    local vx = {} 
		    local vy = {} 




		----------------------------------------------------- 
		-- **Set the range values that you wish to use for vertical and horizontal variables 

		    local Hrange = 27 
		    local Vrange = 19 




		function on.resize() 

		    H=screen:height() 
		    W=screen:width()    
		    h=screen:height() 
		    w=screen:width()    

		    Objects = {} 
		    Vertex = {} 
		    Hvar = 3 
		    Vvar = 10 
		    Radius = H/3 
		    Grab = 1 
		    -- ** wide is the size of your circles and triangles 
		    Wide = H/15 

		   TrackedObject = nil 
		   TrackOffsetx = 0 
		   TrackOffsety = 0 


		    vslider = Slider(0.95*W, 0.8*H, 0.95*W, 0.52*H, 0.95*W, 0.2*H, 0.05*W, 0, 1, Vrange, 1, "strings", Color.blue) 
		    hslider = Slider(0.15*W, 0.925*H, 0.15*W, 0.925*H, 0.8*W, 0.925*H, 0.05*W, 1, 3, Hrange, 1, "vertices", Color.blue) 

		    resetbutton = Button(0.01*W, 0.025*H, W/5, H/10, Color.blue, "reset", false) 
		    typebutton = Button(0.01*W, 0.5*H, W/5, H/10, Color.blue, grablist[Grab], false) 

		        vslider.value = 10 
		        hslider.value = 3 

		Vertex = {} 
		if Hvar > 0 then 

		for m = 1, Hvar do 
		  for k = 1, 3 do 

		      vx[k+3*(m-1)] = Radius*(math.cos(2*math.pi*(k - 1)/Hvar)) + W/2 
		      vy[k+3*(m-1)] = Radius*math.sin(2*math.pi*(k - 1)/Hvar) + H/2 

		      Vertex[k+3*(m-1)] = CircleB(vx[k+3*(m-1)], vy[k+3*(m-1)], Wide, Wide) 

		  -- table.remove(Objects, k + 9)            
		      table.insert(Objects, Vertex[k+3*(m-1)]) 
		  end 
		  end 
		end 
		    screen:invalidate() 

		end 


		CircleB = class() 

		function CircleB:init(x, y, width, height) 

		    self.x = x 
		    self.y = y 
		    self.width = width 
		    self.height = height 
		    self.radius = height/2 
		    self.color = Color.maroon 
		    self.selected = false 
		end 



		CircleB = class() 

		function CircleB:init(x, y, width, height) 

		    self.x = x 
		    self.y = y 
		    self.width = width 
		    self.height = height 
		    self.radius = height/2 
		    self.color = Color.maroon 
		    self.selected = false 
		end 


		function CircleB:contains(x, y) 

		    local r = self.radius 
		    local d = math.sqrt((self.x - x)^2 + (self.y - y)^2) 
		    return d <= r 
		end 


		function CircleB:paint(gc) 

		    local cx = self.x - self.radius 
		    local cy = self.y - self.radius 
		    local diameter = 2*self.radius 
		    if self.selected then 
		        gc:setPen("medium","smooth") 
		   		 gc:setColorRGB(unpack(self.color)) 
		   		 gc:fillArc(cx - diameter/2, cy - diameter/2, 2*diameter, 2*diameter, 0, 360) 
		        gc:setColorRGB(0, 0, 0) 
		        gc:drawArc(cx - diameter/2, cy - diameter/2, 2*diameter, 2*diameter, 0, 360)
		    else
		    	 gc:setPen("thin","smooth") 
		   		 gc:setColorRGB(unpack(self.color)) 
		   		 gc:fillArc(cx, cy, diameter, diameter, 0, 360) 
		        gc:setColorRGB(0, 0, 0) 
		        gc:drawArc(cx, cy, diameter, diameter, 0, 360) 

		    end 
		    platform.window:invalidate() 
		end 

		--------------------------------------------------------------------------- 
		-- The Button 


		Button = class() 

		function round(input) 
		    return math.floor(input + 0.5) 
		end 


		function Button:init(x, y, width, height, color, label, selected) 
		    self.x = x 
		    self.y = y 
		    self.width = width 
		    self.height= height 
		    self.color = color 
		    self.label = label 
		    self.selected = false 
		end 


		function Button:contains(x, y) 

		local sw = self.width or self.height 
		local sh = self.height or self.width 
		return x >= self.x and x <= self.x + sw and 
		       y >= self.y and y <= self.y + sh 
		end 



		function Button:paint(gc) 

		    local x = self.x 
		    local y = self.y 
		    local width = self.width 
		    local height = self.height 
		    local label = self.label 


		    gc:setColorRGB(unpack(self.color))    
		    gc:setPen("thin", "smooth") 


		 local curve = 0.25*height 

		    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
		    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
		    gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
		    gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
		    gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
		    gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
		    gc:fillRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
		    gc:fillRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
		    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
		    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
		    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
		    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 
		    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
		    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 
		    gc:drawRect(round(x + curve), round(y), round(width - 2*curve), round(height)) 
		    gc:drawRect(round(x), round(y + curve), round(width), round(height  - 2*curve)) 

		    gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y)) 
		    gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height)) 
		    gc:drawLine(round(x + width - curve), round(y + height), round(x + curve), round(y + height)) 
		    gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve)) 

		    gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90) 
		    gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90) 
		    gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90) 
		    gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90) 


		--    local fontSize = math.floor(height/5) 
		--    local fontSize = fontSize 
		--     local fontSize = fontSize >= 7 and fontSize or 7 
		--     local fontSize = fontSize <= 24 and fontSize or 24 
		-- gc:setFont("sansserif","b",fontSize) 

		 if self.selected then 
		 gc:setColorRGB(unpack(Color.red)) 

		 else 
		 gc:setColorRGB(unpack(Color.white)) 
		 end 

		 local sw = gc:getStringWidth(label) 
		    local sh = gc:getStringHeight(label) 
		    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle") 

		end 




		 --------------------------------------------------------------------------- 

		-- Sliders 

		Slider = class() 

		function Slider:init(sx, sy, x, y, ex, ey, radius, height, startvalue, range, units, label, color) 

		    self.sx = sx 
		    self.sy = sy 
		    self.x = x 
		    self.y = y 
		    self.ex = ex 
		    self.ey = ey 
		    self.radius = radius 
		    self.width = self.radius/4 
		    self.height = height 
		    self.startvalue = startvalue 
		    self.range = range 
		    self.units = units 
		    self.label = label 
		    self.color = color 
		    self.selected = false 
		    self.value = Xval 
		    self.orient = (ex - sx == 0) and 0 or 1 -- Vertical default 

		    if self.orient == 1 then 
		         self.step = (self.ex - self.sx)/(self.range) 
		    else 
		         self.step = (self.sy - self.ey)/(self.range) 
		    end 


		    self.textorient = 1        

		    if self.orient == 0 and sx < 0.1*W then 
		        self.textorient = -1 
		     end 

		    if self.orient == 1 and H - sy < 0.1*H then 
		        self.textorient = -1 
		    end 

		end 

		function Slider:contains(x, y) 

		    local r = self.radius 
		    local d = math.sqrt((self.x - x)^2 + (self.y - y)^2) 
		    return d <= r 

		end 

		function Slider:paint(gc) 

		    local sx = self.sx 
		    local sy = self.sy 
		    local x = self.x 
		    local y = self.y 
		    local ex = self.ex 
		    local ey = self.ey 
		    local width = self.width 
		    local height = self.height 
		    local radius = self.radius 
		    local orient = self.orient 
		    local text = self.textorient 


		    if orient == 0 then 


		     self.value = math.floor(self.units*(self.range*(sy - y)/(sy - ey) + self.startvalue) + 0.5)/self.units 
		         gc:setColorRGB(unpack(Color.gray)) 
		        local  fontSize = math.floor(W/60+0.5) 
		     local fontSize = fontSize > 6 and fontSize or 7 
		     gc:setFont("sansserif", "b", fontSize) 
		     local sw = gc:getStringWidth(self.label.."="..self.value) 

		     if text == 1 then 
		          gc:drawString(self.label.."="..self.value, sx - 1.3*sw, y, "middle") 
		        else 
		          gc:drawString(self.label.."="..self.value, sx + 0.275*sw, y, "middle") 
		        end 

		        gc:setColorRGB(unpack(Color.gray)) 

		    for k = self.startvalue, self.startvalue + self.range, 2 do 
		        local  fontSize = math.floor(W/100+0.5) 
		     local fontSize = fontSize > 6 and fontSize or 7 
		       gc:setFont("sansserif", "r", fontSize) 
		        local sw = gc:getStringWidth(k) 
		        if text == 1 then 
		            gc:drawString("-", sx - width*1.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle") 
		        else 
		            gc:drawString("-", sx + width*0.75, sy - (k - self.startvalue)*(sy - ey)/self.range, "middle") 

		        end 

		    gc:setPen("thin", "smooth") 
		    gc:setColorRGB(250, 250, 250)    
		    gc:fillPolygon({sx - width/2, sy, sx - width/2, ey, 
		                    sx + width/2, ey, sx + width/2, sy, 
		                    sx - width/2, sy 
		                    }) 
		    gc:fillArc(sx - width/2, ey - width/2, width, width, 0, 180) 

		    gc:setColorRGB(unpack(self.color)) 
		    gc:fillPolygon({sx - width/2, sy, sx - width/2, y, 
		                    sx + width/2, y, sx + width/2, sy, 
		                    sx - width/2, sy 
		                    }) 
		    gc:fillArc(sx - width/2, sy - width/2, width, width, 180, 180) 

		        gc:setColorRGB(unpack(Color.gray)) 
		    gc:drawLine(sx - width/2, sy, sx - width/2, ey) 
		    gc:drawLine(sx + width*0.5, sy, sx + width*0.5, ey) 
		    gc:drawArc(sx - width/2, sy - width/2, width, width, 180, 180) 
		    gc:drawArc(sx - width/2, ey - width/2, width, width, 0, 180) 


		    end 

		    if self.selected then 
		    gc:setPen("medium", "smooth") 
		    gc:setColorRGB(unpack(self.color))    
		    gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360) 
		    gc:setColorRGB(0, 0, 0)    
		    gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)   
			else
				    gc:setColorRGB(unpack(self.color))    
		    gc:fillArc(x - radius/2, y - width, radius, radius, 0, 360) 
		    gc:setColorRGB(80, 80, 80)    
		    gc:drawArc(x - radius/2, y - width, radius, radius, 0, 360) 

		    end 


		    else 

		         gc:setColorRGB(unpack(Color.gray)) 

		    self.value = math.floor(self.units*(self.range*(sx - x)/(sx - ex) + self.startvalue) + 0.5)/self.units 
		        local  fontSize = math.floor(W/60 + 0.5) 
		     local fontSize = fontSize > 6 and fontSize or 7 
		        gc:setFont("sansserif", "r", fontSize) 
		     local str = self.label.." = "..self.value 
		     local sw = gc:getStringWidth(str) 
		     local sh = gc:getStringHeight(str) 
		     if text == 1 then     
		     gc:drawString(str, (sx + ex)/2 - sw/2, y + sh, "middle") 
		     else 
		     gc:drawString(str, (sx + ex)/2 - sw/2, y - sh, "middle")     
		     end 

		    for k = self.startvalue, self.startvalue + self.range, 2 do 
		        local  fontSize = math.floor(W/100) 
		     local fontSize = fontSize > 6 and fontSize or 7 
		        gc:setFont("sansserif", "r", fontSize) 
		        local sw = gc:getStringWidth("|") 
		        if text == 1 then 
		        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy + width*1.5, "middle") 
		        else 
		        gc:drawString("|", sx + (k - self.startvalue)*(ex - sx)/self.range - sw/2, sy - width*1.2, "middle") 

		        end 
		        gc:setPen("thin", "smooth") 
		    gc:setColorRGB(250, 250, 250)    
		    gc:fillPolygon({sx, sy - width/2, ex, ey - width/2, 
		                    ex, ey + width/2, sx, sy + width/2, 
		                    sx, sy - width/2 
		                    }) 
		    gc:fillArc(ex - width/2, ey - width/2, width, width*1, -90, 180) 

		    gc:setColorRGB(unpack(self.color)) 
		    gc:fillPolygon({sx, sy - width/2, x, sy - width/2, 
		                    x, sy + width/2, sx, sy + width/2, 
		                    sx, sy - width/2 
		                    }) 
		    gc:fillArc(sx - width*0.5, sy - width/2, width, width, 90, 180) 

		        gc:setColorRGB(unpack(Color.gray)) 
		    gc:drawLine(sx, sy - width/2, ex, ey - width/2) 
		    gc:drawLine(sx, sy + width*0.5, ex, ey + width*0.5) 
		    gc:drawArc(sx - width/2, sy - width/2, width, width*1, 90, 180) 
		    gc:drawArc(ex - width/2, ey - width/2, width, width*1, -90, 180) 


		     if self.selected then 
		    gc:setPen("medium", "smooth") 
		    gc:setColorRGB(unpack(self.color))    
		    gc:fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360) 
		    gc:setColorRGB(unpack(Color.black))    
		    gc:drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360)    
			else
				    gc:setColorRGB(unpack(self.color))    
		    gc:fillArc(x - width, y - radius/2, radius, radius, 0, 360) 
		    gc:setColorRGB(unpack(Color.black))    
		    gc:drawArc(x - width, y - radius/2, radius, radius, 0, 360) 

		    end 

		    end 


		    end 

		    return self.value 

		end 




		------------------------------------------------------------------------------------- 


		-- Set up the Mouse Controls 

		function on.mouseDown(x,y) 

		for i = #Objects, 1, -1 do 
		    local obj = Objects[i] 
		    if obj:contains(x, y) then 

		        if TrackedObject ~= nil then 
		            TrackedObject.selected = false 
		        end 
		        TrackedObject = obj 
		        obj.selected = true 
		        TrackOffsetx = TrackedObject.x - x 
		        TrackOffsety = TrackedObject.y - y 


		        table.remove(Objects, i) 
		        table.insert(Objects, obj) 

		        platform.window:invalidate() 
		        break 
		    end 
		end    


		    if hslider:contains(x, y) then 
		     hslider.selected = true 
		     screen:invalidate() 
		 end 

		    if vslider:contains(x, y) then 
		     vslider.selected = true 
		     screen:invalidate() 
		 end 

		    if resetbutton:contains(x, y) then 
		     resetbutton.selected = true 
		     screen:invalidate() 
		 end 

		    if typebutton:contains(x, y) then 
		     typebutton.selected = true 
		     screen:invalidate() 
		 end 


		screen:invalidate()    

		end 


		function on.mouseUp(x,y) 

		for i = #Objects, 1, -1 do 
		    local obj = Objects[i] 
		    if obj:contains(x, y) then 

		        if TrackedObject ~= nil then 
		            TrackedObject.selected = false 
		        end 
		        TrackedObject = obj 
		        obj.selected = true 
		        TrackOffsetx = TrackedObject.x - x 
		        TrackOffsety = TrackedObject.y - y 


		        table.remove(Objects, i) 
		        table.insert(Objects, obj) 

		        platform.window:invalidate() 
		        break 
		    end 
		end    



		    for k = 1, #Vertex do 

		    if (Vertex[k].x - x)^2 + (Vertex[k].y - y)^2 < 2*Vertex[k].width^2 then

		    Vertex[k].x = x
		    Vertex[k].y = y

		    end
		    end

		        if TrackedObject ~= nil then 
		            TrackedObject.selected = false 
		        end 
		TrackedObject = nil 

		    if math.abs(hslider.y - y) < 0.05*h then 
		        if hslider.x < x and x < 0.9*w and x > 0.2*w then 
		            on.arrowRight() 
		        elseif hslider.x > x then 
		            on.arrowLeft() 
		        end        
		    end 

		    if math.abs(vslider.x - x) < 0.05*w then 
		        if vslider.y < y and y > 0.2*h and y < 0.9*h and not resetbutton:contains(x, y) then 
		            on.arrowDown() 
		        elseif vslider.y > y then 
		            on.arrowUp() 
		        end        
		    end 

		 if hslider.selected then 
		        hslider.selected = false 
		     screen:invalidate()     
		 end 

		 if vslider.selected then 
		        vslider.selected = false 
		     screen:invalidate()     
		 end 


		    if resetbutton.selected then 
		        on.resize(gc) 
		     resetbutton.selected = false 
		     screen:invalidate() 
		 end 

		    if typebutton.selected then 
		        if Grab == 1 then 
		        Grab = 2 
		        typebutton.color = Color.green 
		        else 
		        Grab = 1 
		        typebutton.color = Color.blue 
		        end 
		        typebutton.label = grablist[Grab] 
		     typebutton.selected = false 
		     screen:invalidate() 
		 end 

		     screen:invalidate() 
		end 


		function on.mouseMove(x,y) 

		    if TrackedObject ~= nil then 
		    for k = 1, #Vertex do 
		    if TrackedObject == Vertex[k] then 
		                TrackedObject.x = x + TrackOffsetx 
		                TrackedObject.y = y + TrackOffsety 
		                    screen:invalidate() 
		            end 
		        end 
		    end   
		 if vslider.selected == true  then 

		   if y >= vslider.ey and y <= vslider.sy then 
		        vslider.y = y 
		        Vvar = vslider.value 
		   end 

		      screen:invalidate() 

		 end 

		 if hslider.selected == true  then 

		   if x >= hslider.sx and x <= hslider.ex then 
		        hslider.x = x 
		        Hvar = hslider.value 
		   end 
		      screen:invalidate() 

		 end 


		    screen:invalidate() 

		end 

		------------------------------------------------------ 
		-- Set up the Keyboard Controls 

		function on.arrowUp() 
		    if Vvar < Vrange then 
		    Vvar = Vvar + 1 
		    vslider.y = vslider.y - vslider.step 
		    end 
		        screen:invalidate() 
		end 

		function on.arrowDown() 
		    if Vvar > 0 then 
		    Vvar = Vvar - 1 
		    vslider.y = vslider.y + vslider.step 
		    end 
		        screen:invalidate() 
		end 


		function on.arrowLeft() 
		if Hvar > 4 then 
		    Hvar = Hvar - 1 
		    hslider.x = hslider.x - hslider.step 
		    end 
		    screen:invalidate() 
		end 

		function on.arrowRight() 
		if Hvar < Hrange then 
		    Hvar = Hvar + 1 
		    hslider.x = hslider.x + hslider.step 
		    end 
		        screen:invalidate() 
		end 

		function on.escapeKey() 
		  --  reset() 
		    on.resize() 
		end 

		function on.enterKey() 
		    reset() 
		end 

		function on.tabKey() 
		   if Grab == 1 then 
		       Grab = 2 
		              typebutton.color = Color.green 

		    else 
		        Grab = 1 
		               typebutton.color = Color.blue 

		    end 
		            typebutton.label = grablist[Grab] 

		        screen:invalidate() 
		end 

		---------------------------------------------------- 

		function Bezier(a,b,k) 
		    if a and b and k then 
		    return a*(1 - k) + b*k 
		    else 
		    return 1 
		    end 

		end 

		function update() 

		       Vertex = {} 
		       Objects = {} 
		       screen:invalidate() 
		end 

		function build() 

		   Vertex = {} 
		   for k = 1, Hvar do 
		       vx[k] = Radius*(math.cos(2*math.pi*(k - 1)/Hvar)) + W/2 
		       vy[k] = Radius*math.sin(2*math.pi*(k - 1)/Hvar) + H/2 

		       Vertex[k] = CircleB(vx[k], vy[k], Wide, Wide)    
		       table.insert(Objects, Vertex[k]) 

		   end 
		          screen:invalidate() 
		end 

		---------------------------------------------------------- 

		function on.paint(gc) 

		    H=screen:height() 
		    W=screen:width()    
		    h=screen:height() 
		    w=screen:width()    

		        gc:setColorRGB(240, 240, 240) 
		        gc:fillRect(0,0,w,h) 


		    if Grab == 1 then 
		        update() 
		        build() 
		       end 


		    for _, obj in ipairs(Objects) do 
		     obj:paint(gc) 
		    end 



		---------------------------------------------------------- 
		    if Vvar > 0  then 

		        for p = 1, Hvar do 
		            for m = 1, 2 do 
		            if #Vertex >= m+3*(p-1) + 1 then 

		            gc:drawLine(Vertex[m+3*(p-1)].x, Vertex[m+3*(p-1)].y, Vertex[m+3*(p-1) + 1].x, Vertex[m+3*(p-1) + 1].y) 
		          end 
		      end 
		    end 

		        for p = 1, Hvar do 
		        for m = 1, 3 do 
		            for k = 1, Vvar do 

		           if #Vertex >= m+3*(p-1) + 2 then 

		           gc:drawLine(Bezier(Vertex[1+3*(p-1)].x, Vertex[1+3*(p-1) + 1].x, k/Vvar), 
		                       Bezier(Vertex[1+3*(p-1)].y, Vertex[1+3*(p-1) + 1].y, k/Vvar), 
		                       Bezier(Vertex[1+3*(p-1) + 1].x, Vertex[1+3*(p-1) + 2].x, k/Vvar), 
		                       Bezier(Vertex[1+3*(p-1) + 1].y, Vertex[1+3*(p-1) + 2].y, k/Vvar)) 
		                           screen:invalidate() 
		           end 
		          end 
		        end 
		        end 
		    end 

		    local str = "Set up your string art then" 
		    local sw = gc:getStringWidth(str) 
		    gc:drawString(str, w - 1.1*sw, 0.1*h, "middle") 
		    local str = "click 'Build' to change to 'Drag' mode" 
		    local sw = gc:getStringWidth(str) 
		    gc:drawString(str, w - 1.1*sw, 0.2*h, "middle") 

		    hslider:paint(gc) 
		    vslider:paint(gc) 
		    resetbutton:paint(gc) 
		    typebutton:paint(gc) 

		    screen:invalidate() 
		end 



		------------------------------------------------------------------------------------- 



		-------------------------------------------------------------------------------------

	
	


platform.apilevel = 1.0
-- Lua Grid
-- Steve Arnold February 2014/Revised Jan 2015
-- steve.arnold@ti.com

-- Section 1 (Lines 30 - ): Initial values, resize and paint  

-- Section 2: 400: SetGraph function
         --   475: Grid class
         --   720: Conversion functions
         --   800: Plot Class
         
-- Section 3: 925: checkBox class
         --   1000: Clicker class
         --   1220: Button class
         
-- Section 4: 1550. KeyBoard and mouse Controls

-- Section 5: 1800: CAS functions

-- Section 6: 3140. Presentation mode and menus


--------------------------------------------------------------------------- 
    local screen = platform.window
    local w = screen:width()
    local h = screen:height()
    
    local gridX
    local gridY
    local gridWidth
    local gridHeight
    local textHeight
    local textWidth
    local xMin
    local xMax
    local yMin
    local yMax
    local xTick
    local yTick

    local fontSize = 12

    local spacer
    local num = 0
    local edit_mode = 0
    local fstr1 = ''
    local funcList = {}
    local brackets = 0
    local square = 2
    local dragAxes = 0
    local initX = 0
    local initY = 0
    local second = 0
    local alpha = 0
    local impFn = ""
    local image_switch
    local casTest = 1
    local view = 1
    local gtext = "Graph View: Square (x)"
    
    local m
    local n

    local helpBox = {}
    local help = 0
    local helpList = {
            
            "___________________________________"
            .."\nTI-Nspire™ Lua Grid"
            .."\n___________________________________"
            .."\n©2013 Texas Instruments"
           .."\n\nPage 1 of 2: Tap the '?' button or press the tabKey for more. Tap 'reset' or escapeKey at any time to exit. More? ▶", 
             
            "Use this template for your grids - use as a simple grid, or as an alternative graphing window. \n"
            .."Set the dimensions and style of your grid within the script.\n"
            
            .."Use the menu for some sample curves, or plot your own functions using f1(x) from a Graphs window, as shown here.\n"
            
            .."\n\Page 2 of 2: Tap '?', 'clear' or press the escapeKey to exit. ∎", 

}
 

    local Colors = {white = 0xffffff, 
                    blue = 0x0000ff,
                    dodgerblue = 0x1E90FF,
                    navy = 0x000080,
                    red = 0xff0000, 
                    green = 0x00ff00, 
                    black = 0x000000, 
                    brown = 0xA52A2A, 
                    orange = ffa500, 
                    yellow = 0xffff00, 
                    magenta = 0xff00ff,
                    gray = 0x808080,                    
                    lightgray = 0xD3D3D3}
                    
    local color = Colors

    local myColor = {0, 0, 255}

	plotColors = {
		color.blue,
		color.red,
		color.green,
		color.brown,
		color.orange,
		color.yellow,
		color.magenta,
        color.lightgray,
        color.gray,
		color.black,
	}


function sin(x)
    if x then return math.sin(x) end
end

function cos(x)
    if x then return math.cos(x) end
end

function tan(x)
    if x then return math.tan(x) end
end

function exp(x)
    if x then return math.exp(x) end
end


function log(x)
    if x then return math.log10(x) end
end


function ln(x)
    if x then return math.log(x) end
end


function sqrt(x)
    if x then return math.sqrt(x) end
end



function on.construction()
    w = screen:width() or 318
    h = screen:height() or 212
    
    on.resize()
    screen:invalidate()
end

function on.activate()

    screen:invalidate()
end


function graphView(square)
        
        if square == 1 then
            gtext = "Graph View Square (y)"   
        elseif square == 2 then
            gtext = "Graph View Square (x)"    
        else
            gtext = "Graph View Standard"               
        end
            edit_button.label = gtext 
            setGraph(square)
            on.escapeKey()
            screen:invalidate()
    
end

function on.resize()

    w = screen:width() or 318
    h = screen:height() or 212

        if not w or w <= 0 then w = 318 end
        if not h or h <=0 then h = 212 end


        gridX = 0.025*w or 50
        gridY = 0.05*h or 20
        gridWidth = 0.95*w or 318 
        gridHeight = 0.85*h or 212
        brackets = 0
        implicitPlot = 0     
        spacer = 0.025*w
     
        hh = 0

     help = 0
     
     edit_mode = 0
    
    reset_button = Button(0.025*w, 0.91*h, w*0.47, 0.08*h, '', myColor, "reset", false, true)
    edit_button = Button(0.504*w, 0.91*h, w*0.47, 0.08*h, '', myColor, "graph settings", false, true)

    xMin = -5
    xMax = 10
    yMin = -3
    yMax = 6
    xTick = 1
    yTick = 1

      numx = math.floor((xMax - xMin)/xTick)    
      numy = math.floor((yMax - yMin)/yTick)
      unitx = gridWidth/(numx)
      unity = gridHeight/(numy)

    setGraph(square)
    
    myGrid = Grid(gridX, gridY, gridWidth, gridHeight, 
                xMin, xMax, yMin, yMax, xTick, yTick, 
                1, "x", "y", square)
                
                --x, y, width, height,
                --xMin, xMax, yMin, yMax, xTick, yTick, 
                --text, xtext, ytext, square

    fstr1 = 'x^2-2*x'
    m = math.random(1, 3)
    n = math.random(-5, 5)
    
    local tally = 0
    
    if fstr1 and fstr1 ~= '' then
        
             funcList =  {fstr1}
    else
        funcList = {}
    end

    Curve1 = Plot(fstr1)

    xmin_Clicker = Clicker(gridX, gridY + gridHeight - math.abs(yMin)*unity, 0.04*w, 0.04*w, 
                "xMin = "..xMin, "", -5, 1, -10, 10, 0.11*w, 0) 

    xmax_Clicker = Clicker(w*0.9 - 0.2*w, gridY + gridHeight - math.abs(yMin)*unity, 0.04*w, 0.04*w, 
                "xMax = "..xMax, "", 5, 1, -10, 10, 0.11*w, 0) 
                
    ymin_Clicker = Clicker(gridX + math.abs(xMin)*unitx, gridY - 0.25*h + gridHeight, 0.05*w, 0.05*w, 
                "yMin = "..yMin, "", -6, 1, -10, 10, 0.11*w, 1) 

    ymax_Clicker = Clicker(gridX + math.abs(xMin)*unitx, 0.2*h, 0.05*w, 0.05*w, 
                "yMax = "..yMax, "", 6, 1, -10, 10, 0.11*w, 1) 


    xtick_Clicker = Clicker(gridX  + math.abs(xMin - 1)*unitx, gridY + gridHeight - math.abs(yMin)*unity, 0.03*w, 0.03*w, 
                "xTick="..xTick, "", 1, 0.25, 0.25, 5, 0.11*w, 0) 

    ytick_Clicker = Clicker(gridX + math.abs(xMin)*unitx, gridY + gridHeight - math.abs(yMin - 2)*unity, 0.05*w, 0.05*w, 
                "yTick="..yTick, "", 1, 0.25, 0.25, 5, 0.11*w, 1) 

        --(x, y, width, height, 
            --label, units, initial_value, step, minimum, maximum, space, orient)
    
    screen:invalidate()

end

--------------------------------------------------------------------------- 

function on.paint(gc)

    w = screen:width()
    h = screen:height()
    
    gc:setColorRGB(200, 200, 200)
    gc:fillRect(0,0,w,h)
   
    gc:setColorRGB(unpack(myColor))

        reset_button:paint(gc)
        edit_button:paint(gc)
 
        myGrid:paint(gc)
        

    if edit_mode ~= 0 then
        xtick_Clicker:paint(gc)
        ytick_Clicker:paint(gc)
        xmin_Clicker:paint(gc)
        xmax_Clicker:paint(gc)
        ymin_Clicker:paint(gc)
        ymax_Clicker:paint(gc)
   end

           if fstr1 then
              Curve1:paint(gc)
           end
   
    local str = "Simple Grid with random graphs"
    local sw = gc:getStringWidth(str)
    gc:drawString(str, gridX + 0.975*gridWidth - sw, 0.015*h)

end










--------------------------------------------------------------------------- 

function setGraph()

    numx = math.floor((xMax - xMin)/xTick)    
    numy = math.floor((yMax - yMin)/yTick)
    unitx = gridWidth/(numx)
    unity = gridHeight/(numy)
    
    if unitx < 0.05*w then
        xTick = 2*xTick
    end
    if unity < 0.05*h then
        yTick = 2*yTick
    end
    numx = math.floor((xMax - xMin)/xTick)    
    numy = math.floor((yMax - yMin)/yTick)
    unitx = gridWidth/(numx)
    unity = gridHeight/(numy)

    originX = gridX + gridWidth*(math.abs(xMin)/(numx*xTick))
    
    if showkeys == 1 and square == 2 then
        originY = gridY + gridHeight*(1 - (math.abs(yMin + 1)/(numy*yTick)))
    
    else
        originY = gridY + gridHeight*((math.abs(yMax)/(numy*yTick)))
    end

    if square == 1 then
        numx = math.floor(gridWidth/unity + 0.5) + 1        
        unitx = unity       
        xMin = xTick*(-math.floor((originX - gridX)/unitx + 0.5) - 1)
        xMax = xTick*numx - xMin
        
    elseif square == 2 then
    
        numy = math.floor(gridHeight/unitx + 0.5) + 1        
        unity = unitx 
             
        yMin = yTick*(-math.floor((gridY + gridHeight - originY)/unity + 0.5) - 1)
        yMax = yTick*numy - yMin

    end

        if myGrid then            
            myGrid.xMin = xMin
            myGrid.xMax = xMax
            myGrid.yMin = yMin
            myGrid.yMax = yMax
            myGrid.xTick = xTick
            myGrid.yTick = yTick

         end
         
         if xmin_Clicker then
             xmin_Clicker.value = xMin
             xmax_Clicker.value = xMax
             ymin_Clicker.value = yMin
             ymax_Clicker.value = yMax
             xtick_Clicker.value = xTick
             ytick_Clicker.value = yTick
             xmin_Clicker.label = "xMin = "..xMin
             xmax_Clicker.label = "xMax = "..xMax
             ymin_Clicker.label = "yMin = "..yMin
             ymax_Clicker.label = "yMax = "..yMax
             xtick_Clicker.label = "xTick = "..xTick
             ytick_Clicker.label = "yTick = "..yTick
         end
       
end




--------------------------------------------------------------------------- 

-- The Grid


Grid = class()

function round(input)
    return math.floor(input+0.5)
end



function Grid:init(x, y, width, height, xMin, xMax, yMin, yMax, xTick, yTick, text, xtext, ytext, square)

    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.xMin = xMin
    self.xMax = xMax
    self.yMin = yMin
    self.yMax = yMax
    self.xTick = xTick
    self.yTick = yTick
    self.text = text
    self.xtext = xtext
    self.ytext = ytext
    self.square = square

end

  
function Grid:contains(x, y)

    local sw = self.width
    local sh = self.height
    return x >= self.x and x <= self.x + sw and
        y >= self.y and y <= self.y + sh

end

function Grid:paint(gc)

    w = screen:width()
    h = screen:height()
    local x = self.x
    local y = self.y
    local width = self.width
    local height = self.height
    local xMin = self.xMin
    local xMax = self.xMax
    local yMin = self.yMin
    local yMax = self.yMax
    local xTick = self.xTick
    local yTick = self.yTick
    local text = self.text
    local square = self.square
   
    gc:setColorRGB(255, 255, 255)
    gc:fillRect(x, y, width, height)
    gc:setColorRGB(150, 150, 150)    
--    gc:setPen("thin", "smooth")
    gc:drawRect(x, y, width, height)
    
   
         
 --       gc:setFont("sansserif", "r", fontSize)

-- Vertical lines
                    gc:setColorRGB(100, 100, 100)

        for k = 0, xMax + xTick, xTick do
        
            if originX + (k)*unitx/xTick > self.x and originX + (k)*unitx/xTick <= self.x + self.width and (k % 1) == 0 then
            gc:drawLine(originX + (k)*unitx/xTick, y, 
                        originX + (k)*unitx/xTick, y + height)
            end
                local sw = gc:getStringWidth(k)
            
            if text == 1 and  originX + (k)*unitx/xTick - sw/2 > self.x and originX + (k)*unitx/xTick - sw/2 <= self.x + self.width then

                    local sw = gc:getStringWidth(k)
                    if (k % 1) == 0 then
                        gc:setColorRGB(100, 100, 100)
                        local sw = gc:getStringWidth(k)
                        gc:drawString(k, originX + (k)*unitx/xTick - sw/2, originY, "top")
                    end
            end
            
        end
        gc:setColorRGB(100, 100, 100)
 --       gc:setFont("sansserif", "r", fontSize)

        for k = 0, xMin, -xTick do

            if originX + (k)*unitx/xTick >= gridX and originX + (k)*unitx/xTick > self.x and originX + (k)*unitx/xTick <= self.x + self.width then
            gc:drawLine(originX + (k)*unitx/xTick, y, 
                        originX + (k)*unitx/xTick, y + height)
            end
                local sw = gc:getStringWidth(k)
            
            if text == 1  and originX + (k)*unitx/xTick - sw/2 > self.x and originX + (k)*unitx/xTick - sw/2 <= self.x + self.width and (k % 1) == 0 and k ~= 0 then
                    gc:setColorRGB(100, 100, 100)
                    gc:drawString(k, originX + (k)*unitx/xTick - sw/2, originY, "top")
            end
        end

-- Horizontal lines

        for k = 0, yMax + yTick, yTick do
            local sw = gc:getStringWidth(k)
            local sh = gc:getStringHeight(k)

            if originY - (k)*unity/yTick >= self.y and originY - (k)*unity/yTick <= self.y + self.height then
            gc:drawLine(x, originY - (k)*unity/yTick, 
                        x + width, originY - (k)*unity/yTick)
            end
            
            if text == 1  and originY - (k)*unity/yTick >= self.y and originY - (k)*unity/yTick <= self.y + self.height  and k ~= 0 then
           
                    gc:setColorRGB(100, 100, 100)
                    local sw = gc:getStringWidth(k)
                gc:drawString(k, originX - 0.015*w - sw, originY - (k)*unity/yTick, "middle")
            end
        end

        for k = 0, yMin, -yTick do

            local sw = gc:getStringWidth(k)
            local sh = gc:getStringHeight(k)

            if originY - (k)*unity/yTick >= 0 and originY - (k)*unity/yTick >= self.y and originY - (k)*unity/yTick <= self.y + self.height then
            gc:drawLine(x, originY - (k)*unity/yTick, 
                        x + width, originY - (k)*unity/yTick)
            end
            if text == 1  and originY - (k)*unity/yTick >= self.y and originY - (k)*unity/yTick <= self.y + self.height and k ~= 0 then
                gc:setColorRGB(100, 100, 100)
                local sw = gc:getStringWidth(k)
                gc:drawString(k, originX - 0.015*w - sw, originY - (k)*unity/yTick, "middle")
            end
        end

    
            
    gc:setPen("thin", "smooth")
    gc:setColorRGB(100, 100, 100)
    gc:drawLine(originX, y, originX, y + height)
    gc:drawLine(x, originY, x + width, originY)
    
    local arrowSize = 0.03*h
    
    gc:fillPolygon({x + width, originY,
                    x + width - arrowSize, originY - arrowSize/2,
                    x + width - arrowSize, originY + arrowSize/2,
                    x + width, originY})

    gc:fillPolygon({x, originY,
                    x + arrowSize, originY - arrowSize/2,
                    x + arrowSize, originY + arrowSize/2,
                    x, originY})

    gc:fillPolygon({originX, y,
                    originX - arrowSize/2, y + arrowSize,
                    originX + arrowSize/2, y + arrowSize,
                    originX, y})

    gc:fillPolygon({originX, y + height,
                    originX - arrowSize/2, y + height - arrowSize,
                    originX + arrowSize/2, y + height - arrowSize,
                    originX, y + height})

    if text == 1 then
    
        
 --       gc:setFont("serif", "i", fontSize)
        
        local str = self.xtext or "x"
        local sw = gc:getStringWidth(str)
        local sh = gc:getStringHeight(str)
        gc:drawString(str, x + width - 1.1*sw, originY, "bottom")
    
        local str = self.ytext or "y"
        local sw = gc:getStringWidth(str)
        gc:drawString(str, originX + 0.02*w, y, "top")

    end

end


function Grid:mouseDown(x, y)

        if self:contains(x, y) then
            dragAxes = 1
            initX = x
            initY = y
            initxMin = xMin
            initxMax = xMax
            inityMin = yMin
            inityMax = yMax
        else
            dragAxes = 0
        end

    screen:invalidate()
end


function Grid:mouseUp(x, y)

    dragAxes = 0
    screen:invalidate()

end

function Grid:mouseMove(x, y)
        if dragAxes == 1 then

                  xMin = math.floor(initxMin - (unconvertx(x) - unconvertx(initX)) + 0.5)
                  xMax = math.floor(initxMax - (unconvertx(x) - unconvertx(initX)) + 0.5)
                  yMin = math.floor(inityMin - (unconverty(y) - unconverty(initY)) + 0.5)
                  yMax = math.floor(inityMax - (unconverty(y) - unconverty(initY)) + 0.5)
                
                  setGraph(square)
                  
        end
    screen:invalidate()
end

--------------------------------------------------------------------------- 
--Conversion functions

function convertx(x)

    w = screen:width()
    h = screen:height()
    
    if x then
    local width = gridWidth
    local height = gridHeight
    if tonumber(x) then
        return x*unitx/xTick + originX or 0
    end
    end

end

function converty(y)

    w = screen:width()
    h = screen:height()

    if y then
    local width = gridWidth
    local height = gridHeight
    if tonumber(y) then
          return originY - y*unity/yTick
    end
    end
end

function unconvertx(x)

    w = screen:width()
    h = screen:height()
    
    if x then    
    local width = gridWidth
    local height = gridHeight or h
    if tonumber(x) then
        return math.floor(10*xTick*(x - originX)/unitx + 0.5)/10 or 0
    end
    end
end

function unconverty(y)

    w = screen:width()
    h = screen:height()
   
    if y then    
    local width = gridWidth
    local height = gridHeight
    if tonumber(y) then
        return math.floor(10*yTick*(originY - y)/unity + 0.5)/10 or 0
    end    
    end
end

--------------------------------------------------------------------------- 
-- The Plot Class

Plot = class()

function Plot:init(func)

    self.func = func
    self.square = square
    self.plotColor = unpack(myColor)
    
end




function Plot:paint(gc)

    w = screen:width()
    h = screen:height()
    
    local func = self.func or ''
   
    local fn = func
    local fnList

    if func and func ~= '' then
    local y1
    local y2
    
    local precision = 150
    if hh and hh == 1 then precision = 5 end
    
    local step = xTick/precision
    
    if not m then m = 1 end
    if not n then n = 2 end
        
        for k = xMin, xMax - step, step do
       
        if math.abs(k) <= step/2 then k = 0.01 end
            if m == 1 then
                gc:setColorRGB(0, 0, 200)
                local str = "x² - "..m.." x"
                local sw = gc:getStringWidth("y = "..str)
                gc:drawString("y = "..str, gridX + 0.975*gridWidth - sw, 0.1*h + (m - 1)*0.05*h)
                 y1 = k^2- m*k
                 y2 = (k+step)^2-m*(k+step)
             
            elseif m == 2 then 
                gc:setColorRGB(0, 200, 0)
                local str = n.." x - "..m
                local sw = gc:getStringWidth("y = "..str)
                gc:drawString("y = "..str, gridX + 0.975*gridWidth - sw, 0.1*h + (m - 1)*0.05*h)
                 y1 = n*k - m
                 y2 = n*(k+step) - m
             elseif m == 3 then 
                 gc:setColorRGB(200, 0, 0)
                 local str = n.." sin(x) + "..m
                 local sw = gc:getStringWidth("y = "..str)
                 gc:drawString("y = "..str, gridX + 0.975*gridWidth - sw, 0.1*h + (m - 1)*0.05*h)
                  y1 = n*sin(k) + m
                  y2 = n*sin(k+step) + m
            end

         if y1 and y2 then
            local ax = convertx(k)
            local ay = converty(y1)
            local bx = convertx(k+step)
            local by = converty(y2)
          
            if ax and ay and bx and by then
            if ax >= gridX and bx >= gridX and ax <= gridX + gridWidth and bx <= gridX + gridWidth and ay >= gridY and by >= gridY and ay <= gridY + gridHeight and by <= gridY + gridHeight and math.abs(y1 - y2) <= unitx   then
                gc:drawLine(ax, ay, bx, by) 
            end
            end
        end
    end
    end

end


--------------------------------------------------------------------------- 
-- The Clicker

Clicker = class()

function Clicker:init(x, y, width, height, label, 
                units, initial_value, step, minimum, maximum, space, orient)
--(x, y, width, height, label, units, initial_value, step, minimum, maximum, space)
	self.x=x
	self.y=y
	self.width = width
	self.height = height
	self.label = label
	self.units = units
	self.space = space
	self.max_selected = false
	self.min_selected = false
	self.clicker_value= initial_value
	self.min = minimum
	self.max = maximum
	self.step=step
	self.textColor = unpack({0,0,0})
	self.color = unpack({250,250,250})
    self.offset = self.space*0.8
    self.strw = 0.2*w
    self.orient = orient


end

  
  
function Clicker:paint(gc)
    local sw = self.width
    local sh = self.height
    local spacer = 0.025*w
    local x = self.x
    
 --   gc:setFont("sansserif", "b", fontSize)
    gc:setColorRGB(self.textColor)
    
    local vertices_min
    local vertices_max
    
    gc:setPen("thin","dotted")
    
    if self.orient == 0 then

    self.strw = gc:getStringWidth(self.label)
    gc:drawString(self.label, x + sw + self.space/2 - self.strw/2, self.y - self.space/10, "bottom")
--    gc:drawString(self.label, x + self.width + 1.4*self.space + spacer, self.y, "middle")

     vertices_min = {x, self.y,
                            x + self.width, self.y - self.height/2,
                            x + self.width, self.y + self.height/2,
                            x, self.y}

     vertices_max = {x + self.width/2 + self.space + spacer, self.y - self.height/2,
                            x + self.width/2 + self.space + spacer, self.y + self.height/2,
                            x + self.width/2 + self.space + self.width + spacer, self.y,
                            x + self.width/2 + self.space + spacer, self.y - self.height/2}
--    gc:drawLine(x + self.width, self.y, x + self.width/2 + self.space + spacer, self.y)
    
    else

    self.strw = gc:getStringWidth(self.label)
    gc:drawString(self.label, x + self.space/4, self.y + spacer/2, "middle")

     vertices_max = {x - self.width/2, self.y - 0.01*h,
                            x, self.y - self.height/2 - 0.01*h,
                            x + self.width/2, self.y - 0.01*h,
                            x - self.width/2, self.y - 0.01*h}

     vertices_min = {x - self.width/2, self.y - 0.01*h + 1.75*spacer,
                            x, self.y + self.height/2 - 0.01*h + 1.75*spacer,
                            x + self.width/2, self.y - 0.01*h + 1.75*spacer,
                            x - self.width/2, self.y - 0.01*h + 1.75*spacer}
--    gc:drawLine(x, self.y - 0.01*h, x, self.y + 1.75*spacer) 
    end


    gc:setPen("thin","smooth")
    gc:setColorRGB(250, 250, 250)
    gc:fillPolygon(vertices_min)
    gc:fillPolygon(vertices_max)    
    
    gc:setColorRGB(80, 80, 80)
    gc:drawPolyLine(vertices_min)
    gc:drawPolyLine(vertices_max)    

               
    if self.min_selected then
    
        gc:setColorRGB(unpack(myColor))
        gc:fillPolygon(vertices_min)
        
        gc:setColorRGB(50, 50, 50)
        gc:drawPolyLine(vertices_min)
    end  
    
    if self.max_selected then
    
        gc:setColorRGB(unpack(myColor))
        gc:fillPolygon(vertices_max)    
        
        gc:setColorRGB(50, 50, 50)
        gc:drawPolyLine(vertices_max)    
    
    end
end

  
  
function Clicker:contains(x, y)

    local sw = self.width
    local sh = self.height
    local spacer = 0.025*w

    if self.orient == 0 then
    
    return x >= self.x and x <= self.x + 2*sw + self.space  and
        y >= self.y - sh*0.6 and y <= self.y + sh*0.6
        
    else
        return x >= self.x - sw/2 and x <= self.x + sw/2 and
        y >= self.y - sh/2 and y <= self.y + 1.5*spacer + sh/2
    
    end
end


function Clicker:mouseDown(x, y)

    if self:contains(x, y) then
    
    if self.orient == 1 then
    
        local spacer = 0.025*w

        if y > self.y + spacer/2 + self.height/2 and y < self.y + 0.5*self.height + 1.5*spacer  then
            self.min_selected = true
        elseif y > self.y - self.height/2 and y < self.y + self.height/2  then
            self.max_selected = true
        end
    
    
    else
        if x > self.x and x < self.x + self.width  then
                self.min_selected = true
                self.max_selected = false
        elseif x > self.x + self.space + self.width and x < self.x + 2*self.width + self.space  then
                self.max_selected = true
                self.min_selected = false
        end
    
    end
    end

    screen:invalidate()
end


function Clicker:mouseUp(x, y)
    
    if self.max_selected == true then
        if self.clicker_value < self.max then
            self.clicker_value = self.clicker_value + self.step
            
        end
            self.max_selected = false
     end
     
     if self.min_selected == true then
         if self.clicker_value > self.min then
            self.clicker_value = self.clicker_value - self.step
         end
            self.min_selected = false

    end

    if self.orient == 0 then
        
        if self == xtick_Clicker then
            xTick = self.clicker_value
            xtick_Clicker.label = "xTick = "..xTick
        elseif self == xmin_Clicker then
            xMin = self.clicker_value or -5
            xmin_Clicker.label = "xMin = "..xMin
        elseif self == xmax_Clicker then
            xMax = self.clicker_value or 5
            xmax_Clicker.label = "xMax = "..xMax
        end

    else
    
        if self == ymin_Clicker then
            yMin = self.clicker_value or -6
            ymin_Clicker.label = "yMin = "..yMin
        elseif self == ytick_Clicker then
            yTick = self.clicker_value
            ytick_Clicker.label = "yTick = "..yTick
         elseif self == ymax_Clicker then
            yMax = self.clicker_value or 6
             ymax_Clicker.label = "yMax = "..yMax
        end
    
    end   
        setGraph(square)
        screen:invalidate()
end







-------------------------------------------------------------------------- 
-- The Button

Button = class()

function round(input)
    return math.floor(input+0.5)
end


function Button:init(x, y, width, height, image, mycolor, label, selected, visible)
    self.x = x
    self.y = y
    self.width = width
    self.height= height
    self.color = mycolor
    self.label = label
    self.image = image
    self.selected = false
    self.visible = visible
end

function Button:contains(x, y)

    local sw = self.width or self.height
    local sh = self.height or self.width
    return x >= self.x and x <= self.x + sw and
       y >= self.y and y <= self.y + sh
end

function Button:paint(gc)
    local x = self.x
    local y = self.y
    local width = self.width
    local height = self.height
    local label = self.label
    local im1 = self.image
    local mycolor = self.color
    local curve = 0.1*height
    local vertices = {round(x + curve), round(y),
                          round(x + width - curve), round(y),
                          round(x + width), round(y + curve),
                          round(x + width), round(y + height - curve),
                          round(x + width - curve), round(y + height),
                          round(x + curve), round(y + height),
                          round(x), round(y + height - curve),
                          round(x), round(y + curve),
                          round(x + curve), round(y),}
    
    
    gc:setColorRGB(unpack(myColor))

-- Define image or graphic

             
        gc:fillPolygon(vertices)                 
                          
        gc:fillArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
        gc:fillArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
        gc:fillArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
        gc:fillArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
        gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
        gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
        gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
        gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
        
        gc:drawPolyLine(vertices)
        
        gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
        gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
        gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
        gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)

        gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
        gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
        gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
        gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
        
        gc:drawArc(round(x), round(y), round(2*curve), round(2*curve), 90, 90)
        gc:drawArc(round(x + width - 2*curve), round(y), round(2*curve), round(2*curve), 0, 90)
        gc:drawArc(round(x + width - 2*curve), round(y + height - 2*curve), round(2*curve), round(2*curve), 270, 90)
        gc:drawArc(round(x), round(y + height - 2*curve), round(2*curve), round(2*curve), 180, 90)
        gc:drawLine(round(x + curve), round(y), round(x + width - curve), round(y))
        gc:drawLine(round(x + width), round(y + curve), round(x + width), round(y - curve + height))
        gc:drawLine(round(x + width - curve), round(y + math.floor(height)), round(x + curve), round(y + math.floor(height)))
        gc:drawLine(round(x), round(y + height - curve), round(x), round(y + curve))


    
-- Label

--	gc:setFont("sansserif","b",fontSize)
	
	if self.selected then
	
	    gc:setColorRGB(200, 0, 0)
	
	else
	    gc:setColorRGB(255, 255, 255)
	end
	
	local sw = gc:getStringWidth(label)
    local sh = gc:getStringHeight(label)
    gc:drawString(label, round(x + width/2 - sw/2), round(y + height*0.4), "middle")

end


-------------------------------------------------------------------------- 

function on.escapeKey()

    on.resize()

    screen:invalidate()

end

function resetall()

     edit_mode = 0
     funcList = {}
     on.resize()
end

--------------------------------------------------------------------------- 


function on.mouseDown(x, y)


    w, h = screen:width(), screen:height()

    local test = nil
    if test then casTest = 1 else casTest = 0 end

    if xmin_Clicker:contains(x, y) then
        xmin_Clicker:mouseDown(x, y)
    end

    if ymin_Clicker:contains(x, y) then
        ymin_Clicker:mouseDown(x, y)
    end

    if xmax_Clicker:contains(x, y) then
        xmax_Clicker:mouseDown(x, y)
    end

    if ymax_Clicker:contains(x, y) then
        ymax_Clicker:mouseDown(x, y)
    end

    if xtick_Clicker:contains(x, y) then
        xtick_Clicker:mouseDown(x, y)
    end

    if ytick_Clicker:contains(x, y) then
        ytick_Clicker:mouseDown(x, y)
    end

    if myGrid:contains(x, y) then
        myGrid:mouseDown(x, y)
    end


        if reset_button:contains(x, y) then            
           reset_button.selected = true
        end

        if edit_button:contains(x, y) then            
           edit_button.selected = true
        end
        

    screen:invalidate()
end

function on.mouseUp(x, y)

    w, h = screen:width(), screen:height()

    if xmin_Clicker:contains(x, y) then
        xmin_Clicker:mouseUp(x, y)
    end

    if ymin_Clicker:contains(x, y) then
        ymin_Clicker:mouseUp(x, y)
    end

    if xmax_Clicker:contains(x, y) then
        xmax_Clicker:mouseUp(x, y)
    end

    if ymax_Clicker:contains(x, y) then
        ymax_Clicker:mouseUp(x, y)
    end

    if xtick_Clicker:contains(x, y) then
        xtick_Clicker:mouseUp(x, y)
    end

    if ytick_Clicker:contains(x, y) then
        ytick_Clicker:mouseUp(x, y)
    end

    if myGrid:contains(x, y) then
        myGrid:mouseUp(x, y)
    end


    if edit_button.selected then           
        if x > edit_button.x and x < edit_button.x + edit_button.width/10 then
            edit_mode = 0
            
            if square == 0 then
                square = 1
                edit_button.label = "GraphView Square (y)"
            elseif square == 1 then
                square = 2
                edit_button.label = "GraphView Square (x)"
            else
                square = 0
                edit_button.label = "GraphView Standard"
            end
            gtext = edit_button.label
            setGraph(square)
        else
            edit_mode = 1 - edit_mode
            if edit_mode == 1 then square = 0 setGraph(square) end
        end
             edit_button.selected = false
   end
        
   if reset_button.selected then
                if x > reset_button.x and x < reset_button.x + reset_button.width/10 then
                    resetall()
                else
                    on.escapeKey()

                end
                reset_button.selected = false
    end


    screen:invalidate()
end


function on.mouseMove(x, y)
    
    if myGrid:contains(x, y) then
        myGrid:mouseMove(x, y)
    end
    
    if y > 0.9*h or help == 1 then
        edit_button.visible = true
         reset_button.visible = true
       if x > reset_button.x and x < reset_button.x + reset_button.width/10 then
            reset_button.color = color.green
            reset_button.label = "reset all"            
        else
            reset_button.color = unpack(myColor)
            reset_button.label = "reset"
        
        end
        if x > edit_button.x and x < edit_button.x + edit_button.width/10 then
            edit_button.color = color.green
            if square == 1 then
                edit_button.label = "GraphView Square (y)"
            elseif square == 2 then
                edit_button.label = "GraphView Square (x)"
            else
                edit_button.label = "GraphView Standard"
            end
        else
            edit_button.color = unpack(myColor)
            edit_button.label = "graph settings"
            xMin_init = xMin
            xMax_init = xMax
            yMin_init = yMin
            yMax_init = yMax
            xTick_init = xTick
            yTick_init= yTick
        
        end    
    else
           edit_button.visible = true
           reset_button.visible = true
    end
 
    screen:invalidate()

end