tags:

views:

654

answers:

2

Hi .. 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

Now i want to draw a Cylinder joining these spheres, the only thing i have for this is radius 0.02. I want to know how to calculate the 3d point, height, direction etc for this cylinder.

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. 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); ruselting NaN

It would be really nice, if someone could help in doing this. Thanks in advance.

A: 

You could simply use a RotateTransform3D to rotate the cylinder to the required direction.

You can apply the transform to one of two properties here is an explanation of the options

Simon Fox
Hi Simon- How do i calculate a required direction from those two sphere points?
SituStarz
You probably just want to create the rotate transform with a AxisAngleRotation3d which describes the axis you want to rotate about (this will determine the direction of the rotation) and the angle of the rotation (which is "how far" about that axis the object is rotated)
Simon Fox
A: 

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.

ChrisF