szymski

Untitled

Aug 1st, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. vec2 = require("deps/vector2")
  2. vec3 = require("deps/vector3")
  3.  
  4. function love.load()
  5.  
  6. end
  7.  
  8. --------------------------
  9. -- UPDATING
  10. --------------------------
  11.  
  12. function love.keypressed(key)
  13. if key == "escape" then
  14. love.event.quit()
  15. end
  16. end
  17.  
  18. --------------------------
  19. -- RENDERING
  20. --------------------------
  21.  
  22. -- Takes table of vec2s and returns a sequential table of vec2 x and y values
  23. function getPolyCoords(poly)
  24. local coords = {}
  25. for i = 1, #poly do
  26. coords[#coords + 1] = poly[i].x
  27. coords[#coords + 1] = poly[i].y
  28. end
  29. return coords
  30. end
  31.  
  32. poly2 = {
  33. vec2.new(150, 150),
  34. vec2.new(350, 150),
  35. vec2.new(430, 200),
  36. vec2.new(400, 280),
  37. vec2.new(400, 320),
  38. vec2.new(200, 320),
  39. vec2.new(80, 180),
  40. -- vec2.new(150, 150),
  41. }
  42.  
  43. poly = {
  44. vec2.new(150, 20),
  45. vec2.new(350, 150),
  46. vec2.new(80, 180),
  47. }
  48.  
  49. function lineIntersection(x1, y1, x2, y2, x3, y3, x4, y4)
  50. local det = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3)
  51. if det == 0 then
  52. return nil
  53. end
  54.  
  55. local ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / det
  56. local ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / det
  57.  
  58. local x = x1 + ua * (x2 - x1)
  59. local y = y1 + ua * (y2 - y1)
  60.  
  61. return x, y
  62. end
  63.  
  64. function offsetPolygon(poly, offset)
  65. poly = getPolyCoords(poly)
  66.  
  67. local newPoly = {}
  68. local polySize = #poly
  69. local offsetEdges = {}
  70.  
  71. -- Calculate the offset edges
  72. for i = 1, polySize, 2 do
  73. local nextIndex = i + 2
  74.  
  75. if nextIndex > polySize then
  76. nextIndex = 1
  77. end
  78.  
  79. local x1, y1 = poly[i], poly[i + 1]
  80. local x2, y2 = poly[nextIndex], poly[nextIndex + 1]
  81.  
  82. local dx, dy = x2 - x1, y2 - y1
  83. local len = math.sqrt(dx * dx + dy * dy)
  84.  
  85. dx, dy = dx / len * offset, dy / len * offset
  86.  
  87. local nx, ny = -dy, dx
  88.  
  89. local offsetX1, offsetY1 = x1 + nx, y1 + ny
  90. local offsetX2, offsetY2 = x2 + nx, y2 + ny
  91.  
  92. table.insert(offsetEdges, {offsetX1, offsetY1, offsetX2, offsetY2})
  93. end
  94.  
  95. -- Find the intersection of offset edges to create the new vertices
  96. for i = 1, #offsetEdges do
  97. local edge1 = offsetEdges[i]
  98. local edge2 = offsetEdges[i % #offsetEdges + 1]
  99.  
  100. local x, y = lineIntersection(edge1[1], edge1[2], edge1[3], edge1[4], edge2[1], edge2[2], edge2[3], edge2[4])
  101.  
  102. table.insert(newPoly, vec2.new(x, y))
  103. end
  104.  
  105. return newPoly
  106. end
  107.  
  108. -- Draws a filled polygon given a table of vec2's
  109. function drawPoly(poly)
  110. love.graphics.polygon("fill", getPolyCoords(poly))
  111. end
  112.  
  113. -- Draws poly outline
  114. function drawPolyOutline(poly)
  115. love.graphics.polygon("line", getPolyCoords(poly))
  116. end
  117.  
  118. local polyx = offsetPolygon(poly2, -50)
  119.  
  120. function love.draw()
  121. love.graphics.setLineWidth(2)
  122.  
  123. love.graphics.setColor(0.8, 0.7, 0.2)
  124. drawPolyOutline(poly2)
  125.  
  126. love.graphics.setColor(0.1, 0.8, 0.4)
  127. drawPolyOutline(polyx)
  128.  
  129. -- local textY = 5
  130.  
  131. -- local prevSubDist = 0
  132. -- for i = 1, #poly - 1 do
  133. -- local p1, p2, p3 = poly[i], poly[i + 1], poly[i + 2]
  134. -- local diff = p2 - p1
  135. -- local length = diff:Magnitude()
  136. -- local diffNormalized = diff:Unit()
  137. -- local normal = vec2.new(-diff.y, diff.x):Unit()
  138. -- local angle = math.atan2(diff.y, diff.x)
  139.  
  140. -- love.graphics.setColor(55, 55, 55)
  141. -- drawLine(p1, p2)
  142. -- love.graphics.setColor(255, 0, 0, 100)
  143. -- love.graphics.circle("line", p2.x, p2.y, DIST)
  144.  
  145. -- local subDist = 0
  146. -- local subFact = 1
  147. -- if p3 then
  148. -- local p2Diff = p3 - p2
  149. -- local p2Angle = math.atan2(p2Diff.y, p2Diff.x)
  150. -- local angleDiff = p2Angle - angle - math.pi
  151. -- -- if angleDiff > math.pi then
  152. -- -- angleDiff = angleDiff - math.pi
  153. -- -- end
  154. -- love.graphics.print("" .. angleDiff, 5, textY)
  155. -- textY = textY + 25
  156.  
  157. -- local sth = p3 - p1
  158. -- local l = sth:Magnitude()
  159.  
  160. -- subFact = l
  161.  
  162. -- --subDist = DIST * math.tan(angleDiff / 2)
  163.  
  164.  
  165. -- dist = length
  166.  
  167. -- local diff2 = p3 - p2
  168. -- local dist2 = diff2:Magnitude()
  169. -- local diff2Normalized = diff2:Unit()
  170. -- local normal2 = vec2.new(-diff2.y, diff2.x):Unit()
  171.  
  172.  
  173.  
  174. -- local a = p1 + normal * DIST + diffNormalized * (dist - subDist)
  175. -- local b = p2 + normal2 * DIST
  176. -- love.graphics.setColor(255, 0, 255)
  177. -- drawLine(a, b)
  178.  
  179. -- local x = (a - b):Magnitude() / 2
  180. -- local c = x / math.sin(angleDiff / 2)
  181. -- subDist = c
  182. -- -- subDist = 100
  183.  
  184. -- if angleDiff > -math.pi then
  185. -- subDist = -c
  186. -- end
  187. -- end
  188.  
  189. -- dist = length
  190.  
  191.  
  192. -- love.graphics.setColor(255, 155, 55)
  193. -- drawLine(p1 + normal * DIST + diffNormalized * prevSubDist,
  194. -- p1 + normal * DIST + diffNormalized * (dist - subDist))
  195.  
  196. -- love.graphics.setColor(255, 155, 55)
  197. -- drawLine(p1 - normal * DIST - diffNormalized * prevSubDist,
  198. -- p1 - normal * DIST + diffNormalized * (dist + subDist))
  199.  
  200. -- prevSubDist = subDist
  201. -- end
  202. end
  203.  
  204. function drawLine(p1, p2)
  205. love.graphics.line(p1.x, p1.y, p2.x, p2.y)
  206. end
  207.  
Advertisement
Add Comment
Please, Sign In to add comment