首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从矢量计算相交坐标

从矢量计算相交坐标
EN

Stack Overflow用户
提问于 2015-03-09 13:47:22
回答 3查看 1.9K关注 0票数 0

给定一个向量(或两点),如何得到该向量在给定区间内相交的离散坐标?

我使用这种方法,在给定射线(向量)的情况下,我可以计算出这条射线相交的图像中的像素,并将它们作为图像的索引。在三维情况下,光线总是在图像的平面上。

此外,向量来自另一个坐标系,而不是用于图像索引的坐标系,但这只是坐标系统之间的缩放。

我正在寻找一个三维解决方案,但2D可以接受。

编辑:间隔是一个2d空间,所以解决方案是这个2d间隔中的一组点。这将在带有CUDAfy.NET的GPU上运行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-10 19:38:05

经过一番搜索,我找到了Bresenham的线条算法,这或多或少就是我所需要的。

如果您对我使用的算法感兴趣,请参考this answer

票数 0
EN

Stack Overflow用户

发布于 2015-03-09 13:56:55

向量P1,P2就是所有这些点:

代码语言:javascript
复制
vector := P1 + a * (P2-P1)  with a in [0;1]

Yout intervall P3,P4就是所有这些要点:

代码语言:javascript
复制
interval := P3 + b * (P4 - P3)  with b in [0,1]

他们在同一点上互相交流:

代码语言:javascript
复制
vector == interval
P1 + a * (P2-P1) == P3 + b * (P4-P3)

在2d中,这是两个具有两个未知数的方程,->可解

票数 0
EN

Stack Overflow用户

发布于 2015-03-09 15:48:28

以下是我的假设:

  • 图像的左下角是原点(坐标为(0, 0)的点)。
  • 你有一个宽度w和高度h的图像
  • 你可以把向量以线性方程的形式存在(例如,y=mx+b,其中m是斜率,b是y-截距)。

在这些假设下,对执行以下操作,找到与图像的边缘相交的离散坐标

代码语言:javascript
复制
    /// <summary>
    /// Find discreet coordinates where the line y=mx+b intersects the edges of a w-by-h image.
    /// </summary>
    /// <param name="m">slope of the line</param>
    /// <param name="b">y-intercept of the line</param>
    /// <param name="w">width of the image</param>
    /// <param name="h">height of the image</param>
    /// <returns>the points of intersection</returns>
    List<Point> GetIntersectionsForImage(double m, double b, double w, double h)
    {
        var intersections = new List<Point>();

        // Check for intersection with left side (y-axis).
        if (b >= 0 && b <= h)
        {
            intersections.Add(new Point(0.0, b));
        }

        // Check for intersection with right side (x=w).
        var yValRightSide = m * w + b;
        if (yValRightSide >= 0 && yValRightSide <= h)
        {
            intersections.Add(new Point(w, yValRightSide));
        }

        // If the slope is zero, intersections with top or bottom will be taken care of above.
        if (m != 0.0)
        {
            // Check for intersection with top (y=h).
            var xValTop = (h - b) / m;
            if (xValTop >= 0 && xValTop <= w)
            {
                intersections.Add(new Point(xValTop, h));
            }

            // Check for intersection with bottom (y=0).
            var xValBottom = (0.0 - b) / m;
            if (xValBottom >= 0 && xValBottom <= w)
            {
                intersections.Add(new Point(xValBottom, 0));
            }
        }

        return intersections;
    }

以下是确保其工作正常的测试:

代码语言:javascript
复制
    [TestMethod]
    public void IntersectingPoints_AreCorrect()
    {
        // The line y=x intersects a 1x1 image at points (0, 0) and (1, 1).
        var results = GetIntersectionsForImage(1.0, 0.0, 1.0, 1.0);
        foreach (var p in new List<Point> { new Point(0.0, 0.0), new Point(1.0, 1.0) })
        {
            Assert.IsTrue(results.Contains(p));
        }

        // The line y=1 intersects a 2x2 image at points (0, 1), and (2, 1).
        results = GetIntersectionsForImage(0.0, 1.0, 2.0, 2.0);
        foreach (var p in new List<Point> { new Point(0.0, 1.0), new Point(2.0, 1.0) })
        {
            Assert.IsTrue(results.Contains(p));
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28943561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档