Hexagonal Grid - Adjacent hexagons array
in
Programming Questions
•
4 months ago
Hello all,
At this moment I am working on a program to show an emergent system like The Game of Life from Conway. I want each hexagon to follow 3 different rules, causing an emergent behaviour.
I managed to make a hexagonal grid, but now I want to add 3 rules, where all units should listen to:
1. your color should be the average color (grayscale) of your neighbors (the 6 adjacent cells have a colorvalue, so the colorvalue of this hexagon should be the sum allcolorvalues/6.
2 if the colorvalues of your neigbors are to close to eachother, your value should rise to the maximum colorvalue (with RGB this is 255 (white), when the average colorvalue of your neighbors is LOWER then 127 (half of 255).
3. if the colorvalues of your neigbors are to close to eachother, your value should rise to the minimum colorvalue (with RGB this is 0 (black), when the average colorvalue of your neighbors is HIGHER then 127 (half of 255).
In this way there is no form of saturation and all units should behave emergent.
Does anyone know how I can give those rules to all the hexagons?
Bellow you will find the code for the hexagongrid.
Code:
Hexagon[][] hexagon;
int rad = 15;
int hexcountx, hexcounty;
void setup()
{
size(1200, 800);
smooth();
frameRate(50);
hexcountx = (height/(rad));
hexcounty = (width/(rad)*2);
hexagon = new Hexagon [hexcountx][hexcounty];
for (int i = 0; i < hexcountx; i++){
for (int j = 0; j < hexcounty; j++){
if ((j % 2) == 0) {
hexagon[i][j] = new Hexagon((3 * rad * i), (.866 * rad * j), rad);
} else {
hexagon[i][j] = new Hexagon(3 * rad * (i + .5), .866 * rad * j, rad);
}
}
}
}
void draw(){
for (int i = 0; i < hexcountx; i ++ ) {
for (int j = 0; j < hexcounty; j ++ ) {
hexagon[i][j].display();
}
}
}
class Hexagon{
float x,y,radi;
float angle = 360.0 / 6;
Hexagon(float cx, float cy, float r)
{
x=cx;
y=cy;
radi=r;
}
void display(){
beginShape();
for (int i = 0; i < 6; i++)
{
vertex(x + radi * cos(radians(angle * i)),
y + radi * sin(radians(angle * i)));
}
colorMode(HSB, 500);
fill(500);
endShape(CLOSE);
}
}
1