当前位置:   article > 正文

c# 二维图形绘制实践

c# 二维图形绘制实践

1.等边三角形

1.1 概述

1.2 代码

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public partial class TriangleForm : Form
  5. {
  6. public TriangleForm()
  7. {
  8. //InitializeComponent();
  9. // 确保窗体大小足够大,以容纳三角形
  10. this.ClientSize = new Size(300, 300);
  11. this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁
  12. }
  13. protected override void OnPaint(PaintEventArgs e)
  14. {
  15. base.OnPaint(e);
  16. // 定义三角形的大小和位置
  17. int sideLength = 100; // 等边三角形外接圆的半径
  18. int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标
  19. int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标
  20. //30度转换为弧度
  21. double degrees = 30;
  22. double radians = Math.PI * degrees / 180;
  23. double sinValue = Math.Sin(radians);
  24. double cosValue = Math.Cos(radians);
  25. float sinLen = (float)sinValue * sideLength;
  26. float cosLen = (float)cosValue * sideLength;
  27. // 计算三角形顶点的位置
  28. PointF topVertex = new PointF(centerX, centerY - sideLength );
  29. PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);
  30. PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);
  31. // 创建一个Brush对象来填充三角形
  32. using (SolidBrush brush = new SolidBrush(Color.LightBlue))
  33. {
  34. // 绘制等边三角形
  35. e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });
  36. // 如果你还想绘制三角形的边框,可以使用Pen对象
  37. using (Pen pen = new Pen(Color.Black, 2))
  38. {
  39. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, leftVertex, rightVertex });
  40. }
  41. }
  42. }
  43. [STAThread]
  44. static void Main()
  45. {
  46. Application.EnableVisualStyles();
  47. Application.SetCompatibleTextRenderingDefault(false);
  48. Application.Run(new TriangleForm());
  49. }
  50. }

1.3 运行结果

2 立方体

2.1 概要

立方体是用等边三角型的图转换过来的

2.2 代码

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public partial class TriangleForm : Form
  5. {
  6. public TriangleForm()
  7. {
  8. //InitializeComponent();
  9. // 确保窗体大小足够大,以容纳三角形
  10. this.ClientSize = new Size(300, 300);
  11. this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁
  12. }
  13. protected override void OnPaint(PaintEventArgs e)
  14. {
  15. base.OnPaint(e);
  16. // 定义三角形的大小和位置
  17. int sideLength = 100; // 等边三角形的边长
  18. int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标
  19. int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标
  20. //30度转换为弧度
  21. double degrees = 30;
  22. double radians = Math.PI * degrees / 180;
  23. double sinValue = Math.Sin(radians);
  24. double cosValue = Math.Cos(radians);
  25. float sinLen = (float)sinValue * sideLength;
  26. float cosLen = (float)cosValue * sideLength;
  27. //中心点
  28. PointF topVertex_center = new PointF(centerX, centerY);
  29. // 计算三角形顶点的位置
  30. PointF topVertex = new PointF(centerX, centerY - cosLen);
  31. PointF topVertex_left = new PointF(centerX - cosLen, centerY - cosLen + sinLen);
  32. PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);
  33. PointF topVertex_buttom = new PointF(centerX, centerY + sinLen*2);
  34. PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);
  35. PointF topVertex_right = new PointF(centerX + cosLen, centerY - cosLen + sinLen);
  36. // 创建一个Brush对象来填充三角形
  37. using (SolidBrush brush = new SolidBrush(Color.LightBlue))
  38. {
  39. // 绘制等边三角形
  40. //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });
  41. // 如果你还想绘制三角形的边框,可以使用Pen对象
  42. using (Pen pen = new Pen(Color.Black, 2))
  43. {
  44. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, topVertex_left, leftVertex, topVertex_buttom, rightVertex, topVertex_right });
  45. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_left });
  46. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_right });
  47. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_buttom });
  48. }
  49. }
  50. }
  51. [STAThread]
  52. static void Main()
  53. {
  54. Application.EnableVisualStyles();
  55. Application.SetCompatibleTextRenderingDefault(false);
  56. Application.Run(new TriangleForm());
  57. }
  58. }

2.3 运行结果

 

3 立方体透视图

3.1 概要

