使用圆查找相交线在Javascript中不起作用

时间:2017-03-16 12:43:26

标签: javascript jquery svg geometry trigonometry

我跟着the formula找到了与圆相交的线。但它不起作用。这是我的代码。

   <svg height="397" width="851">
  <circle cx="334.4" cy="198.5" r="150.8" stroke="black" stroke-width="3" fill="red" />
   <line x1="485.60978255954285" y1="231.75390342766823" x2="488.9584783979701" y2="231.75390342766823" style="stroke:rgb(255,0,0);stroke-width:2" />
  Sorry, your browser does not support inline SVG.  
</svg> 

     <script>
   var line1point= {x: 485.60978255954285, y: 231.75390342766823};
     var line2point= {x: 488.9584783979701, y: 231.75390342766823};
     var center = {x: 334.4, y: 198.5};
     var intersect = inCircle(line1point, line2point, center, 150.8);
     alert(intersect);
     function inCircle (line1point, line2point, center, r){
         var l1 = line1point, l2 = line2point, c = center;

         return (Math.abs((l2.x - l1.x) * c.x + c.y * (l1.y - l2.y) + (l1.x - l2.x) * l1.y + (l1.y - l2.y) * c.y)
                 / Math.sqrt(((l2.x - l1.x) * (l2.x - l1.x)) + ((l1.y - l2.y) *(l1.y - l2.y))) <= r);
     }

     </script>

Fiddle Link

我尝试了本论坛第二个回答中给出的公式。 Stackexchange link

Output Image

0 个答案:

没有答案