Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vec2 = require("deps/vector2")
- vec3 = require("deps/vector3")
- function love.load()
- end
- --------------------------
- -- UPDATING
- --------------------------
- function love.keypressed(key)
- if key == "escape" then
- love.event.quit()
- end
- end
- --------------------------
- -- RENDERING
- --------------------------
- -- Takes table of vec2s and returns a sequential table of vec2 x and y values
- function getPolyCoords(poly)
- local coords = {}
- for i = 1, #poly do
- coords[#coords + 1] = poly[i].x
- coords[#coords + 1] = poly[i].y
- end
- return coords
- end
- poly2 = {
- vec2.new(150, 150),
- vec2.new(350, 150),
- vec2.new(430, 200),
- vec2.new(400, 280),
- vec2.new(400, 320),
- vec2.new(200, 320),
- vec2.new(80, 180),
- -- vec2.new(150, 150),
- }
- poly = {
- vec2.new(150, 20),
- vec2.new(350, 150),
- vec2.new(80, 180),
- }
- function lineIntersection(x1, y1, x2, y2, x3, y3, x4, y4)
- local det = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3)
- if det == 0 then
- return nil
- end
- local ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / det
- local ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / det
- local x = x1 + ua * (x2 - x1)
- local y = y1 + ua * (y2 - y1)
- return x, y
- end
- function offsetPolygon(poly, offset)
- poly = getPolyCoords(poly)
- local newPoly = {}
- local polySize = #poly
- local offsetEdges = {}
- -- Calculate the offset edges
- for i = 1, polySize, 2 do
- local nextIndex = i + 2
- if nextIndex > polySize then
- nextIndex = 1
- end
- local x1, y1 = poly[i], poly[i + 1]
- local x2, y2 = poly[nextIndex], poly[nextIndex + 1]
- local dx, dy = x2 - x1, y2 - y1
- local len = math.sqrt(dx * dx + dy * dy)
- dx, dy = dx / len * offset, dy / len * offset
- local nx, ny = -dy, dx
- local offsetX1, offsetY1 = x1 + nx, y1 + ny
- local offsetX2, offsetY2 = x2 + nx, y2 + ny
- table.insert(offsetEdges, {offsetX1, offsetY1, offsetX2, offsetY2})
- end
- -- Find the intersection of offset edges to create the new vertices
- for i = 1, #offsetEdges do
- local edge1 = offsetEdges[i]
- local edge2 = offsetEdges[i % #offsetEdges + 1]
- local x, y = lineIntersection(edge1[1], edge1[2], edge1[3], edge1[4], edge2[1], edge2[2], edge2[3], edge2[4])
- table.insert(newPoly, vec2.new(x, y))
- end
- return newPoly
- end
- -- Draws a filled polygon given a table of vec2's
- function drawPoly(poly)
- love.graphics.polygon("fill", getPolyCoords(poly))
- end
- -- Draws poly outline
- function drawPolyOutline(poly)
- love.graphics.polygon("line", getPolyCoords(poly))
- end
- local polyx = offsetPolygon(poly2, -50)
- function love.draw()
- love.graphics.setLineWidth(2)
- love.graphics.setColor(0.8, 0.7, 0.2)
- drawPolyOutline(poly2)
- love.graphics.setColor(0.1, 0.8, 0.4)
- drawPolyOutline(polyx)
- -- local textY = 5
- -- local prevSubDist = 0
- -- for i = 1, #poly - 1 do
- -- local p1, p2, p3 = poly[i], poly[i + 1], poly[i + 2]
- -- local diff = p2 - p1
- -- local length = diff:Magnitude()
- -- local diffNormalized = diff:Unit()
- -- local normal = vec2.new(-diff.y, diff.x):Unit()
- -- local angle = math.atan2(diff.y, diff.x)
- -- love.graphics.setColor(55, 55, 55)
- -- drawLine(p1, p2)
- -- love.graphics.setColor(255, 0, 0, 100)
- -- love.graphics.circle("line", p2.x, p2.y, DIST)
- -- local subDist = 0
- -- local subFact = 1
- -- if p3 then
- -- local p2Diff = p3 - p2
- -- local p2Angle = math.atan2(p2Diff.y, p2Diff.x)
- -- local angleDiff = p2Angle - angle - math.pi
- -- -- if angleDiff > math.pi then
- -- -- angleDiff = angleDiff - math.pi
- -- -- end
- -- love.graphics.print("" .. angleDiff, 5, textY)
- -- textY = textY + 25
- -- local sth = p3 - p1
- -- local l = sth:Magnitude()
- -- subFact = l
- -- --subDist = DIST * math.tan(angleDiff / 2)
- -- dist = length
- -- local diff2 = p3 - p2
- -- local dist2 = diff2:Magnitude()
- -- local diff2Normalized = diff2:Unit()
- -- local normal2 = vec2.new(-diff2.y, diff2.x):Unit()
- -- local a = p1 + normal * DIST + diffNormalized * (dist - subDist)
- -- local b = p2 + normal2 * DIST
- -- love.graphics.setColor(255, 0, 255)
- -- drawLine(a, b)
- -- local x = (a - b):Magnitude() / 2
- -- local c = x / math.sin(angleDiff / 2)
- -- subDist = c
- -- -- subDist = 100
- -- if angleDiff > -math.pi then
- -- subDist = -c
- -- end
- -- end
- -- dist = length
- -- love.graphics.setColor(255, 155, 55)
- -- drawLine(p1 + normal * DIST + diffNormalized * prevSubDist,
- -- p1 + normal * DIST + diffNormalized * (dist - subDist))
- -- love.graphics.setColor(255, 155, 55)
- -- drawLine(p1 - normal * DIST - diffNormalized * prevSubDist,
- -- p1 - normal * DIST + diffNormalized * (dist + subDist))
- -- prevSubDist = subDist
- -- end
- end
- function drawLine(p1, p2)
- love.graphics.line(p1.x, p1.y, p2.x, p2.y)
- end
Advertisement
Add Comment
Please, Sign In to add comment