2014-01-08 7 views
5

私はpoint3Dクラスとvector3Dクラスで作業しています。ある3D点から別の3D点までの距離を一定の距離だけ調整する方法

  • 点A点は座標0,0,0にある。
  • 点B点は座標1,1,1に存在する。
  • ベクトルAB - 2点AとBの間の長さが距離= 1.73205078であることを示すベクトルAB。

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. 

Iは、(位置に調整私は0.5の代わりに1に、点Aと点Bとの間の距離を短くしたい点Bを調整したいです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クラスを定義しました。

+1

ベクトル[1 1 1]の長さは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

が、それは元の2つの点の間に右に座って、です。

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のようになります。あなたは、AとBを変更し、代わりにこの値をとる場合は、シミュレーションとして

、。さて、同じ点は、しかしdistanceToAdjust = 1.2を使用して

enter image description here

enter image description here

念頭に置いて、この二つのことをしてください:距離で

  • 変更は、常に基準点を必要としています。私のサンプルでは、​​私はAと仮定した。そのため、それは各newCoordinateパラメータの初期化の最初の部分として表示されます。
  • distanceToAdjustは、倍率と見なされました。

補足:視覚化を助けるために私が使用した素晴らしいツールcan be found here

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

ポイントAは常に[0,0,0]

Point3D new = B.Normalize() * distance; 
です。任意の2点について

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

ない高速なソリューションけれども。

3

"2点AとBとの間の距離= 1であり、長さが" No、距離は約1.732三の平方根です。

(0,0,0)から(0,0,1)までの距離は1です。(0,0,0)から(0,1,1)までの距離は2の平方根です。 (二次元の三角形とPythagoasの定理を考える)(0,0,0)から(1,1,1)までの距離は3の平方根です。 (その寸法は、前の三角形のhypothenuseに沿って平面上にある2次元で三角形を考える。AB =√(1²+(√2)²)。)


私はあなたがドンと仮定」 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

um ... 2ではなく、3の平方根です。 '| V | = sqrt(x^2 + y^2 + z^2) ' –

+0

@Jはスポットライトです。 – OnoSendai

+0

@J ...:はい、そうです。 Doh。 :) – Guffa

関連する問題