Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the points between two given points and given distance?

I have point A (35.163 , 128.001) and point B (36.573 , 128.707)

I need to calculate the points lies within point A and point B

using the standard distance formula between 2 points, I found D = 266.3

each of the points lies within the line AB (the black point p1, p2, ... p8) are separated with equal distance of d = D / 8 = 33.3

How could I calculate the X and Y for p1 , p2, ... p8?

example of Java or C# language are welcomed

or just point me a formula or method will do.

Thank you.

**The above calculation is actually used to calculate the dummy point for shaded level in my map and working for shaded area interpolation purpose*

find the X and Y for all the points between A and B

like image 669
jhyap Avatar asked Jan 21 '14 05:01

jhyap


2 Answers

that's easy but you need some math knowledge.

        PointF pointA, pointB;

        var diff_X = pointB.X - pointA.X;
        var diff_Y = pointB.Y - pointA.Y;
        int pointNum = 8;

        var interval_X = diff_X / (pointNum + 1);
        var interval_Y = diff_Y / (pointNum + 1);

        List<PointF> pointList = new List<PointF>();
        for (int i = 1; i <= pointNum; i++)
        {
            pointList.Add(new PointF(pointA.X + interval_X * i, pointA.Y + interval_Y*i));
        }
like image 168
Kanjie Lu Avatar answered Oct 19 '22 22:10

Kanjie Lu


Straitforward trigonometric solution could be something like that:

// I've used Tupple<Double, Double> to represent a point;
// You, probably have your own type for it
public static IList<Tuple<Double, Double>> SplitLine(
  Tuple<Double, Double> a, 
  Tuple<Double, Double> b, 
  int count) {

  count = count + 1;

  Double d = Math.Sqrt((a.Item1 - b.Item1) * (a.Item1 - b.Item1) + (a.Item2 - b.Item2) * (a.Item2 - b.Item2)) / count;
  Double fi = Math.Atan2(b.Item2 - a.Item2, b.Item1 - a.Item1);

  List<Tuple<Double, Double>> points = new List<Tuple<Double, Double>>(count + 1);

  for (int i = 0; i <= count; ++i)
    points.Add(new Tuple<Double, Double>(a.Item1 + i * d * Math.Cos(fi), a.Item2 + i * d * Math.Sin(fi)));

  return points;
}

...

IList<Tuple<Double, Double>> points = SplitLine(
  new Tuple<Double, Double>(35.163, 128.001),
  new Tuple<Double, Double>(36.573, 128.707),
  8);

Outcome (points):

(35,163, 128,001)                    // <- Initial point A
(35,3196666666667, 128,079444444444)
(35,4763333333333, 128,157888888889)
(35,633, 128,236333333333)
(35,7896666666667, 128,314777777778)
(35,9463333333333, 128,393222222222)
(36,103, 128,471666666667)
(36,2596666666667, 128,550111111111)
(36,4163333333333, 128,628555555556)
(36,573, 128,707)                    // <- Final point B
like image 8
Dmitry Bychenko Avatar answered Oct 19 '22 21:10

Dmitry Bychenko