透视图是用前面的立方体,去移动顶点演化出来的

3.2 代码

  1. using System;
  2. using System.Drawing;
  3. using System.Net;
  4. using System.Windows.Forms;
  5. public partial class TriangleForm : Form
  6. {
  7. public TriangleForm()
  8. {
  9. //InitializeComponent();
  10. // 确保窗体大小足够大,以容纳三角形
  11. this.ClientSize = new Size(300, 300);
  12. this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁
  13. }
  14. protected override void OnPaint(PaintEventArgs e)
  15. {
  16. base.OnPaint(e);
  17. // 定义三角形的大小和位置
  18. int sideLength = 100; // 等边三角形的边长
  19. int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标
  20. int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标
  21. //30度转换为弧度
  22. double degrees = 30;
  23. double radians = Math.PI * degrees / 180;
  24. double sinValue = Math.Sin(radians);
  25. double cosValue = Math.Cos(radians);
  26. float sinLen = (float)sinValue * sideLength;
  27. float cosLen = (float)cosValue * sideLength;
  28. float y_yi = 20;
  29. float x_yi = 10;
  30. //中心点
  31. PointF topVertex_center = new PointF(centerX+ x_yi, centerY- y_yi);
  32. PointF topVertex_center_hou = new PointF(centerX - x_yi, centerY + y_yi);
  33. // 计算三角形顶点的位置
  34. PointF topVertex = new PointF(centerX- x_yi, centerY - cosLen+ y_yi);
  35. PointF topVertex_left = new PointF(centerX - cosLen, centerY - cosLen + sinLen);
  36. PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);
  37. PointF topVertex_buttom = new PointF(centerX+ x_yi, centerY + sinLen*2- y_yi);
  38. PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);
  39. PointF topVertex_right = new PointF(centerX + cosLen, centerY - cosLen + sinLen);
  40. // 创建一个Brush对象来填充三角形
  41. using (SolidBrush brush = new SolidBrush(Color.LightBlue))
  42. {
  43. // 绘制等边三角形
  44. //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });
  45. // 如果你还想绘制三角形的边框,可以使用Pen对象
  46. using (Pen pen = new Pen(Color.Black, 2))
  47. {
  48. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, topVertex_left, leftVertex, topVertex_buttom, rightVertex, topVertex_right });
  49. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_left });
  50. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_right });
  51. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_buttom });
  52. }
  53. float[] dashValues = { 50, 5 }; // 虚线由5个像素的实线和5个像素的空白组成
  54. Pen dashedPen = new Pen(Color.Black, 1);
  55. e.Graphics.DrawLine(dashedPen, topVertex_center_hou, leftVertex);
  56. e.Graphics.DrawLine(dashedPen, topVertex_center_hou, rightVertex);
  57. e.Graphics.DrawLine(dashedPen, topVertex_center_hou, topVertex);
  58. }
  59. }
  60. [STAThread]
  61. static void Main()
  62. {
  63. Application.EnableVisualStyles();
  64. Application.SetCompatibleTextRenderingDefault(false);
  65. Application.Run(new TriangleForm());
  66. }
  67. }

3.3 运行结果

4.等边三角形的内切圆和外接圆

4.1 概要

4.2 代码

  1. using System;
  2. using System.Drawing;
  3. using System.Net;
  4. using System.Windows.Forms;
  5. public partial class TriangleForm : Form
  6. {
  7. public TriangleForm()
  8. {
  9. //InitializeComponent();
  10. // 确保窗体大小足够大,以容纳三角形
  11. this.ClientSize = new Size(300, 300);
  12. this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁
  13. }
  14. protected override void OnPaint(PaintEventArgs e)
  15. {
  16. base.OnPaint(e);
  17. // 定义三角形的大小和位置
  18. int sideLength = 100; // 内接圆半径
  19. int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标
  20. int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标
  21. //30度转换为弧度
  22. double degrees = 30;
  23. double radians = Math.PI * degrees / 180;
  24. double sinValue = Math.Sin(radians);
  25. double cosValue = Math.Cos(radians);
  26. float sinLen = (float)sinValue * sideLength;
  27. float cosLen = (float)cosValue * sideLength;
  28. // 计算三角形顶点的位置
  29. PointF topVertex = new PointF(centerX, centerY - sideLength);
  30. PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);
  31. PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);
  32. // 设置圆形的边界矩形(位置和大小)
  33. Rectangle rect = new Rectangle(centerX- (int)sinLen, centerY- (int)sinLen, (int)(sinLen*2), (int)(sinLen*2)); // x=50, y=50, 宽度=100, 高度=100
  34. Rectangle rect2 = new Rectangle(centerX - (int)sideLength, centerY - (int)sideLength, sideLength*2, sideLength*2); // x=50, y=50, 宽度=100, 高度=100
  35. // 创建一个Brush对象来填充三角形
  36. using (SolidBrush brush = new SolidBrush(Color.LightBlue))
  37. {
  38. // 绘制等边三角形
  39. //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });
  40. // 如果你还想绘制三角形的边框,可以使用Pen对象
  41. using (Pen pen = new Pen(Color.Black, 2))
  42. {
  43. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, leftVertex, rightVertex, });
  44. e.Graphics.DrawEllipse(pen, rect2);
  45. e.Graphics.DrawEllipse(pen, rect);
  46. }
  47. }
  48. }
  49. [STAThread]
  50. static void Main()
  51. {
  52. Application.EnableVisualStyles();
  53. Application.SetCompatibleTextRenderingDefault(false);
  54. Application.Run(new TriangleForm());
  55. }
  56. }

4.3 运行结果

5.直角三角形的内接圆

5.1 概要

5.2 代码

  1. using System;
  2. using System.Drawing;
  3. using System.Net;
  4. using System.Windows.Forms;
  5. public partial class TriangleForm : Form
  6. {
  7. public TriangleForm()
  8. {
  9. //InitializeComponent();
  10. // 确保窗体大小足够大,以容纳三角形
  11. this.ClientSize = new Size(300, 300);
  12. this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁
  13. }
  14. protected override void OnPaint(PaintEventArgs e)
  15. {
  16. base.OnPaint(e);
  17. // 定义三角形的大小和位置
  18. int sideLength = 50; // 内接圆半径
  19. int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标
  20. int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标
  21. //30度转换为弧度
  22. double degrees = 22.5;
  23. double radians = Math.PI * degrees / 180;
  24. double sinValue = Math.Sin(radians);
  25. double cosValue = Math.Cos(radians);
  26. double tanValue = Math.Tan(radians);
  27. float sinLen = (float)sinValue * sideLength;
  28. float cosLen = (float)cosValue * sideLength;
  29. float tanLen = (float)(sideLength/ tanValue);
  30. // 计算三角形顶点的位置
  31. PointF topVertex = new PointF(centerX+ sideLength, centerY - tanLen);
  32. PointF leftVertex = new PointF(centerX - tanLen, centerY + sideLength);
  33. PointF rightVertex = new PointF(centerX + sideLength, centerY + sideLength);
  34. // 设置圆形的边界矩形(位置和大小)
  35. //Rectangle rect = new Rectangle(centerX - (int)sinLen, centerY - (int)sinLen, (int)(sinLen * 2), (int)(sinLen * 2)); // x=50, y=50, 宽度=100, 高度=100
  36. Rectangle rect2 = new Rectangle(centerX - (int)sideLength, centerY - (int)sideLength, sideLength * 2, sideLength * 2); // x=50, y=50, 宽度=100, 高度=100
  37. // 创建一个Brush对象来填充三角形
  38. using (SolidBrush brush = new SolidBrush(Color.LightBlue))
  39. {
  40. // 绘制等边三角形
  41. //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });
  42. // 如果你还想绘制三角形的边框,可以使用Pen对象
  43. using (Pen pen = new Pen(Color.Black, 2))
  44. {
  45. e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, leftVertex, rightVertex, });
  46. e.Graphics.DrawEllipse(pen, rect2);
  47. //e.Graphics.DrawEllipse(pen, rect);
  48. }
  49. }
  50. }
  51. [STAThread]
  52. static void Main()
  53. {
  54. Application.EnableVisualStyles();
  55. Application.SetCompatibleTextRenderingDefault(false);
  56. Application.Run(new TriangleForm());
  57. }
  58. }

5.3 运行结果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/729027
推荐阅读
相关标签
  

闽ICP备14008679号