计算顶部三角形坐标方法:

         /// <summary>
/// 获取顶三角形坐标
/// </summary>
/// <param name="r">外接圆半径(顶点到中心的距离)</param>
/// <param name="n">N角星</param>
/// <param name="x1">左横坐标</param>
/// <param name="y1">纵坐标</param>
/// <param name="x2">又横坐标</param>
private void GetCoordinate(double r, int n, out double x1, out double y1, out double x2)
{
double unitAngle = ;
if(n < )
{
//奇数角星锐角30,偶数角星锐角和为45
unitAngle = n % == ? : ;
}
else
{
//奇数角星锐角和为180,偶数角星锐角和为360
unitAngle = n % == ? / n : / n;
}
double l = Math.PI / ; //弧度单位
double a = Math.Sin( / ( * n) * l), //多角芒星各角连中心分割所得的夹角的一半
b = Math.Sin(unitAngle/ * l), //芒星内角锐角的一半
c = Math.Sin(( - / ( * n) - unitAngle/) * l), //芒星除了内锐角的其他内角与中心点连线的夹角
d = Math.Cos(( / ( * n)) * l);
x1 = (a * r * b) / c; //正弦定理
y1 = (d * r * b) / c;
x2 = r - x1; //x2与x1关于中心点垂线对称,右移r个长度
x1 += r; //右移r个长度
y1 -= r; //下移r个长度
//取正数
x1 = x1 < ? x1 * (-) : x1;
y1 = y1 < ? y1 * (-) : y1;
x2 = x2 < ? x2 * (-) : x2;
}

1、画空心正N角星

思路:通过计算得到顶部一个三角形的坐标,画出三角形,再画出三角形顺时针旋转一周的其他N-1个三角形。组合起来得到一个伪的多角芒星。

         /// <summary>
/// 画多角芒星,正多角星
/// </summary>
/// <param name="r">外接圆半径</param>
/// <param name="n">角数量</param>
/// <returns>返回包含正多角星的Canvas</returns>
private Canvas DrawingPentacle(double r, int n)
{
double x1, x2, y1;
GetCoordinate(r, n, out x1, out y1, out x2);
Canvas canvas = new Canvas() { Width = , Height = };
//重复N次画出N个三角形斜边
for (int i = ; i <= n; i++)
{
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.BlueViolet, ), Geometry.Parse(string.Format("M {0},0 L{1},{2} M 50,0 L{3},{2}", r, x1, y1, x2)));
}
//顺时针旋转
dv.Transform = new RotateTransform(i * / n, , ); //作为图片资源放到图片控件中
RenderTargetBitmap rtb = new RenderTargetBitmap(, , , , PixelFormats.Default);
rtb.Render(dv);
Image image = new Image() { Source = rtb };
canvas.Children.Add(image);
}
return canvas;
}
04-14 16:56