简体   繁体   English

WPF中的3d矢量计算

[英]3d vector calculation in WPF

I draw two spheres in 3d WPF which has points like Point3D(0,0,0) and Point3D(-1.0,1.0,2.0) with the radius is 0.10 我在3d WPF中绘制了两个球体,它们的点如Point3D(0,0,0)和Point3D(-1.0,1.0,2.0)的半径为0.10

Now i want to draw a Cylinder joining these spheres, the only thing i have for this is radius 0.02. 现在,我想绘制一个连接这些球体的圆柱体,对此我唯一拥有的就是半径0.02。 I want to know how to calculate the 3d point, height, direction etc for this cylinder. 我想知道如何计算此圆柱的3d点,高度,方向等。

I tried by finding the midpoint btw sphere points, it's placing the cylinder in the middle of those two spheres but not in the right direction. 我尝试通过找到btw球体的中点,将圆柱体放置在这两个球体的中间,但方向不正确。 I am trying to rotate using in the code below. 我正在尝试在下面的代码中轮换使用。

    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;

but the double angle = Vector3D.AngleBetween(v1, v2); 但双角= Vector3D.AngleBetween(v1,v2); ruselting NaN 罗素钠

It would be really nice, if someone could help in doing this. 如果有人可以帮助做到这一点,那就太好了。 Thanks in advance. 提前致谢。

You could simply use a RotateTransform3D to rotate the cylinder to the required direction. 您可以简单地使用RotateTransform3D将圆柱体旋转到所需的方向。

You can apply the transform to one of two properties here is an explanation of the options 您可以将转换应用于两个属性之一, 这里是选项的说明

The direction you need is the vector between the centres of the spheres: 您需要的方向是球心之间的向量:

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

Then divide these by the length of the vector to get a unit vector: 然后将它们除以向量的长度即可得到单位向量:

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;

Then you can use this to calculate your rotation matrix. 然后,您可以使用它来计算旋转矩阵。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM