Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TypicalPanda97/7ffd0d24b4fa72c68b8c35ecadd3ce35 to your computer and use it in GitHub Desktop.
Save TypicalPanda97/7ffd0d24b4fa72c68b8c35ecadd3ce35 to your computer and use it in GitHub Desktop.
--[[
Creates a quadratic bezier curve
coord1, coord2, coord3: tables with x and y values
finesse: controls how many steps are calculated, suggested 100
if there are gaps in the line you can try to increase this
Returns a array of xy coords
]]
local function CreateQuadraticBezierCurve(coord1, coord2, coord3, finesse)
local line = {}
local x1 = nil
local y1 = nil
for i = 0, finesse do
local percent = i / finesse
local x2 = (1 - percent)^2 * coord1.x + 2 * (1 - percent) *
percent * coord2.x + percent^2 * coord3.x
local y2 = (1 - percent)^2 * coord1.y + 2 * (1 - percent) *
percent * coord2.y + percent^2 * coord3.y
x2 = x2>=0 and math.floor(x2+0.5) or math.ceil(x2-0.5)
y2 = y2>=0 and math.floor(y2+0.5) or math.ceil(y2-0.5)
if (not(x1 == x2 and y1 == y2))
and (terrainLayoutResult[x2] ~= nil and terrainLayoutResult[y2] ~= nil)
then
x1 = x2
y1 = y2
table.insert(line, {x = x1, y = y1})
end
end
return line
end
--[[Example
local line = CreateQuadraticBezierCurve({x=1, y=gridSize}, {x=1, y=1}, {x=gridSize, y=1}, 100)
for l in pairs(line) do
local coord = line[l]
terrainLayoutResult[coord.x][coord.y].terrainType = tt_plains
end
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment