如何在WPF c#中使用MeshGeometry3D绘制弧?

时间:2018-03-17 08:56:26

标签: wpf c#-3.0 viewport3d

我想使用MeshGeometry3D在Vieport3D中绘制Arc。我已经搜索了很多但没有找到任何东西,我发现使用的是PathGeometry。我是WPF的新手,所以不太了解3D图形。如何我可以这样做吗?

谢谢

1 个答案:

答案 0 :(得分:0)

这里是我为Arc创建网格几何的代码。我希望这会对某人有所帮助。

 private GeometryModel3D GetModel(double radius, Vector3D normal, Point3D center, int resolution, double StartAngle, double EndAngle)
    {
        var mod = new GeometryModel3D();
        var geo = new MeshGeometry3D();

        // Generate the circle in the XZ-plane
        // Add the center first
        geo.Positions.Add(new Point3D(0, 0, 0));

        // Iterate from angle 0 to 2*PI
        double dev = (2 * Math.PI) / resolution;
        double thik = 0.02;
        //float spaceangle = StartAngle + 1;
        if (StartAngle != EndAngle)
        {
            for (double i = StartAngle; i < EndAngle; i += dev)
            {
                geo.Positions.Add(new Point3D(radius * Math.Cos(i), 0, -radius * Math.Sin(i)));
                geo.Positions.Add(new Point3D((radius-thik) * Math.Cos(i), 0, (-(radius-thik)) * Math.Sin(i)));
            }


            for (int i = 3; i < geo.Positions.Count; i += 1)
            {
                geo.TriangleIndices.Add(i - 3);
                geo.TriangleIndices.Add(i - 1);
                geo.TriangleIndices.Add(i - 2);

                geo.TriangleIndices.Add(i - 1);
                geo.TriangleIndices.Add(i);
                geo.TriangleIndices.Add(i - 2);
            }
        }


        mod.Geometry = geo;
        // Create transforms
        var trn = new Transform3DGroup();
        // Up Vector (normal for XZ-plane)
        var up = new Vector3D(0, 1, 0);
        // Set normal length to 1
        normal.Normalize();
        var axis = Vector3D.CrossProduct(up, normal); // Cross product is rotation axis
        var angle = Vector3D.AngleBetween(up, normal); // Angle to rotate
        trn.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(axis, angle)));
        trn.Children.Add(new TranslateTransform3D(new Vector3D(center.X, center.Y, center.Z)));

        mod.Transform = trn;
        return mod;


    }