need id's to delete to get a hexagrid
in
Programming Questions
•
8 months ago
I'm trying to get a hexagon grid, not in processing but in ICE, a node based programming system (
https://vimeo.com/19028797 ).
for example, i have this grid:
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Then i need to keep the dark ones:
[|][ ][|][ ][|]
[ ][|][ ][|][ ]
[|][ ][|][ ][|]
[ ][|][ ][|][ ]
To place a hexagon shape over it.
Since i'm better in processing (and i like processing) i thought lets try it here first (with ICE back in my head).
For ICE the easiest will be to delete the id's of the points i don't want, so in processing i try to create an array of the id's i have to remove.
There are 4 cases,
xSteps odd and ySteps odd
xSteps odd and ySteps even
xSteps even and ySteps odd
xSteps even and ySteps even
I solved the first 2, i only have problems with the last 2 (they are quite similair).
for xSteps - ySteps 4 by 5 it will be like this:
0 2 5 7 8 10 13 15 16 18 - 21 23 24 26 29 31...
hope someone can help
- import java.util.*;
- // try different odds and even for x and y
- // make sure z works with more then 1 layer
- int xSteps = 5;
- int ySteps = 5;
- int zSteps = 3;
- int maxPoints = xSteps*ySteps*zSteps;
- float spacing = 20;
- ArrayList<Point> points = new ArrayList<Point>();
- void setup() {
- size(800, 800, OPENGL);
- smooth();
- createGrid();
- ArrayList<Integer> toRemoveIndexes = getToRemoveIndexes();
- println(toRemoveIndexes.size());
- for (int i = toRemoveIndexes.size()-1; i >= 0; i--) {
- println(toRemoveIndexes.get(i));
- int r = toRemoveIndexes.get(i);
- points.remove(r);
- }
- println(points.size());
- }
- ArrayList<Integer> getToRemoveIndexes() {
- ArrayList<Integer> indexes = new ArrayList<Integer>();
- if (odd(xSteps) && odd(ySteps)) {
- for (int i = 0; i < maxPoints; i++) {
- // i += 2 would also work instead of modullo
- // but this is probably easier in ICE
- if (i%2==0) indexes.add(i);
- }
- }
- else if (odd(xSteps) && even(ySteps)) {
- int offset = 0;
- for (int i = 0; i < maxPoints; i++) {
- if (i % (xSteps * ySteps) == 0) {
- if (offset == 1) offset = 0;
- else offset = 1;
- }
- if (i%2==0) indexes.add(i+offset);
- }
- }
- // wrong
- else if (even(xSteps) && odd(ySteps)) {
- for (int i = 0; i < maxPoints; i++) {
- int offset = 0;
- if ( i % xSteps == 0) {
- if (offset == 1) offset = 0;
- else offset = 1;
- }
- if (i%2==0) indexes.add(i+offset);
- }
- }
- // todo
- else if (even(xSteps) && even(ySteps)) {
- }
- return indexes;
- }
- boolean odd(int number) {
- return !(number % 2 == 0);
- }
- boolean even(int number) {
- return number % 2 == 0;
- }
- void draw() {
- background(0);
- noStroke();
- lights();
- translate(width/2, height/2, 400);
- rotateX(map(mouseY, 0, height, -PI, PI));
- rotateY(map(mouseX, 0, width, -PI, PI));
- translate(-(spacing*xSteps/2), -(spacing*ySteps/2));
- for (Point p : points) {
- pushMatrix();
- translate(p.x, p.y, p.z);
- box(5);
- popMatrix();
- }
- }
- void createGrid() {
- // clear
- points = new ArrayList<Point>();
- int id = 0;
- for (int z = 0 ; z < zSteps; z++) {
- for (int y = 0 ; y < ySteps; y++) {
- for (int x = 0 ; x < xSteps; x++) {
- points.add(new Point(id, x*spacing, y*spacing, z*spacing*2));
- id++;
- }
- }
- }
- }
- class Point {
- float x, y, z;
- int id;
- Point(int id, float x, float y, float z) {
- this.id = id;
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
1