C# 画图及古诗填字

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

画图

画图的效果如下:

画图部分主要使用了 Bitmap、Graphics 
详细的函数是 MakeMap
入参说明
string bg : 背景图
Rectangle rect :画图区域
int row_count :行数
int col_count :列数
string fn :保存到的文件
代码如下:
  1.         public void MakeMap(string bg ,Rectangle rect ,int row_count,int col_count ,string fn)
  2.         {
  3.             Bitmap bmp = new Bitmap(bg);
  4.             Graphics g = Graphics.FromImage(bmp);           
  5.             SHIUtils u = new SHIUtils();
  6.             u.init_data(data);
  7.             
  8.             int line_w = 1;
  9.             int cell_w = (rect.Width - line_w*(col_count+1)) / col_count;
  10.             int cell_h = (rect.Height - line_w * (row_count + 1)) / row_count;
  11.             int w = line_w * (col_count + 1) + cell_w * col_count;
  12.             int h = line_w * (row_count + 1) + cell_h * row_count;
  13.             int x0 = rect.X + (rect.Width - w) / 2;
  14.             int y0 = rect.Y + (rect.Height - h ) / 2;
  15.             Pen pen = new Pen(Color.FromArgb(196, 196, 196));
  16.             for (int col = 0; col <= col_count; col++)
  17.             {
  18.                 int x = x0 + (line_w + cell_w) * col;
  19.                 g.DrawLine(pen, x, y0, x, y0 + h - line_w);
  20.             }
  21.             for (int row = 0; row <= row_count; row++)
  22.             {
  23.                 int y = y0 + (line_w + cell_h) * row;
  24.                 g.DrawLine(pen, x0, y, x0 + w - line_w, y);
  25.             }
  26.             // g.SmoothingMode = SmoothingMode.HighQuality;
  27.             g.TextRenderingHint = TextRenderingHint.AntiAlias;
  28.             Font font = new Font("汉呈波波浓墨行楷", cell_w*90/100, FontStyle.Regular);
  29.             SolidBrush brush = new SolidBrush(Color.Black);
  30.             SolidBrush brush_cell = new SolidBrush(Color.FromArgb(225, 225, 225));
  31.             StringFormat sf = new StringFormat();
  32.             SHIMap map = u.make_map(col_count, row_count);
  33.             for (int col = 0; col < col_count; col++)
  34.                 for (int row = 0; row < row_count; row++)
  35.                 {
  36.                     MapHole cell= map.map[col, row];
  37.                     if (cell.chr != ' ')
  38.                     {
  39.                         int x = x0 + (line_w + cell_w) * col + line_w;
  40.                         int y = y0 + (line_w + cell_h) * row + line_w;
  41.                         g.FillRectangle(brush_cell, new Rectangle(x , y , cell_w  , cell_h));
  42.                         string s = cell.chr.ToString();
  43.                         if (cell.sentence_list.Count == 2)
  44.                             s = "?";
  45.                         sf.Alignment = StringAlignment.Center;
  46.                         sf.LineAlignment = StringAlignment.Center;
  47.                         g.DrawString(s, font, brush, new Rectangle(x, y, cell_w, cell_h), sf);
  48.                     }
  49.                 }            
  50.             g.Dispose();
  51.             bmp.Save(fn, ImageFormat.Png);
  52.             bmp.Dispose();
  53.         }
复制代码
 古诗填字

