Ecrire une fonction pour détecter deux cercles. Javascript

Par kingja, il y a 5 ans


Bonjour je suis débutant en javascript , j'essaye de faire une fonction(AreCirclesIntersecting) pour détecter si deux cercles se croisent. Mais j'arrive pas. Une personne peut me donné la réponse svp

// c0: {center: {x: float, y: float}, radius: float} // c1: {center: {x: float, y: float}, radius: float} // Returns true if there is an intersection, false otherwise. function AreCirclesIntersecting(c0, c1) { } const circles = [ {center: {x: 10.0, y: 10.0}, radius: 5.0}, {center: {x: 20.0, y: 20.0}, radius: 15.0}, {center: {x: 20.0, y: 10.0}, radius: 5.0}, {center: {x: 20.0, y: 25.0}, radius: 7.5}, ]; const q7_result1 = AreCirclesIntersecting(circles[0], circles[1]); console.log(q7_result1); // Expected output: true const q7_result2 = AreCirclesIntersecting(circles[0], circles[2]); console.log(q7_result2); // Expected output: true const q7_result3 = AreCirclesIntersecting(circles[1], circles[3]); console.log(q7_result3); // Expected output: false const q7_result4 = AreCirclesIntersecting(circles[2], circles[3]); console.log(q7_result4); // Expected output: false

Ce que je veux

Ecrire une fonction pour détecter si deux cercles se croisent (en 2D).

9 réponses

kingja, il y a 5 ans

Salut, je vois mais ça pose pas problème si j'ai que 2 argurments ? Qui sont c0 et c1?

kingja, il y a 5 ans

Comme c'est en 2 dimensions

Pierrot01, il y a 5 ans

``

function intersection(c0,c1) {

    x0 = c0['center']['x'];
    y0 = c0['center']['y'];
    r0 = c0['center']['r'];
    x1 = c1['center']['x'];
    y1 = c1['center']['y'];
    r1 = c1['center']['r'];

    /*
    voilou mon grand ;)
    */
    var a, dx, dy, d, h, rx, ry;
    var x2, y2;

    /* dx and dy are the vertical and horizontal distances between
     * the circle centers.
     */
    dx = x1 - x0;
    dy = y1 - y0;

    /* Determine the straight-line distance between the centers. */
    d = Math.sqrt((dy*dy) + (dx*dx));

    /* Check for solvability. */
    if (d > (r0 + r1)) {
        /* no solution. circles do not intersect. */
        return false;
    }
    if (d < Math.abs(r0 - r1)) {
        /* no solution. one circle is contained in the other */
        return false;
    }

    /* 'point 2' is the point where the line through the circle
     * intersection points crosses the line between the circle
     * centers.  
     */

    /* Determine the distance from point 0 to point 2. */
    a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

    /* Determine the coordinates of point 2. */
    x2 = x0 + (dx * a/d);
    y2 = y0 + (dy * a/d);

    /* Determine the distance from point 2 to either of the
     * intersection points.
     */
    h = Math.sqrt((r0*r0) - (a*a));

    /* Now determine the offsets of the intersection points from
     * point 2.
     */
    rx = -dy * (h/d);
    ry = dx * (h/d);

    /* Determine the absolute intersection points. */
    var xi = x2 + rx;
    var xi_prime = x2 - rx;
    var yi = y2 + ry;
    var yi_prime = y2 - ry;

    return [xi, xi_prime, yi, yi_prime];

}

``

a++

kingja, il y a 5 ans

ah d'accord ça met d'une grande aide merci :)

kingja, il y a 5 ans

je suppose qui faut déclarer x0, y0 , etc ?

Pierrot01, il y a 5 ans

nan, tu supposes mal :D
tu reprends tel quel ;)

a+

kingja, il y a 5 ans

Je pose la question comme cà m'a dit que x0 n'est pas définie x)

kingja, il y a 5 ans

d'ailleurs comment on voit qui renvoie le co et c1 ? parce que sur la dernière ligne il envoie 4 valeurs mais moi j'ai juste besoin qui renvoie true ou false d'après les valeurs de mon tableau ?