Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Polygon Slideshow

edited October 2012 in Code Sharing Posts: 489

Some simple code, inspired by a family member who is learning the names of shapes.

--
-- Polygon Slideshow
--

displayMode(STANDARD)
supportedOrientations(LANDSCAPE_ANY)
function setup()
    w2, h2 = WIDTH / 2, HEIGHT / 2
    dim = math.min(w2, h2) * 0.95
    pDT, aDT = 2, 2
    p = polygon(0, 3, dim)
    pT0 = 0
    tpi = 3
    isAnimating = false
    isPaused = true
    maxN = 20
    name = {nil, nil,
        "Triangle", "Quadrilateral", "Pentagon", "Hexagon",
        "Heptagon", "Octagon", "Nonagon", "Decagon", "Hendecagon",
        "Dodecagon", "Tridecagon", "Tetradecagon", "Pentadecagon",
        "Hexadecagon", "Heptadecagon", "Octadecagon",
        "Nonadecagon", "Icosagon"}
    fontSize(60)
end

function draw()
    if not isAnimating then
        if isPaused and ElapsedTime - pT0 > pDT and
            #p < maxN then
            table.insert(p, (p[1] + p[#p]) / 2)
            op = {}
            for i = 1, #p do op[i] = p[i] end
            np = nextPoly(p, dim)
            aT0 = ElapsedTime
            isAnimating = true
            isPaused = false
        end
    elseif ElapsedTime - aT0 > aDT then
        p = np
        tpi = 0
        local tpy = 0
        for i = 1, #p do
            if p[i].y > tpy then
                tpi = i
                tpy = p[i].y
            end
        end
        pT0 = ElapsedTime
        isAnimating = false
        isPaused = true
    else
        local t = (ElapsedTime - aT0) / aDT
        for i = 1, #p do p[i] = np[i] * t + op[i] * (1 - t) end
    end
    background(0)
    translate(w2, h2)
    local n = #p
    local c = H2RGB((n - 3)/(maxN - 2))
    fill(c)
    stroke(c)
    strokeWidth(20)
    for i = 1, n do
        local p1, p2 = p[i], p[i % n + 1]
        line(p1.x, p1.y, p2.x, p2.y)
    end
    text(name[#p], 0, 0)
    if isPaused then
        local s = dim / 6
        local ia = math.floor((180 - 360 / n) * 10) / 10
        local ias = string.format("%g", ia)..string.char(0xC2, 0xB0)
        local o = p[tpi]
        local e1 = p[tpi % n + 1] - o
        local e2 = p[(tpi - 2) % n + 1] - o
        local a1 = math.atan2(e1.x, e1.y)
        local a2 = math.atan2(e2.x, e2.y) + 2 * math.pi
        local x0 = o.x + s * math.sin(a1)
        local y0 = o.y + s * math.cos(a1)
        strokeWidth(10)
        for t = 0.05, 1, 0.05 do
            local a = a1 + (a2 - a1) * t
            local x1 = o.x + s * math.sin(a)
            local y1 = o.y + s * math.cos(a)
            line(x0, y0, x1, y1)
            x0, y0 = x1, y1
        end
        local to = o - s * 2.5 * o:normalize()
        text(ias, to.x, to.y)       
    end
end

function nextPoly(p, r)
    local n = #p
    local a0 = math.atan2(p[n].x, p[n].y)
    return polygon(a0, n, r)
end

function polygon(a0, n, r)
    local p = {}
    for i = 1, n do
        local a = a0 - (n - i) * (2 * math.pi / n)
        p[i] = vec2(math.sin(a), math.cos(a)) * r
    end
    return p  
end

-- Hue ([0, 1)) to RGB; (S = 1, V = 1)
function H2RGB(h)
    local r, g, b = 0, 0, 0
    local i = h * 6
    local x = (1 - math.abs(i % 2 - 1))
    if i < 1 then r, g = 1, x
    elseif i < 2 then r, g = x, 1
    elseif i < 3 then g, b = 1, x
    elseif i < 4 then g, b = x, 1
    elseif i < 5 then r, b = x, 1
    else r, b = 1, x end
    return color(255 * r, 255 * g, 255 * b)
end
Tagged:

Comments

  • Posts: 78

    I love the way of slide transition by increase another side. Thanks for sharing code.

  • Posts: 78

    Last time, I modified the another analog clock created by @bee. I used the clock to teach my daughter to read time. By tapping on the screen to increase or decrease the hour and minute.

Sign In or Register to comment.