WPF 等长的多个圆弧构成一个圆

4 篇文章 0 订阅

效果

前台代码

<Grid x:Name="grid" />

后台代码

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApp
{
    /// <summary>
    /// Window3.xaml 的交互逻辑
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();

            string[] colorList = { "Red","Yellow","Green","#FF0000FF","Pink" };

            for (int i = 0; i < colorList.Length; i++)
            {
                var color = (Color)ColorConverter.ConvertFromString(colorList[i]);
                Paint(new Point(40, 40), 30, colorList.Length, i, new SolidColorBrush(color));
            }
        }

        public void Paint(Point center, double radius, int num, int index, Brush brush)
        {
            double 每段弧度 = 2 * Math.PI / num;
            double 起始弧度 = 每段弧度 * index;
            double 终止弧度 = 每段弧度 * (index + 1);

            var 起始弧度的X偏移 = Math.Sin(起始弧度);
            var 起始弧度的Y偏移 = Math.Cos(起始弧度);

            var 终止弧度的X偏移 = Math.Sin(终止弧度);
            var 终止弧度的Y偏移 = Math.Cos(终止弧度);

            Point 起点 = new Point(center.X + radius * 起始弧度的X偏移, center.Y - radius * 起始弧度的Y偏移);
            Point 终点 = new Point(center.X + radius * 终止弧度的X偏移, center.Y - radius * 终止弧度的Y偏移);

            string data = $"M{起点.X},{起点.Y} A{radius},{radius},0,0,1 {终点.X},{终点.Y}";

            Path path = new Path();
            path.Stroke = brush;
            path.StrokeThickness = 5;
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            path.Data = (Geometry)converter.ConvertFrom(data);

            grid.Children.Add(path);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值