1. 水平主桿樹連線系統

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

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        List<Tuple<int, int>> nodes = new List<Tuple<int, int>>();
        bool wtf = false;
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            Pen pen = new Pen(new SolidBrush(Color.Black));
            pen.Width = 2;
            for (int x = 1; x <= 8; x++)
            {
                e.Graphics.DrawLine(pen, x * 40, 0, x * 40, 400);
                e.Graphics.DrawLine(pen, 0, x * 40, 400, x * 40);
            }

            int s = 0;
            foreach (var item in nodes)
            {
                s += item.Item2;
            }
            if (wtf)
            {
                int x1 = nodes[0].Item1;
                int x2 = nodes[6].Item1;
                int y = s / 7;
                Pen pen2 = new Pen(new SolidBrush(Color.Red));
                pen2.Width = 4;
                int len = 0;
                e.Graphics.DrawLine(pen2, x1 * 40, y * 40, x2 * 40, y * 40);
                foreach (var item in nodes)
                {
                    e.Graphics.DrawLine(pen2, item.Item1 * 40, item.Item2 * 40, item.Item1 * 40, y * 40);
                    len += Math.Abs(item.Item2 - y);
                }
                len += x2 - x1;
                a.Text = len.ToString();
            }
            foreach (var item in nodes)
            { // fill...
                e.Graphics.FillEllipse(Brushes.Black, 40 * item.Item1 - 5, 40 * item.Item2 - 5, 10, 10);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            nodes.Clear();
            wtf = false;
            var rd = new Random();
            var start = 1;
            var end = 7;
            if (1 == rd.Next(2))
            {
                start++;
                end++;
            }
            var ey = new HashSet<int>();
            for (int i = start; i <= end; i++)
            {
                int y;
                do { y = rd.Next(1, 9); } while (ey.Contains(y));
                ey.Add(y);
                nodes.Add(new Tuple<int, int>(i, y));
            }
            panel1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            wtf = true;
            panel1.Refresh();
        }
    }
}

Last updated