TernaryDiagram.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. 
  2. using OTSCommon.DBOperate.Model;
  3. using OTSIncAReportApp.DataOperation.DataAccess;
  4. using OTSIncAReportApp.OTSRstMgrFunction;
  5. using OTSIncAReportApp.OTSSampleReportInfo;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Drawing2D;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using static OTSIncAReportApp.OTSReport_Export;
  16. namespace OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration
  17. {
  18. /// <summary>
  19. /// 三元图
  20. /// </summary>
  21. class TernaryDiagram
  22. {
  23. /// <summary>
  24. /// 三元图列表
  25. /// </summary>
  26. List<string> TemplateList;
  27. frmReportConditionChoose m_condition;
  28. public int condition = -1;
  29. //绘制图例
  30. public Bitmap DrawATernaryDiagramLegend(List<Color> Color_list, OTSReport_Export m_otsreport_export, string a_PartSizeFile)
  31. {
  32. Bitmap map = new Bitmap(260, 115);
  33. Graphics g = Graphics.FromImage(map);
  34. SolidBrush sbrush_White = new SolidBrush(Color.White);
  35. SolidBrush sbrush = new SolidBrush(Color.Black);
  36. g.FillRectangle(sbrush_White, 0, 0, 260, 115);
  37. Font myFont = new Font("Arial", 10, FontStyle.Regular);
  38. Font myFont2 = new Font("Arial", 10, FontStyle.Regular);
  39. g.DrawString("(length,μm)", myFont, sbrush, 88, 3);
  40. //legend(length,microns)
  41. Pen mypen = new Pen(Color.Black, 1);
  42. g.DrawLine(mypen, 70, 19, 170, 19);
  43. //设置标签名称
  44. List<string> listName = new List<string>();
  45. //获取粒级表
  46. string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + a_PartSizeFile;
  47. DataSet ds = XMLoperate.GetXml(pathe);
  48. string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
  49. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  50. {
  51. if (sizestr.Split(',')[i].Length > 0)
  52. {
  53. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  54. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  55. listName.Add(d1.ToString() + "~" + d2.ToString());
  56. }
  57. }
  58. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  59. listName.Add(d.ToString() + "~MAX");
  60. int PositionJeight = 27;
  61. //string str_unit = " μm";
  62. string str_unit = "";
  63. for (int i = 0; i < listName.Count; i = i + 3)
  64. {
  65. if (i < listName.Count)
  66. {
  67. SolidBrush sbrush_Color = new SolidBrush(Color_list[i]);
  68. g.DrawRectangle(mypen, 0, PositionJeight, 8, 8);
  69. g.FillRectangle(sbrush_Color, 1, PositionJeight + 1, 7, 7);
  70. g.DrawString(listName[i].ToString() + str_unit, myFont2, sbrush, 12, PositionJeight - 2);
  71. }
  72. if (i + 1 < listName.Count)
  73. {
  74. SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 1]);
  75. g.DrawRectangle(mypen, 94, PositionJeight, 8, 8);
  76. g.FillRectangle(sbrush_Color, 95, PositionJeight + 1, 7, 7);
  77. g.DrawString(listName[i + 1].ToString() + str_unit, myFont2, sbrush, 106, PositionJeight - 2);
  78. }
  79. if (i + 2 < listName.Count)
  80. {
  81. SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 2]);
  82. g.DrawRectangle(mypen, 188, PositionJeight, 8, 8);
  83. g.FillRectangle(sbrush_Color, 189, PositionJeight + 1, 7, 7);
  84. g.DrawString(listName[i + 2].ToString() + str_unit, myFont2, sbrush, 199, PositionJeight - 2);
  85. }
  86. PositionJeight = PositionJeight + 15;
  87. }
  88. return map;
  89. }
  90. public Bitmap DrawATernaryPicture(int selectindex, c_TemplateClass m_mbszclass, List<Color> Color_list, OTSReport_Export m_otsreport_export, string a_PartSizeFile)
  91. {
  92. initialization(m_otsreport_export);
  93. ResultFile resultFile = m_otsreport_export.m_ReportApp.m_rstDataMgr.ResultFilesList[m_otsreport_export.m_ReportApp.m_rstDataMgr.GetWorkingResultId()];
  94. string template = TemplateList[selectindex];
  95. if (template == "")
  96. { //三元相图模板
  97. template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
  98. }
  99. //获取粒级表
  100. string pathtpfs = Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
  101. List<string> nameList = new List<string>();
  102. DataSet ds2 = XMLoperate.GetXmlData(pathtpfs, "XMLData");
  103. DataTable dt = ds2.Tables["Member"];
  104. string TemplateName = "";
  105. //遍历第一层节点
  106. foreach (DataRow element in dt.Rows)
  107. {
  108. TemplateName = element["TemplateName"].ToString();
  109. if (TemplateName == template)
  110. {
  111. string Element = element["Element"].ToString();
  112. nameList.Add(Element.Split('.')[0]);
  113. nameList.Add(Element.Split('.')[1]);
  114. nameList.Add(Element.Split('.')[2]);
  115. break;
  116. }
  117. }
  118. DataTable dt_point_sort = new DataTable();
  119. dt_point_sort.Columns.Add("X", typeof(double));
  120. dt_point_sort.Columns.Add("Y", typeof(double));
  121. dt_point_sort.Columns.Add("Color_position");
  122. DataTable particles = GetParticles(resultFile.FilePath, nameList, 0, m_otsreport_export, m_mbszclass);
  123. foreach (DataRow item in particles.Rows)
  124. {
  125. if (item["particleLocation"].ToString() == "0,0,0")
  126. {
  127. continue;
  128. }
  129. double top = Convert.ToDouble(item["top"]);
  130. double left = Convert.ToDouble(item["left"]);
  131. double right = Convert.ToDouble(item["right"]);
  132. //x=right+top/2,y=(√3/2)*top
  133. double Y = 0.866 * top * 500;
  134. double X = (right + top / 2) * 500;
  135. double[] point_ = { Math.Round(X, 2), Math.Round(Y) };
  136. DataRow dr = dt_point_sort.NewRow();
  137. dr["X"] = Math.Round(X, 2);
  138. dr["Y"] = Math.Round(Y);
  139. dr["Color_position"] = item["Color_position"];
  140. dt_point_sort.Rows.Add(dr);
  141. }
  142. #region 图形
  143. int Line = 500;
  144. Bitmap map = new Bitmap(530, 530);
  145. Graphics g = Graphics.FromImage(map);
  146. SolidBrush sbrush_White = new SolidBrush(Color.White);
  147. g.FillRectangle(sbrush_White, 0, 0, 530, 530);
  148. Pen mypen = new Pen(Color.Black, 1);
  149. g.DrawImage(map, 0, 0, map.Width, map.Height);
  150. g.DrawLine(mypen, new Point(Line + 10, Line), new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))));//"\"
  151. g.DrawLine(mypen, new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))), new Point(0 + 10, Line));//"/"
  152. g.DrawLine(mypen, new Point(0 + 10, Line), new Point(Line + 10, Line));//"_"
  153. mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  154. mypen.DashPattern = new float[] { 2, 2 };
  155. mypen = new Pen(Color.FromArgb(212, 212, 212), 1);
  156. int point = Line / 10;
  157. for (int i = 1; i < 10; i++)
  158. {
  159. //"\"
  160. int x1_a = point / 2 + ((point / 2) * (i - 1));
  161. int x2_a = Line - (43 * i);
  162. int y1_a = point * i;
  163. g.DrawLine(mypen, new Point(x1_a + 10, x2_a), new Point(y1_a + 10, Line));
  164. //"/"
  165. int x1_b = Line - (point / 2 + ((point / 2) * (i - 1)));
  166. int x2_b = Line - (43 * i);
  167. int y1_b = Line - (point * i);
  168. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(y1_b + 10, Line));
  169. //"_"
  170. int x1_c = point / 2 + ((point / 2) * (i - 1));
  171. int x2_c = Line - (43 * i);
  172. int y1_c = Line - (point / 2 + ((point / 2) * (i - 1)));
  173. int y2_c = Line - (43 * i);
  174. g.DrawLine(mypen, new Point(x1_c + 10, x2_c), new Point(y1_c + 10, y2_c));
  175. g.DrawLine(mypen, new Point(y1_a + 10, Line + 10), new Point(y1_a + 10, Line));
  176. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(x1_b + 10 + 10, x2_b));
  177. g.DrawLine(mypen, new Point(x1_c + 10 - 10, x2_c), new Point(y1_c + 10, y2_c));
  178. g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_c - 20, x2_c - 5));
  179. g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(y1_b, Line + 8));
  180. g.DrawString(((10 - i) * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_b + 20, x2_b - 5));
  181. }
  182. for (int i = 0; i < dt_point_sort.Rows.Count; i++)
  183. {
  184. mypen = new Pen(Color.Black, 1);
  185. SolidBrush mysbrush = new SolidBrush(Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])]);
  186. g.FillRectangle(mysbrush, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 8, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 4, 5);
  187. g.DrawRectangle(mypen, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 7, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 5, 5);
  188. }
  189. Font myFont = new Font("Arial Unicode MS", 13, FontStyle.Bold);
  190. Font Font_features = new Font("Arial Unicode MS", 10, FontStyle.Bold);
  191. SolidBrush sbrush = new SolidBrush(Color.Black);
  192. g.DrawString("features:" + dt_point_sort.Rows.Count.ToString(), Font_features, sbrush, 400, 200);
  193. g.DrawString(TemplateName, myFont, sbrush, 265 - (TemplateName.Length * 10 / 2), 10);
  194. g.DrawString(TemplateName.Split('.')[0].ToString(), myFont, sbrush, 265 - (TemplateName.Split('.')[0].ToString().Length * 10 / 2), 45);//顶点名字
  195. g.DrawString(TemplateName.Split('.')[1].ToString(), myFont, sbrush, 0, 510);//左侧名字
  196. g.DrawString(TemplateName.Split('.')[2].ToString(), myFont, sbrush, 500 - (TemplateName.Split('.')[0].ToString().Length * 10), 510);//左侧名字
  197. #endregion
  198. return map;
  199. }
  200. private void initialization(OTSReport_Export m_otsreport_export)
  201. {
  202. //加载三元相图各项
  203. //string pathtpf = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TrigTemplateFileFolder + /*"\\" +*/ a_PartSizeFile;// Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
  204. string pathtpf = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TrigTemplateFileFolder + "\\" + m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TriTempFile;
  205. TemplateList = new List<string>();
  206. DataSet ds = DataOperation.DataAccess.XMLoperate.GetXmlData(pathtpf, "XMLData");
  207. DataTable dt = ds.Tables["Member"];
  208. foreach (DataRow item in dt.Rows)
  209. {
  210. if (item["TemplateName"].ToString() != "")
  211. {
  212. TemplateList.Add(item["TemplateName"].ToString());
  213. }
  214. }
  215. m_condition = m_otsreport_export.m_ReportApp.m_conditionChoose;
  216. }
  217. private DataTable GetParticles(string filepath, List<string> nameList, int sel, OTSReport_Export m_otsreport_export, c_TemplateClass m_mbszclass)
  218. {
  219. ParticleData particledata = new ParticleData(filepath);
  220. //设置标签名称
  221. List<string> listName = new List<string>();
  222. //获取粒级表
  223. string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + m_mbszclass.M_SYXT.str_cb_syxt_ljb.ToString();
  224. DataSet ds = XMLoperate.GetXml(pathe);
  225. string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
  226. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  227. {
  228. if (sizestr.Split(',')[i].Length > 0)
  229. {
  230. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  231. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  232. listName.Add(d1.ToString() + "~" + d2.ToString());
  233. }
  234. }
  235. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  236. listName.Add(d.ToString() + "~MAX");
  237. string filedAndParticl = "";
  238. string po = "";
  239. string con = "";
  240. int row = m_condition.m_conditionData.ConditionItemList.Count;
  241. if (condition != -1)
  242. {
  243. List<string> li = new List<string>() { "DMAX", "DMIN", "Area", "FERET" };
  244. con = li[condition];
  245. }
  246. else
  247. {
  248. if (row < 4)
  249. {
  250. con = "DMAX";
  251. }
  252. else
  253. {
  254. con = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.SIZE_CAL_METHOD_TYPE).ToString();
  255. }
  256. }
  257. switch (con)
  258. {
  259. case "DMAX":
  260. po = "DMAX";
  261. break;
  262. case "DMIN":
  263. po = "DMIN";
  264. break;
  265. case "ECD":
  266. po = "Area";
  267. break;
  268. case "FERET":
  269. po = "DFERET";
  270. break;
  271. }
  272. DataTable dtp = particledata.GetParticleAllHaveXray(filedAndParticl);
  273. for (int i = 0; i < dtp.Rows.Count; i++)
  274. {
  275. if (Convert.ToInt32(dtp.Rows[i]["TypeId"]) < 0 || dtp.Rows[i]["TypeName"].ToString() == "Invalid")
  276. {
  277. dtp.Rows[i].Delete();
  278. }
  279. }
  280. dtp.AcceptChanges();
  281. //创建一个临时表
  282. DataTable ret_dt = new DataTable();
  283. //然后额外存放三列,用于存放拆分后三个顶点的值
  284. ret_dt.Columns.Add("sizeLevel");
  285. ret_dt.Columns.Add("particleLocation");
  286. ret_dt.Columns.Add("top");
  287. ret_dt.Columns.Add("left");
  288. ret_dt.Columns.Add("right");
  289. ret_dt.Columns.Add("Color_position");
  290. for (int k = 0; k < listName.Count; k++)
  291. {
  292. string str = listName[k];
  293. string d1 = str.Split('~')[0];
  294. string d2 = str.Split('~')[1];
  295. if (d2 == "MAX")
  296. {
  297. d2 = "999";
  298. }
  299. DataRow[] datas = dtp.Select(getWhere(d2, d1, po));
  300. foreach (var item in datas)
  301. {
  302. DataRow dr = ret_dt.NewRow();
  303. dr["sizeLevel"] = str;
  304. string element = item["Element"].ToString();
  305. string strRet = getStrRet(nameList, element);
  306. dr["particleLocation"] = strRet;
  307. dr["top"] = strRet.Split(',')[0];
  308. dr["left"] = strRet.Split(',')[1];
  309. dr["right"] = strRet.Split(',')[2];
  310. dr["Color_position"] = k.ToString();
  311. ret_dt.Rows.Add(dr);
  312. }
  313. }
  314. return ret_dt;
  315. }
  316. private string getWhere(string max, string min, string col)
  317. {
  318. return col + ">=" + min + " and " + col + "<=" + max;
  319. }
  320. private string getStrRet(List<string> template, string element)
  321. {
  322. List<string> name = new List<string>()
  323. { "h","he",
  324. "li","be","b","c","n","o","f","ne",
  325. "na","mg","al","si","p","s","cl","ar",
  326. "k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
  327. "rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe",
  328. "cs","ba","la",
  329. "ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
  330. "hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
  331. "fr","ra","ac",
  332. "th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr"
  333. };
  334. List<double> value = new List<double>()
  335. { 1.008,4.003,
  336. 6.941,9.012,10.811,12.011,14.007,15.999,18.998,20.180,
  337. 22.990,24.305,26.982,28.086,30.974,32.066,35.453,39.948,
  338. 39.098,40.08,44.956,47.88,50.942,51.996,54.938,55.847,58.933,58.70,63.546,65.39,69.72,72.61,74.922,78.96,79.904,83.80,
  339. 85.468,87.62,88.906,91.22,92.906,95.94,98.00,101.07,102.906,106.42,107.868,112.41,114.82,118.71,121.76,127.60,126.905,131.29,
  340. 132.905,137.33,138.906,
  341. 140.12,140.908,144.24,145.0,150.36,151.97,157.25,158.925,162.50,64.93,167.26,168.934,173.04,174.967,
  342. 178.49,180.948,183.85,186.207,190.20,192.22,195.08,196.967,200.59,204.38,207.2,208.980,209.00,210.00,222.00,
  343. 223.00,226.025,227.028,
  344. 232.038,231.036,238.029,237.048,244.00,243.00,247.00,247.00,251.00,252.00,257.00,258.00,259.00,260.00
  345. };
  346. double d_ASum = 0;
  347. double d_BSum = 0;
  348. double d_CSum = 0;
  349. string aElements = template[0];
  350. string bElements = template[1];
  351. string cElements = template[2];
  352. foreach (var item in element.Split(';'))
  353. {
  354. if (item.Contains(aElements + "-"))
  355. {
  356. if (aElements != "")
  357. {
  358. d_ASum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(aElements.ToLower())];
  359. }
  360. }
  361. for (int i = 0; i < bElements.Split(',').Length; i++)
  362. {
  363. string e = bElements.Split(',')[i];
  364. if (item.Contains(e + "-"))
  365. {
  366. d_BSum = d_BSum + Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(e.ToLower())];
  367. }
  368. }
  369. if (item.Contains(cElements + "-"))
  370. {
  371. if (cElements != "")
  372. d_CSum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(cElements.ToLower())];
  373. }
  374. }
  375. double allNums = d_ASum + d_BSum + d_CSum;
  376. string strRet = "";
  377. if ((allNums) == 0)
  378. {
  379. strRet = "0,0,0";
  380. }
  381. else
  382. {
  383. double aPercent = 0, bPercent = 0, cPercent = 0;
  384. if (d_ASum != 0)
  385. {
  386. aPercent = d_ASum / allNums;
  387. }
  388. if (d_BSum != 0)
  389. {
  390. bPercent = d_BSum / allNums;
  391. }
  392. if (d_CSum != 0)
  393. {
  394. cPercent = d_CSum / allNums;
  395. }
  396. strRet = aPercent.ToString() + "," + bPercent.ToString() + "," + cPercent.ToString();
  397. }
  398. return strRet;
  399. }
  400. /// <summary>
  401. /// 三角形绘制
  402. /// </summary>
  403. /// <param name="center">中心点位置</param>
  404. /// <param name="sideLength">边长</param>
  405. /// <param name="Blue">颜色</param>
  406. /// <param name="g">Graphics</param>
  407. private void Triangle(PointF center, float sideLength, Color Blue, Graphics g)
  408. {
  409. Pen pen = new Pen(Color.Black, 2);
  410. // 计算正三角形的高度(从中心到底边的垂直距离)
  411. float height = (float)(Math.Sqrt(3) / 2 * sideLength);
  412. // 计算三个顶点的坐标
  413. float cenrerX = center.X;
  414. float cenrerY = center.Y;
  415. PointF topVertex = new PointF(cenrerX, cenrerY - height / 2);
  416. PointF leftVertex = new PointF(cenrerX - sideLength / 2, cenrerY + height / 2 * (float)Math.Sqrt(3) / 3);
  417. PointF rightVertex = new PointF(cenrerX + sideLength / 2, cenrerY + height / 2 * (float)Math.Sqrt(3) / 3);
  418. // 为了演示,这里我们仍然使用上面计算得到的顶点
  419. PointF[] vertices = { topVertex, leftVertex, rightVertex };
  420. g.DrawPolygon(pen, vertices);
  421. // 创建一个Brush对象来定义填充颜色
  422. Brush brush = new SolidBrush(Blue); // 例如,填充颜色为蓝色
  423. // 使用Graphics.FillPolygon方法填充正三角形
  424. g.FillPolygon(brush, vertices);
  425. }
  426. /// <summary>
  427. /// 矩形
  428. /// </summary>
  429. /// <param name="center">中心点位置</param>
  430. /// <param name="sideLength">边长</param>
  431. /// <param name="Blue">颜色</param>
  432. /// <param name="g">Graphics</param>
  433. private void Rectangle(PointF center, float sideLength, Color Blue, Graphics g)
  434. {
  435. PointF pointF = new PointF(center.X - (sideLength/2), center.Y - (sideLength / 2));
  436. Brush brush = new SolidBrush(Blue);
  437. Pen pen = new Pen(Color.Black, 2);
  438. g.DrawRectangle(pen, pointF.X, pointF.Y, sideLength, sideLength);
  439. g.FillRectangle(brush, pointF.X, pointF.Y, sideLength, sideLength);
  440. }
  441. /// <summary>
  442. /// 圆形
  443. /// </summary>
  444. /// <param name="center">中心点位置</param>
  445. /// <param name="sideLength">边长</param>
  446. /// <param name="Blue">颜色</param>
  447. /// <param name="g">Graphics</param>
  448. private void circle(PointF center, float sideLength, Color Blue, Graphics g)
  449. {
  450. Pen pen = new Pen(Color.Black, 2);
  451. Brush brush = new SolidBrush(Blue); // 例如,填充颜色为蓝色
  452. PointF pointF = new PointF(center.X - (sideLength / 2), center.Y - (sideLength / 2));
  453. g.DrawEllipse(pen, pointF.X , pointF.Y , sideLength , sideLength);
  454. g.FillEllipse(brush, pointF.X, pointF.Y, sideLength, sideLength);
  455. }
  456. /// <summary>
  457. /// 菱形
  458. /// </summary>
  459. /// <param name="center">中心点位置</param>
  460. /// <param name="sideLength">边长</param>
  461. /// <param name="Blue">颜色</param>
  462. /// <param name="g">Graphics</param>
  463. private void rhombus(PointF center, float sideLength, Color Blue, Graphics g)
  464. {
  465. // 保存当前的Graphics状态,以便稍后恢复
  466. GraphicsState state = g.Save();
  467. // 将坐标系移动到旋转的中心点,并旋转45度
  468. g.TranslateTransform(center.X, center.Y);
  469. g.RotateTransform(45);
  470. // 在旋转后的坐标系中绘制正方形
  471. Rectangle rect = new Rectangle((int)(-sideLength / 2), (int)(-sideLength / 2), (int)sideLength, (int)sideLength);
  472. Pen pen = new Pen(Color.Black, 2);
  473. g.DrawRectangle(pen, rect);
  474. Brush brush = new SolidBrush(Blue);
  475. g.FillRectangle(brush, rect);
  476. // 恢复原始的Graphics状态
  477. g.Restore(state);
  478. }
  479. public void ShapeList(int order, PointF center, float sideLength, Color Blue, Graphics g)
  480. {
  481. if (order ==0)
  482. {
  483. //三角
  484. Triangle(center, sideLength+2, Blue, g);
  485. }
  486. else if(order==1)
  487. {
  488. //矩形
  489. Rectangle(center, sideLength, Blue, g);
  490. }
  491. else if (order==2)
  492. {
  493. //圆形
  494. circle(center, sideLength, Blue, g);
  495. }
  496. else if (order ==3)
  497. {
  498. //菱形
  499. rhombus(center, sideLength, Blue, g);
  500. }
  501. }
  502. }
  503. }