C# 绘图及古诗填字

绘图

绘图的结果如下:

绘图部分主要使用了 Bitmap、Graphics 

具体的函数是 MakeMap

入参说明

string bg : 背景图
Rectangle rect :绘图区域
int row_count :行数
int col_count :列数
string fn :保存到的文件

代码如下:

        public void MakeMap(string bg ,Rectangle rect ,int row_count,int col_count ,string fn)
        {
            Bitmap bmp = new Bitmap(bg);
            Graphics g = Graphics.FromImage(bmp);           

            SHIUtils u = new SHIUtils();
            u.init_data(data);
            
            int line_w = 1;
            int cell_w = (rect.Width - line_w*(col_count+1)) / col_count;
            int cell_h = (rect.Height - line_w * (row_count + 1)) / row_count;

            int w = line_w * (col_count + 1) + cell_w * col_count;
            int h = line_w * (row_count + 1) + cell_h * row_count;
            int x0 = rect.X + (rect.Width - w) / 2;
            int y0 = rect.Y + (rect.Height - h ) / 2;

            Pen pen = new Pen(Color.FromArgb(196, 196, 196));
            for (int col = 0; col <= col_count; col++)
            {
                int x = x0 + (line_w + cell_w) * col;
                g.DrawLine(pen, x, y0, x, y0 + h - line_w);
            }
            for (int row = 0; row <= row_count; row++)
            {
                int y = y0 + (line_w + cell_h) * row;
                g.DrawLine(pen, x0, y, x0 + w - line_w, y);
            }

            // g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            Font font = new Font("汉呈波波浓墨行楷", cell_w*90/100, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Black);
            SolidBrush brush_cell = new SolidBrush(Color.FromArgb(225, 225, 225));
            StringFormat sf = new StringFormat();
            SHIMap map = u.make_map(col_count, row_count);
            for (int col = 0; col < col_count; col++)
                for (int row = 0; row < row_count; row++)
                {
                    MapHole cell= map.map[col, row];
                    if (cell.chr != ' ')
                    {
                        int x = x0 + (line_w + cell_w) * col + line_w;
                        int y = y0 + (line_w + cell_h) * row + line_w;
                        g.FillRectangle(brush_cell, new Rectangle(x , y , cell_w  , cell_h));
                        string s = cell.chr.ToString();
                        if (cell.sentence_list.Count == 2)
                            s = "?";
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        g.DrawString(s, font, brush, new Rectangle(x, y, cell_w, cell_h), sf);
                    } 
                }            

            g.Dispose(); 
            bmp.Save(fn, ImageFormat.Png);
            bmp.Dispose();
        }

 古诗填字

绘图的内容由SHIUtils生成。

SHIMap map = u.make_map(col_count, row_count);

 函数中使用了随机数,每次调用生成的内容都不一样,具体的代码如下:

 public class SHIUtils
    {
        public static Char Char_comma = ',';
        public static Char Char_period = '。';
        public static Char Char_period_1 = '!';
        public static Char Char_period_2 = '?';
        public static Char Char_period_3 = '、';
        public static Char Char_period_4 = ';';
        public static Char Char_period_5 = ':';
        public static Char Char_period_6 = '\r';
        public static Char Char_period_7 = '\n';
        public Random rd = new Random(Guid.NewGuid().GetHashCode());
        public void clear()
        {

        }
        private SHIData _data = null;
        private Dictionary<char, int> _char_dict = new Dictionary<char, int>();
        private Dictionary<int, SHI> _shi_dict = new Dictionary<int, SHI>();
        private Dictionary<int,  Sentence> _sentence_dict= new Dictionary<int, Sentence>();
        private Dictionary<char, Dictionary<int, Sentence>> _sentence_index = new Dictionary<char, Dictionary<int, Sentence>>();
        private Dictionary<char, List<Sentence>> _sentence_index_list = new Dictionary<char, List< Sentence>>();
        private Dictionary<int, Dictionary<int, Sentence>> _sentence_index_len = new Dictionary<int, Dictionary<int, Sentence>>();
        private Dictionary<int, List< Sentence>> _sentence_index_len_list = new Dictionary<int, List<Sentence>>();
        public Dictionary<int, List<Sentence>> get_sentence_index_len_list()
        {
            return _sentence_index_len_list;
        }
        public Dictionary<int, SHI> get_shi_dict()
        {
            return _shi_dict;
        }
        private void do_init()
        {
            clear();
            make_char_dict();
            make_sentence_list();
        }
        private void make_char_dict()
        {
            _char_dict.Clear();
            foreach (SHI i in _data.Items)
            {
                foreach (Char ch in i.contson)
                {
                    if (_char_dict.ContainsKey(ch))
                        continue;
                    _char_dict[ch] = _char_dict.Count;
                }
            }
        }
        private void make_sentence_list()
        {
            _shi_dict.Clear();
            _sentence_dict.Clear();
            _sentence_index.Clear();
            _sentence_index_list.Clear();
            _sentence_index_len.Clear();
            _sentence_index_len_list.Clear();
            foreach (SHI i in _data.Items)
            {
                _shi_dict[i.id] = i;
                Sentence s = null;
                int idx = 0;
                foreach (Char ch in i.contson)
                {
                    if (ch == '\r')
                        continue;
                    if (ch == '\n')
                        continue;
                    if (s == null)
                    {
                        s = new Sentence();
                        s.shi_id = i.id;
                        s.idx = idx;
                        s.id = s.shi_id * 1000 + idx;
                        idx++;
                        _sentence_dict[s.id]=s;
                    }

                    if ((ch == Char_comma) || (ch == Char_period) || (ch == Char_period_1) || (ch == Char_period_2) || (ch == Char_period_3) || (ch == Char_period_4)||(ch==Char_period_5) || (ch == Char_period_6) || (ch == Char_period_7))
                    {
                        s.chr_end = ch;
                        foreach (Char ch_s in s.chrlist)
                        {
                            Dictionary<int, Sentence> ls = null;
                            if (!_sentence_index.TryGetValue(ch_s,out ls))
                            {
                                ls = new Dictionary<int, Sentence>();
                                _sentence_index[ch_s] = ls;
                            }
                            if (! ls.ContainsKey(s.id))
                                ls[s.id] =s;
                        }
                        {
                            Dictionary<int, Sentence> ls = null;
                            if (!_sentence_index_len.TryGetValue(s.chrlist.Count,out ls))
                            {
                                ls = new Dictionary<int, Sentence>();
                                _sentence_index_len[s.chrlist.Count] = ls;                               
                            }
                            if (!ls.ContainsKey(s.id))
                            {
                                ls[s.id] = s;
                            }
                        }
                        s = null;
                    }
                    else
                    {
                        s.chrlist.Add(ch);
                        s.txt = s.txt + ch;
                    }
                }
            }

            foreach(KeyValuePair<int, Dictionary<int, Sentence>> kv in _sentence_index_len)
            {
                List<Sentence> ls = new List<Sentence>();
                _sentence_index_len_list[kv.Key] = ls;
                foreach (KeyValuePair<int, Sentence> kv2 in kv.Value)
                {
                    ls.Add(kv2.Value);
                }
            }
            foreach (KeyValuePair<Char, Dictionary<int, Sentence>> kv in _sentence_index)
            {
                List<Sentence> ls = new List<Sentence>();
                _sentence_index_list[kv.Key] = ls;
                foreach (KeyValuePair<int, Sentence> kv2 in kv.Value)
                {
                    ls.Add(kv2.Value);
                }
            }
        }
        public void init_data(SHIData data)
        {

            _data = data;
            do_init();
        }
        public void load()
        {

        }
        public Sentence get_sentence_rand_by_len(int len)
        {
            List<Sentence> ls = new List<Sentence>();
            if (_sentence_index_len_list.TryGetValue(len, out ls))
            {
                int i = rd.Next(ls.Count);
                return ls[i];
            }
            else
            {
                return null;
            }
        }
        private int fill_map_with_hole_list(SHIMap sm,List<MapHole> hole_list ,int step)
        {
            int c = 0;
            foreach (MapHole hole in hole_list )
            {
                Char ch = hole.chr;
                List<Sentence> ls = null;
               
                if ( _sentence_index_list.TryGetValue(ch,out ls))
                {
                    int idx_0 = rd.Next(ls.Count);
                    for (int i=0;i<ls.Count-1;i++)
                    {
                        int idx = (i + idx_0) % (ls.Count);
                        Sentence s = ls[idx]; 
                        if (s.chrlist.Count < _data.min_s_chr_cnt)
                            continue;
                        if (sm.sentence_list.ContainsKey(s.txt))
                            continue;
                        int pos = s.chrlist.IndexOf(ch);
                        if (pos>=0)
                        {
                            if ((i % 2) == 0)
                            {
                                int x1 = hole.x - pos;
                                int y1 = hole.y;
                                if (sm.can_fill_horizontal(s, x1, y1))
                                {
                                    sm.fill_horizontal(s, x1, y1, step);
                                    c++;
                                }
                            } else
                            {
                                int x1 = hole.x ;
                                int y1 = hole.y - pos;
                                if (sm.can_fill_vertical(s, x1, y1))
                                {
                                    sm.fill_vertical(s, x1, y1, step);
                                    c++;
                                }
                            }
                        }

                    }
                }
            }
            return c;

        }
        private void fill_map(SHIMap sm)
        {            
            Sentence s0 = get_sentence_rand_by_len(7);
            if (s0==null)
                s0 = get_sentence_rand_by_len(5);
            if (s0 == null)
                s0 = get_sentence_rand_by_len(4);
            int x0 = (sm.width - s0.chrlist.Count) / 2;
             int y0 = (sm.height - 2) / 2;
           //   int x0 = 0;
           //  int y0 = 0;

            if (!sm.can_fill_horizontal(s0, x0, y0))
                return;
            sm.fill_horizontal(s0, x0, y0, 1);
            for (int step=2; step < 1000; step++)
            {
                int c = 0;
                for (int i = sm.step_list.Count-1;i>=0;i--)
                {
                    if (i <= sm.step_list[i].step - 2)
                        break;
                    c = c + fill_map_with_hole_list(sm, sm.step_list[i].hole_list, step);
                }
               if (c<=0)
                {
                    break;
                }
            }

        }
        
        public SHIMap make_map(int width, int height)
        {
            SHIMap sm = new SHIMap();
            sm.init_map(width, height);
            fill_map(sm);         
            return sm;
        }
    }
}

