1. 程式人生 > 其它 >c# Polygon 畫多邊形,

c# Polygon 畫多邊形,

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 WpfMosaic
{
   public  class RandomShape:Grid 
    {
        public RandomShape() {
            Width = 100;
            Height = 100;
            //Background= new SolidColorBrush(Colors.Gray);
            Polygon p = new Polygon();
            p.Stroke = new SolidColorBrush(Colors.White);
            p.StrokeThickness = 1;
            p.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)Utils.rnd.Next(0, 256), (byte)Utils.rnd.Next(0, 256), (byte)Utils.rnd.Next(0, 256)));
            PointCollection ps = GetPoints(new Point(50,50),Utils.rnd.Next(10,50),Utils.rnd.Next(3,25));
            p.Points = ps;

            Children.Add(p);
        }


  
        /// <param name="pointCenter">中心座標</param>
        /// <param name="r">半徑</param>
        /// <param name="count">等分分數</param>
        /// <returns></returns>
        private PointCollection GetPoints(Point pointCenter, int r, int count)
        {
            Point[] point = new Point[count];
            PointCollection pointCollection = new PointCollection();
            for (int i = 0; i < count; i++)
            {
                point[i].X = (int)(r * Math.Cos((i + 1) * 360 / count * Math.PI / 180)) + pointCenter.X;
                point[i].Y = (int)(r * Math.Sin((i + 1) * 360 / count * Math.PI / 180)) + pointCenter.Y;
                pointCollection.Add(point[i]);
            }
   
            return pointCollection;
        }

    }
}

  

fffffffffffffffff test red font.