 # Voronoi C#

Greeting,
I have to calculate the 2d Voronoi based area for a selection of points using c# API. Is there any method or way to calculate Voronoi cells using API?

Hello
I am sure there is one in Grasshopper but I don’t think it is visible. Because there is just that !
https://developer.rhino3d.com/api/grasshopper/search.html?SearchText=voronoi

For rhino only see this way around

1 Like

Thanks for the response i also found this: Grasshopper.Kernel.Geometry.Voronoi. I do not get how to use it though.

As there is no real documentation … it is treasure hunt …

the most simple coud be to use that c#

1 Like

I am using c# within visual studio for this specific project. in python we have access to nodes. it seams like in c# API we cannot use nodes.

Here the C# function from @AndersDeleuran
GhPythonCSharpVoronoi_.gh (55.8 KB)

``````  private void RunScript(List<Point3d> nodePts, object y, ref object polylines)
{

//# Create a boundingbox and get its corners
BoundingBox bb = new BoundingBox(nodePts);
Vector3d d = bb.Diagonal;
double dl = d.Length;
double f = dl / 15;
bb.Inflate(f, f, f);
Point3d[] bbCorners = bb.GetCorners();

//# Create a list of nodes
Node2List nodes = new Node2List();
foreach(Point3d p in nodePts)
{
Node2 n = new Node2(p.X, p.Y);
nodes.Append(n);
}

//Create a list of outline nodes using the BB
Node2List outline = new Node2List();
foreach (Point3d p in bbCorners)
{
Node2 n = new Node2(p.X, p.Y);
outline.Append(n);
}

//# Calculate the delaunay triangulation
var delaunay = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Connectivity(nodes, 0.1, false);

// # Calculate the voronoi diagram
var voronoi = Grasshopper.Kernel.Geometry.Voronoi.Solver.Solve_Connectivity(nodes, delaunay, outline);

//# Get polylines from the voronoi cells and return them to GH
List<Polyline> polys = new  List<Polyline> ();
foreach( var c in voronoi)
{

Polyline pl = c.ToPolyline();

}
polylines = polys;
}``````
2 Likes

Thank you i think this will solve my problem. I’ll try it and share the result.

1 Like

Worked like a charm. Thank you for your help and time

No problem I learned also, it could be useful for me in the future.

1 Like

Hello! very usefull code! in case Iwanted to visualize the delaunay triangulation how could I do that?