Howdy, Stranger!

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

Graphs

in Code Sharing Posts: 676
function setup()
    -- Change this
    form = math.sin(x) - math.sqrt(x) / 5
    -- Not!
    yOFx = HEIGHT/2
    xOFy = WIDTH/2
    parameter.integer("size", 5, 100, 50)
    parameter.integer("scaleY",1, 25, 1)
    parameter.number("stepX", 0.2, 1, 1)
    parameter.integer("MoveX", -1000, 1000, 0)
    parameter.integer("MoveY", -1000, 1000, 0)
    parameter.action("To Default", function()
        MoveX = 0
        MoveY = 0
        scaleY = 1
        size = 10
    end)
end


function draw()
    local MoveX = -MoveX
    local MoveY = -MoveY

    resetStyle()
    background(40, 40, 50)
    strokeWidth(1)
    fill(255, 255, 255, 255)
    stroke(255, 255, 255, 255)
    line(0, yOFx + MoveY, WIDTH, yOFx + MoveY)
    line(xOFy + MoveX, 0, xOFy + MoveX, HEIGHT)

    local t = {}

    for x = -250, 250, stepX do
        local formula = form
        formula = formula * scaleY 
        table.insert(t, {formula, x})
    end

    for a, b in ipairs(t) do
        strokeWidth(0)
        --[[fill(255, 255, 255, 255)
        local k = 0
        local m = {5, 10, 15, 20, 25, 30, 35, 50, 75, 120, 250}
        for r, t in ipairs(m) do
            if size > t then
                k = k + 1
            end
        end
        ellipse(xOFy + b[2] * size + MoveX, yOFx + b[1] * size + MoveY, k)]]
        if b[1] * size > 0 then
            fill(35, 255, 0, 255)
            rect(xOFy + b[2] * size + MoveX - size/4, yOFx + MoveY, size * stepX, b[1] * size)
        else
            fill(255, 0, 0, 255)
            rect(xOFy + b[2] * size + MoveX - size/4, yOFx + MoveY + b[1] * size, size * stepX, math.abs(b[1]) * size)
        end
    end
end

We had this in my math group. Helped me a lot :) Thanks Codea :)

Comments

  • Posts: 67

    You call X at the beginning of the code, but it's undefined. I gen an error saying that math.sin expected a number and got nil.

  • Posts: 67

    Is this what you were aiming for?

    function setup()
        x=0
        -- Change this
    --    form = math.sin(x) --Commented out bc it wouldnt work
        -- Not!
        yOFx = HEIGHT/2
        xOFy = WIDTH/2
        parameter.integer("size", 5, 100, 50)
        parameter.integer("scaleY",1, 25, 1)
        parameter.number("stepX", 0.2, 1, 1)
        parameter.integer("MoveX", -1000, 1000, 0)
        parameter.integer("MoveY", -1000, 1000, 0)
        parameter.action("To Default", function()
            MoveX = 0
            MoveY = 0
            scaleY = 1
            size = 10
        end)
    end
    
    
    function draw()
        local MoveX = -MoveX
        local MoveY = -MoveY
    
        resetStyle()
        background(40, 40, 50)
        strokeWidth(1)
        fill(255, 255, 255, 255)
        stroke(255, 255, 255, 255)
        line(0, yOFx + MoveY, WIDTH, yOFx + MoveY)
        line(xOFy + MoveX, 0, xOFy + MoveX, HEIGHT)
    
        local t = {}
    
        for x = -250, 250, stepX do
            local formula = math.sin(x) --I changed this.
            formula = formula * scaleY 
            table.insert(t, {formula, x})
        end
    
        for a, b in ipairs(t) do
            strokeWidth(0)
            --[[fill(255, 255, 255, 255)
            local k = 0
            local m = {5, 10, 15, 20, 25, 30, 35, 50, 75, 120, 250}
            for r, t in ipairs(m) do
                if size > t then
                    k = k + 1
                end
            end
            ellipse(xOFy + b[2] * size + MoveX, yOFx + b[1] * size + MoveY, k)]]
            if b[1] * size > 0 then
                fill(35, 255, 0, 255)
                rect(xOFy + b[2] * size + MoveX - size/4, yOFx + MoveY, size * stepX, b[1] * size)
            else
                fill(255, 0, 0, 255)
                rect(xOFy + b[2] * size + MoveX - size/4, yOFx + MoveY + b[1] * size, size * stepX, math.abs(b[1]) * size)
            end
        end
    end
    
  • Posts: 676

    My code works fine for me :P

  • dave1707dave1707 Mod
    edited July 1 Posts: 5,896

    @TokOut Your code might work fine for you, but anyone who copies the code from above will receive the following error:

    Main:4: bad argument #1 to 'sin' (number expected, got nil)
    stack traceback:
        [C]: in function 'math.sin'
        Main:4: in function 'setup'
    

    x is not defined before you try to execute the line

    form = math.sin(x) - math.sqrt(x) /5
    

    One suggestion: if you post code for someone else to try, copy what you post and try to execute it yourself to make sure it works the way you expect it to.

  • Posts: 676

    I did...

  • Posts: 207

    @TokOut Try it one more time... copy your code and paste it into a new project. I didn't even try out your code, but just by looking at it, it's obvious that it will definitely trigger an error.

Sign In or Register to comment.