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); } /// 中心坐标 /// 半径 /// 等分分数 /// 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; } } }

 钻石图形: