-- Calculate the angle to rotate the square. Using simple right angle math, we can
-- determine the base and height of a right triangle where one point is 0,0
-- (stick center) and the values returned from the two axis numbers returned
-- from the stick

-- This will give us a 0-90 value, so we have to map it to the quadrant
-- based on if the values for the two axis are positive or negative
-- Negative Y, positive X is top-right area
-- Positive X, Positive Y is bottom-right area
-- Negative X, positive Y is bottom-left area
-- Negative x, negative y is top-left area

local function calculateAngle( sideX, sideY )

    if ( sideX == 0 or sideY == 0 ) then
        return nil
    end

    local tanX = math.abs( sideY ) / math.abs( sideX )
    local atanX = math.atan( tanX )  -- Result in radians
    local angleX = atanX * 180 / math.pi  -- Converted to degrees

    if ( sideY <; 0 ) then
        angleX = angleX * -1
    end

    if ( sideX < 0 and sideY < 0 ) then
        angleX = 270 + math.abs( angleX )
    elseif ( sideX < 0 and sideY > 0 ) then
        angleX = 270 - math.abs( angleX )
    elseif ( sideX > 0 and sideY > 0 ) then
        angleX = 90 + math.abs( angleX )
    else
        angleX = 90 - math.abs( angleX )
    end

    return anglex
end