user3117033
user3117033

Reputation: 195

How do I create a set of equidistant points between two known points?

The method get two points and return a List of 30 points. When I display the points in paint event I see a big circle of points. I want to display this points not as circle but as line order like ...... . . . like this

At the top of the class im doing:

point1 = new Point(80, 80);
point2 = new Point(280, 300);
extendedPoints = ExtendPoints(point1, point2);

At first I did this:

private static List<PointF> ExtendPoints(PointF pt1, PointF pt4)
        {
            extendedPoints.Add(pt1);
                float x = (Math.Max(pt1.X, pt4.X) - Math.Min(pt1.X, pt4.X)) / 2 + Math.Min(pt1.X, pt4.X);
                float y = (Math.Max(pt1.Y, pt4.Y) - Math.Min(pt1.Y, pt4.Y)) / 2 + Math.Min(pt1.Y, pt4.Y);
                var pt2 = new PointF(x, y);
                extendedPoints.Add(pt2);
            extendedPoints.Add(pt4);
            return extendedPoints;
        }

The result is that i give two points and get a List of 3 points i create a one new point in the middle of the distance between the two given points:

enter image description here

You can see there are 3 red points on the left pictureBox image. I created a new one point in the exact middle of the two other given points.

In this case in the top of the class i did:

point1 = new Point(80, 80);
point2 = new Point(280, 300);
extendedPoints = ExtendPoints(point1, point2);

I give two points and get List of three points and i display the three points. Now i need to make more two things:

Inside the method first thing i need to add another point so the List will return fours points equal distances between all points. I need somehow to add another Math calculation for the new second point.

I tried to do it like this:

extendedPoints.Add(pt1);
                float x = (Math.Max(pt1.X, pt4.X) - Math.Min(pt1.X, pt4.X)) / 3 + Math.Min(pt1.X, pt4.X);
                float y = (Math.Max(pt1.Y, pt4.Y) - Math.Min(pt1.Y, pt4.Y)) / 3 + Math.Min(pt1.Y, pt4.Y);
                var pt2 = new PointF(x, y);
                float a = (Math.Max(pt1.X, pt4.X) - Math.Min(pt1.X, pt4.X)) / 2 / 3 + Math.Min(pt1.X, pt4.X);
                float b = (Math.Max(pt1.Y, pt4.Y) - Math.Min(pt1.Y, pt4.Y)) / 2 / 3 + Math.Min(pt1.Y, pt4.Y);
                var pt3 = new PointF(a, b);
                extendedPoints.Add(pt2);
                extendedPoints.Add(pt3);
            extendedPoints.Add(pt4);

Im getting four points but the last point is not equal in the distance:

enter image description here

And then after that i need to use a FOR LOOP for exmaple:

for (int i = 0; i < 100; i++)

And it will create 100 points equal distances between the two given points !

So i dont know how to do the part of getting 4 equal distances points and how to do the for loop ? And i need to be according to my code now.

Upvotes: 0

Views: 1788

Answers (1)

MrWuf
MrWuf

Reputation: 1498

Not quite sure what you are doing, but this should give you a series of equidistant points in a straight line between two known endpoints

private static List<PointF> ExtendPoints(PointF pt1, PointF pt4, int numberOfPoints)
{
       extendedPoints = new List<PointF>();
       extendedPoints.Add(pt1);

       for(double d = 1; d < numberOfPoints-1; d++)
       {
            float a = (Math.Max(pt1.X, pt4.X) - Math.Min(pt1.X, pt4.X)) * d / (double)(numberOfPoints-1) + Math.Min(pt1.X, pt4.X);
            float b = (Math.Max(pt1.Y, pt4.Y) - Math.Min(pt1.Y, pt4.Y)) * d / (double)(numberOfPoints-1) + Math.Min(pt1.Y, pt4.Y);
            var pt2 = new PointF(a, b);
            extendedPoints.Add(pt2);
       }

       extendedPoints.Add(pt4);
       return extendedPoints;
}

Upvotes: 4

Related Questions