C# 绘图及古诗填字

2024-06-09 01:28

本文主要是介绍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;}}
}

例图

 

 

 

这篇关于C# 绘图及古诗填字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1043873

相关文章

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