Circles-of-given-radius-through-two-points

What am I missing here? This is partially correct…

function getCircles(point1, point2, r) {
    // Extract coordinates from points
    const [x1, y1] = point1;
    const [x2, y2] = point2;

    // Calculate distance between two points
    const distance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);

    // Case: Coincident points
    if (distance === 0) {
        return "Coincident point. Infinite solutions";
    }

    // Case: Points further apart than circle diameter
    if (distance > 2 * r) {
        // Calculate midpoint between the two points
        const midX = (x1 + x2) / 2;
        const midY = (y1 + y2) / 2;

        // Calculate angle of the line connecting the two points with respect to the horizontal axis
        const angle = Math.atan2(y2 - y1, x2 - x1);

        // Calculate distance from midpoint to center of each circle (radius)
        const d = Math.sqrt(r ** 2 - (distance / 2) ** 2);

        // Calculate coordinates of the centers of the two circles
        const centerX1 = midX + d * Math.cos(angle + Math.PI / 2);
        const centerY1 = midY + d * Math.sin(angle + Math.PI / 2);
        const centerX2 = midX + d * Math.cos(angle - Math.PI / 2);
        const centerY2 = midY + d * Math.sin(angle - Math.PI / 2);

        // Round coordinates to four decimal digits and return as an array of arrays
        return [
            [Math.round(centerX1 * 10000) / 10000, Math.round(centerY1 * 10000) / 10000],
            [Math.round(centerX2 * 10000) / 10000, Math.round(centerY2 * 10000) / 10000]
        ];
    } else if (distance === 2 * r) {
        // Calculate midpoint between the two points
        const midX = (x1 + x2) / 2;
        const midY = (y1 + y2) / 2;

        // Return the midpoint coordinates as the center of the circle
        return [[midX, midY]];
    } else {
        return "No intersection. Points further apart than circle diameter";
    }
}

Challenge: Circles of given radius through two points

Link to the challenge:

https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/circles-of-given-radius-through-two-points

Have you looked at what function returns for failing test cased? Ie.:

console.log(getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.