0

I'm creating an image filter that will create a honeycomb effect by finding the averages of groups of hexagons. This code will generate a grid of hexagons for any given size, and will eventually be limited to the size of the canvas:

float unitSize = 20;
float eqTriH = 0.8660254;
int cellsX;
int cellsY;
void setup() {
  size(700, 700);
  cellsX = int(width/(unitSize*3));
  cellsY = int(height/(unitSize*eqTriH*2+eqTriH));
  smooth();
}

void draw() {
  for (int i = 0; i < cellsX; i++) {
    for (int j = 0; j < cellsY; j++) {
      beginShape();
      for (int k = 0; k < 6; k++) vertex(unitSize+i*unitSize*3+cos(TWO_PI/6*k)*unitSize, unitSize*eqTriH+j*unitSize*2*eqTriH+sin(TWO_PI/6*k)*unitSize);
      endShape(CLOSE);
      beginShape();
      for (int k = 0; k < 6; k++) vertex(unitSize*2.5+i*unitSize*3+cos(TWO_PI/6*k)*unitSize, unitSize*eqTriH*2+j*unitSize*2*eqTriH+sin(TWO_PI/6*k)*unitSize);
      endShape(CLOSE);
    }
  }
}

This works fine, however, I can't figure out how to, for every hexagon that is drawn, get an array of pixel locations inside each hexagon.

If this question is too vague, please let me know so I can get more specific.

EDIT: when I say I want to determine if a pixel is in the shape, I mean, once I can do this, I'm going to loop through, THEN create an array.

Tessa Painter
  • 2,113
  • 2
  • 16
  • 21
  • Is your end goal to find out whether a point is inside a hexagon? Why do you need to loop over the actual pixels? What are you doing with them? – Kevin Workman Dec 12 '17 at 17:45
  • @KevinWorkman The end goal would be ultimate to find the average color of everything inside a polygon. – Tessa Painter Dec 13 '17 at 05:14

1 Answers1

1

There are two approaches:

  • walk through all image pixels, determine what hexagon every pixel belongs to, and write its coordinate in corresponding list/array

  • rasterize every hexagon, getting scan lines - ready-to-use continuous pixels arrays.

I think that the second approach is better for your case. You have coordinates of vertices. It seems that your hexagons are flat-topped (horizontal), so scan upper trapezoid then bottom one.

pseudocode for bottom trapezoid:

for y = (centery; y <= centery + ysize; y++)
    left_x = (int) themostleftx + (y - centery) * eqTriH
    right_x = (int) themostright - (y - centery) * eqTriH
    get horizontal segment of pixels (left_x,y)-(right_x,y)
MBo
  • 77,366
  • 5
  • 53
  • 86