给定一个向量(或两点),如何得到该向量在给定区间内相交的离散坐标?
我使用这种方法,在给定射线(向量)的情况下,我可以计算出这条射线相交的图像中的像素,并将它们作为图像的索引。在三维情况下,光线总是在图像的平面上。
此外,向量来自另一个坐标系,而不是用于图像索引的坐标系,但这只是坐标系统之间的缩放。
我正在寻找一个三维解决方案,但2D可以接受。
编辑:间隔是一个2d空间,所以解决方案是这个2d间隔中的一组点。这将在带有CUDAfy.NET的GPU上运行。
发布于 2015-03-10 19:38:05
经过一番搜索,我找到了Bresenham的线条算法,这或多或少就是我所需要的。
如果您对我使用的算法感兴趣,请参考this answer。
发布于 2015-03-09 13:56:55
向量P1,P2就是所有这些点:
vector := P1 + a * (P2-P1) with a in [0;1]Yout intervall P3,P4就是所有这些要点:
interval := P3 + b * (P4 - P3) with b in [0,1]他们在同一点上互相交流:
vector == interval
P1 + a * (P2-P1) == P3 + b * (P4-P3)在2d中,这是两个具有两个未知数的方程,->可解
发布于 2015-03-09 15:48:28
以下是我的假设:
(0, 0)的点)。w和高度h的图像y=mx+b,其中m是斜率,b是y-截距)。在这些假设下,对执行以下操作,找到与图像的边缘相交的离散坐标
/// <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;
}以下是确保其工作正常的测试:
[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));
}
}https://stackoverflow.com/questions/28943561
复制相似问题