简体   繁体   English

C# 获取 2 点之间的所有像素坐标 - 错误

[英]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) (黄色圆圈为起点,紫色圆圈为终点)

Detailed picture here详细图片在这里

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.

相关问题 C#,将鼠标悬停在位图像素上并获取坐标? - C#, hover over bitmap pixel and get the coordinates? 是否有可能从c#图2d中获得像素的所有点 - is it possible to get all point of pixel from line c# drawing 2d 使用C#在Metro应用程序中的鼠标指针上获取像素颜色 - Get the Pixel color on mouse point in Metro app using c# (C#) 如何为我的屏幕获取点到像素的关系? - (C#) How to get the point to pixel relationship for my screen? C#如何获取屏幕上特定点的坐标。 (不是鼠标定位) - C# How to get the Coordinates of a specific Point on Screen. (not mouselocation) C#WindowsForms PictureBox:控件坐标和图像中像素位置之间的转换 - C# WindowsForms PictureBox: Transformation between control coordinates and pixel position in image c#wpf-我可以获取图像控件中单击的像素的坐标 - c# wpf - Can I get the coordinates to a pixel clicked in an image control 如何在C#中获取矩形内的所有像素值 - How to get all the pixel values inside a rectangle in C# 如何使用 C# 从 Tiff 获取点像素值到数组? - How can I get the point pixel values to an array from Tiff using C#? 从像素坐标列表C#中查找圆的中心坐标 - Find the center coordinate of a circle from a list of pixel coordinates C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM