How to Draw Cubic Bezier Curve via Lua Script?

Hi folks,

Is it possible to draw a cubic Bezier curve via Lua script with app.useTool? I’m having trouble working out how I might order a points array to give to the curve. In part this is because I’m not acclimated to working with the tool manually either :blush: . The curve tool might use a different type of curve altogether, but here’s what I’m familiar with:

In a format like this SVG example,

svg example

<svg width="190" height="160" xmlns="http://www.w3.org/2000/svg">
  <path d="M 10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80" stroke="black" fill="transparent"/>
</svg>

the M moves the sub-path to the initial anchor point x and y, then C is followed by three coordinates which stand for the first control point, second control point and second anchor point. (The S command is a reflection of the curve, not really needed for this example; I’m only concerned with the left side of the curve at the moment.)

Here’s some Lua code as it stands

local pts = {
    -- Horizontal line.
    -- Point(10, 80), --  anchor point 0
    -- Point(40, 10), -- control point 0
    -- Point(65, 10), -- control point 1
    -- Point(95, 80)  --  anchor point 1

    -- Diagonal line.
    Point(10, 80), --  anchor point 0
    Point(95, 80), --  anchor point 1
    Point(40, 10), -- control point 0
    Point(65, 10)  -- control point 1
}

local dlg = Dialog { title = "Curve Test" }

dlg:button {
    id = "ok",
    text = "OK",
    focus = true,
    onclick = function()
        local args = dlg.data
        if args.ok then
            app.useTool {
                tool = "curve",
                points = pts
            }
            app.refresh()
        end
    end
}

dlg:show { wait = false }

I realize there are work-arounds, such evaluating a range of points from a step in [0, 1] then connecting line segments. I was curious, though, if this tool could be used.

Thanks for any insights y’all might have!
Best,
Jeremy