Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created February 6, 2013 03:18
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 basicxman/4719954 to your computer and use it in GitHub Desktop.
Save basicxman/4719954 to your computer and use it in GitHub Desktop.
Hexagon = {}
function Hexagon:new(x, y, height)
obj = {}
setmetatable(obj, self)
self.__index = self
obj.x = x
obj.y = y
obj.height = height
obj:generate_points()
return obj
end
function Hexagon:generate_points()
local h = self
h.l = (h.height / 2) / math.sin(45)
h.dX = math.cos(45) * h.l
h.dY = math.sin(45) * h.l
h.cX = h.dX + (h.l / 2)
h.cY = h.dY
h.vertices = {
0, h.dY
, h.dX, 0
, h.dX + h.l, 0
, h.dX + h.l + h.dX, h.dY
, h.dX + h.l, h.dY + h.dY
, h.dX, h.dY + h.dY
}
end
function Hexagon:update()
local h = self
local n
h.drawVertices = {}
for i, v in ipairs(h.vertices) do
if i % 2 == 1 then
n = h.x + v - h.cX
else
n = h.y + v - h.cY
end
table.insert(h.drawVertices, n)
end
end
function Hexagon:draw()
local h = self
love.graphics.polygon('line', h.drawVertices)
end
require("hexagon")
function love.load()
hex = Hexagon:new(300, 300, 50)
end
function love.update(dt)
hex:update()
end
function love.draw()
love.graphics.print("Current FPS: " .. tostring(love.timer.getFPS()), 10, 10)
love.graphics.point(300, 300)
hex:draw()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment