拉斐尔的相交路径

发布于 2024-11-04 06:01:41 字数 73 浏览 5 评论 0原文

我试图找出拉斐尔的两条路径是否相交。我尝试过 getBBox() 但它返回路径本身周围的框的坐标。有没有更简单的方法来实现这一目标?

I am trying to found out if two paths are intersected in Raphael. I have tried getBBox() but that returns the coordinates of a box around the path itself. Is there an easier way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2024-11-11 06:01:41

之前的答案可能适用于拉斐尔的早期版本。 API 现在包含一个 pathIntersection 方法,该方法返回相交点数组。您可以简单地检查返回值的长度。

The previous answers may have been for an earlier version of Raphael. The API now includes a pathIntersection method which returns an array of intersecting points. You can simply check the length of the return value.

花桑 2024-11-11 06:01:41

暴力破解方法。获取两条路径的所有点,看看两个点是否相同。

我给你做了这个,但也许你应该想出一个更好的比较解决方案。根据您的路径有多长,这可能很重。

var paper = Raphael(0, 0, '100%', '100%');

var path1 = paper.path("M0 0L100 100");
var path2 = paper.path("M100 0L0 100");

var array1 = new Array();
var array2 = new Array();

for(var i = 0; i < path1.getTotalLength(); i++) {
    array1.push(path1.getPointAtLength(i));
}

for(var i = 0; i < path2.getTotalLength(); i++) {
    array2.push(path2.getPointAtLength(i));
}

for(var i = 0; i < array1.length; i++) {
    for(var k = 0; k < array2.length; k++) {
        // the threshold +-1 is important!
        if(array1[i].x < ( array2[k].x + 1 ) &&
           array1[i].x > ( array2[k].x - 1 )) {
               if(array1[i].y < ( array2[k].y + 1 ) &&
                  array1[i].y > ( array2[k].y - 1 )) {
                   alert('yeah'); // uncomment this. It will alert 4 times.
               } 
        }  
    }
}

Bruteforce method. Get all the points for the two path and see if two points are the same.

I made you this but maybe you should come up with a better comparing solution. Depending on how long your paths are, this can be heavy.

var paper = Raphael(0, 0, '100%', '100%');

var path1 = paper.path("M0 0L100 100");
var path2 = paper.path("M100 0L0 100");

var array1 = new Array();
var array2 = new Array();

for(var i = 0; i < path1.getTotalLength(); i++) {
    array1.push(path1.getPointAtLength(i));
}

for(var i = 0; i < path2.getTotalLength(); i++) {
    array2.push(path2.getPointAtLength(i));
}

for(var i = 0; i < array1.length; i++) {
    for(var k = 0; k < array2.length; k++) {
        // the threshold +-1 is important!
        if(array1[i].x < ( array2[k].x + 1 ) &&
           array1[i].x > ( array2[k].x - 1 )) {
               if(array1[i].y < ( array2[k].y + 1 ) &&
                  array1[i].y > ( array2[k].y - 1 )) {
                   alert('yeah'); // uncomment this. It will alert 4 times.
               } 
        }  
    }
}
記憶穿過時間隧道 2024-11-11 06:01:41

我想你需要自己实现一些东西,因为 Raphael 似乎没有提供这种功能。这是一个可能有帮助的圆交叉示例这里有更具体的内容

在运行实际算法之前,您可能需要检查边界框是否相交。如果存在,请检查实际路径。

I guess you need to implement something yourself as it seems Raphael doesn't provide this sort of functionality. Here's a circle intersection example that might help. Here's something more specific.

Before running your actual algo you probably want to check if the bounding boxes intersect. If they do, check actual paths.

空城之時有危險 2024-11-11 06:01:41

使用这个可爱的交叉库。有了它,你可以做这样的事情:

var shape1 = new Path(pathElement1),
    shape2 = new Path(pathElement2);

var inter = Intersection.intersectShapes(shape1, shape2);

    if(inter.points.length > 0) {
        // yes they intersect!
    }

我的示例中的 inter 对象包含其他好东西。

Use this lovely intersection library. With that you can do stuff like this:

var shape1 = new Path(pathElement1),
    shape2 = new Path(pathElement2);

var inter = Intersection.intersectShapes(shape1, shape2);

    if(inter.points.length > 0) {
        // yes they intersect!
    }

The inter object in my example contains other good stuff to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文