Hi!

My c# component crashes when I try to return a list of points and only works when I return a single point.

I can´t see what I´m doing wrong. Help. Thanks.

Code below:

Hi!

My c# component crashes when I try to return a list of points and only works when I return a single point.

I can´t see what I´m doing wrong. Help. Thanks.

Code below:

Hi can you share the full code? Whats HexLns(), and why do you call it not only once? Returns it an error message or does it run in endless loop?

It’s very likely that you should cache `HexLns()`

.

Sure ,here is the complete code:

I´m getting a List of LineCurves from HexLns(), and trying to get all the PointAtStart of those LineCurves in the Pts() function.

As I said in my previous post, if I change the function to return a single PointAtStart, it will work, it can return any point coming from the LineCurves at HexLns() List but when I try to make it into a Point3d List, it just runs an endless loop.

I need all the PointAtStart and PointAtEnd comming from the LineCurves in HexLns(), for now I was just trying to make it work with PointAtStart.

Can you spot what I´m doing wrong?

*(Warning: if you run the code as is with the function Pts() as a List, it will run into an endless loop)*

Thanks

```
class HankingHex
{
int sides;
int numVertex;
int numPcs;
public int xNumCell;
public int yNumCell;
double size;
List<Hexagon> hex;
public HankingHex(double size, int xNumCell, int yNumCell)
{
this.size = size;
this.xNumCell = xNumCell;
this.yNumCell = yNumCell;
hex = new List<Hexagon>();
sides = 6;
numPcs = xNumCell * yNumCell;
numVertex = numPcs * sides;
}
public List<LineCurve> HexLns() ///// Hexagon LineCurves
{
double h = 2 * size;
double w = Math.Sqrt(3) * size;
double vertCent = h * 0.75;
double intXCell = Convert.ToInt32(w);
double intYCell = Convert.ToInt32(h);
double matX = intXCell * xNumCell;
double matY = intYCell * yNumCell;
int row = 0;
List<LineCurve> lc = new List<LineCurve>();
for (double cenY = h; cenY < matY; cenY += vertCent)
{
double offX = ((row % 2) == 0) ? w : w / 2;
for (double cenX = offX; cenX < matX; cenX += w)
{
hex.Add(new Hexagon(size, cenX, cenY));
}
row++;
}
for (int i = 0; i < hex.Count; i++)
{
lc.AddRange(hex[i].HexU());
}
return lc;
}
public List<Point3d> Pts() ////ERROR endless loop
{
List<Point3d> startPts = new List <Point3d>();
for ( int i = 0; i < HexLns().Count; i++)
{
startPts.Add(HexLns()[i].PointAtStart);
}
return startPts;
}
}
///////////// Hexagon Cell Creation
class Hexagon
{
double cenX;
double cenY;
double size;
int sides;
public Hexagon(double _size, double _cenX, double _cenY)
{
this.cenX = _cenX;
this.cenY = _cenY;
this.size = _size;
sides = 6;
}
public LineCurve[] HexU()
{
Point3d[] pts = new Point3d[sides];
LineCurve[] lns = new LineCurve[sides];
for (int i = 0; i < sides; i++)
{
double angle_deg = 60 * i - 30;
double angle_rad = Math.PI / 180 * angle_deg;
double edX = cenX + size * Math.Cos(angle_rad);
double edY = cenY + size * Math.Sin(angle_rad);
pts[i] = new Point3d(edX, edY, 0);
}
for (int j = 0; j < sides - 1; j++)
{
lns[j] = new LineCurve(pts[j], pts[j + 1]);
}
lns[5] = new LineCurve(pts[sides - 1], pts[0]);
return lns;
}
}
```

¿Could you elaborate more please?

thanks

Ok from just looking at your file, you do something very wrong. You call HexLen() more then once. Now if you do things like this, you should ensure to do it only once. Thats what @gankeyu means by caching it (calling and storing it) .Now you also have to make sure to clear your „hex“ collection at least if you recompute it. Otherwise you are adding the same hexagons over and over again. I think before proceeding with coding on this one, you should really learn to understand the differences between fields, properties and local variables. If you define the List of Hexagons as a public property, you can always access its count without invoking any method.