当前位置: 首页 > news >正文

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;}}
}

例图

 

 

 

相关文章:

  • Android基础-进程间通信
  • 熟悉的软件架构风格及详细介绍
  • 自动驾驶人工智能
  • 【深度学习】之 卷积(Convolution2D)、最大池化(Max Pooling)和 Dropout 的NumPy实现
  • arm系统中双网卡共存问题
  • 区块链共识机制技术一--POW(工作量证明)共识机制
  • Transformer论文精读
  • App UI 风格,引领时尚
  • 无头+单向+非循环链表的实现
  • web学习笔记(六十五)
  • Recognize Anything: A Strong Image Tagging Model(RAM模型使用方法)
  • 各品牌电视安装第三方软件失败的解决方法
  • 理解数仓建模
  • 移动安全赋能化工能源行业智慧转型
  • 软件2_算法功能23
  • [deviceone开发]-do_Webview的基本示例
  • angular2 简述
  • ComponentOne 2017 V2版本正式发布
  • CSS 提示工具(Tooltip)
  • Docker入门(二) - Dockerfile
  • Java 内存分配及垃圾回收机制初探
  • Javascripit类型转换比较那点事儿,双等号(==)
  • Javascript编码规范
  • Js基础——数据类型之Null和Undefined
  • js学习笔记
  • Nacos系列:Nacos的Java SDK使用
  • Python实现BT种子转化为磁力链接【实战】
  • rabbitmq延迟消息示例
  • Service Worker
  • SQL 难点解决:记录的引用
  • Sublime Text 2/3 绑定Eclipse快捷键
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 关于字符编码你应该知道的事情
  • 记一次删除Git记录中的大文件的过程
  • 简析gRPC client 连接管理
  • 解析 Webpack中import、require、按需加载的执行过程
  • 七牛云假注销小指南
  • 手写双向链表LinkedList的几个常用功能
  • 云大使推广中的常见热门问题
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 06-01 点餐小程序前台界面搭建
  • Nginx实现动静分离
  • ​io --- 处理流的核心工具​
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # linux 中使用 visudo 命令,怎么保存退出?
  • #stm32整理(一)flash读写
  • (23)Linux的软硬连接
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (七)Knockout 创建自定义绑定
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (一)SpringBoot3---尚硅谷总结
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .java 9 找不到符号_java找不到符号