TernaryDiagram.cs 24 KB

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