2011-12-23 17 views
2

私はModelVisual3Dをいくつかの立方体で持っています。グループ全体をその中心で回転させたい場合は、どうすればいいですか?ModelVisual3Dの中心

RotateTransform3D rt; 
AxisAngleRotation3D ar; 
Transform3DGroup grp; 

rt = new RotateTransform3D(); 
ar = new AxisAngleRotation3D();      

ar.Axis = new Vector3D(1, 0, 0); 
ar.Angle = x; //x a value 0-360 
rt.Rotation = ar; 
rt.CenterX = //*Here i need the center of the ModelVisual3D X* 
rt.CenterY = //*Here i need the center of the ModelVisual3D Y* 
rt.CenterZ = //*Here i need the center of the ModelVisual3D Z* 

grp = new Transform3DGroup(); 
grp.Children.Add(rt); 

cubes.Transform = grp; //cubes is the ModelVisual3D object that i want to rotate 

答えて

2

あなたはすべてのポイントの平均値を計算することができます

は、ここに私のコードです。 Pコード:

Point avg 
for (point in points) 
    avg = avg + point 
    ++count 

avg /= count 

これはあなたの中心です。

単純な物理シミュレーションでは、各ポイントにウェイトを追加します。

1
public Point3D GetCenter(ModelVisual3D model) 
{ 
    var rect3D = Rect3D.Empty; 
    UnionRect(model, ref rect3D); 
    _center = new Point3D((rect3D.X + rect3D.SizeX/2), (rect3D.Y + rect3D.SizeY/2), (rect3D.Z + rect3D.SizeZ/2)); 
    return _center; 
} 

private void UnionRect(ModelVisual3D model, ref Rect3D rect3D) 
{ 
    for (int i = 0; i < model.Children.Count; i++) 
    { 
     var child = model.Children[i] as ModelVisual3D; 
     UnionRect(child, ref rect3D); 
    } 
    if (model.Content != null) 
     rect3D.Union(model.Content.Bounds); 
} 
+0

はStackOverflowのへようこそ!ポストの精度を向上させるためのサンプルコードの簡単な説明を提供する方が良いでしょう。 –

0

簡単にあなたが知っていれば、どのように:

Rect3D d1 = MeshGeometry3D_1.Bounds; 
d1.Union(MeshGeometry3D_2.Bounds); 
// to the center of the block: 
Vector3D cVect = new Vector3D(d1.SizeX/2, d1.SizeY/2, d1.SizeZ/2);   
columnModel.Transform = new TranslateTransform3D(-cVect);