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

相关文章

AI绘图怎么变现?想做点副业的小白必看!

在科技飞速发展的今天,AI绘图作为一种新兴技术,不仅改变了艺术创作的方式,也为创作者提供了多种变现途径。本文将详细探讨几种常见的AI绘图变现方式,帮助创作者更好地利用这一技术实现经济收益。 更多实操教程和AI绘画工具,可以扫描下方,免费获取 定制服务:个性化的创意商机 个性化定制 AI绘图技术能够根据用户需求生成个性化的头像、壁纸、插画等作品。例如,姓氏头像在电商平台上非常受欢迎,

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

C# 防止按钮botton重复“点击”的方法

在使用C#的按钮控件的时候,经常我们想如果出现了多次点击的时候只让其在执行的时候只响应一次。这个时候很多人可能会想到使用Enable=false, 但是实际情况是还是会被多次触发,因为C#采用的是消息队列机制,这个时候我们只需要在Enable = true 之前加一句 Application.DoEvents();就能达到防止重复点击的问题。 private void btnGenerateSh

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

C# Hash算法之MD5、SHA

MD5我们用的还是比较多的,一般用来加密存储密码。但是现在很多人觉MD5可能不太安全了,所以都用上了SHA256等来做加密(虽然我觉得都差不多,MD5还是能玩)。 还是跟上一篇说的一样,当一个算法的复杂度提高的同时肯定会带来效率的降低,所以SHA和MD5比较起来的话,SHA更安全,MD5更高效。 由于HASH算法的不可逆性,所以我认为MD5和SHA主要还是应用在字符串的"加密"上。 由于