简体   繁体   English

如何计算两个给定点和给定距离之间的点?

[英]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) 我有A (35.163 , 128.001)A (35.163 , 128.001)B (36.573 , 128.707)B (36.573 , 128.707)

I need to calculate the points lies within point A and point B 我需要计算点在A点和B点之间

using the standard distance formula between 2 points, I found D = 266.3 使用2点之间的标准距离公式,我发现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 每个点都位于线AB内(黑点p1,p2,... p8)以等距离分开d = D / 8 = 33.3

How could I calculate the X and Y for p1 , p2, ... p8? 如何计算p1,p2,... p8的X和Y?

example of Java or C# language are welcomed 欢迎使用Java或C#语言的示例

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* **以上计算实际上用于计算我地图中阴影级别的虚拟点并用于阴影区域插值*

找出A和B之间所有点的X和Y

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

Straitforward trigonometric solution could be something like that: Straitforward三角解可能是这样的:

// 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

Subtract A from B, component-wise, to get the vector from A to B. Multiply that vector by the desired step value and add it to A. (Note that with eight intermediate steps as you've illustrated, the step distance is 1.0 / 9.0.) Something like this, assuming you really want seven points: 从B减去A,将向量从A减去到B。将该向量乘以所需的步长值并将其添加到A。(请注意,如图所示,在八个中间步长中,步距为1.0 / 9.0。)类似这样,假设您确实想要七点:

vec2 A = vec2 (35.163, 128.001);
vec2 B = vec2 (36.573, 128.707);
vec2 V = B - A;
for (i = 1; i < 8; i++) {
    vec2 p[i] = A + V * (float)i / 8.0;
}

(Sorry, don't know any Java or C#.) (抱歉,不知道任何Java或C#。)

let A be point (x a , y a ), and B be point (x b , y b ) 设A为点(x a ,y a ),B为点(x b ,y b

alpha = tan -1 ((y b - y a )/(x b - x a )) alpha = tan -1 ((y b -y a )/(x b -x a ))

p1 = (x a + d * cos(alpha), y a + d * sin(alpha)) p1 =(x a + d * cos(alpha),y a + d * sin(alpha))

p k = (x a + kd * cos(alpha), y a + kd * sin(alpha)), k = 1 to 7 p k =(x a + kd * cos(alpha),y a + kd * sin(alpha)),k = 1至7

(An equivalent way would be to use vector arithmetic) (等效的方法是使用向量算法)

  • At first find the slope of AB line. 首先找到AB线的斜率。 Get help and formula from here: http://www.purplemath.com/modules/slope.htm 从此处获取帮助和公式: http : //www.purplemath.com/modules/slope.htm

  • Then consider a triangle of Ap1E(think there is a point E which is right to A and below to p1). 然后考虑一个Ap1E的三角形(认为有一个点E相对于A且在p1之下)。

  • You already know the angle AEp1 is 90degree. 您已经知道角度AEp1为90度。 and you have calculated angle p1AE(from the slope of AB). 并且已经计算了角度p1AE(从AB的斜率开始)。
  • Now find AE and Ep1. 现在找到AE和Ep1。

  • Xp1=Xa+AE and Yp1=Ya+Ep1 Xp1 = Xa + AE和Yp1 = Ya + Ep1

This will not be very difficult in C# or java. 在C#或Java中,这并不是很难。 Once you understand the logic, you will find pleasure implementing on your own way. 一旦理解了逻辑,便会发现以自己的方式实现的乐趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM