using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace HotelPms.Share.Windows.GraphicsApi
{
public class Star
{
///
/// 画五角星
///
/// 多少个角
/// Graphics参数
public static void Draw(int pointed, Graphics g, PointF center, int len1 = 100, int len2 = 50)
{
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
//g.SmoothingMode = SmoothingMode.HighQuality;//对图片进行平滑处理
g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置图像呈现的质量
g.CompositingQuality = CompositingQuality.HighQuality;
Pen p = new Pen(Color.Red);//画笔的颜色
double[] angles1 = GetAngles(-Math.PI / 2, pointed);//五角星外围的点角度的一个数组
double[] angles2 = GetAngles(-Math.PI / 2 + Math.PI / pointed, pointed);//五角星内围的点角度的一个数组
PointF[] points1 = GetPoints(center, len1, angles1);//五角星外围的点的一个数组
PointF[] points2 = GetPoints(center, len2, angles2);//五角星内围的点的一个数组
PointF[] points = new PointF[points1.Length + points2.Length];//最终合成多边形所有点的数组
for (int i = 0, j = 0; i < points.Length; i += 2, j++)
{
points[i] = points1[j];
points[i + 1] = points2[j];
}
g.DrawPolygon(p, points);//画一个多边形
g.FillPolygon(Brushes.Gold, points);//填充颜色
}
///
/// 获得所有角度的数组
///
/// 开始的角度
/// 个数
///
public static double[] GetAngles(double startAngle, int pointed)
{
double[] angles = new double[pointed];
angles[0] = startAngle;
for (int i = 1; i < angles.Length; i++)
{
angles[i] = angles[i - 1] + GetAngleLength(pointed);
}
return angles;
}
///
/// 获得角度的增量
///
///
///
public static double GetAngleLength(int pointed)
{
return 2 * Math.PI / pointed;
}
///
/// 获得五角星的各个点
///
/// 中心点坐标
/// 距离中心点的长度
/// 和水平方向的夹角
///
public static PointF GetPoint(PointF ptCenter, double length, double angle)
{
return new PointF((float)(ptCenter.X + length * Math.Cos(angle)), (float)(ptCenter.Y + length * Math.Sin(angle)));
}
///
/// 返回一个坐标的数组
///
/// 中心点
/// 距离中心点的长度
/// 两点之间的夹角
///
public static PointF[] GetPoints(PointF ptCenter, double length, params double[] angles)
{
PointF[] points = new PointF[angles.Length];
for (int i = 0; i < points.Length; i++)
{
points[i] = GetPoint(ptCenter, length, angles[i]);
}
return points;
}
}
}