如何调整一个3D点距另一个3D点的距离给定距离

时间:2014-01-08 16:51:42

标签: c# math vector point

我正在使用point3D和vector3D类,我需要帮助调整给定距离的点。

  • A点 - 位于坐标0,0,0处的点。
  • B点 - 位于坐标1,1,1的点。
  • 矢量AB - 矢量AB,它告诉我两点A和B之间的长度是距离= 1.73205078。

enter image description here

代码:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
Vector3D AtoB = A - B;
Double distanceBetweenAandB = AtoB.Length; // the distance will be 1.73205078 here.

我想调整点B.我想将点A和点B之间的距离减小到0.5而不是1(调整到位置C,如图所示)。我正试图弄清楚如何做到这一点。

点A(0,0,0)已知,点B(1,1,1)已知且调整距离已知(0.5)。我该如何计算?

伪代码:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
Double distanceToAdjust = 0.5;

Point3D newCoordinate = B - distanceToAdjust; // this doesnt work!

调整后的B点如下图所示:

enter image description here

我使用自己定义的Point3D类和Vector3D类。

5 个答案:

答案 0 :(得分:4)

让我们为您的积分假设您的参数,并创建第3个,我们称之为newCoordinate,并且该点A将作为您的参考:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };

Double distanceToAdjust = 0.5;

Point3D newCoordinate = new Point3D { 
                                        A.X + ((B.X - A.X) * distanceToAdjust),
                                        A.Y + ((B.Y - A.Y) * distanceToAdjust),
                                        A.Z + ((B.Z - A.Z) * distanceToAdjust)
                                    }

我们在这里看到原始点:

Original plotted points

假设这个值,newCoordinate将位于X = 0.5,Y = 0.5,Z = 0.5。好的图表如下:

enter image description here

就在那里,坐在两个原点之间。

作为模拟,如果您更改A和B并改为采用此值:

Point3D A = new Point3D { X = -8, Y = 4, Z = 3 };
Point3D B = new Point3D { X = 3, Y = 2, Z = 1 };

然后newCoordinate位置将是X = -2.5,Y = 3,Z = 2.

enter image description here

现在,相同点,但使用distanceToAdjust = 1.2

enter image description here

记住这两件事:

  • 距离的变化总是需要一个参考点。在我的样本中,我假设A;这就是为什么它显示为每个newCoordinate参数初始化的第一部分。
  • distanceToAdjust被视为乘数因子

附录:我用来帮助实现can be found here的漂亮工具。

答案 1 :(得分:4)

假设你实现了向量操作:

如果A点总是[0,0,0]

Point3D new = B.Normalize() * distance;

任意两点

Point3D newCoord = A + ((B - A).Normalize() * distance); //move to origin, normalize, scale and move back

虽然不是快速解决方案。

答案 2 :(得分:3)

  

“两点A和B之间的长度是距离= 1”

不,距离是三的平方根,约为1.732。

从(0,0,0)到(0,0,1)的距离是1.从(0,0,0)到(0,1,1)的距离是2的平方根。 (想想二维的三角形和Pythagoas定理。)从(0,0,0)到(1,1,1)的距离是三的平方根。 (想想一个二维的三角形,其中该尺寸位于沿前一个三角形的连字符的平面上.AB =√(1²+(√2)²)。)


我假设您不想从任何东西中减去0.5,但实际上将距离乘以0.5,即从A到B的中途。您可以通过获取A点和A点之间距离的那一部分来计算C点。每个维度中的B点:

Point3D C = new Point3D {
  A.X + (B.X - A.X) * distanceToAdjust,
  A.Y + (B.Y - A.Y) * distanceToAdjust,
  A.Z + (B.Z - A.Z) * distanceToAdjust
};

答案 3 :(得分:1)

在伪代码中,这是我最终实现的方式

pointA = …
pointB = …
vectorAB = B-A
desiredDistance = 0.5;  // where 0.5 is vectorAB.Length/desiredDistance
vectorAC = vectorAB * desiredDistance ; 

pointC = A+vectorAC;

实际代码:

Vector3D pointC = (Vector3D)(A + (float)desiredDistance  * (B - A)); 

答案 4 :(得分:0)

我不确定这是否是你需要的但是可以在你的Point3D类中创建一个允许减法/加法的方法吗?

(尽可能简单地猜测Point3D类)像

这样的东西
   public class Point3D
   {

       public double X,Y,Z

       public void ChangeCord(Point3D point)
       {
           X =- point.X;
           Y =- point.Y;
           Z =- point.Z;
       }
   }

所以它可能只是:

    Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
    Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
    Double distanceToAdjust = 0.5;

    Point3D newCoordinate = B.ChangeCord(new Point3d{ X = 0.5, Y = 0.5, Z = 0.5 });
相关问题