{"author_name":"r2d2upgrade","cat":"LD #31","comments":[{"author_name":"torcado194","time":"December 1, 2014 12:55 pm","epoch":1417456500,"text":"This is really informative!<br \/>\n one question: Why did you chose to move every other column UP instead of DOWN? Is that the more common way to do it?","spam":"N"}],"epoch":1415787000,"likes":22,"metadata":{"p_key":"26040","p_author":"r2d2upgrade","p_authorkey":"32730","p_urlkey":"61463","p_title":"Hexagonal Grids!","p_cat":"LD #31","p_event":"LD31","p_time":"1415787000","p_likes":"22","p_comments":"1","p_status":"UPD5","us_key":"32730","us_name":"r2d2upgrade","us_username":"r2d2upgrade","event_start":"1417737600","event_key":"26","event_name":"LD31"},"text":"<p>So, I did the maths on how to draw a perfect hexagon. Now, using the same logic from that<br \/>\n tutorial, in this tutorial I will show you how to make a hexagonal grid!<\/p>\n <p><strong>Maths:<\/strong><br \/>\n Let&#8217;s say we have a hexagon with width <strong>a<\/strong> and height <strong>b<\/strong>.<br \/>\n (If you used the last tutorial, then the value of <strong>a<\/strong> from the last tutorial becomes twice that<br \/>\n value in this tutorial)<\/p>\n <p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_001.png\"><img class=\"alignnone size-full wp-image-397678\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_001.png\" alt=\"HexGrid_001\" width=\"201\" height=\"172\" \/><\/a><\/p>\n <p>Now we generate a bunch of hexagons to fill up the screen area.<br \/>\n For this example the point of reference is in the center of the hexagon.<\/p>\n <div>Each hexagon will be <strong>3\/4*a<\/strong> to the side of the last one, and <strong>b<\/strong> above or below:<\/div>\n <p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_002.png\"><img class=\"alignnone size-medium wp-image-397679\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_002-300x291.png\" alt=\"HexGrid_002\" width=\"300\" height=\"291\" \/><\/a><\/p>\n <p>Remember to keep <strong>a<\/strong> and <strong>b<\/strong> as whole numbers!<\/p>\n <p>So the screen should look like this: (grey = hexagon, dark grey = overlap)<\/p>\n <p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_003.png\"><img class=\"alignnone size-full wp-image-397680\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_003.png\" alt=\"HexGrid_003\" width=\"160\" height=\"98\" \/><\/a><\/p>\n <p>But we need every 2nd column to go up a bit, so that the hexagons interconnect properly.<br \/>\n So we use this equation, where <strong>x<\/strong> and <strong>y<\/strong> are the coordinates of the center of each hexagon:<\/p>\n <p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_004.png\"><img class=\"alignnone size-full wp-image-397681\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_004.png\" alt=\"HexGrid_004\" width=\"193\" height=\"15\" \/><\/a><\/p>\n <p>Now your screen will look like this:<\/p>\n <p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_005.png\"><img class=\"alignnone size-full wp-image-397682\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/11\/HexGrid_005.png\" alt=\"HexGrid_005\" width=\"178\" height=\"114\" \/><\/a><\/p>\n <p>Excellent!<\/p>\n <p>Now we can code it!<\/p>\n <p><strong>Pseudo Code:<\/strong><\/p>\n <p><em><strong>Initialize:<\/strong><\/em><br \/>\n width = (width of hexagon)<br \/>\n height = (height of hexagon)<\/p>\n <p><em><strong>Creation Loop:<\/strong><\/em><br \/>\n create hexagon<br \/>\n x = x + (3*width)\/4<br \/>\n if edge of screen is reached {<br \/>\n go back to starting edge of screen<br \/>\n y = y + height<br \/>\n }<\/p>\n <p><em><strong>Creation code for each hexagon:<\/strong><\/em><br \/>\n y = y + mod(sin((90*(x\/width))*height\/2)<\/p>\n <p>&nbsp;<\/p>\n <p>&nbsp;<\/p>\n <p>And there you have a hexagonal grid!<\/p>\n <p>And this is the last of my little group of tutorials :3<br \/>\n Thanks for the comments on how to refine the logic and code of previous tutorials <img src=\"http:\/\/ludumdare.com\/compo\/wp-includes\/images\/smilies\/simple-smile.png\" alt=\":)\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n \n <p>Tags: <a href=\"http:\/\/ludumdare.com\/compo\/tag\/code\/\" rel=\"tag\">code<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/tutorial\/\" rel=\"tag\">tutorial<\/a><\/p>","time":"November 12th, 2014 5:10 am","title":"Hexagonal Grids!"}