How To Draw Hexagon Grid Java
- Download demo projection - 26.7 KB
Introduction
The goal of my projection is to create a modular, reusable hexagon based map that could be used in simple games and ALife applications. I wanted to leverage as much functionality as possible from .Cyberspace, which meant using GDI+ and Forms. Drawing shapes with GDI+ and capturing mouse events with Forms is adequately fiddling, which would let me to spend my programming fourth dimension solving on more important issues (like hexagon geometry!). This is the first "version" of the hex map, and past no means consummate.
Hexagon
Hexagon based games, whether traditional board games or calculator-based, provide more than strategic and tactical game-play when compared to simple square based games (like the Checkers game lath). The hexagon has half-dozen sides, which allows movement in six directions, instead of 4. The altitude from the center of a hexagon to the center of each neighboring hexagon is equal, which eliminates the baloney of calculating diagonal altitude in a traditional square based map. Hexagons are more pleasing to look at, which counts for something, right?
The core of my code is based on the geometry of the hexagon. When I use the word hexagon, I really mean regular hexagon, which is a six-sided polygon where all six sides take the same length. The dazzler of the hexagon based map is that you really only need to know one thing: the length of a side of a hexagon. After that, you can calculate everything else you need to know.
If yous know the length of side south, then yous can summate r and h. The values for a and b are pretty much irrelevant because you can calculate them from s, r, and h, and you lot don't actually need a and b for any calculations anyhow. And then, how do y'all find r and h?
h = sin( xxx°) * south r = cos( 30°) * southward b = s + ii * h a = 2 * r
My namespace is Hexagonal
For lack of a better term, I called my namespace Hexagonal, and that's where all my cadre classes live. The class Math has a bunch of static methods to handle geometric calculations. Some people may argue that these are trigonometric calculations, but for my purposes, trigonometry is a subset of geometry.
public static bladder CalculateH(float side) { return ConvertToFloat(Organisation.Math.Sin(DegreesToRadians(30)) * side); } public static bladder CalculateR(float side) { return ConvertToFloat(Organisation.Math.Cos(DegreesToRadians(30)) * side); } public static double DegreesToRadians(double degrees) { return degrees * System.Math.PI / 180; }
The Sin and Cos methods in System.Math take arguments in radians, not degrees. And then, we demand a helper method to catechumen degrees to radians.
The Hex object represents a hexagon. When creating a Hex object, y'all need to know a few things - the length of a side, the x,y coordinates of the upper vertex, and the orientation of the hex. I introduced the concept of orientation so that hexes could be created with the flat side downwards or a pointy side downwards. The orientation will touch how the vertices are calculated.
The vertices are numbered somewhat arbitrarily on my part, but we demand to refer to vertices in some manner. The important method in Hex is CalculateVertices(), which is private and chosen by the constructor. I also created an enumeration for hexagonal orientation.
public course Hex { private System.Cartoon.PointF[] points; individual bladder side; private float h; private float r; private Hexagonal.HexOrientation orientation; individual float 10; individual bladder y; ... individual void CalculateVertices() { h = Hexagonal.Math.CalculateH(side); r = Hexagonal.Math.CalculateR(side); switch (orientation) { instance Hexagonal.HexOrientation.Flat: points = new System.Cartoon.PointF[vi]; points[0] = new PointF(x, y); points[1] = new PointF(ten + side, y); points[ii] = new PointF(x + side + h, y + r); points[iii] = new PointF(x + side, y + r + r); points[4] = new PointF(ten, y + r + r); points[5] = new PointF(x - h, y + r ); break; case Hexagonal.HexOrientation.Pointy: points = new Arrangement.Drawing.PointF[vi]; points[0] = new PointF(x, y); points[1] = new PointF(x + r, y + h); points[2] = new PointF(x + r, y + side + h); points[3] = new PointF(x, y + side + h + h); points[4] = new PointF(ten - r, y + side + h); points[5] = new PointF(x - r, y + h); suspension; default: throw new Exception(" No HexOrientation defined for Hex object."); } } } public enum HexOrientation { Apartment = 0, Pointy = 1, }
The Hex class was designed to be uncomplicated. All it does is remember its position in two dimensional space. The Board form is a collection of Hex objects that represent a game board. For this offset version, the merely blazon of board that can be created is rectangular. Arranging hexagons in a rectangular shape can be washed adequately just using a two dimensional array. For example, a board with Flat orientation would map to a two dimensional assortment similar this:
The well-nigh important method in the Lath class is Initialize(), which is private and called from the constructor. Initialize() creates a ii dimensional array of Hex objects with all the calculations for the hex vertices.
public form Board { private Hexagonal.Hex[,] hexes; private int width; individual int height; private int xOffset; private int yOffset; private int side; individual float pixelWidth; private float pixelHeight; private Hexagonal.HexOrientation orientation; ... private void Initialize(int width, int height, int side, Hexagonal.HexOrientation orientation, int xOffset, int yOffset) { this.width = width; this.height = height; this.xOffset = xOffset; this.yOffset = yOffset; this.side = side; this.orientation = orientation; hexes = new Hex[meridian, width]; this.boardState = new BoardState(); bladder h = Hexagonal.Math.CalculateH(side); float r = Hexagonal.Math.CalculateR(side); float hexWidth = 0; bladder hexHeight = 0; switch (orientation) { case HexOrientation.Flat: hexWidth = side + h; hexHeight = r + r; this.pixelWidth = (width * hexWidth) + h; this.pixelHeight = (height * hexHeight) + r; intermission; case HexOrientation.Pointy: hexWidth = r + r; hexHeight = side + h; this.pixelWidth = (width * hexWidth) + r; this.pixelHeight = (acme * hexHeight) + h; break; default: break; } bool inTopRow = simulated; bool inBottomRow = fake; bool inLeftColumn = fake; bool inRightColumn = false; bool isTopLeft = fake; bool isTopRight = fake; bool isBotomLeft = false; bool isBottomRight = false; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { #region Position Booleans if (i == 0) { inTopRow = truthful; } else { inTopRow = false; } if (i == meridian - i) { inBottomRow = true; } else { inBottomRow = false; } if (j == 0) { inLeftColumn = true; } else { inLeftColumn = false; } if (j == width - ane) { inRightColumn = true; } else { inRightColumn = imitation; } if (inTopRow && inLeftColumn) { isTopLeft = true; } else { isTopLeft = false; } if (inTopRow && inRightColumn) { isTopRight = true; } else { isTopRight = faux; } if (inBottomRow && inLeftColumn) { isBotomLeft = true; } else { isBotomLeft = fake; } if (inBottomRow && inRightColumn) { isBottomRight = true; } else { isBottomRight = false; }

0 Response to "How To Draw Hexagon Grid Java"
Post a Comment