wpfc#-3.0viewport3d

How can I draw an Arc using MeshGeometry3D in WPF c#?


I want to draw Arc in Vieport3D using MeshGeometry3D.I have searched for it a lot but not found anything ,what I found is using PathGeometry.I am new to WPF so don't know much about 3D graphics.How can I do this?

thanks


Solution

  • here is the code which I wrote to create mesh-geometry for Arc.I hope this will help someone.

     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;
    
    
        }