Hexagonal mesh


self.m = mesh()

local t = {}

for a = 0, 360, 60  do
    table.insert(t, vec2(self.size * math.cos(math.rad(a)), self.size * math.sin(math.rad(a))))
end
 
self.m.vertices = t

First of all Thanks for reading…So I’ve got some hexagons…currently I’m drawing several lines connecting to eachother…buuuut that’s lame.

So I’m trying to make a mesh to give me control over filling the interior of the hexagon. My battle plan was above. Go counter clockwise 60,120,180,249,300,360 degrees and use that as my mesh vertices. And it worked…sort of…But there’s a problem: there a perfectly rhombuscular patch of while left out of the mesh for some unknown reason…

What are I doing wrong?

You need to create triangles, ie your vertices need to be ordered so that each set of three vertices make a triangle. Typically, you will make a triangle between each side of the haxeagon, and the centre.

But Codea has a handy triangulate function that I haven’t used but may do this for you. Try using that on your table t and see if it helps.

If not, you need to change your code to something like this (untested)

local t1 = {}

for a = 0, 360, 60  do --calculate points round circle
    table.insert(t1, vec2(self.size * math.cos(math.rad(a)), self.size * math.sin(math.rad(a))))
end

--calculate triangles
t2={}
for i=1,#t-1 do
    table.insert(t2,t[i])
    table.insert(t2,vec2(0,0))
    table.insert(t2,t[i+1])
end

self.m.vertices = t2

A very simple polygon class takes a series of vec2s and makes a mesh using triangulate:

function setup()
    fill(0, 97, 255, 255)
    poly=polygon (vec2(50,50), vec2(100,50), vec2(200,100), vec2(50, 200))
end

function draw()
    background(27, 27, 54, 255)
    poly:draw()
end

function polygon(...)
    local m=mesh()
    m.vertices=triangulate(arg)
    m:setColors(color(fill()))
    return m
end

@Ignatz As @yojimbo2000 recommended, why not just use triangulate(vertices) ?

I think you’ll find I said that, but I thought he might want to understand what was needed

@Ignatz Sorry, I missed that.