在WPF中的3d矢量计算

时间:2009-08-12 00:42:01

标签: c# wpf 3d

我在3D WPF中画了两个球,它们有点像 Point3D(0,0,0)和Point3D(-1.0,1.0,2.0),半径为0.10

现在我想绘制一个连接这些球体的圆柱体,我唯一拥有的是半径0.02。我想知道如何计算这个圆柱体的3d点,高度,方向等。

我尝试通过找到中点btw球体点,它将圆柱体放置在这两个球体的中间但不是正确的方向。我想在下面的代码中使用旋转。

    Vector3D v1 = new Vector3D(0, 0, 0);
    Vector3D v2 = new Vector3D(1.0, -1.0, 2.0);

    Vector3D center = v1+ v2/2;
    Vector3D axis = Vector3D.CrossProduct(v1, v2);
    double angle = Vector3D.AngleBetween(v1, v2);
    AxisAngleRotation3D axisAngle = new AxisAngleRotation3D(axis, angle);
    RotateTransform3D myRotateTransform = new RotateTransform3D(axisAngle, center);
    center.X = myRotateTransform.CenterX;
    center.Y = myRotateTransform.CenterY;
    center.Z = myRotateTransform.CenterZ;

但双角= Vector3D.AngleBetween(v1,v2);扼杀NaN

如果有人可以帮忙做这件事,那将是非常好的。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您只需使用RotateTransform3D将圆柱旋转到所需的方向。

您可以将转换应用于两个属性之一here是对选项的解释

答案 1 :(得分:0)

你需要的方向是球体中心之间的向量:

direction.x = sphere1.Center.x - sphere2.Center.x;
direction.y = sphere1.Center.y - sphere2.Center.y;
direction.x = sphere1.Center.z - sphere2.Center.z;

然后将这些除以矢量的长度以得到单位矢量:

double length = Math.Sqrt(direction.x * direction.x +
                          direction.y * direction.y +
                          direction.z * direction.z);

direction.x /= length;
direction.y /= length;
direction.z /= length;

然后你可以用它来计算你的旋转矩阵。

相关问题