How to get all points between two Point objects?

Suppose I have my first Point structure:

Point start = new Point(1, 9);

and my second:

Point end = new Point(4, 9);

I want to get all the points between the beginning and the end. So, for example, I would like to get 2.9 and 3.9 in an array. Does .NET have something for this?

+5
source share
3 answers

There are no built-in functions for this, since there are no points between the points. In mathematics, there is a line between two points. From the point of view of computer graphics, lines can be smoothed and therefore not rounded to full integers.

If you are looking for a quick way to create all the integral numbers between them, I think Bresenhams-Line-Algorithm would be your choice. But this is not built into .NET, you have to code it yourself (or take Matthew Watson's implementation):

http://en.wikipedia.org/wiki/Bresenham's_line_algorithm

, .

+5

, . @Cody Grey, . , .

My Line:

public class Line {
    public Point p1, p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point[] getPoints(int quantity) {
        var points = new Point[quantity];
        int ydiff = p2.Y - p1.Y, xdiff = p2.X - p1.X;
        double slope = (double)(p2.Y - p1.Y) / (p2.X - p1.X);
        double x, y;

        --quantity;

        for (double i = 0; i < quantity; i++) {
            y = slope == 0 ? 0 : ydiff * (i / quantity);
            x = slope == 0 ? xdiff * (i / quantity) : y / slope;
            points[(int)i] = new Point((int)Math.Round(x) + p1.X, (int)Math.Round(y) + p1.Y);
        }

        points[quantity] = p2;
        return points;
    }
}


:

var line = new Line(new Point(10, 15), new Point(297, 316));
var points = line.getPoints(20);

20 , (). , !

+3

, , , - wiki (https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm), .

, .

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, 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(x, y);
            points.Add(p);
            Debug.WriteLine("Added Point: " + p.X.ToString() + "," + p.Y.ToString());

            error += deltaErr;
            Debug.WriteLine("Error is now: " + error.ToString());

            while (error >= 0.0f)
            {
                Debug.WriteLine("   Moving Y to " + y.ToString());
                y++;
                points.Add(new Point(x, y));
                error -= 1.0f;
            }
        }

        if (points.Last() != p2)
        {
            int index = points.IndexOf(p2);
            points.RemoveRange(index + 1, points.Count - index - 1);
        }
    }

    return points;
}
+1

All Articles