Co mógłbym poprawić w tym kodzie z naciskiem na optymalizacje? Nie znam się na programowaniu więc proszę ekspertów. Czytałem że jest bardzo dużo różnych algorytmów, ale nie wiem który jest najszybszy, nie wiem gdzie to sprawdzić. Co poza optymalizacjami jest do poprawy? Jak napisać do tego testy automatyczne?Żeby to uruchomić należy pobrać https://love2d.org/Zapisać poniższy kod do pliku main.lua, wkleić ten plik do nowo utworzonego folderu i przenieść ten folder na ikonę love.exe albo lovec.exe```lua-- Polygon class definitionlocal Polygon = {}Polygon._index = Polygonfunction Polygon:init(...) self.vertices = { ... } self.flat = {} for _, vertex in ipairs(self.vertices) do table.insert(self.flat, vertex[1]) table.insert(self.flat, vertex[2]) endendfunction Polygon.new(...) local instance = setmetatable({}, Polygon) instance:init(...) return instanceendfunction Polygon:draw(...) love.graphics.setColor(...) love.graphics.polygon("line", self.flat)endfunction Polygon:move(dx, dy) for i, point in ipairs(self.vertices) do point[1] = point[1] + dx point[2] = point[2] + dy self.flat[i * 2 - 1] = point[1] self.flat[i * 2] = point[2] endendfunction Polygon:isPointInside(x, y) local inside = false local vertices = self.vertices local pX, pY = vertices[#vertices][1], vertices[#vertices][2] for i = 1, #vertices do local x1, y1 = vertices[i][1], vertices[i][2] if (y > math.min(pY, y1)) then if (y <= math.max(pY, y1)) then if (x <= math.max(pX, x1)) then if (pY ~= y1) then local xInter = (y - pY) * (x1 - pX) / (y1 - pY) + pX if (pX == x1 or x <= xInter) then inside = not inside end end end end end pX, pY = x1, y1 end return insideendfunction Polygon:getDistanceFromPoly(poly) local minDistance = math.huge for _, point1 in ipairs(self.vertices) do for _, point2 in ipairs(poly.vertices) do local distance = math.sqrt((point1[1] - point2[1]) ^ 2 + (point1[2] - point2[2]) ^ 2) minDistance = math.min(minDistance, distance) end end return minDistanceendfunction Polygon:isPolyInside(poly) for _, vertex in ipairs(self.vertices) do if poly:isPointInside(vertex[1], vertex[2]) then return true end end return falseend-- Calculate the distance between a point and a line segmentfunction Polygon.pointLineSegmentDistance(p, v1, v2) local x1, y1 = v1[1], v1[2] local x2, y2 = v2[1], v2[2] local x3, y3 = p[1], p[2] local dx = x2 - x1 local dy = y2 - y1 local t = math.max(0, math.min(1, ((x3 - x1) * dx + (y3 - y1) * dy) / (dx * dx + dy * dy))) local px = x1 + t * dx local py = y1 + t * dy return math.sqrt((x3 - px) ^ 2 + (y3 - py) ^ 2)end-- Calculate the distance between two polygons considering line segmentsfunction Polygon:getDistanceFromPolySegment(poly) local minDistance = math.huge for _, point in ipairs(self.vertices) do for i = 1, #poly.vertices do local v1 = poly.vertices[i] local v2 = poly.vertices[i % #poly.vertices + 1] local distance = Polygon.pointLineSegmentDistance(point, v1, v2) minDistance = math.min(minDistance, distance) end end return minDistanceend-- Define your polygonslocal draggingPolygon = nillocal polygon1 = Polygon.new({ 100, 100 }, { 150, 200 }, { 200, 100 })local polygon2 = Polygon.new({ 300, 300 }, { 350, 400 }, { 400, 300 })-- Main game loopfunction love.draw() polygon1:draw(1, 0, 0) polygon2:draw(0, 0, 1) love.graphics.setColor(0, 1, 0) -- green love.graphics.print("Is Poly Inside: " .. tostring(polygon1:isPolyInside(polygon2) or polygon2:isPolyInside(polygon1)), 10, 10) love.graphics.print("Distance Vertex: " .. math.min(polygon1:getDistanceFromPoly(polygon2), polygon2:getDistanceFromPoly(polygon1)), 10, 25) love.graphics.print("Distance Line Segment: " .. math.min(polygon1:getDistanceFromPolySegment(polygon2), polygon2:getDistanceFromPolySegment(polygon1)), 10, 40)endfunction love.mousepressed(x, y, button) if button == 1 then -- left mouse button if polygon1:isPointInside(x, y) then draggingPolygon = polygon1 elseif polygon2:isPointInside(x, y) then draggingPolygon = polygon2 end endendfunction love.mousemoved(x, y, dx, dy) if love.mouse.isDown(1) and draggingPolygon then -- left mouse button is down -- Move the polygon being dragged draggingPolygon:move(dx, dy) endend-- Stop dragging the polygon on mouse releasefunction love.mousereleased(x, y, button) if button == 1 then draggingPolygon = nil -- Stop dragging the polygon endend
`#programowanie #pytaniedoeksperta
