简体   繁体   English

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

[英]How to adjust the distance of one 3D point from another 3D point by a given distance

I am working with point3D and vector3D classes and I need some help adjusting a point by a given distance. 我正在使用point3D和vector3D类,我需要一些帮助调整给定距离的点。

  • Point A - point residing at coordinate 0,0,0. A点 - 位于坐标0,0,0处的点。
  • Point B - point residing at coordinate 1,1,1. B点 - 位于坐标1,1,1处的点。
  • Vector AB - vector AB which tells me the length between the two points A and B is distance = 1.73205078. 向量AB - 向量AB告诉我两点A和B之间的长度是距离= 1.73205078。

在此输入图像描述

Code: 码:

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.

I would like to adjust point B. I would like to reduce the distance between point A and point B to 0.5 instead of 1 (adjusting to position C as shown in the diagram). 我想调整点B.我想将点A和点B之间的距离减小到0.5而不是1(调整到位置C,如图所示)。 I am trying to work out how to do this. 我想弄清楚如何做到这一点。

Point A (0,0,0) is known, point B (1,1,1) is known and the distance to adjust by is known (0.5). 点A(0,0,0)是已知的,点B(1,1,1)是已知的并且调整的距离是已知的(0.5)。 How do I calculate? 我该如何计算?

Pseudo code: 伪代码:

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!

Adjusted point B shown in diagram below: 调整后的B点如下图所示:

在此输入图像描述

I am using my own defined Point3D class and Vector3D class. 我正在使用自己定义的Point3D类和Vector3D类。

Let's assume your given parameters for your points, and create a 3rd, which we'll call newCoordinate , and that point A will be your reference: 让我们假设您的点数给定参数,并创建第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)
                                    }

Here we see the original points: 在这里,我们看到原始点:

原始绘图点

Assuming this values, newCoordinate would sit at X=0.5, Y=0.5, Z=0.5. 假设这个值, newCoordinate将位于X = 0.5,Y = 0.5,Z = 0.5。 Nice graph follows: 好的图表如下:

在此输入图像描述

There it is, sitting right in between the two original points. 就在那里,坐在两个原点之间。

As a simulation, if you change A and B and assume this values instead: 作为模拟,如果您更改A和B并改为采用此值:

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

Then newCoordinate position would be X=-2.5, Y=3, Z=2. 然后newCoordinate位置将是X = -2.5,Y = 3,Z = 2。

在此输入图像描述

Now, same points, but using distanceToAdjust = 1.2 : 现在,相同点,但使用distanceToAdjust = 1.2

在此输入图像描述

Keep this two things in mind: 记住这两件事:

  • Changes in distance always need a reference point. 距离的变化总是需要一个参考点。 In my sample, I assumed A; 在我的样本中,我假设A; that's why it appears as the first portion of each newCoordinate parameter initialization. 这就是为什么它作为每个newCoordinate参数初始化的第一部分出现的原因。
  • distanceToAdjust was taken as a multiplier factor . distanceToAdjust被视为乘数因子

Addendum: The nifty tool I used to help visualization can be found here . 附录:我可以在这里找到用于帮助可视化的漂亮工具。

Assuming you implemented vector operations: 假设您实现了向量操作:

if point A is always [0,0,0] 如果A点总是[0,0,0]

Point3D new = B.Normalize() * distance;

for any two points 任何两点

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

not fast solution though. 但不是快速解决方案。

"the length between the two points A and B is distance = 1" “两点A和B之间的距离是距离= 1”

No, the distance is the square root of three, about 1.732. 不,距离是三的平方根,约为1.732。

The distance from (0,0,0) to (0,0,1) is 1. The distance from (0,0,0) to (0,1,1) is the square root of two. 从(0,0,0)到(0,0,1)的距离是1.从(0,0,0)到(0,1,1)的距离是2的平方根。 (Think a triangle in two dimensions, and Pythagoas theorem.) The distance from (0,0,0) to (1,1,1) is the square root of three. (想想二维的三角形和Pythagoas定理。)从(0,0,0)到(1,1,1)的距离是三的平方根。 (Think a triangle in two dimensions, where that dimension is on a plane along the hypothenuse of the previous triangle. AB = √(1² + (√2)²).) (想想一个二维的三角形,其中该尺寸位于沿前一个三角形的低位线的平面上.AB =√(1²+(√2)²)。)


I assume that you don't want to subtract 0.5 from anything, but actually multiply the distance by 0.5, ie getting halfways from A to B. You can calculate the point C by taking that part of the distance between point A and point B in each dimension: 我假设您不想从任何东西中减去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
};

In pseudo code, here's how I ended up implementing 在伪代码中,这是我最终实现的方式

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

pointC = A+vectorAC;

Actual code: 实际代码:

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

I'm unsure if this is what you would need but is it possible to create a method within your Point3D class to allow subtraction/addition? 我不确定这是否是您需要的但是可以在Point3D类中创建一个允许减法/加法的方法吗?

(Just guessing the Point3D class as simply as it could be)Something like (只是尽可能简单地猜测Point3D类)

   public class Point3D
   {

       public double X,Y,Z

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

So it could just be: 所以它可能只是:

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

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

相关问题 如何生成与给定平面相距一定距离的随机3D点? - How to generate a random 3D point with a distance to a given plane? 来自现有点、方向和距离的 3D 点 - 3D point from existing point, direction and distance c# 使用距离和角度从开始 3D 点计算 3D 点 - c# Calculate a 3D point from starting 3D point using distance and angles 在3D空间中找到一个点到三角形的距离 - Finding the distance of a point from a triangle in 3d space 3D点上的垂直点 - 3D Perpendicular Point on Line From 3D point 从3d空间中的一组点移动到具有最短可能累积距离的另一组点 - Moving from one set of points in 3d space to another set of points with the shortest possible accumulative distance 如何在3D三角形的给定点(x,z)上找到y? - How to find y at given point (x,z) on 3D triangle? 检查3D点是否位于给定的3D线上(两个3D点之间) - Check if a 3D Point lies on a given 3D line(between two 3D Points) 使用距原点的距离作为第三个参数,确定由两个 3D 点定义的线上任意点的 x、y、z 坐标? - Determine the x,y,z coordinates of any point on a line defined by two 3D points using distance from origin-point as the third parameter? 尝试从2D图像精确测量3D距离 - Trying to accurately measure 3D distance from a 2D image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM