• WinForm画网格并填充颜色


    因为研究CodeCombat上的最后一题,自己尝试分解题目,然后想到需要画网格,还有最优化的方法

    源代码如下

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace DrawGrid
    {
        public partial class Form1 : Form
        {
            int multiple = 20;
            int width = 20;
            int height = 16;
            int w = 4;
            int h = 4;
            Pen bluePen = new Pen(Color.Black);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Console.WriteLine("Form长:{0},宽:{1}", this.Width, this.Height);
                this.Multiple();
                DrawGrid(width, height, w, h, e);
               
                List<Rectangle> l = new List<Rectangle>();
                Rectangle r;
                r = new Rectangle(0, w, 2 * w, h); l.Add(r);
                r = new Rectangle(3 * w, 0, 2 * w, h); l.Add(r);
                r = new Rectangle(2 * w, 3 * w, 2 * w, h); l.Add(r);
                foreach (Rectangle r1 in l)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Black), r1);
                }
            }
    
            /// <summary>
            /// 将比例放大
            /// </summary>
            private void Multiple()
            {
                width = width * multiple;
                height = height * multiple;
                w = w * multiple;
                h = h * multiple;
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                Console.WriteLine("X={0},Y={1}", e.X, e.Y);
            }
    
            /// <summary>
            /// 画网格
            /// </summary>
            /// <param name="width"></param>
            /// <param name="height"></param>
            /// <param name="w"></param>
            /// <param name="h"></param>
            private void DrawGrid(int width, int height, int w, int h, PaintEventArgs e)
            {
                Point p1 = new Point();
                Point p2 = new Point();
    
                p1.X = 0; p2.X = width;
                for (int y = 0; y <= height; y = y + h)
                {
                    p1.Y = y; p2.Y = y;
                    DrawLine(p1, p2, e);
                }
                p1.Y = 0; p2.Y = height;
                for (int x = 0; x <= width; x = x + w)
                {
                    p1.X = x; p2.X = x;
                    DrawLine(p1, p2, e);
                }
            }
    
            /// <summary>
            /// 画直线
            /// </summary>
            /// <param name="p1"></param>
            /// <param name="p2"></param>
            private void DrawLine(Point p1, Point p2, PaintEventArgs e)
            {
                e.Graphics.DrawLine(bluePen, p1, p2);
            }
        }
    }

    自己的测试图片如上图,是一个5*4的网格;

    有三个地方已经被填充了。

    剩下空白的地方,需可以用矩形填充。

    考虑使用最少的矩形填充,应该就是4个矩形了。一目了然。

    不过,如果考虑用程序实现的话,就复杂了。以后再尝试

  • 相关阅读:
    python中多进程+协程的使用以及为什么要用它
    python爬虫——多线程+协程(threading+gevent)
    Python几种并发实现方案的性能比较
    Python threadpool传递参数
    python线程池(threadpool)模块使用笔记
    python下的select模块使用 以及epoll与select、poll的区别
    python中的select模块
    Python中threading的join和setDaemon的区别及用法
    python队列Queue
    和为S的连续正数序列
  • 原文地址:https://www.cnblogs.com/chucklu/p/3969927.html
Copyright © 2020-2023  润新知