How To Draw A Hexagonal Grid In Java
I am trying to scroll upwardly xix lines of code into a single for loop, just I am feeling a bit stumped. The reason I ask, is considering I desire the grid to be other sizes instead of 5.
In Primary::drawHexGridAdvanced()
, I am trying to deduce the similarities between each line as opposed to Main::drawHexGridBasic()
where I am hard-coding values.
I am not certain how to decide the outset of the 10
for each column in each row, because the blueprint for north == v
is 0, -1 -2 -2 -two
afterwards that each consecutive column is just incremented except when the loop reaches the halfway point...
Information and Understanding
`north` must exist odd n | columns-per row sequence --+------------------------- three | ii 3 2 v | iii 4 5 4 3 7 | 4 5 6 7 6 5 4 9 | 5 half dozen 7 8 9 8 seven half dozen 5
int[] columns(int north) { int[] columns = new int[n]; int h = (int) java.lang.Math.floor(n / 2); for (int i = 0; i < n; i++) { columns[i] = n - java.lang.Math.abs(i - h); } return columns; } // Prints [5, 6, 7, viii, nine, viii, 7, 6, 5] System.out.println(java.util.Arrays.toString(columns(north)));
Python looks so much more elegant:
def hex(n): for x in [(n-abs(x-int(northward/ii))) for x in range(n)]: for y in range(northward-x): impress(' '), for y in range(ten): print(' * '), print('') hex(5) # * * * # * * * * # * * * * * # * * * * # * * *
Hither is my expected output:
Main.java
package Foo.Bar.Hexagon; import java.awt.BasicStroke; import java.awt.Color; import coffee.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Stroke; import javax.swing.JFrame; import javax.swing.JPanel; public grade Master extends JPanel { individual static final long serialVersionUID = 1L; private final int WIDTH = 1200; private terminal int HEIGHT = 800; private last int W2 = WIDTH / two; individual final int H2 = Summit / 2; private Font font = new Font("Arial", Font.BOLD, 24); FontMetrics metrics; public Main() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); } @Override public void paintComponent(Graphics k) { Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(iv.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER)); g2d.setFont(font); metrics = g.getFontMetrics(); drawCircle(g2d, W2, H2, 660, truthful, true, 0x4488FF, 0); drawHexGridAdvanced(g2d, 5, 60); } private void drawHexGridAdvanced(Graphics thousand, int due north, int r) { double ang30 = Math.toRadians(30); double xOff = Math.cos(ang30) * r; double yOff = Math.sin(ang30) * r; int h = n / 2; int cols = 0; int row = 0; int col = 0; cols = 3; row = 0; col = 0; drawHex(yard, +0, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 - yOff * (n - cols) * 3), r); row = 0; col = i; drawHex(g, +ane, -h + row, (int) (W2 + xOff * (-cols + (col * ii + 1))), (int) (H2 - yOff * (north - cols) * 3), r); row = 0; col = two; drawHex(thousand, +2, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 - yOff * (n - cols) * iii), r); cols = 4; row = ane; col = 0; drawHex(g, -1, -h + row, (int) (W2 + xOff * (-cols + (col * ii + one))), (int) (H2 - yOff * (n - cols) * iii), r); row = 1; col = ane; drawHex(grand, +0, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 - yOff * (north - cols) * 3), r); row = one; col = 2; drawHex(g, +ane, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 - yOff * (n - cols) * 3), r); row = ane; col = 3; drawHex(grand, +2, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 - yOff * (n - cols) * 3), r); cols = v; row = two; col = 0; drawHex(k, -2, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + one))), (int) (H2 + yOff * (n - cols) * 3), r); row = ii; col = 1; drawHex(g, -1, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 + yOff * (n - cols) * three), r); row = 2; col = 2; drawHex(g, +0, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + i))), (int) (H2 + yOff * (n - cols) * iii), r); row = 2; col = three; drawHex(g, +1, -h + row, (int) (W2 + xOff * (-cols + (col * two + 1))), (int) (H2 + yOff * (n - cols) * 3), r); row = two; col = 4; drawHex(k, +2, -h + row, (int) (W2 + xOff * (-cols + (col * ii + 1))), (int) (H2 + yOff * (north - cols) * 3), r); cols = 4; row = 3; col = 0; drawHex(k, -two, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + one))), (int) (H2 + yOff * (n - cols) * 3), r); row = three; col = 1; drawHex(g, -1, -h + row, (int) (W2 + xOff * (-cols + (col * two + one))), (int) (H2 + yOff * (due north - cols) * three), r); row = 3; col = ii; drawHex(g, +0, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + one))), (int) (H2 + yOff * (n - cols) * iii), r); row = iii; col = three; drawHex(one thousand, +1, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + ane))), (int) (H2 + yOff * (due north - cols) * 3), r); cols = three; row = 4; col = 0; drawHex(g, -ii, -h + row, (int) (W2 + xOff * (-cols + (col * ii + 1))), (int) (H2 + yOff * (n - cols) * iii), r); row = four; col = 1; drawHex(g, -1, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 + yOff * (n - cols) * 3), r); row = four; col = 2; drawHex(g, +0, -h + row, (int) (W2 + xOff * (-cols + (col * 2 + 1))), (int) (H2 + yOff * (n - cols) * three), r); } individual void drawHexGridBasic(Graphics yard, int n, int r) { double ang30 = Math.toRadians(xxx); double xOff = Math.cos(ang30) * r; double yOff = Math.sin(ang30) * r; int h = n / two; drawHex(yard, +0, -2, W2 - (int) (xOff * 2), H2 - (int) (yOff * 6), r); drawHex(m, +1, -2, W2 - (int) (xOff * 0), H2 - (int) (yOff * half dozen), r); drawHex(thou, +2, -two, W2 + (int) (xOff * 2), H2 - (int) (yOff * 6), r); drawHex(g, -1, -i, W2 - (int) (xOff * 3), H2 - (int) (yOff * 3), r); drawHex(grand, +0, -1, W2 - (int) (xOff * i), H2 - (int) (yOff * 3), r); drawHex(one thousand, +1, -ane, W2 + (int) (xOff * 1), H2 - (int) (yOff * iii), r); drawHex(chiliad, +2, -1, W2 + (int) (xOff * 3), H2 - (int) (yOff * 3), r); drawHex(g, -2, +0, W2 - (int) (xOff * iv), H2 - (int) (yOff * 0), r); drawHex(g, -1, +0, W2 - (int) (xOff * 2), H2 - (int) (yOff * 0), r); drawHex(g, +0, +0, W2 - (int) (xOff * 0), H2 - (int) (yOff * 0), r); drawHex(g, +one, +0, W2 + (int) (xOff * 2), H2 - (int) (yOff * 0), r); drawHex(1000, +two, +0, W2 + (int) (xOff * 4), H2 - (int) (yOff * 0), r); drawHex(grand, -two, +1, W2 - (int) (xOff * iii), H2 + (int) (yOff * 3), r); drawHex(g, -1, +ane, W2 - (int) (xOff * 1), H2 + (int) (yOff * 3), r); drawHex(g, +0, +one, W2 + (int) (xOff * 1), H2 + (int) (yOff * 3), r); drawHex(yard, +1, +i, W2 + (int) (xOff * iii), H2 + (int) (yOff * iii), r); drawHex(g, -two, +2, W2 - (int) (xOff * 2), H2 + (int) (yOff * 6), r); drawHex(grand, -1, +2, W2 - (int) (xOff * 0), H2 + (int) (yOff * 6), r); drawHex(g, +0, +2, W2 + (int) (xOff * 2), H2 + (int) (yOff * 6), r); } private void drawHex(Graphics one thousand, int posX, int posY, int 10, int y, int r) { Hexagon hex = new Hexagon(x, y, r); Cord text = String.format("%s : %s", coord(posX), coord(posY)); int westward = metrics.stringWidth(text); int h = metrics.getHeight(); g.setColor(new Color(0x008844)); thou.fillPolygon(hex); g.setColor(new Color(0xFFDD88)); g.drawPolygon(hex); g.setColor(new Colour(0xFFFFFF)); g.drawString(text, x - w/2, y + h/2); } individual String coord(int value) { render (value > 0 ? "+" : "") + Integer.toString(value); } public void drawCircle(Graphics2D one thousand, int ten, int y, int diameter, boolean centered, boolean filled, int colorValue, int lineThickness) { drawOval(g, 10, y, diameter, diameter, centered, filled, colorValue, lineThickness); } public void drawOval(Graphics2D grand, int x, int y, int width, int height, boolean centered, boolean filled, int colorValue, int lineThickness) { // Store before changing. Stroke tmpS = g.getStroke(); Color tmpC = k.getColor(); grand.setColor(new Color(colorValue)); g.setStroke(new BasicStroke(lineThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); int x2 = centered ? x - (width / 2) : x; int y2 = centered ? y - (height / ii) : y; if (filled) g.fillOval(x2, y2, width, height); else thou.drawOval(x2, y2, width, elevation); // Set values to previous when washed. k.setColor(tmpC); grand.setStroke(tmpS); } public static void master(String[] args) { JFrame f = new JFrame(); Main p = new Principal(); f.setContentPane(p); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }
Haxagon.java
package Foo.Bar.Hexagon; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Bespeak; import java.awt.Polygon; import java.awt.Stroke; public form Hexagon extends Polygon { private static concluding long serialVersionUID = 1L; public static final int SIDES = half-dozen; private Point[] points = new Betoken[SIDES]; private Bespeak center = new Signal(0, 0); private int radius; private int rotation = ninety; public Hexagon(Point middle, int radius) { npoints = SIDES; xpoints = new int[SIDES]; ypoints = new int[SIDES]; this.middle = centre; this.radius = radius; updatePoints(); } public Hexagon(int x, int y, int radius) { this(new Point(x, y), radius); } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; updatePoints(); } public int getRotation() { return rotation; } public void setRotation(int rotation) { this.rotation = rotation; updatePoints(); } public void setCenter(Signal center) { this.heart = center; updatePoints(); } public void setCenter(int x, int y) { setCenter(new Point(x, y)); } private double findAngle(double fraction) { return fraction * Math.PI * 2 + Math.toRadians((rotation + 180) % 360); } private Point findPoint(double bending) { int ten = (int) (center.x + Math.cos(angle) * radius); int y = (int) (middle.y + Math.sin(angle) * radius); return new Point(x, y); } protected void updatePoints() { for (int p = 0; p < SIDES; p++) { double angle = findAngle((double) p / SIDES); Point signal = findPoint(bending); xpoints[p] = point.10; ypoints[p] = point.y; points[p] = point; System.out.printf("%d. (%d, %d)\n", p, point.x, signal.y); } } public void drawPolygon(Graphics2D k, int 10, int y, int lineThickness, int colorValue, boolean filled) { // Store before changing. Stroke tmpS = g.getStroke(); Colour tmpC = g.getColor(); g.setColor(new Color(colorValue)); g.setStroke(new BasicStroke(lineThickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER)); if (filled) thousand.fillPolygon(xpoints, ypoints, npoints); else yard.drawPolygon(xpoints, ypoints, npoints); // Set values to previous when done. g.setColor(tmpC); g.setStroke(tmpS); } }
Source: https://stackoverflow.com/questions/20734438/algorithm-to-generate-a-hexagonal-grid-with-coordinate-system
Posted by: lesherporwhou.blogspot.com
0 Response to "How To Draw A Hexagonal Grid In Java"
Post a Comment