Help with hexagon

Hi everyone!

I need help to make a hexagonal polygon. I have tried to manipulate the star example given in display.newPolygon() api but I achieve nothing :unsure:

I really need help how to create 6 hexagons that will be filled with an image.

Code? None, I just have written the above mentioned example and I have removed and put values without any success

Any help is welcome

Thanks in advance

DoDi

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2020/02/drawHex.zip

drawHex.png

You can do this with some simple trig if you know the radius - https://www.quora.com/How-can-you-find-the-coordinates-in-a-hexagon

My problem is that I can’t understand how the vertices are drawn. I don’t understand if they are pieces that come together or lines that enclose a space. I need help with the vertices.

local R = 45 local N = 6 local vertices = {} local i = 0 for t = 0, 2\*math.pi, 2\*math.pi/N do     i=i+1; vertices[i]= R\*math.cos(t)     i=i+1; vertices[i]= R\*math.sin(t) end

I found this in an old post from another page but simulator give me warnings and the resulting polygon do not fill  :wacko:

I am requesting mathematical help because I am a graphic artist and my left side of the brain is not as good as the right one.

Thanks in advance

DoDi

Hi @dodi_games

The first and the last points can not be the same. See documentation for more details.

For t=0 and t=2*math.pi, sin(t) and cos(t) functions give the same results. Replace 

for t = 0, 2\*math.pi, 2\*math.pi/N do -- You get N+1 points

with

for t = 0, 2\*math.pi \* ( ( N-1 )/N ), 2\*math.pi/N do

Have a nice day:)

ldurniat

I think I know whats going on, can you show us the code where you build the polygon?

As an artist, once you get a handle on trig it will be easy because you can visualize everything.  I believe the reason your vertices failed is that they are simple sine and cosine values which means they are all no larger than 1.

They need to be multiplied by the distance to the center of the polygon

[lua]

local cx = display.contentWidth * 0.5

local cy = display.contentHeight * 0.5

local numSides = 6

local size = 30 – distance from the center of the polygon

local unitCircle = 2 * math.pi – full circle in radians

local rad = unitCircle/ numSides

local startRad = 0 – in radians

– try startRad = rad/2 for a first rotation

local verts = {}

for i=1,numSides do

    local xVert = math.cos(startRad + rad * i ) * size

    local yVert = math.sin(startRad + rad * i ) * size

    verts[#verts+1] = xVert

    verts[#verts+1] = yVert

    local circ = display.newCircle( cx + xVert,cy +  yVert, 2 )

    circ:setFillColor( 0.9, 0.95, 0.12 )

end

local polygon = display.newPolygon( cx, cy, verts )

polygon:setFillColor( 0, 0.68, 0.99 )

polygon.strokeWidth = 1

polygon:setStrokeColor( 0.85, 0.22, 0.09 )

polygon:toBack( )

[/lua]

To get an handle on the visuals, you should check out the math is fun unit circle

Once you get a grasp on how this works, you can use the module @roaminggamer posted above

*note - a more accurate variable for “size” would have been “radius” but since I was doing the calculations in radians it seemed like that would unduly confuse things  :wink:

**This code only works for polygons with an equal number of sides

@Idurniat

I replace it and let you know  :wink:

@sporkfin

It is a function that forms the polygon according to what the user selects. This is the piece of code that creates the hexagons

elseif ( appData.selectedPolygon == "hexagonal" ) then         --create the vertices array     local R = 45     local N = 6     local vertices = {}     local i = 0       for t = 0, 2\*math.pi, 2\*math.pi/N do         i=i+1; vertices[i]= R\*math.cos(t)         i=i+1; vertices[i]= R\*math.sin(t)       end     --hexagonal pattern loop     for i = 1, 8 do       P.poly[i] = display.newPolygon( 0, 0, vertices )       P.poly[i]:setFillColor(1,1,1)       group:insert( P.poly[i] )     end     --polygon position     P.poly[1].x = centerX; P.poly[1].y = centerY-508     P.poly[2].x = centerX-254; P.poly[2].y = centerY-254         etc... end

your code looks very interesting i will try to implement it.

@all

THANKS!

I will try all the suggestions and come back

DoDi

Yeah, that code snippet doesn’t account for the distance between the center of the polygon and the vertices.  I think my snippet is a little more logical but adding the distance might fix your current code

[lua]

 i=i+1; vertices[i]= R*math.cos(t) * distance

 i=i+1; vertices[i]= R*math.sin(t) * distance

[/lua]

@sporkfin

From the point of view that I’m not good at math, your code is far superior to snipet. I can understand it better. Thanks!