/**
* Circle Math module
* @module circleMath
* @requires interactables.js
*/
import * as interactables from './interactables.js';
/**
* Calculates the center and radius of a circle element.
*
* @param {HTMLElement} circleElement - An html element that will be calculated as a circle based on its position and width.
* @returns {Object} An object containing the center coordinates (`centerX`, `centerY`)
* and radius (`radius`) of the circle.
*
* @description
* This function calculates the center and radius of the circle element, taking into account
* specific conditions for certain interactable elements, such as patches and needles.
* If the element is a "big patch" or a "star patch," it adjusts the radius accordingly.
*/
function getCircleCenter(circleElement) {
const rect = circleElement.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
let radius = rect.width / 2;
// Adjust radius for specific interactable elements
if (interactables.isBigPatch(circleElement)) {
radius = 50;
}
if (interactables.isStarPatch(circleElement)) {
radius = 20;
}
if (interactables.isNeedle(circleElement)) {
radius = 100;
}
return { centerX, centerY, radius };
}
/**
* Checks if one circle is inside another.
*
* @param {HTMLElement} circle1 - The first circle element to check.
* @param {HTMLElement} circle2 - The second circle element to check.
* @returns {boolean} Returns `true` if either the first circle is completely inside the second circle or vice versa, otherwise `false`.
*
* @description
* This function calculates the distance between the centers of the two circles and compares it
* to the radii of both circles to determine if one circle is fully inside the other.
*/
export function isCircleInsideCircle(circle1, circle2) {
const { centerX: c1x, centerY: c1y, radius: r1 } = getCircleCenter(circle1);
const { centerX: c2x, centerY: c2y, radius: r2 } = getCircleCenter(circle2);
const distance = Math.sqrt(Math.pow(c2x - c1x, 2) + Math.pow(c2y - c1y, 2));
// Check if the distance between centers is less than the difference in radii
return Math.abs(r1 - r2) - distance >= 0;
}