一般来说绘制正N边形,使用Blend直接画出来就好。不过可能是博主受WInform影响比较大,比较喜欢使用画出来的图形。如果要绘制正N边形,前面的绘制五角星的公式可以通用的(http://blog.csdn.net/yysyangyangyangshan/article/details/9378871)。
主要是利用圆,根据三角函数和圆的半径计算出圆上的N个点即可。
计算N各点的方法如下:

private PointCollection GetPolygonPoint(Point center, double r,int polygonBound)
{
double g = 18; double perangle = 360 / polygonBound; double pi = Math.PI; List<Point> values = new List<Point>(); for (int i = 0; i < (int) polygonBound; i++)
{
Point p2 = new Point(r * Math.Cos((g + 36) * pi / 180), r * Math.Sin((g + 36) * pi / 180)); values.Add(p2); g += perangle;
} PointCollection pcollect = new PointCollection(values); return pcollect;
}

g是起始角度,也是可以修改的。这个方法默认是在原点画出,r表示半径,polygonBound表示边数。
对五角星类做一下简单的修改,

public partial class RegularPolygonControl : UserControl
{
private double radius = 20; private Brush selectBackground = new SolidColorBrush(Color.FromRgb(0xEB, 0x42, 0x00)); private Brush unselectBackgroud = new SolidColorBrush(Color.FromRgb(0x99, 0x93, 0x93)); /// <summary>
/// 半径
/// </summary>
public double Radius
{
get
{
object result = GetValue(RadiusProperty); if (result == null)
{
return radius;
} return (double)result;
} set
{
SetValue(RadiusProperty, value); this.InvalidateVisual();
}
} public static DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(RegularPolygonControl), new UIPropertyMetadata()); /// <summary>
/// 选中颜色
/// </summary>
public Brush SelectBackground
{
get
{
object result = GetValue(SelectBackgroundProperty); if (result == null)
{
return selectBackground;
} return (Brush)result;
} set
{
SetValue(SelectBackgroundProperty, value); //this.InvalidateVisual();
}
} public static DependencyProperty SelectBackgroundProperty =
DependencyProperty.Register("SelectBackground", typeof(Brush), typeof(RegularPolygonControl), new UIPropertyMetadata()); /// <summary>
/// 未选中颜色
/// </summary>
public Brush UnSelectBackground
{
get
{
object result = GetValue(UnSelectBackgroundProperty); if (result == null)
{
return unselectBackgroud;
} return (Brush)result;
} set
{
SetValue(UnSelectBackgroundProperty, value);
}
} public static DependencyProperty UnSelectBackgroundProperty =
DependencyProperty.Register("UnSelectBackground", typeof(Brush), typeof(RegularPolygonControl), new UIPropertyMetadata()); protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc); Point center = new Point(); PointCollection Points = GetPolygonPoint(center, Radius, 12); Canvas ca = new Canvas(); Polygon plg = new Polygon(); plg.Points = Points; plg.Stroke = Brushes.Transparent; plg.StrokeThickness = 2; plg.Fill = this.SelectBackground; plg.FillRule = FillRule.Nonzero; ca.Children.Add(plg); ca.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; ca.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; this.Content = ca;
} /// <summary>
///根据半径和圆心确定N个点
/// </summary>
/// <param name="center"></param>
/// <returns></returns>
private PointCollection GetPolygonPoint(Point center, double r,int polygonBound)
{
double g = 18; double perangle = 360 / polygonBound; double pi = Math.PI; List<Point> values = new List<Point>(); for (int i = 0; i < (int) polygonBound; i++)
{
Point p2 = new Point(r * Math.Cos((g + 36) * pi / 180), r * Math.Sin((g + 36) * pi / 180)); values.Add(p2); g += perangle;
} PointCollection pcollect = new PointCollection(values); return pcollect;
}
}

效果如下:
   

再做一下简单的变化,可以绘制出一般抽奖用的转盘图形,修改如下,

public class TurnTable : UserControl
{
private double radius = 20; private Brush selectBackground = new SolidColorBrush(Color.FromRgb(0xEB, 0x42, 0x00)); private Brush unselectBackgroud = new SolidColorBrush(Color.FromRgb(0x99, 0x93, 0x93)); /// <summary>
/// 半径
/// </summary>
public double Radius
{
get
{
object result = GetValue(RadiusProperty); if (result == null)
{
return radius;
} return (double)result;
} set
{
SetValue(RadiusProperty, value); this.InvalidateVisual();
}
} public static DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(TurnTable), new UIPropertyMetadata()); /// <summary>
/// 选中颜色
/// </summary>
public Brush SelectBackground
{
get
{
object result = GetValue(SelectBackgroundProperty); if (result == null)
{
return selectBackground;
} return (Brush)result;
} set
{
SetValue(SelectBackgroundProperty, value); //this.InvalidateVisual();
}
} public static DependencyProperty SelectBackgroundProperty =
DependencyProperty.Register("SelectBackground", typeof(Brush), typeof(TurnTable), new UIPropertyMetadata()); protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc); Point center = new Point(); PointCollection Points = GetPolygonPoint(center, Radius, 12); Canvas ca = new Canvas(); Polygon plg = new Polygon(); plg.Points = Points; plg.Stroke = Brushes.Black; plg.StrokeThickness = 2; plg.Fill = this.SelectBackground; plg.FillRule = FillRule.Nonzero; ca.Children.Add(plg); //外接圆
Brush b = new SolidColorBrush(Colors.Yellow); Pen p = new Pen(b, 2); var path = new Path(); double circleRadius = Radius + 10; EllipseGeometry eg = new EllipseGeometry(center, circleRadius, circleRadius); path.Stroke = Brushes.Black; path.StrokeThickness = 1; path.Data = eg; ca.Children.Add(path); ca.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; ca.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; this.Content = ca;
} /// <summary>
///根据半径和圆心确定N个点
/// </summary>
/// <param name="center"></param>
/// <returns></returns>
private PointCollection GetPolygonPoint(Point center, double r, int polygonBound)
{
double g = 18; double perangle = 360 / polygonBound; double pi = Math.PI; List<Point> values = new List<Point>(); for (int i = 0; i < (int)polygonBound; i++)
{
Point p2 = new Point(r * Math.Cos((g + 36) * pi / 180), r * Math.Sin((g + 36) * pi / 180)); values.Add(p2); values.Add(center); g += perangle;
} PointCollection pcollect = new PointCollection(values); return pcollect;
}
}

