Skip to content

Instantly share code, notes, and snippets.

@geekhunger
Last active November 19, 2015 11:57
Show Gist options
  • Save geekhunger/c1ac4c8f09b945be0413 to your computer and use it in GitHub Desktop.
Save geekhunger/c1ac4c8f09b945be0413 to your computer and use it in GitHub Desktop.
Fake lineCapMode() for line(), when noSmooth() is activated in Codea.
local _line = line
local function lineCap(d)
for x = 1, d do
y = math.sqrt(d*d - x*x)
rect(0, 0, x, y)
end
end
function line(x1, y1, x2, y2, capMode)
local p1, p2 = vec2(x1, y1), vec2(x2, y2)
pushStyle()
pushMatrix()
rectMode(CENTER)
if capMode then
noSmooth()
local angle = math.deg(vec2(0, 1):angleBetween(p2 - p1))
local size = strokeWidth()
if capMode == ROUND then -- default condition when smooth()!
pushMatrix()
translate(p1.x, p1.y)
rotate(angle)
lineCap(size)
popMatrix()
translate(p2.x, p2.y)
rotate(angle)
lineCap(size)
popMatrix()
elseif capMode == PROJECT then
pushMatrix()
translate(p1.x, p1.y)
rotate(angle)
rect(0, 0, size)
popMatrix()
translate(p2.x, p2.y)
rotate(angle)
rect(0, 0, size)
popMatrix()
elseif capMode == SQUARE then
--pass condition because this is default line() behaviour when noSmooth()!
end
end
_line(p1.x, p1.y, p2.x, p2.y)
popMatrix()
popStyle()
end
function setup()
smooth()
end
function draw()
background(45)
stroke(255, 0, 0)
strokeWidth(30)
line(30, 30, 300, 200, ROUND)
stroke(255, 90, 0)
strokeWidth(20)
line(30, 30, 300, 200, PROJECT)
stroke(255, 120, 20)
strokeWidth(10)
line(30, 30, 300, 200, SQUARE)
stroke(0)
strokeWidth(10)
line(30, 120, 300, 150, ROUND) -- uses default line() with is now affected by smooth() from setup()
stroke(0)
strokeWidth(10)
line(30, 100, 300, 130) -- uses default line() with is now affected by smooth() from setup()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment