C# pointsGrid

Line 58. I am trying to learn C#. I looked some things up. I am trying to get a grid of points.
What I am missing?

pointGrid00.gh (10.8 KB)

https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A180030

private void RunScript(Plane plane, double s, int Ex, int Ey, ref object P)
{
  List<Point3d> points = new List<Point3d>();
  Vector3d x = plane.XAxis * s;
  Vector3d y = plane.YAxis * s;
  Point3d origin = plane.Origin;
  for (int i = 0; i < Ex; i++)
  {
    for (int j = 0; j < Ey; j++)
    {
      points.Add(origin + (x * i + y * j));
    }
  }
  P = points;
}

Grid.gh (3.6 KB)

1 Like

Do you might know what is wrong is my code?

Vector3d x_dir = new Vector3d(Plane.XAxis);
x_dir = x_dir * Dis;
Vector3d y_dir = new Vector3d(Plane.YAxis);
y_dir = y_dir * Dis;
Point3d Base = new Point3d(Plane.Origin);
int i = 0;
int j = 0;
for (i = 0; i <= XNum; i++){
  for (j = 0; j <= YNum; j++){
    Point3d newPt = new Point3d(Base);
    Vector3d V1 = new Vector3d(x_dir);
    V1 = V1 * i;
    Vector3d V2 = new Vector3d(y_dir);
    V2 = V2 * j;
    newPt = newPt + V1 + V2;
    Points.Add(newPt);
  }
}

Nothing.


Points.gh (7.1 KB)

1 Like

Regarding the error message, I cannot open Rhino right now, usually the error message is telling you the exact reason. I might look at it later or you post it here, but I cant spot it from the code. Looks good so far!

What @Mahdiyar did differently is a syntax improvement. Since Vector3d and Point3d are structs and no classes, whenever you get an instance, a struct will always be copied. Therefore you don‘t need to use the new keyword, using the copy constructor. If you don‘t know if its a struct or not, and the object has a copy constructor, then your approach is always the clearest way of indicating duplication.

The other improvement is that loop iteration variables (i and j) can be initialized within the loop definition. You don‘t need to do it, but it shortens code. if you write „for“ and press tab, it will autocreate this „for“ loop (its a code snippet).

I propose to stick to C# naming conventions, local variables and field variables written camelCase and public fields, properties, methods, events, delegates and classes in PascalCase. I do diverge a bit by writing private fields _camelCase. This way its obvious what is what.

1 Like

:thinking: I feel quite dull when saying I cannot solve this problem. My eyes are now diverging (for reference see profile picture).
:sweat_smile: Please, can someone tell me why it does not except the Point3d? I also did Point3d newPn (0,0,0) but that does not work either. Or Rhino.Geometry.Point3d, etc.
:no_mouth: And I went through several tutorials, I can make swarms too.

List<Point3d> pnts = new List<Point3d>();
for(int i = 0; i < x;i++){
  for(int j = 0; j < y;j++){
    pnts.Add(Point3d(i, j, 0));
  }
}
A = pnts;

question3.gh (6.1 KB)

Type
pnts.Add(new Point3d(i, j, 0))
instead of
pnts.Add(Point3d(i, j, 0))
since you construct a new point.

1 Like