1. 設計一個可讓使用者輸入正多邊形邊數及顏色並繪製出正多邊形的程式

題目可以做好一點嗎,公式給錯是怎樣 = =

namespace Q1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        List<Point> dots = new List<Point>();
        Color? color;

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "B") color = Color.Black;
            if (textBox2.Text == "R") color = Color.Red;
            if (textBox2.Text == "G") color = Color.Green;
            if (textBox2.Text == "L") color = Color.Blue;


            int n = int.Parse(textBox1.Text);
            dots.Clear();
            for (int i = 1; i <= n; i++)
            {
                dots.Add(new Point(
                                  (int)(150.0 * Math.Cos(Math.PI * 2 * i/n) + 175),
                (int)(150.0 * Math.Sin(Math.PI * 2 * i/n) + 175)
                    ));
            }
            panel1.Refresh();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (color == null) return;
            foreach (var item in dots)
            {
                foreach (var item1 in dots)
                {
                    e.Graphics.DrawLine(new Pen(new SolidBrush(color.Value), 2), item, item1);
                }
            }
        }
    }
}

Last updated