void SetupHexagonalGrid() { // Make cell regions: we assume cells have only 1 region but that can change in the future int l = _numCells; int qx, qy; int q = (int)(Math.Sqrt (l)); q = q * 12 / 13; if (q<1) q= 1; qx=qy=q; int qx2 = qx * 4 / 3; double stepX = (transform.localScale.y / transform.localScale.x) / qx; double stepY = (transform.localScale.x / transform.localScale.y) / qy; double halfStepX = stepX*0.5; double halfStepY = stepY*0.5; Segment [,,] sides = new Segment[qx2,qy,6]; // 0 = left-up, 1 = top, 2 = right-up, 3 = right-down, 4 = down, 5 = left-down int c = -1; int subdivisions = goodGridCurvature > 0 ? 3: 1; for (int j=0;j<qy;j++) { for (int k=0;k<qx2;k++) { Point center = new Point((double)k/qx-0.5+halfStepX,(double)j/qy-0.5+halfStepY); center.x -= k * halfStepX/2; Cell cell = new Cell( (++c).ToString(), new Vector2((float)center.x, (float)center.y)); double offsetY = (k % 2==0) ? 0: -halfStepY; Segment leftUp = (k>0 && offsetY<0) ? sides[k-1, j, 3]: new Segment(center.Offset(-halfStepX, offsetY), center.Offset(-halfStepX/2, halfStepY + offsetY), k==0 || (j==qy-1 && offsetY==0)); sides[k, j, 0] = leftUp; Segment top = new Segment(center.Offset(-halfStepX/2, halfStepY + offsetY), center.Offset(halfStepX/2, halfStepY + offsetY), j==qy-1); sides[k, j, 1] = top; Segment rightUp = new Segment(center.Offset(halfStepX/2, halfStepY + offsetY), center.Offset(halfStepX, offsetY), k==qx2-1 || (j==qy-1 && offsetY==0)); sides[k, j, 2] = rightUp; Segment rightDown = (j > 0 && k<qx2-1 && offsetY<0) ? sides[k+1,j-1,0]: new Segment(center.Offset(halfStepX, offsetY), center.Offset(halfStepX/2, -halfStepY + offsetY), (j==0 && offsetY<0)|| k==qx2-1); sides[k, j, 3] = rightDown; Segment bottom = j>0 ? sides[k, j-1, 1] : new Segment(center.Offset(halfStepX/2, -halfStepY + offsetY), center.Offset(-halfStepX/2, -halfStepY +offsetY), true); sides[k, j, 4] = bottom; Segment leftDown; if (offsetY<0 && j>0) { leftDown = sides[k-1, j-1, 2]; } else if (offsetY==0 && k>0) { leftDown = sides[k-1, j, 2]; } else { leftDown = new Segment(center.Offset(-halfStepX/2, -halfStepY+offsetY), center.Offset(-halfStepX, offsetY), true); } sides[k, j, 5] = leftDown; if (j==0) { // leftDown.CropBottom(); // bottom.CropBottom(); // rightDown.CropBottom(); } if (k==qx2-1) { top.CropRight(); rightUp.CropRight(); rightDown.CropRight(); bottom.CropRight(); } Region cr = new Region (cell); if (subdivisions>1) { if (!top.deleted) cr.segments.AddRange (top.Subdivide(subdivisions, _gridCurvature)); if (!rightUp.deleted) cr.segments.AddRange (rightUp.Subdivide(subdivisions, _gridCurvature)); if (!rightDown.deleted) cr.segments.AddRange (rightDown.Subdivide(subdivisions, _gridCurvature)); if (!bottom.deleted) cr.segments.AddRange (bottom.Subdivide(subdivisions, _gridCurvature)); if (!leftDown.deleted) cr.segments.AddRange (leftDown.Subdivide(subdivisions, _gridCurvature)); if (!leftUp.deleted) cr.segments.AddRange (leftUp.Subdivide(subdivisions, _gridCurvature)); } else { if (!top.deleted) cr.segments.Add (top); if (!rightUp.deleted) cr.segments.Add (rightUp); if (!rightDown.deleted) cr.segments.Add (rightDown); if (!bottom.deleted) cr.segments.Add (bottom); if (!leftDown.deleted) cr.segments.Add (leftDown); if (!leftUp.deleted) cr.segments.Add (leftUp); } Connector connector = new Connector(); connector.AddRange(cr.segments); cr.polygon = connector.ToPolygon(); // FromLargestLineStrip(); if (cr.polygon!=null) { cell.region = cr; cells.Add (cell); } } } }
void SetupBoxGrid(bool strictQuads) { // Make cell regions: we assume cells have only 1 region but that can change in the future int l = _numCells; int qx, qy; int q = (int)(Math.Sqrt (l)); if (strictQuads) { qx=qy=q; } else { qx=l; qy=1; if (q<1) q=1; if ( (int)(q*q) != l) { // not squared if (!GetTwoFactors(l, out qx, out qy)) { // if number > 10 and it's prime, reduce by one so we can avoid ugly accordian grids if (l>10) GetTwoFactors(l-1, out qx, out qy); } } else { qx = qy = q; } } double stepX = (transform.localScale.y / transform.localScale.x) / qx; double stepY = (transform.localScale.x / transform.localScale.y) / qy; double halfStepX = stepX*0.5; double halfStepY = stepY*0.5; Segment [,,] sides = new Segment[qx,qy,4]; // 0 = left, 1 = top, 2 = right, 3 = bottom int c = -1; int subdivisions = goodGridCurvature > 0 ? 3: 1; for (int k=0;k<qx;k++) { for (int j=0;j<qy;j++) { Point center = new Point((double)k/qx-0.5+halfStepX,(double)j/qy-0.5+halfStepY); Cell cell = new Cell( (++c).ToString(), new Vector2((float)center.x, (float)center.y)); Segment left = k>0 ? sides[k-1, j, 2] : new Segment(center.Offset(-halfStepX, -halfStepY), center.Offset(-halfStepX, halfStepY), true); sides[k, j, 0] = left; Segment top = new Segment(center.Offset(-halfStepX, halfStepY), center.Offset(halfStepX, halfStepY), j==qy-1); sides[k, j, 1] = top; Segment right = new Segment(center.Offset(halfStepX, halfStepY), center.Offset(halfStepX, -halfStepY), k==qx-1); sides[k, j, 2] = right; Segment bottom = j>0 ? sides[k, j-1, 1] : new Segment(center.Offset(halfStepX, -halfStepY), center.Offset(-halfStepX, -halfStepY), true); sides[k, j, 3] = bottom; Region cr = new Region (cell); if (subdivisions>1) { cr.segments.AddRange (top.Subdivide(subdivisions, _gridCurvature)); cr.segments.AddRange (right.Subdivide(subdivisions, _gridCurvature)); cr.segments.AddRange (bottom.Subdivide(subdivisions, _gridCurvature)); cr.segments.AddRange (left.Subdivide(subdivisions, _gridCurvature)); } else { cr.segments.Add (top); cr.segments.Add (right); cr.segments.Add (bottom); cr.segments.Add (left); } Connector connector = new Connector(); connector.AddRange(cr.segments); cr.polygon = connector.ToPolygon(); // FromLargestLineStrip(); if (cr.polygon!=null) { cell.region = cr; cells.Add (cell); } } } }