2014-01-08 42 views
5

我正在使用point3D和vector3D类,我需要一些帮助以给定距离调整点。如何调整一个3D点与另一个3D点之间的距离

  • 点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

我使用自己的定义三维点类和Vector3D类。

+1

向量'[111]'的长度不是1,它是'SQRT(3)= 1.732(...)'。 –

回答

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

让我们假设你给出的参数为您的积分,创造了第三个,我们称之为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

0

我不确定这是否是你需要的,但是可以在你的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 }); 
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 

没有快速的解决方案,但。

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)的距离是三的平方根。 (在两个维度,其中该尺寸是沿前一三角形的hypothenuse的平面认为三角形AB =√(1²+(√2)2)。。)


我假设你不t要从任何东西中减去0.5,但实际上将距离乘以0.5,即从A到B一半。你可以通过A点和B点之间的距离的一部分在每个维度计算点C:

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

呃......不是2,它是3的平方根。'| V | = sqrt(x^2 + y^2 + z^2)' –

+0

我必须说@J ...是现货。 – OnoSendai

+0

@J ...:是的,你说得对。卫生署。 :) – Guffa