Skip to content

Instantly share code, notes, and snippets.

@JohnEarnest
Created February 22, 2014 02:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnEarnest/9147835 to your computer and use it in GitHub Desktop.
Save JohnEarnest/9147835 to your computer and use it in GitHub Desktop.
Playing with hexagons.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import static java.lang.Math.*;
public class HexScape extends JPanel {
public static void main(String[] args) {
JFrame window = new JFrame("HexScape");
HexScape app = new HexScape();
app.setPreferredSize(new Dimension(640, 480));
window.add(app);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.pack();
window.setVisible(true);
while(true) {
app.repaint();
try { Thread.sleep(1000 / 60); }
catch(InterruptedException e) {}
}
}
List<Hex> hexes = new ArrayList<Hex>();
void layer(double z, Color c) {
final double v = cos(PI/6) * Hex.r; // vertical offset
final double h = cos(PI/6) * 2 * v; // horizontal offset
for(int a = -3; a <= 3; a++) {
for(int b = -3; b <= 3; b++) {
hexes.add(new Hex(2*h*a, 2*v*b, z, c));
}
for(int b = -3; b <= 3; b++) {
hexes.add(new Hex(2*h*a-h, 2*v*b-v, z, c));
}
}
}
HexScape() {
layer(1.0, new Color( 33, 255, 242));
layer(1.1, new Color(255, 127, 0));
layer(1.4, new Color(251, 0, 7));
layer(1.7, new Color( 30, 20, 0));
}
public void paint(Graphics gd) {
Graphics2D g = (Graphics2D)gd;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color( 30, 20, 0));
g.fillRect(0, 0, getWidth(), getHeight());
g.translate(getWidth()/2, getHeight()/2);
for(Hex h : hexes) {
h.draw(g);
h.a = (h.a + h.s) % 2;
}
}
}
class Hex {
double a = random() * 1.7; // animation timer
double s = .04; // animation speed
static final double r = 60; // radius (full size)
final double x;
final double y;
final double z;
final Color c;
Hex(double x, double y, double z, Color c) {
this.x = x;
this.y = y;
this.z = z;
this.c = c;
}
Point2D.Double point(double x, double y) {
double r = sqrt(x*x + y*y);
double a = atan2(y, x);
double s = 320 * z;
double h = s * tanh(r / s);
return new Point2D.Double(h * cos(a), h * sin(a));
}
void draw(Graphics2D g) {
double av = (a % 1.0)-1; // animation value (normalized)
double ea = av*av*av +1; // cubic ease out
Path2D.Double h = new Path2D.Double();
if (a <= 1) {
Point2D.Double start = point(x + r * ea * cos(0), y + r * ea * sin(0));
h.moveTo(start.x, start.y);
for(int z = 1; z < 6; z++) {
Point2D.Double p = point(
x + (r * ea * cos(z * PI / 3)),
y + (r * ea * sin(z * PI / 3))
);
h.lineTo(p.x, p.y);
}
}
else {
Point2D.Double start = point(x + r * cos(0), y + r * sin(0));
h.moveTo(start.x, start.y);
for(int z = 1; z <= 6; z++) {
Point2D.Double p = point(
x + (r * cos(z * PI / 3)),
y + (r * sin(z * PI / 3))
);
h.lineTo(p.x, p.y);
}
for(int z = 6; z >= 0; z--) {
Point2D.Double p = point(
x + (r * ea * cos(z * PI / 3)),
y + (r * ea * sin(z * PI / 3))
);
h.lineTo(p.x, p.y);
}
}
h.closePath();
g.setColor(c);
g.fill(h);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment