Как определить, находится ли один круг внутри другого круга? Я думал, что понял это, используя метод isInside.
Примечание. Этот код будет работать на сайте http://sketch.paperjs.org.
Ожидайте, что Circle2 будет внутри 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));
Выходы:
ложь ложь
2 ответа
Из документации isInside требует, чтобы в качестве параметра был передан прямоугольный объект, а не путь.
Рабочий код
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));
Выходы:
ложь Правда
Чтобы узнать, находится ли circle_small
с радиусом Rs
внутри circle_big
с радиусом Rb
, просто проверьте, что расстояние между их центрами (Cb, Cs) меньше< /эм> Rb-Rs
.
Если вы позволяете обоим кругам «соприкасаться» в одной точке, тогда условие меньше или равно значению Rb-Rs
.
Общий случай:
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
Похожие вопросы
Новые вопросы
javascript
По вопросам программирования на ECMAScript (JavaScript/JS) и его различных диалектах/реализациях (кроме ActionScript). Обратите внимание, что JavaScript — это НЕ Java. Включите все теги, относящиеся к вашему вопросу: например, [node.js], [jQuery], [JSON], [ReactJS], [angular], [ember.js], [vue.js], [typescript], [стройный] и т. д.