WPF Path Data PathGeometry PathFigure Segments BezierSegment,LineSegment,ArcSegment

 BezierSegment

//Bezier Curve
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp170
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PathFigureDemo();
        }

        private void PathFigureDemo()
        {
            PathFigure pfi = new PathFigure();
            pfi.StartPoint = new Point(100, 150);
            pfi.Segments.Add(
                new BezierSegment(
                    new Point(100, 0),
                    new Point(500, 500),
                    new Point(800, 200), true));           
            PathGeometry pathGeo = new PathGeometry();
            pathGeo.Figures.Add(pfi);

            Path ph = new Path();
            ph.Stroke = new SolidColorBrush(Colors.Red);
            ph.StrokeThickness = 5;
            ph.Data = pathGeo;
            this.Content= ph;
        }
    }
}

 

 

LineSegment

public MainWindow()
{
    InitializeComponent();
    LineSegmentFigureDemo();
}

void LineSegmentFigureDemo()
{
    PathFigure pfi = new PathFigure();
    pfi.StartPoint = new Point(0, 0);
    pfi.Segments.Add(
        new LineSegment(new Point(1600, 900), true));
    PathGeometry pathGeo = new PathGeometry();
    pathGeo.Figures.Add(pfi);
    Path ph = new Path();
    ph.Stroke = new SolidColorBrush(Colors.Red);
    ph.StrokeThickness = 20;
    ph.Data = pathGeo;
    this.Content = ph;
}

 

 

 

 

ArcSegment

 public MainWindow()
 {
     InitializeComponent();
     ArcSegmentFigureDemo();
 }

 void ArcSegmentFigureDemo()
 {
     PathFigure pfi = new PathFigure();
     pfi.StartPoint = new Point(200, 200);
     pfi.Segments.Add(
        new ArcSegment(new Point(800, 800), new Size(300, 300), 50, true, SweepDirection.Clockwise,
        true));
     PathGeometry pathGeo = new PathGeometry();
     pathGeo.Figures.Add(pfi);

     Path ph = new Path();
     ph.Stroke = new SolidColorBrush(Colors.Red);
     ph.StrokeThickness = 20;
     ph.Fill = new SolidColorBrush(Colors.Cyan);
     ph.Data = pathGeo;
     this.Content = ph;
 }

 

 

Mixed LineSegment,BezierSegment,ArcSegment 

public MainWindow()
{
    InitializeComponent();
    MixedLineBezierArcSegmentDemo();
}

void MixedLineBezierArcSegmentDemo()
{
    PathFigure pfi = new PathFigure();
    pfi.StartPoint = new Point(0, 0);
    pfi.Segments.Add(new LineSegment(new Point(1600, 900), true));
    pfi.Segments.Add(new ArcSegment(new Point(800, 800), new Size(300, 300), 50, true, SweepDirection.Clockwise,true));
    pfi.Segments.Add(new BezierSegment(
            new Point(100, 0),
            new Point(500, 500),
            new Point(800, 200), true));
    PathGeometry pathGeo = new PathGeometry();
    pathGeo.Figures.Add(pfi);
    double area= pathGeo.GetArea(0, ToleranceType.Absolute);
    this.Title = area.ToString();
    Path ph = new Path();
    ph.Stroke = new SolidColorBrush(Colors.Red);
    ph.StrokeThickness = 20;
    ph.Fill = new SolidColorBrush(Colors.Cyan);
    ph.Data = pathGeo;
    
    this.Content = ph;
}

 

 

/// <summary>Gets the area, within the specified tolerance, of the filled region of the <see cref="T:System.Windows.Media.Geometry" /> object.</summary>
/// <param name="tolerance">The maximum bounds on the distance between points in the polygonal approximation of the geometry. Smaller values produce more accurate results but cause slower execution. If <paramref name="tolerance" /> is less than .000001, .000001 is used instead.</param>
/// <param name="type">One of the <see cref="T:System.Windows.Media.ToleranceType" /> values that specifies whether the tolerance factor is an absolute value or relative to the area of the geometry.</param>
/// <returns>The area of the filled region of the geometry.</returns>
[SecurityCritical]
public unsafe virtual double GetArea(double tolerance, ToleranceType type)
{
    ReadPreamble();
    if (IsObviouslyEmpty())
    {
        return 0.0;
    }
    PathGeometryData pathGeometryData = GetPathGeometryData();
    if (pathGeometryData.IsEmpty())
    {
        return 0.0;
    }
    double result = default(double);
    fixed (byte* pPathData = pathGeometryData.SerializedData)
    {
        int num = MilCoreApi.MilUtility_GeometryGetArea(pathGeometryData.FillRule, pPathData, pathGeometryData.Size, &pathGeometryData.Matrix, tolerance, type == ToleranceType.Relative, &result);
        if (num == -2003304438)
        {
            result = 0.0;
        }
        else
        {
            HRESULT.Check(num);
        }
    }
    return result;
}

 

posted @ 2024-06-15 21:18  FredGrit  阅读(11)  评论(0编辑  收藏  举报