Bezier curves in Playdate API

Of course!

local accuracy = 0.005

function bezier(t, p0X, p0Y, p1X, p1Y, p2X, p2Y, p3X, p3Y)
	
	local cX = 3 * (p1X - p0X)
	local bX = 3 * (p2X - p1X) - cX
	local aX = p3X - p0X - cX - bX
	
	local cY = 3 * (p1Y - p0Y)
	local bY = 3 * (p2Y - p1Y) - cY
	local aY = p3Y - p0Y - cY - bY
	
	return ((aX * math.pow(t, 3)) + (bX * math.pow(t, 2)) + (cX * t) + p0X),
			((aY * math.pow(t, 3)) + (bY * math.pow(t, 2)) + (cY * t) + p0Y)	
end


function curveLine(p0X, p0Y, p1X, p1Y, p2X, p2Y, p3X, p3Y)
	local x, y = p0X, p0Y
	local pX, pY = 0, 0
	
	for i = 0, 1, accuracy do
		pX, pY = bezier(i, p0X, p0Y, p1X, p1Y, p2X, p2Y, p3X, p3Y)
		gfx.drawLine(x, y, pX, pY)
		x, y = pX, pY
	end
end

curveLine(10, 10, 30, 200, 30, 200, 380, 230)	

The result should be something like this
bezier-line-4

8 Likes