例图

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/689288.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

前端三大件速成 05 javascript(1)js组成、引入、基本语法

文章目录 一、js组成二、js的引入三、基本语法1、变量2、基本规范3、关键字4、数据类型&#xff08;1&#xff09;基本数据类型&#xff08;2&#xff09;引用数据类型&#xff08;3&#xff09;数据类型转换&#xff08;4&#xff09;typeof运算符 5、运算符6、流程控制&#…

数据结构与算法笔记:基础篇 - 散列表(下):为什么散列表和链表经常会一起使用?

概述 已经学习了这么多章节了&#xff0c;你有没有发现&#xff0c;两种数据结构&#xff0c;散列表和链表&#xff0c;经常会被放在一起使用。你还记得&#xff0c;前面的章节中都有哪些地方讲到散列表和链表的组合使用吗&#xff1f; 在链表那一节&#xff0c;我讲到如何用…

MAVEN:自定义模板Archetype的创建

目录 一、简介 二、具体步骤 三、 vscode通过模板创建项目 四、通过IDEA创建 一、简介 有时候MAVEN自带的模板库并不能满足我们创建项目的需求&#xff0c;为了能够快速创建项目&#xff0c;免去每次复杂的配置&#xff0c;所以我们需要自定义模板库&#xff0c;本次操作基于…

nss刷题(4)

1、[SWPUCTF 2021 新生赛]easyrce <?php error_reporting(0); highlight_file(__FILE__); if(isset($_GET[url])) { eval($_GET[url]); } ?> if(isset($_GET[url])) isset函数用来检测url变量是否存在&#xff1b;$_GET函数获取变量数据 eval($_GET[url]); eval函数用…

数据挖掘--数据预处理

数据清理 缺失值 如果数据集含有分类属性&#xff0c;一种简单的填补缺失值的方法为&#xff0c;将属于同一类的对象的该属性值的均值赋此缺失值&#xff1b;对于离散属性或定性属性&#xff0c;用众数代替均值。更复杂的方法&#xff0c;可以将其转换为分类问题或数值预测问…

Liunx环境下redis主从集群搭建(保姆级教学)02

Redis在linux下的主从集群配置 本次演示使用三个节点实例一个主节点&#xff0c;两个从节点&#xff1a;7000端口&#xff08;主&#xff09;&#xff0c;7001端口&#xff08;从&#xff09;&#xff0c;7002端口&#xff08;从&#xff09;&#xff1b; 主节点负责写数据&a…

[译文] LLM安全:3.网络LLM攻击及提示注入知识普及(PortSwigger)

这是作者新开的一个专栏&#xff0c;主要翻译国外知名安全厂商的技术报告和安全技术&#xff0c;了解它们的前沿技术&#xff0c;学习它们威胁溯源和恶意代码分析的方法&#xff0c;希望对您有所帮助。当然&#xff0c;由于作者英语有限&#xff0c;会借助LLM进行校验和润色&am…

SpringBoot+Vue幼儿园管理系统(前后端分离)

技术栈 JavaSpringBootMavenMyBatisMySQLVueElement-UI 系统角色 教师用户管理员 功能截图

Plotly : 超好用的Python可视化工具

文章目录 安装&#xff1a;开始你的 Plotly 之旅基本折线图&#xff1a;简单却强大的起点带颜色的散点图&#xff1a;数据的多彩世界三维曲面图&#xff1a;探索数据的深度气泡图&#xff1a;让世界看到你的数据小提琴图&#xff1a;数据分布的优雅展现旭日图&#xff1a;分层数…