这样 两个控件一起看一下效果,

<Window x:Class="TestSomeGraphics.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="1200" Height="900" xmlns:my="clr-namespace:TestSomeGraphics"> <Grid>
<my:RegularPolygonControl HorizontalAlignment="Left" x:Name="regularControl1" Radius="200" SelectBackground="Gray"
Margin="235,358,0,-358" />
<my:TurnTable HorizontalAlignment="Left" Radius="100" Margin="639,358,0,0" x:Name="turnTableControl1" VerticalAlignment="Top" />
</Grid> </Window>

如图,

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/6326307

WPF-24:绘制正多边形的更多相关文章

  1. wpf 后台绘制圆弧

    wpf 前台绘制圆弧很简单,如:<Path x:Name="path_data" Stroke="#FFE23838" StrokeThickness=& ...

  2. C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码

    原文:C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码 C#WPF 如何绘制几何图形? 怎么绘制坐标系?绘制sin曲线(正弦曲线)? 这离不开Path(Syst ...

  3. WPF 图形绘制 及各种线帽、箭头的实现

    原文:WPF 图形绘制 及各种线帽.箭头的实现  /// <summary>     /// 矩形类     /// </summary>     public sealed ...

  4. WPF特效-绘制实时2D激光雷达图

    原文:WPF特效-绘制实时2D激光雷达图 接前两篇: https://blog.csdn.net/u013224722/article/details/80738619 https://blog.cs ...

  5. 在WPF中绘制多维数据集

    原文 https://stuff.seans.com/2008/08/13/drawing-a-cube-in-wpf/ 是时候使用WPF绘制一个简单的3D对象了.作为WPF中3D图形的快速介绍,让我 ...

  6. 【C#】第3章补充(一)如何在WPF中绘制正弦曲线

    分类:C#.VS2015 创建日期:2016-06-19 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.要点 本例子提前使用了教材第13章介绍的基本知识. 二 ...

  7. WPF 如何绘制不规则按钮,并且有效点击范围也是不规则的

    最近在做一个东西,如地图,点击地图上的某一区域,这一区域需要填充成其他颜色.区域是不规则的,而且点击该区域的任一点,都能够变色.普通的按钮只是简单的加载一幅图肯定是不行的.查了很多资料,终于把它搞定了 ...

  8. WPF拖动绘制

    using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using ...

  9. wpf GeometryDrawing 绘制文字

    <GeometryDrawing x:Key="GeometryDrawingText"> <GeometryDrawing.Geometry> <R ...

随机推荐

  1. Python字典的操作与使用

    字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...

  2. maven-Android项目环境搭建

    参考:http://blog.csdn.net/earbao/article/details/40741051 android maven环境搭建: 1.Maven的版本要求3.1.1 2.设置AND ...

  3. Windows下模拟Linux开发

    1.背景 Linux环境下开发是大势所趋,也是开发者必须掌握的技能.然windows系统已深入人心,实在不想放弃windows下的成熟应用,因此可以在Windows上模拟一个Linux系统.这样就满足 ...

  4. 信息熵(Entropy)究竟是用来衡量什么的?

    信息熵(Entropy)究竟是用来衡量什么的? ——与Philip ZHANG商榷 思明 Philip ZHANG先生在反驳彭小明的时候,提出一个观点,他说:“ 就语言文 字来说,总体效率不是用民族主 ...

  5. python 间谍程序传输文件 socket编程

    本程序实现了,把目标机器的某个目录(可控)的所有的某种类型文件(可控)全部获取并传到己方的机器上. 1.用了base64的encode(infile,outfile)加密,以及decode(infil ...

  6. C语言的学习-基础知识点

    ---BOOL BOOL BOOL a = YES; printf("%d\n", a); a = NO; printf("%d", a); , b = ; B ...

  7. mp4文件格式之fragment mp4

    目前网络上对mp4文件格式的总结已经相当多了,我就不在这里抄别人总结的东西了,想入门的话百度一下会有许多这方面的文章.这里介绍一下其他文章中很少涉及的一种mp4文件结构,即fragment mp4,也 ...

  8. js 操作剪切板

    1.IE浏览器 window.clipboardData: setData() //设置值 getData()//获取值 clearData()//删除值 /******* ** IE 浏览器下支持w ...

  9. Windows消息机制(转)1

    Windows的应用程序一般包含窗口(Window),它主要为用户提供一种可视化的交互方式,窗口是总是在某个线程(Thread)内创建的.Windows系统通过消息机制来管理交互,消息(Message ...

  10. (三)原生JS实现 - 插件 - 弹出层

    创建遮罩层 _createCover: function() { var newMask = document.createElement("div"); newMask.id = ...