Hexagon PolyLines C#

unhandled
csharp

#1

Hi all,

I try make a Hexagon Grid on a Surface in c#.Just like the Lunchbox component.

As base I used the scipts Nathan miller provided here: http://wiki.theprovingground.org/scripts-paneling

var p = new List<Polyline>();
double uStep = 1.0 / U;
double vStep = 1.0 / V;

t = t * uStep;

S.SetDomain(0, new Interval(0, 1));
S.SetDomain(1, new Interval(0, 1));

for (int i = 0; i < U; i++)
{
  for (int j = 0; j < V; j++)
  {

    if (j == V - 1) // upper strip
    {
      var pts = new List<Point3d>(7);
      pts.Add(S.PointAt(i * uStep, (j - t) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j + t) * vStep));//toMove
      pts.Add((S.PointAt((i + 1) * uStep, (j - t) * vStep)));
      pts.Add(S.PointAt((i + 1) * uStep, (j + 1 ) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j + 1 ) * vStep));//toMove
      pts.Add(S.PointAt(i * uStep, (j + 1) * vStep));
      pts.Add(S.PointAt(i * uStep, (j - t) * vStep));

      p.Add(new Polyline(pts));
    }

    if (j == 0)// lowest strip
    {
      var pts = new List<Point3d>(7);

      pts.Add(S.PointAt(i * uStep, (j ) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j ) * vStep));//toMove
      pts.Add((S.PointAt((i + 1) * uStep, (j ) * vStep)));
      pts.Add(S.PointAt((i + 1) * uStep, (j + 1 + t) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j + 1 - t) * vStep));//toMove
      pts.Add(S.PointAt(i * uStep, (j + 1 + t) * vStep));
      pts.Add(S.PointAt(i * uStep, (j - t) * vStep));

      p.Add(new Polyline(pts));
    }

    if (j % 2 == 0 && (j != V - 1) && (j != 0))
    {
      var pts = new List<Point3d>(7);

      pts.Add(S.PointAt(i * uStep, (j - t) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j + t) * vStep));//toMove
      pts.Add((S.PointAt((i + 1) * uStep, (j - t) * vStep)));
      pts.Add(S.PointAt((i + 1) * uStep, (j + 1 + t) * vStep));
      pts.Add(S.PointAt((i + 1 - 0.5) * uStep, (j + 1 - t) * vStep));//toMove
      pts.Add(S.PointAt(i * uStep, (j + 1 + t) * vStep));
      pts.Add(S.PointAt(i * uStep, (j - t) * vStep));

      p.Add(new Polyline(pts));
    }

    // odd rows
    if (j % 2 == 1)
    {
      if (i < U - 1 )
      {
        var pts = new List<Point3d>(7);

        pts.Add(S.PointAt((i + 0.5 ) * uStep, (j - t) * vStep));
        pts.Add(S.PointAt((i + 1) * uStep, (j + t) * vStep));
        pts.Add(S.PointAt((i + 1.5) * uStep, (j - t) * vStep));//moveDown
        pts.Add(S.PointAt((i + 1.5) * uStep, (j + 1 + t) * vStep));//moveUp
        pts.Add(S.PointAt((i + 1) * uStep, (j + 1 - t ) * vStep));
        pts.Add(S.PointAt((i + 0.5) * uStep, (j + 1 + t) * vStep));
        pts.Add(S.PointAt((i + 0.5 ) * uStep, (j - t) * vStep));

        p.Add(new Polyline(pts));
      }

      //left row
      if (i == 0)
      {
        var pts = new List<Point3d>(7);

        pts.Add(S.PointAt(i * uStep, (j + t) * vStep));
        pts.Add(S.PointAt((i + 0.5) * uStep, (j - t) * vStep));
        pts.Add(S.PointAt((i + 0.5) * uStep, (j + 1 + t) * vStep));
        pts.Add(S.PointAt(i * uStep, (j + 1 - t) * vStep));
        pts.Add(S.PointAt(i * uStep, (j + t) * vStep));

        p.Add(new Polyline(pts));
      }

      //right row
      if (i == U - 1)
      {
        var pts = new List<Point3d>(7);

        pts.Add(S.PointAt((i + 0.5) * uStep, (j - t) * vStep));
        pts.Add(S.PointAt((i + 1) * uStep, (j + t) * vStep));
        pts.Add(S.PointAt((i + 1) * uStep, (j + 1 - t) * vStep));
        pts.Add(S.PointAt((i + 0.5) * uStep, (j + 1 + t) * vStep));
        pts.Add(S.PointAt((i + 0.5) * uStep, (j - t) * vStep));

        p.Add(new Polyline(pts));
      }
    }
  }
}
Panels = p;

Its some kind of working but not really.

Maybe someone likes to help me puzzle, if not I would understand since its already a few lines, but as we say in my village: asking is free.

Regads!

HexGrid.gh (6.2 KB)


(David Rutten) #2

That’s a lot of code to look through for something that’s some kind of working but not really. Can you narrow it down to a specific loop or even line?


#3

Hi,

sorry for being so inprecise about that, when I read it again(some kind of working but not really) it actually made me laugh a bit.So thanks for answering even after that lazy description.
I will attach some pictures to explain myself this time( I hope).
So the actual state is that when the v divisions are a odd number it worlk fine:

Even numbers cause the following overlap in the highest raw.:

I wasnt able to figure out which line it causes, I think I might add a loop condition more to for the even cases.But I am not sure in total if I am on the rght way.

A minor issue should be put the correct t value so that it goes from triangle to hexagon to triangle in a range from 0 to 1. But also there I still failed.

So,thanks again,

if you are not interested anymoreI would completly understand.

Have a nice Weekend!


(1138127311) #4

Hi Baris
I used to do this kind of work ,and I share you my file and screenshot, Hope that can help you


Hexagons from surface.gh (17.9 KB)


#5

HI @tangchisg,

thanks for sharing!

You are using a different approch I think and changing mine to this would be a starting from zero basically.

I also found one Peter made with the same logic, but its way too advanced for me: http://www.grasshopper3d.com/forum/topics/hexagon-grid-deform?page=1&commentId=2985220%3AComment%3A1350619&x=1#2985220Comment1350619

I think a third approach would be do it completly by Pythagoras, but also that would be start by zero.

I ll keep trying…

Again thanks for sharing, its really nice done I think!

Regards!