2012-08-31 19 views
15

enter image description hereएक जावास्क्रिप्ट फ़ंक्शन जो एक्स, वाई बिंदुओं को दो सर्किलों के बीच छेड़छाड़ देता है?

मैं (एक्स, वाई) केंद्र दो हलकों और उनके त्रिज्या का स्थान मिला है, लेकिन मैं उनके चौराहे अंक (लाल के साथ चिह्नित) जावास्क्रिप्ट का उपयोग खोजने की जरूरत है।

मुझे लगता है कि गणित का सबसे अच्छा स्पष्टीकरण here (दो सर्किलों का छेड़छाड़) पाया जाता है, लेकिन मैं वास्तव में गणित को समझ नहीं पा रहा हूं इसलिए मैं इसे लागू करने में सक्षम नहीं हूं।

उदाहरण के लिए डी = || पी 1 - पी 0 || , क्या करते हैं || पक्ष में? क्या इसका मतलब यह है कि परिणामी संख्या हमेशा सकारात्मक होती है?

और पी 2 = पी 0 + ए (पी 1 - पी 0)/डी, पी यहाँ कुछ नहीं है (10, 50)? लेकिन जावास्क्रिप्ट में (10,50) +13 करना आपको 63 देता है, इसलिए यह केवल पहले नंबर को अनदेखा करता है, तो क्या होने का अनुमान है? क्या परिणाम यहां (23,63) होना चाहिए या? और पी 1-पी 0 भाग या (40,30) - (10,60), आप इसे जावास्क्रिप्ट में कैसे व्यक्त करते हैं?

+2

ये वेक्टर कार्य हैं; आप दो आयामों में काम कर रहे हैं। परिणाम प्राप्त करने के लिए आपको जेएस में समकक्ष वेक्टर बीजगणित कार्यों को बनाने की आवश्यकता है। – Xophmeister

+2

... या नीचे जावास्क्रिप्ट पर जुड़े सी कार्यान्वयन का अनुवाद करें। – duskwuff

+2

'डी = || पी 1 - पी 0 || 'बिंदु पी 0 और पी 1 के बीच की दूरी के लिए खड़ा है, इसलिए डी = एसकर्ट ((x1-x2) ² + (वाई 1-वाई 2) ²) –

उत्तर

31

जावास्क्रिप्ट को साइट पर सी समारोह अनुवादित:

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

ES6 से शुरू, 'Math.hypot (dx, dy)' का उपयोग 'Math.sqrt ((dy * dy) + (dx * dx) के बजाय किया जा सकता है)'। – Arnauld

संबंधित मुद्दे