[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## Help with hexagon Started by dodi_games Feb 03 2020 05:04 PM

10 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
• Contributor

• 408 posts
• Corona SDK

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

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

DoDi

[TOPIC: post.html]
#2

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 7,666 posts
• Corona SDK

• ldurniat, agramonte and dodi_games like this

[TOPIC: post.html]
#3

### SGS

[GLOBAL: userInfoPane.html]
SGS
• Corona Geek

• 2,219 posts
• Corona SDK

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

• dodi_games likes this

[TOPIC: post.html]
#4

### dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
• Contributor

• 408 posts
• Corona SDK

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

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.

DoDi

[TOPIC: post.html]
#5

### ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
• Contributor

• 403 posts
• Corona SDK

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

• dodi_games likes this

[TOPIC: post.html]
#6

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 812 posts
• Corona SDK

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

• dodi_games likes this

[TOPIC: post.html]
#7

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 812 posts
• Corona SDK

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

```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( )
```

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

• dodi_games likes this

[TOPIC: post.html]
#8

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 812 posts
• Corona SDK

*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

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

• dodi_games likes this

[TOPIC: post.html]
#9

### dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
• Contributor

• 408 posts
• Corona SDK

@Idurniat

I replace it and let you know

@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

[TOPIC: post.html]
#10

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 812 posts
• Corona SDK

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

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

• dodi_games likes this

[TOPIC: post.html]
#11

### dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
• Contributor

• 408 posts
• Corona SDK

@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!

[topic_controls]

[/topic_controls]