2012-08-31 9 views
15

enter image description here¿Una función de JavaScript que devuelve los puntos x, y de intersección entre dos círculos?

que tiene las coordenadas (x, y) ubicación en el centro de dos círculos y su radio, pero tengo que encontrar sus puntos de intersección (marcadas con rojo) utilizando JavaScript.

Creo que la mejor explicación en cuanto a las matemáticas se encuentra here (Intersección de dos círculos), pero realmente no entiendo las matemáticas, así que no puedo implementarlo.

Por ejemplo d = || P1 - P0 || , ¿qué hacen los || ¿representar? ¿Significa que el número resultante siempre es positivo?

Y también P2 = P0 + a (P1 - P0)/d, ¿no son las P aquí algo así como (10, 50)? Pero hacer (10,50) +13 en JavaScript le da 63, por lo que simplemente ignora el primer número, entonces, ¿qué se supone que va a pasar? ¿Debería ser el resultado (23,63) aquí o? Y también la parte P1-P0 o (40,30) - (10,60), ¿cómo se expresa eso en JavaScript?

+2

Estas son funciones vectoriales; estás operando en dos dimensiones, después de todo. Necesita construir funciones de álgebra vectorial equivalentes en JS para obtener el resultado que desea. – Xophmeister

+2

... o traducir la implementación de C vinculada en la parte inferior a JavaScript. – duskwuff

+2

'd = || P1 - P0 ||' representa la distancia entre el punto P0 y P1, entonces d = sqrt ((x1-x2) ² + (y1-y2) ²) –

Respuesta

31

traducido la función de C en el sitio para JavaScript:

function intersection(x0, y0, r0, x1, y1, r1) { 
     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]; 
    } 
+0

Comenzando con ES6, se puede usar 'Math.hypot (dx, dy)' en lugar de 'Math.sqrt ((dy * dy) + (dx * dx))'. – Arnauld

Cuestiones relacionadas