关于如何使用KevLinDev交集的简单示例

时间:2014-05-05 11:01:32

标签: javascript

有人可以给我一个非常简单的例子,如何在javascript中使用KevLinDev类方法intersectCircleLine。一些价值观:

 circle_x = 100;
 circle_y = 100;
 radius = 200; 
 a1.x = -100;
 a1.y = -100;
 a2.x = 0; 
 a2.y = 0;

http://www.kevlindev.com/gui/math/intersection/index.htm#Anchor-intersectCircleLin-40934

我想知道如何:

  1. 在我的代码中包含intersectionctions.js。
  2. 如何将上述变量传递给函数
  3. 如何获得结果?

1 个答案:

答案 0 :(得分:1)

包括这些文字:

<script type="text/javascript" src="http://www.kevlindev.com/gui/math/intersection/Intersection.js"></script>
<script type="text/javascript" src="http://www.kevlindev.com/gui/math/point2d/Point2D.js"></script>

查看该库的source code,这样的内容似乎就是您想要的:

var circle = new Point2D(circle_x, circle_y),
    point1 = new Point2D(a1.x, a1.y),
    point2 = new Point2D(a2.x, a2.y),
    intersection = Intersection.intersectCircleLine(circle, radius, point1, point2);

对于这个例子:

var circle = new Point2D(50, 50),
    point1 = new Point2D(0, 0),
    point2 = new Point2D(100, 100),
    intersection = Intersection.intersectCircleLine(circle, 50, point1, point2);

console.log(intersection)将显示:

{ // (Intersection Object)
    status: "Intersection", // Can also be "Inside" or "Outside".
    points: [               // If `status` is "Intersection". Otherwise, it's [].
        { // (Point2D Object)
            x: 85.35533905932738,
            y: 85.35533905932738
        },
        { // (Point2D Object)
            x: 14.644660940672624,
            y: 14.644660940672624
        }
    ]
}

<强> Working example