Checking for Circles within Circles, c#

Hello,

Beginning grasshopper question here, couldn’t find the answer through googling, etc…

I’d like to randomly populate a plane with non-overlapping circles of random diameter, using the c# component on the latest Mac release (not WIP). I can successfully check for intersection using the attached, but this still permits circles within circles, as seen in the image. If someone were able to point me to the easiest c# way to get a bool value indicating whether one circle is within another (with the assumption that they are co-planar), I’d be most appreciative and grateful!

c#Intersections.gh (4.5 KB)

Many thanks!, Michael

You can easily check intersecting and overlapping of two circles by comparing the distance of their centers to sum of their radius-es.

....
 bool overlap = false;
  for (int j = 0; j < circles.Count; j++)
  {
    double distance = tempPnt.DistanceTo(circles[j].Center);
    double sumOfRadius = tempCircle.Radius + circles[j].Radius;
    if(distance < sumOfRadius)
      overlap = true;
  }
...

Michael.gh (15.9 KB)

1 Like

Something like that ? The same as @Mahdiyar :clap:


c#Intersections.gh (4.3 KB)

Some experiment making the range of radius evolving from [0.5, 0.5] to [0.5, 5.5]

6 Likes

…Just a quick GIF of the result:

1 Like

Hi @michael.theodore ,
you could also have a look at this course:

2 Likes

Thanks so much for your reply, and for all the other generous replies on this thread, so appreciated, all of you!!!

I should have mentioned that ultimately I’m going to want to compare curves of arbitrary closed shapes, so this trick for circle comparison won’t apply in that case.

Is the easiest thing in this more general case to convert the circles into Breps? In the attached, I convert to Breps, and in order to check whether they intersect, I use Rhino.Geometry.Brep.CreateBooleanIntersection (perhaps this is my first mistake?).
Should I expect to get a null when there is no intersection (or is the lack of intersection not the same thing as “failure”?).

Commenting out either of these two lines shows that this code isn’t quite correct (because the results of the test later on are always the same):
//Point3d myPnt2 = new Point3d(10, 10, 0);
Point3d myPnt2 = new Point3d(3, 3, 0);

Any pointers on how to check for intersection between two Breps?

Update: I think this may be the method that I want, but haven’t been able to find sample code, and am confused about proper way to call: https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Intersect_Intersection_BrepBrep.htm

Thanks again, MichaelbrepIntersection.gh (6.1 KB)

For curve containement you can first use intersection event (what you did) and then
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_Contains_2.htm

Thanks for your reply.

I’m confused because the problem case is where the intersection test fails (inner curve is contained within but not touching the outer curve) , and therefore there is no event (unless I’m misunderstanding what you’re suggesting)…

What I mean is
for (int j = 0; j < numCircles; j++){
var events = Rhino.Geometry.Intersect.Intersection.CurveCurve(circles[j].ToNurbsCurve(), tempCircle.ToNurbsCurve(), intersection_tolerance, overlap_tolerance);
if (events.Count > 0) {
overlap = true;
}
else
{
add here Curve.Contains(…)
if (contain)
{
overlap =true;
}
}
}

Again, I appreciate your reply!, and again I’m sorry to remain confused, as Curve.Contains expects a Point3d argument. I see how that helps determine if a point is contained by the curve, but I don’t understand how that helps determine whether an inner curve is contained by an outer curve.

Something like that


c#Intersectionsv2.gh (8.4 KB)

1 Like

Minkowski Sum is useful for detecting such collisions (or, overlap / inclusions).

// Rolf

2 Likes

Incredibly helpful, and now I totally get it. Thanks!!!

Beside @laurent_delrieu brilliant method, RhinoCommon has a specific method to determines whether two coplanar simple closed curves are disjoint or intersect: Curve.PlanarClosedCurveRelationship.

...      
  bool overlap = false;
  foreach(Curve curve in circles)
  {
    if (Curve.PlanarClosedCurveRelationship(tempCircle, curve, Plane.WorldXY, 0.1) != 0)
      overlap = true;
  }
  if (!overlap)
    circles.Add(tempCircle);
...

Michael.gh (13.3 KB)

2 Likes

Thanks I didn’t knew this one. Rhinocommon is full of good functions. The most difficult is to find them.

2 Likes

Awesome, thanks so much! This has been incredibly helpful and productive for me as I begin learning this environment. Much appreciated.