Grasshopper: Get intersection points between a list of lines with C#

The following code will produce a list of points that correspond with the intersection points between an assorted list of lines.

It is written for the C# component in Grasshopper, but can be adapted to a compiled component if desired.

Note the check that 0<a<1 and 0<b<1. This represents the requirement that we want intersections to occur along the length of the line, otherwise we aren’t interested in them. If we are also interested in implied intersections (i.e. where we imagine each line continues to infinity in both directions) then we can discard this ‘if’ statement.

And a note to my future self, this code worked on quick tests but was buggy on a real project, so the code below is for reference only!

  private void RunScript(List<Line> x, ref object A)
  {

    List<Point3d> rtn_pts = new List<Point3d>();
    for (int i = 0; i < x.Count - 1; i++)
    {
      for (int j = i + 1; j < x.Count; j++)
      {
        double a;
        double b;
        Rhino.Geometry.Intersect.Intersection.LineLine(x[i], x[j], out a, out b);
        if(a >= 0 && a <= 1 && b > 0 && b < 1)
        {
          rtn_pts.Add(new Point3d(x[i].PointAt(a)));
        }
      }
    }

    A = rtn_pts;

  }

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: