broke
broke

Reputation: 8302

How do I get all the points between two Point objects?

Lets say I have my first Point struct:

Point start = new Point(1, 9);

and my second:

Point end = new Point(4, 9);

I want to get all the points between the start and end. So for example I would want 2,9 and 3,9 in an array. Does .NET have something built in for this?

Upvotes: 9

Views: 16819

Answers (4)

Prem
Prem

Reputation: 301

You can get between points using below code. User just need to define that how many points to get between two points. Here I defined as 10 points.

PointF pStart = new PointF(10, 10);
PointF pEnd = new PointF(100, 100);

PointF[] betPoints = new PointF[10];
for (int i = 1; i <= 10; i++)
{
    betPoints[i].X = (Math.Abs(pStart.X - pEnd.X) / 10) * i + pEnd.X;
    betPoints[i].Y = (Math.Abs(pStart.Y - pEnd.Y) / 10) * i + pEnd.Y;
}

Upvotes: 0

dogear
dogear

Reputation: 21

I know it's been quite a long time since you first asked this question but I was looking for something similar recently and found this wiki (https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm), which contained pseudo code.

So I've implemented a function with the pseudo code to perform this calculation and add the points to a List.

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;
}

Upvotes: 2

Ryan Steffer
Ryan Steffer

Reputation: 434

This is what I ended up doing. As @Cody Gray mentioned in his comment, there are infinite points on a line. So you need to specify how many points you are looking to retrieve.

My Line class:

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;
    }
}


Usage:

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

That will return a Point array of 20 Points evenly spaced between the two endpoints (inclusive). Hope that helps!

Upvotes: 14

thewhiteambit
thewhiteambit

Reputation: 1436

There are no build in functions for this, since there are no points between points. Mathematicaly there is a line between two points. In terms of Computer-Graphics, lines could be antialiased and so beeing not rounded to full Integer numbers.

If you are looking for a fast method of creating all integral numbers inbetween, I guess Bresenhams-Line-Algorithm would be your choice. But this is not build into .NET, you have to code it by yourself (or take Matthew Watson's implementation):

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

There are even fasther algorithms for doing it, but I would go for Bresenham.

Upvotes: 9

Related Questions