//随机多边形:
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; } } }
钻石图形:
<Canvas> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,50, 50,0, 100,50" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="9"> <Polygon.Fill> <SolidColorBrush x:Name="Color1" Color="Gray" Opacity="0.4"/> </Polygon.Fill> </Polygon> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,50, 50,0, 100,50" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="60"> <Polygon.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform Angle="180"/> <TranslateTransform/> </TransformGroup> </Polygon.RenderTransform> <Polygon.Fill> <SolidColorBrush x:Name="Color2" Color="Red" Opacity="0.4"/> </Polygon.Fill> </Polygon> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,50, 50,0, 100,50" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="110"> <Polygon.Fill> <SolidColorBrush x:Name="Color3" Color="Gray" Opacity="0.4"/> </Polygon.Fill> </Polygon> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,0, 50,0, 100,120" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="9" Canvas.Top="51"> <Polygon.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform Angle="0"/> <TranslateTransform/> </TransformGroup> </Polygon.RenderTransform> <Polygon.Fill> <SolidColorBrush x:Name="Color4" Color="Gray" Opacity="0.4"/> </Polygon.Fill> </Polygon> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,0, 50,0, 100,120" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="110" Canvas.Top="51"> <Polygon.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="-1"/> <SkewTransform/> <RotateTransform Angle="0"/> <TranslateTransform/> </TransformGroup> </Polygon.RenderTransform> <Polygon.Fill> <SolidColorBrush x:Name="Color5" Color="Gray" Opacity="0.4"/> </Polygon.Fill> </Polygon> <Polygon HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill" Points="0,0, 100,0, 50,120" Stroke="White" StrokeThickness="1" RenderTransformOrigin="0.5,0.5" Canvas.Left="59" Canvas.Top="51"> <Polygon.Fill> <SolidColorBrush x:Name="Color6" Color="Blue" Opacity="0.4"/> </Polygon.Fill> </Polygon> </Canvas>