skip to Main Content

How can one determine if a circle is inside another circle? I thought I had it figured out using isInside method.

Note: This code will run on http://sketch.paperjs.org

Expect Circle2 to be inside Circle1

var circle1 = new Path.Circle(new Point(100, 100), 100);
var circle2 = new Path.Circle(new Point(100, 100), 50);

circle1.strokeWidth = 2;
circle1.strokeColor = "black";
circle1.fillColor = "blue";
circle2.strokeWidth = 2;
circle2.strokeColor = "black";
circle2.fillColor = "green";

console.log(circle1.isInside(circle2));
console.log(circle2.isInside(circle1));

Outputs:

false false

2

Answers


  1. Chosen as BEST ANSWER

    From the docs isInside requires a rectangle object to be passed in as a parameter, not a path.

    Working Code

    var circle1 = new Path.Circle(new Point(100, 100), 100);
    var circle2 = new Path.Circle(new Point(100, 100), 50);
    
    circle1.strokeWidth = 2;
    circle1.strokeColor = "black";
    circle1.fillColor = "blue";
    circle2.strokeWidth = 2;
    circle2.strokeColor = "black";
    circle2.fillColor = "green";
    
    console.log(circle1.isInside(circle2.bounds));
    console.log(circle2.isInside(circle1.bounds));
    

    Outputs:

    false true


  2. To know if circle_small with radius Rs is enterely inside circle_big with radius Rb just check that the distance between their centers (Cb, Cs) is less than Rb-Rs.

    If you allow both circles "touch" in one point then the condition is less or equal to Rb-Rs.

    General case:

    var discen = Math.sqrt( (Cb_x - Cs_x)*(Cb_x - Cs_x) + (Cb_y - Cs_y)*(Cb_y - Cs_y) );
    var radiff = Rb - Rs;
    if (discen < radiff)
        //circle_small fully inside circle_big, no touching point
    else if ( Math.abs(discen-radiff) < 0.0001 ) //better that discen==radiff
        //circle_small fully inside circle_big, one tangent common (touching point)
    else if (discen < Rb+Rs)
        //Both circles intersect, partially one inside the other
    else
        //Not intersecting circles
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search