WPF实现能自由改变形状的四边形和六边形

效果图

WPF实现能自由改变形状的四边形和六边形_第1张图片

实现步骤

程序的C#部分,使用简单的三角函数实现正六边形的确定

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 path
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LineSegment line1 = new LineSegment(new Point(0.5 + 0.5 * Math.Cos(Math.PI / 6), 0.5 + 0.5 * Math.Cos(Math.PI / 3)), true);
            LineSegment line2=new LineSegment(new Point(0.5+0.5*Math.Cos(Math.PI/6), 0.5-0.5*Math.Cos(Math.PI/3)), true);

            PathFigure figure1 = new PathFigure();
            figure1.StartPoint = new Point(0.5 - 0.5 * Math.Cos(Math.PI / 6), 0.5 - 0.5 * Math.Cos(Math.PI / 3));
            figure1.Segments.Add(line1);
            PathFigure figure2 = new PathFigure();
            figure2.StartPoint = new Point(0.5 - 0.5 * Math.Cos(Math.PI / 6), 0.5 + 0.5 * Math.Cos(Math.PI / 3));
            figure2.Segments.Add(line2);

            PathGeometry path1 = new PathGeometry();
            path1.Figures.Add(figure1);
            PathGeometry path2 = new PathGeometry();
            path2.Figures.Add(figure2);

            x1.Geometry = path1;
            x2.Geometry = path2;
        }

        private void Slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry();
        }
        private void Slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry();
        }
        private void Slider3_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry();
        }
        private void Slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry();
        }

        private void ChangeGeometry()
        {
            LineSegment l1 = new LineSegment(new Point(0.5, (100 - s1.Value) / 200), true);
            LineSegment l2 = new LineSegment(new Point((100 - s3.Value) / 200, 0.5), true);
            LineSegment l3 = new LineSegment(new Point(0.5, (100 + s2.Value) / 200), true);
            LineSegment l4 = new LineSegment(new Point((100 + s4.Value) / 200, 0.5), true);

            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(0.5, (100 - s1.Value) / 200);
            pathFigure.Segments.Add(l2);
            pathFigure.Segments.Add(l3);
            pathFigure.Segments.Add(l4);
            pathFigure.Segments.Add(l1);

            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures.Add(pathFigure);

            gd.Geometry = pathGeometry;
            gd.Pen = new Pen(new SolidColorBrush(Colors.Black), 0.005);
        }
        private void Slider61_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void Slider62_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void Slider63_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void Slider64_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void Slider65_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void Slider66_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            ChangeGeometry6();
        }
        private void ChangeGeometry6()
        {
            LineSegment l1 = new LineSegment(new Point(0.5, (100 - s61.Value) / 200), true);
            LineSegment l2 = new LineSegment(new Point(s62.Value * Math.Cos(Math.PI / 6) / 200 + 0.5, 0.5 - s62.Value * Math.Cos(Math.PI / 3) / 200), true);
            LineSegment l3 = new LineSegment(new Point(s63.Value * Math.Cos(Math.PI / 6) / 200 + 0.5, 0.5 + s63.Value * Math.Cos(Math.PI / 3) / 200), true);
            LineSegment l4 = new LineSegment(new Point(0.5, (100 + s64.Value) / 200), true);
            LineSegment l5 = new LineSegment(new Point(0.5 - s65.Value * Math.Cos(Math.PI / 6) / 200, 0.5 + s65.Value * Math.Cos(Math.PI / 3) / 200), true);
            LineSegment l6 = new LineSegment(new Point(0.5 - s66.Value * Math.Cos(Math.PI / 6) / 200, 0.5 - s66.Value * Math.Cos(Math.PI / 3) / 200), true);

            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(0.5, (100 - s61.Value) / 200);
            pathFigure.Segments.Add(l2);
            pathFigure.Segments.Add(l3);
            pathFigure.Segments.Add(l4);
            pathFigure.Segments.Add(l5);
            pathFigure.Segments.Add(l6);
            pathFigure.Segments.Add(l1);

            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures.Add(pathFigure);

            gd6.Geometry = pathGeometry;
            gd6.Pen = new Pen(new SolidColorBrush(Colors.Black), 0.005);
        }
    }
}

C#的xaml部分


    
    	
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                
                    
                        
                    
                
            
            
                
                    
                        
                            
                        
                    
                    
                
            
        
        
            
                
            
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
            
                
            
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
            
                
                    
                        
                        
                        
                    
                    
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                        
                            
                        
                        
                            
                        
                    
                
            
            
                
                    
                    
                    
                
                
                    
                    
                    
                
                
                    
                    
                
                
                    
                
                
                    
                
            
        
        
            
                
            
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
            
                
            
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
            
                
            
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
            
                
                    
                        
                        
                        
                    
                    
                    
                    
                        
                            
                        
                    
                    
                        
                            
                        
                        
                            
                        
                        
                            
                        
                    
                
            
            
                
                    
                    
                    
                
                
                    
                    
                    
                
                
                    
                    
                
                
                    
                
                
                    
                
            
        
        
    
    
        
            
            
        
        
            
            
        
        
            
                
                    
                        
                            
                            
                            
                                
                                    
                                
                            
                            
                                
                                    
                                
                            
                        
                    
                
            
        
        
            
                
            
                
            
                
            
                
        

        
            
                
                    
                        
                            
                                
                                    
                                
                            
                            
                            
                                
                                    
                                
                            
                            
                                
                                    
                                
                            
                            
                                
                                    
                                
                            
                        
                    
                
            
        
        
            
                
            
                
            
                
            
                
            
                
            
                
        
    

以上就是WPF实现能自由改变形状的四边形和六边形的详细内容,更多关于WPF四边形 六边形的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(WPF实现能自由改变形状的四边形和六边形)