多边形是绘制二维、三维图形的最重要的元素之一。用C#画美国国旗代码如下:

 

 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Drawing.Drawing2D;  
  10. namespace _005多边形  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.             SetStyle(ControlStyles.ResizeRedraw, true);     // 将控件设置为调整控件大小时重绘控件  
  18.             this.Width = 500;// 设置宽度  
  19.             this.Height = 310;  // 设置高度  
  20.         }  
  21.         protected override void OnPaint(PaintEventArgs e)  
  22.         {  
  23.             Graphics g = e.Graphics;    // 实例化Graphics对象  
  24.             g.SmoothingMode = SmoothingMode.AntiAlias;  // 对抗锯齿  
  25.             this.DrawFlag(g,20,20,this.Width - 50);  
  26.             //base.OnPaint(e);  
  27.         }  
  28.         private void DrawStar(Graphics g, float r, float xc, float yc)  
  29.         {   
  30.             // r 决定了星星的大小  
  31.             float sin36 = (float)Math.Sin(36 * Math.PI / 180.0f);  
  32.             float sin72 = (float)Math.Sin(72 * Math.PI / 180.0f);  
  33.             float cos36 = (float)Math.Cos(36 * Math.PI / 180.0f);  
  34.             float cos72 = (float)Math.Cos(72 * Math.PI / 180.0f);  
  35.  
  36.             float rr1 = r * cos72 / cos36;  
  37.  
  38.             // 填充星星  
  39.             PointF[] pts = new PointF[10];  
  40.             pts[0] = new PointF(xc,yc -r);  
  41.             pts[1] = new PointF(xc + r1 * sin36,yc - r1 *cos36);  
  42.             pts[2] = new PointF(xc + r * sin72,yc - r * cos72);  
  43.             pts[3] = new PointF(xc + r1 * sin72,yc + r * cos72);  
  44.             pts[4] = new PointF(xc + r * sin36,yc + r * cos36);  
  45.             pts[5] = new PointF(xc,yc + r1);  
  46.             pts[6] = new PointF(xc - r1 * sin72,yc + r * cos36);  
  47.             pts[7] = new PointF(xc - r1 * sin72,yc + r * cos72);  
  48.             pts[8] = new PointF(xc - r * sin72,yc - r * cos72);  
  49.             pts[9] = new PointF(xc - r1 * sin36,yc - r1 * cos36);  
  50.             g.FillPolygon(Brushes.White,pts);  
  51.         }  
  52.         private void DrawFlag(Graphics g, float x0, float y0, float width)  
  53.         {  
  54.             SolidBrush whiteBrush = new SolidBrush(Color.White);    // 实例化白色画刷  
  55.             SolidBrush blusBrush = new SolidBrush(Color.Blue);      // 实例化蓝色画刷  
  56.             SolidBrush redBrush = new SolidBrush(Color.Red);        // 实例化红色画刷  
  57.  
  58.             float height = 10 * width / 19;  
  59.             // 画白色矩形背景  
  60.             g.FillRectangle(whiteBrush,x0,y0,width,height);  
  61.  
  62.             // 画七条红色条纹  
  63.             for (int i = 0; i < 7; i++)  
  64.             {  
  65.                 g.FillRectangle(redBrush,x0,y0 +2 * i * height / 13,width,height/13);  
  66.             }  
  67.  
  68.             // 画蓝色方框  
  69.             RectangleF blueBox = new RectangleF(x0,y0,2 * width / 5,7 * height / 13);  
  70.             g.FillRectangle(blusBrush,blueBox);  
  71.  
  72.             // 蓝色方框内画50颗星星  
  73.             #region 把蓝色方框分成11 * 9 平方的方块,并且在每一个方块中放一个星星  
  74.             float offset = blueBox.Width / 40;  
  75.             float dx = (blueBox.Width - 2 * offset) / 11;  
  76.             float dy = (blueBox.Height - 2 * offset)/ 9;  
  77.  
  78.             for (int j = 0; j < 9; j++)  
  79.             {  
  80.                 float yc = y0 + offset + j * dy + dy / 2;  
  81.                 for (int i = 0; i < 11; i++)  
  82.                 {  
  83.                     float xc = x0 + offset + i * dx + dx / 2;  
  84.                     if ((i + j) %2 == 0)  
  85.                     {  
  86.                         DrawStar(g,this.Width / 55,xc,yc);                  
  87.                     }  
  88.                 }  
  89.             }  
  90.             #endregion  
  91.             whiteBrush.Dispose();  
  92.             blusBrush.Dispose();  
  93.             redBrush.Dispose();  
  94.         }  
  95.     }  
  96. }