-- O(n!)

local function createCurve(t, points)
	if SETTINGS.Debug == true then
		for _, point in points do
			newInstance("Part", {
				Position = point,
				Anchored = true,
				CanCollide = false,
				Size = Vector3.one,
				Color = Color3.fromRGB(0,255,0),
				Transparency = .5,
				Parent = workspace.Debris
			})
		end
	end

	-- could use recursion, but lua doesn't have optimized tail calls. 
	while #points > 1 do
		local tempPoints = points
		points = {}

		for i = 1, #tempPoints - 1 do
			local p1 = tempPoints[i]
			local p2 = tempPoints[i + 1]

			table.insert(points, lerp(p1, p2, t))
		end
	end
	
	if SETTINGS.Debug == true then
		newInstance("Part", {
			Position = points[1],
			Anchored = true,
			CanCollide = false,
			Size = Vector3.one,
			Color = Color3.fromRGB(255,0,0),
			Transparency = .5,
			Parent = workspace.Debris
		})
	end
	
	-- points[1] will contain the correct position at time 't'
	return points[1]
end

return createCurve
