Drawing Hexagons

Moderators: LCMark, LCfraser

Post Reply
pink
Posts: 252
Joined: Wed Mar 12, 2014 6:18 pm

Drawing Hexagons

Post by pink » Wed Feb 20, 2019 3:23 pm

I am ultimately going to make a widget with a pattern of hexagons, so far I have created the "center" hexagon and will draw the rest around it... before I repeat this 36 more times, is there a simpler way than what I have below?

is it possible after creating the first polygon and filling it, to just shift the whole thing over and fill it again in a different spot? or any other alternative to calculating each point for every hex?

Thanks!

Code: Select all

public handler OnPaint()
   variable tH as Number
   variable tW as Number
   variable tS as Number
   variable tX0 as Number
   variable tX1 as Number
   variable tX2 as Number
   variable tX3 as Number
   variable tY0 as Number
   variable tY1 as Number
   variable tY2 as Number
   variable tHM as Number
   variable tWM as Number
   variable tHexPath as Path
   
   put my height into tHM
   put my width into tWM

   set the paint of this canvas to my foreground paint

   put (the minimum of tHM and tWM)/7 into tH
   put tH * 1.1547 into tW
   put tH * 0.5774 into tS
   
   put (tWM/2) - (tW/2) into tX0
   put tX0 + ((tW-tS)/2) into tX1
   put tX1 + tS into tX2
   put tX0 + tW into tX3
   
   put (tHM/2) - (tH/2) into tY0
   put tY0 + (tH/2) into tY1
   put tY0 + tH into tY2
   
   put polygon path with points [point [tX0,tY1],point [tX1,tY0],point [tX2,tY0],point [tX3,tY1],point [tX2,tY2],point [tX1,tY2]] into tHexPath
   add tHexPath to this canvas
   fill this canvas

end handler
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2166
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Drawing Hexagons

Post by [-hh] » Wed Feb 20, 2019 4:08 pm

You could try:
  • Option (1).
    Compute the polygon path when an update is needed, not with every paint.
    Then use translate in OnPaint.
  • Option (2), similar to LC Script.
    Compute all polygon paths when an update is needed, not with every paint.
p.s. You could also draw the hexagons as regular polygons.
See for example viewtopic.php?p=98716#p98716
shiftLock happens

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3729
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Drawing Hexagons

Post by richmond62 » Wed Feb 20, 2019 4:18 pm


[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2166
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Drawing Hexagons

Post by [-hh] » Wed Feb 20, 2019 4:31 pm

Here a LCB handler for computing the points of a regular N-gon.

Code: Select all

handler regularPoints(in pA as List) returns List
   -- pA = [ N, radius, xLoc, yLoc]
   variable tI as Number
   variable tP as Number
   put 2*pi/pA[1] into tP
   put [] into tM
   repeat with tI from 1 up to pA[1]
       push point [pA[3]+pA[2]*sin(tI*tP),pA[4]-pA[2]*cos(tI*tP)] onto tM
   end repeat
   return tM
end handler

Code: Select all

-- usage
variable tPath as Path
put polygon path with points regularPoints([6,100,250,250]) into tPath
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6969
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Drawing Hexagons

Post by FourthWorld » Wed Feb 20, 2019 5:17 pm

Are you making a game board?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3729
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Drawing Hexagons

Post by richmond62 » Wed Feb 20, 2019 8:20 pm

Are you making a game board?
Aren't we all?

Hence my link to the other discussion above this post.

pink
Posts: 252
Joined: Wed Mar 12, 2014 6:18 pm

Re: Drawing Hexagons

Post by pink » Wed Feb 20, 2019 9:09 pm

Hermann - Thanks, i will most definitely give that a try

Richard - Yes, making a game board, sort of a hexagonal version of 2048
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6969
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Drawing Hexagons

Post by FourthWorld » Wed Feb 20, 2019 11:19 pm

Cool. Looking forward to it.

Unless this is an exercise in LCB, in LCS you can obtain the points of a regular polygon with "the effective points".
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

pink
Posts: 252
Joined: Wed Mar 12, 2014 6:18 pm

Re: Drawing Hexagons

Post by pink » Thu Feb 21, 2019 2:31 am

Hermann - is there an easy way to rotate this polygon by 90 degrees?

I tried the below code, when I add the rotate code, the hex flies off the canvas (it's not visible at all at 90 degrees, if I change it to 45 degrees, it is partially visible off to the side)

Code: Select all

  
   put polygon path with points regularPoints([6,my width/7,my width/2,my height/2]) into tHexPath
   rotate tHexPath by 90
   add tHexPath to this canvas
   fill this canvas
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3729
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Drawing Hexagons

Post by richmond62 » Thu Feb 21, 2019 10:02 am

Frankly, being a very lazy person, I'd import a vector hexagon into a widget, then clone away like crazy
and rotation would be dead easy.
-
hexStack.png
hexStack.png (6.46 KiB) Viewed 1418 times
-
Attachments
vector hex.livecode.zip
Here's the stack
(797 Bytes) Downloaded 33 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6969
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Drawing Hexagons

Post by FourthWorld » Thu Feb 21, 2019 3:07 pm

What are the advantages of using a widget rather than the built-in graphic object?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

pink
Posts: 252
Joined: Wed Mar 12, 2014 6:18 pm

Re: Drawing Hexagons

Post by pink » Thu Feb 21, 2019 6:23 pm

Honestly not sure if there is an advantage... I've been trying to replace more parts of my stacks with homemade widgets partially as a programming exercise, but also because it is can really clean up my scripts.

The game board is 37 hexagons, and to resize each one, and then position them properly relative to one another is quite a task and I haven't really gotten it to work well. (It is also perfectly plausible that I am overlooking some more simple method.)

In another app, I recreated a configurable Kanban/Scrum board as a widget, so instead of being a combination of fields and graphics that all needed to get resized and repositioned, it's now a single widget that resizes dynamically with the stack. Was hoping to do the same with the hex board.
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2166
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Drawing Hexagons

Post by [-hh] » Thu Feb 21, 2019 6:45 pm

(Just now remembered.) Here a LC Script stack that draws a hexagonal grid.
viewtopic.php?p=168657
Richmond wrote:being a very lazy person, I'd import a vector hexagon into a widget, then clone away like crazy and rotation would be dead easy.
This is mentally lazy, ignoring 4 lines of basic math (which is much more faster).
shiftLock happens

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3729
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Drawing Hexagons

Post by richmond62 » Thu Feb 21, 2019 7:29 pm

This is mentally lazy
I know, I know: that was written while 5 kids were doing a 30 minute test this morning. 8)

Post Reply

Return to “LiveCode Builder”