[英]C# get all pixel coordinates between 2 point- error
So first of all, I have several X1,Y1,X2 and Y2 coordinates.所以首先,我有几个 X1,Y1,X2 和 Y2 坐标。 X1/Y1 represents the starting points, X2/Y2 the end points so I draw a line from these.
X1/Y1 代表起点,X2/Y2 代表终点,所以我从这些点画一条线。 I need to store all the pixels/points between this 2 points.
我需要存储这两个点之间的所有像素/点。 I have an algorithm which seems to do it but there are some errors.
我有一个似乎可以做到的算法,但有一些错误。 I have no clue that why only half of them will be stored in the dictionary.
我不知道为什么只有一半会存储在字典中。
The code (Point p1 => X1 and Y1, Point p2 => X2 and Y2):代码(点 p1 => X1 和 Y1,点 p2 => X2 和 Y2):
public List<Point> GetPoints(Point p1, Point p2)
{
List<Point> points = new List<Point>();
// no slope (vertical line)
if (p1.X == p2.X)
{
for (double y = p1.Y; y <= p2.Y; y++)
{
Point p = new Point(p1.X, (int)y);
points.Add(p);
}
}
else
{
// swap p1 and p2 if p2.X < p1.X
if (p2.X < p1.X)
{
Point temp = p1;
p1 = p2;
p2 = temp;
}
double deltaX = p2.X - p1.X;
double deltaY = p2.Y - p1.Y;
double error = -1.0f;
double deltaErr = Math.Abs(deltaY / deltaX);
double y = p1.Y;
for (double x = p1.X; x <= p2.X; x++)
{
Point p = new Point((int)x, (int)y);
points.Add(p);
error += deltaErr;
while (error >= 0.0f)
{
y++;
points.Add(new Point((int)x, (int)y));
error -= 1.0f;
}
}
if (points.Last() != p2)
{
int index = points.IndexOf(p2);
points.RemoveRange(index + 1, points.Count - index - 1);
}
}
return points;
}
Some explanation: at the end of this whole procedure, I'll get a Point list with all the coordinates between the points -> which is good.一些解释:在整个过程结束时,我会得到一个点列表,其中包含点之间的所有坐标 -> 这很好。 After that, I draw these points but as u can see on the picture, only half of them got stored (the white lines show the stored coordinates between the points).
之后,我绘制了这些点,但正如您在图片上看到的那样,只有一半被存储(白线显示了点之间存储的坐标)。 Is there anything I didn't notice in the code?
代码中有什么我没有注意到的吗? Why only those got stored?
为什么只有那些被存储? Thank u in advance!
提前谢谢你!
(Yellow circles are the starting points and the purple ones are the end points) (黄色圆圈为起点,紫色圆圈为终点)
I have an idea.我有个主意。 All your success stored point directed from left-top corner to bottom-right corner and all points that is not stored directed from left-bottom to top-right.
您所有的成功存储点从左上角指向右下角,所有未存储的点从左下角指向右上角。 So the problem is in the Y-coordinate direction.
所以问题出在Y坐标方向。
In first scenario Y-coordinate changes from from smaller to larger value so y++;
在第一种情况下,Y 坐标从较小的值变为较大的值,因此
y++;
will bring us closer to the end point and last point will be equal to the endPoint and points.Last() != p2
returns false.将使我们更接近终点,最后一个点将等于 endPoint,并且
points.Last() != p2
返回 false。
In second scenario deltaY is negative so y++
will move us away from the end point and int index = points.IndexOf(p2);
在第二种情况下,deltaY 是负数,因此
y++
将使我们远离终点并且int index = points.IndexOf(p2);
returns -1
because there is no endPoint in the collection.返回
-1
,因为集合中没有 endPoint。 So所以
points.RemoveRange(index + 1, points.Count - index - 1);
// points.RemoveRange(-1 + 1, points.Count - -1 - 1);
// points.RemoveRange(0, points.Count);
will delete all points.将删除所有点。
You need to calculate deltaYdirection with var deltaYdirection = (int) Math.Sign(deltaY)
and use y += deltaYdirection;
您需要使用
var deltaYdirection = (int) Math.Sign(deltaY)
计算 deltaYdirection 并使用y += deltaYdirection;
instead of y++;
而不是
y++;
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.