画图的内容由SHIUtils生成。
SHIMap map = u.make_map(col_count, row_count);
 函数中使用了随机数,每次调用生成的内容都不一样,详细的代码如下:
  1. public class SHIUtils
  2.     {
  3.         public static Char Char_comma = ',';
  4.         public static Char Char_period = '。';
  5.         public static Char Char_period_1 = '!';
  6.         public static Char Char_period_2 = '?';
  7.         public static Char Char_period_3 = '、';
  8.         public static Char Char_period_4 = ';';
  9.         public static Char Char_period_5 = ':';
  10.         public static Char Char_period_6 = '\r';
  11.         public static Char Char_period_7 = '\n';
  12.         public Random rd = new Random(Guid.NewGuid().GetHashCode());
  13.         public void clear()
  14.         {
  15.         }
  16.         private SHIData _data = null;
  17.         private Dictionary<char, int> _char_dict = new Dictionary<char, int>();
  18.         private Dictionary<int, SHI> _shi_dict = new Dictionary<int, SHI>();
  19.         private Dictionary<int,  Sentence> _sentence_dict= new Dictionary<int, Sentence>();
  20.         private Dictionary<char, Dictionary<int, Sentence>> _sentence_index = new Dictionary<char, Dictionary<int, Sentence>>();
  21.         private Dictionary<char, List<Sentence>> _sentence_index_list = new Dictionary<char, List< Sentence>>();
  22.         private Dictionary<int, Dictionary<int, Sentence>> _sentence_index_len = new Dictionary<int, Dictionary<int, Sentence>>();
  23.         private Dictionary<int, List< Sentence>> _sentence_index_len_list = new Dictionary<int, List<Sentence>>();
  24.         public Dictionary<int, List<Sentence>> get_sentence_index_len_list()
  25.         {
  26.             return _sentence_index_len_list;
  27.         }
  28.         public Dictionary<int, SHI> get_shi_dict()
  29.         {
  30.             return _shi_dict;
  31.         }
  32.         private void do_init()
  33.         {
  34.             clear();
  35.             make_char_dict();
  36.             make_sentence_list();
  37.         }
  38.         private void make_char_dict()
  39.         {
  40.             _char_dict.Clear();
  41.             foreach (SHI i in _data.Items)
  42.             {
  43.                 foreach (Char ch in i.contson)
  44.                 {
  45.                     if (_char_dict.ContainsKey(ch))
  46.                         continue;
  47.                     _char_dict[ch] = _char_dict.Count;
  48.                 }
  49.             }
  50.         }
  51.         private void make_sentence_list()
  52.         {
  53.             _shi_dict.Clear();
  54.             _sentence_dict.Clear();
  55.             _sentence_index.Clear();
  56.             _sentence_index_list.Clear();
  57.             _sentence_index_len.Clear();
  58.             _sentence_index_len_list.Clear();
  59.             foreach (SHI i in _data.Items)
  60.             {
  61.                 _shi_dict[i.id] = i;
  62.                 Sentence s = null;
  63.                 int idx = 0;
  64.                 foreach (Char ch in i.contson)
  65.                 {
  66.                     if (ch == '\r')
  67.                         continue;
  68.                     if (ch == '\n')
  69.                         continue;
  70.                     if (s == null)
  71.                     {
  72.                         s = new Sentence();
  73.                         s.shi_id = i.id;
  74.                         s.idx = idx;
  75.                         s.id = s.shi_id * 1000 + idx;
  76.                         idx++;
  77.                         _sentence_dict[s.id]=s;
  78.                     }
  79.                     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))
  80.                     {
  81.                         s.chr_end = ch;
  82.                         foreach (Char ch_s in s.chrlist)
  83.                         {
  84.                             Dictionary<int, Sentence> ls = null;
  85.                             if (!_sentence_index.TryGetValue(ch_s,out ls))
  86.                             {
  87.                                 ls = new Dictionary<int, Sentence>();
  88.                                 _sentence_index[ch_s] = ls;
  89.                             }
  90.                             if (! ls.ContainsKey(s.id))
  91.                                 ls[s.id] =s;
  92.                         }
  93.                         {
  94.                             Dictionary<int, Sentence> ls = null;
  95.                             if (!_sentence_index_len.TryGetValue(s.chrlist.Count,out ls))
  96.                             {
  97.                                 ls = new Dictionary<int, Sentence>();
  98.                                 _sentence_index_len[s.chrlist.Count] = ls;                              
  99.                             }
  100.                             if (!ls.ContainsKey(s.id))
  101.                             {
  102.                                 ls[s.id] = s;
  103.                             }
  104.                         }
  105.                         s = null;
  106.                     }
  107.                     else
  108.                     {
  109.                         s.chrlist.Add(ch);
  110.                         s.txt = s.txt + ch;
  111.                     }
  112.                 }
  113.             }
  114.             foreach(KeyValuePair<int, Dictionary<int, Sentence>> kv in _sentence_index_len)
  115.             {
  116.                 List<Sentence> ls = new List<Sentence>();
  117.                 _sentence_index_len_list[kv.Key] = ls;
  118.                 foreach (KeyValuePair<int, Sentence> kv2 in kv.Value)
  119.                 {
  120.                     ls.Add(kv2.Value);
  121.                 }
  122.             }
  123.             foreach (KeyValuePair<Char, Dictionary<int, Sentence>> kv in _sentence_index)
  124.             {
  125.                 List<Sentence> ls = new List<Sentence>();
  126.                 _sentence_index_list[kv.Key] = ls;
  127.                 foreach (KeyValuePair<int, Sentence> kv2 in kv.Value)
  128.                 {
  129.                     ls.Add(kv2.Value);
  130.                 }
  131.             }
  132.         }
  133.         public void init_data(SHIData data)
  134.         {
  135.             _data = data;
  136.             do_init();
  137.         }
  138.         public void load()
  139.         {
  140.         }
  141.         public Sentence get_sentence_rand_by_len(int len)
  142.         {
  143.             List<Sentence> ls = new List<Sentence>();
  144.             if (_sentence_index_len_list.TryGetValue(len, out ls))
  145.             {
  146.                 int i = rd.Next(ls.Count);
  147.                 return ls[i];
  148.             }
  149.             else
  150.             {
  151.                 return null;
  152.             }
  153.         }
  154.         private int fill_map_with_hole_list(SHIMap sm,List<MapHole> hole_list ,int step)
  155.         {
  156.             int c = 0;
  157.             foreach (MapHole hole in hole_list )
  158.             {
  159.                 Char ch = hole.chr;
  160.                 List<Sentence> ls = null;
  161.                
  162.                 if ( _sentence_index_list.TryGetValue(ch,out ls))
  163.                 {
  164.                     int idx_0 = rd.Next(ls.Count);
  165.                     for (int i=0;i<ls.Count-1;i++)
  166.                     {
  167.                         int idx = (i + idx_0) % (ls.Count);
  168.                         Sentence s = ls[idx];
  169.                         if (s.chrlist.Count < _data.min_s_chr_cnt)
  170.                             continue;
  171.                         if (sm.sentence_list.ContainsKey(s.txt))
  172.                             continue;
  173.                         int pos = s.chrlist.IndexOf(ch);
  174.                         if (pos>=0)
  175.                         {
  176.                             if ((i % 2) == 0)
  177.                             {
  178.                                 int x1 = hole.x - pos;
  179.                                 int y1 = hole.y;
  180.                                 if (sm.can_fill_horizontal(s, x1, y1))
  181.                                 {
  182.                                     sm.fill_horizontal(s, x1, y1, step);
  183.                                     c++;
  184.                                 }
  185.                             } else
  186.                             {
  187.                                 int x1 = hole.x ;
  188.                                 int y1 = hole.y - pos;
  189.                                 if (sm.can_fill_vertical(s, x1, y1))
  190.                                 {
  191.                                     sm.fill_vertical(s, x1, y1, step);
  192.                                     c++;
  193.                                 }
  194.                             }
  195.                         }
  196.                     }
  197.                 }
  198.             }
  199.             return c;
  200.         }
  201.         private void fill_map(SHIMap sm)
  202.         {            
  203.             Sentence s0 = get_sentence_rand_by_len(7);
  204.             if (s0==null)
  205.                 s0 = get_sentence_rand_by_len(5);
  206.             if (s0 == null)
  207.                 s0 = get_sentence_rand_by_len(4);
  208.             int x0 = (sm.width - s0.chrlist.Count) / 2;
  209.              int y0 = (sm.height - 2) / 2;
  210.            //   int x0 = 0;
  211.            //  int y0 = 0;
  212.             if (!sm.can_fill_horizontal(s0, x0, y0))
  213.                 return;
  214.             sm.fill_horizontal(s0, x0, y0, 1);
  215.             for (int step=2; step < 1000; step++)
  216.             {
  217.                 int c = 0;
  218.                 for (int i = sm.step_list.Count-1;i>=0;i--)
  219.                 {
  220.                     if (i <= sm.step_list[i].step - 2)
  221.                         break;
  222.                     c = c + fill_map_with_hole_list(sm, sm.step_list[i].hole_list, step);
  223.                 }
  224.                if (c<=0)
  225.                 {
  226.                     break;
  227.                 }
  228.             }
  229.         }
  230.         
  231.         public SHIMap make_map(int width, int height)
  232.         {
  233.             SHIMap sm = new SHIMap();
  234.             sm.init_map(width, height);
  235.             fill_map(sm);         
  236.             return sm;
  237.         }
  238.     }
  239. }
复制代码
例图



 

 

 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表