立创小tips

立创小tips 原理图中 1-修改图纸属性 保存完&#xff0c;绘制原理图的界面就出现了&#xff0c;然后我们鼠标点击原理图的边缘变成红色就可以高边表格的属性了。 2-鼠标右键可以移动整个原理图 3-查看封装 点击任意一个元器件&#xff0c;在右侧就会显示封装属性&#xff…

[word] word图片环绕方式怎么设置? #经验分享#笔记#媒体

word图片环绕方式怎么设置&#xff1f; 在文档中图片排版是很常见的&#xff0c;在图片排版的过程中我们如何利用小技巧快速处理呢&#xff1f;下面给大家分享word图片环绕方式怎么设置的操作方法&#xff0c;一起来学习下吧&#xff01; 1、修改图片环绕方式 在Word文档中图…

【背包-BM70 兑换零钱(一)】

题目 BM70 兑换零钱(一) 描述 给定数组arr&#xff0c;arr中所有的值都为正整数且不重复。每个值代表一种面值的货币&#xff0c;每种面值的货币可以使用任意张&#xff0c;再给定一个aim&#xff0c;代表要找的钱数&#xff0c;求组成aim的最少货币数。 如果无解&#xff0c;…

python数据分析-心脏瓣膜手术风险分析与预测

研究背景 人的心脏有四个瓣膜&#xff0c;主动脉银、二尖、肺动脉和三尖源 不管是那一个膜发生了病变&#xff0c;都会导致心脏内的血流受到影响&#xff0c;这就是通常所说的心脏期膜病&#xff0c;很多是需要通过手术的方式进行改善的。随着人口老龄化的加剧,&#xff0c;心…

[word] word批注怎么删除 #学习方法#媒体

word批注怎么删除 word批注怎么删除&#xff1f;Word批注主要是用注释和评论文档内容&#xff0c;不管是学习上还是职场上都会用到批注&#xff0c;现在就来教大家快速删除批注的技巧。 1.删除一条批注&#xff1a;选中要删除的批注后&#xff0c;点击【批注】下的删除按钮&a…

277 基于MATLAB GUI火灾检测系统

基于MATLAB GUI火灾检测系统&#xff0c;可以实现图片和视频的火苗检测。火焰识别的三个特征&#xff1a;1个颜色特征&#xff0c;2个几何特征颜色特征&#xff1a;HSV颜色空间下&#xff0c;对三个通道值进行阈值滤波&#xff0c;几何特征1&#xff1a;长宽比&#xff0c;几何…

高考志愿填报选专业,兴趣、擅长、热门就业怎么选?

高考成绩发布后&#xff0c;接下来的重任就是填报志愿&#xff0c;在有限的时间里&#xff0c;选择好学校&#xff0c;选个专业确实不容易。很多人都说填报志愿要从兴趣方面来着手....那么兴趣靠谱吗&#xff1f; 选专业可以根据兴趣吗&#xff1f; 在应试教育的大环境中&…

Java学习-JDBC(一)

JDBC 概念 JDBC(Java Database Connectivity)Java数据库连接JDBC提供了一组独立于任何数据库管理系统的APIJava提供接口规范&#xff0c;由各个数据库厂商提供接口的实现&#xff0c;厂商提供的实现类封装成jar文件&#xff0c;也就是我们俗称的数据库驱动jar包JDBC充分体现了…

AIGC+营销:AI在营销领域的演变与营销人员的新角色

一、AI在营销领域的演变 随着AI技术的不断发展&#xff0c;营销领域也迎来了新的变革。从目前的“AI Copilot”阶段&#xff0c;到未来的“AI Agent”和“AI自主营销团队”阶段&#xff0c;AI的角色将逐渐从辅助人类到独立承担更多职责。 AI Copilot&#xff08;副驾驶&#…

MATLAB算法实战应用案例精讲-【数模应用】因子分析(附MATLAB和python代码实现)

目录 前言 算法原理 SPSS因子分析 操作步骤 结果分析 SPSSAU 因子分析案例 1、背景 2、理论 3、操作 4、SPSSAU输出结果 5、文字分析 6、剖析 疑难解惑 同源方差或共同方法变异偏差,Harman单因子检验? 提示出现奇异矩阵? 因子得分和综合得分? 因子分析计…

19、Go Gin框架集成Swagger

介绍&#xff1a; Swagger 支持在 Gin 路由中使用一系列注释来描述 API 的各个方面。以下是一些常用的 Swagger 注释属性&#xff0c;这些属性可以在 Gin 路由的注释中使用&#xff1a; Summary: 路由的简短摘要。Description: 路由的详细描述。Tags: 用于对路由进行分类的标…