当前位置: 首页 > news >正文

c# 二维图形绘制实践

1.等边三角形

1.1 概述

1.2 代码

using System;
using System.Drawing;
using System.Windows.Forms;public partial class TriangleForm : Form
{public TriangleForm(){//InitializeComponent();// 确保窗体大小足够大,以容纳三角形  this.ClientSize = new Size(300, 300);this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁  }protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定义三角形的大小和位置  int sideLength = 100; // 等边三角形外接圆的半径int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标  int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标  // 将30度转换为弧度  double degrees = 30;double radians = Math.PI * degrees / 180;double sinValue = Math.Sin(radians);double cosValue = Math.Cos(radians);float sinLen = (float)sinValue * sideLength;float cosLen = (float)cosValue * sideLength;// 计算三角形顶点的位置  PointF topVertex = new PointF(centerX, centerY - sideLength );PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);// 创建一个Brush对象来填充三角形  using (SolidBrush brush = new SolidBrush(Color.LightBlue)){// 绘制等边三角形  e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });// 如果你还想绘制三角形的边框,可以使用Pen对象  using (Pen pen = new Pen(Color.Black, 2)){e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, leftVertex, rightVertex });}}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new TriangleForm());}
}

1.3 运行结果

2 立方体

2.1 概要

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

2.2 代码

using System;
using System.Drawing;
using System.Windows.Forms;public partial class TriangleForm : Form
{public TriangleForm(){//InitializeComponent();// 确保窗体大小足够大,以容纳三角形  this.ClientSize = new Size(300, 300);this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁  }protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定义三角形的大小和位置  int sideLength = 100; // 等边三角形的边长  int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标  int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标  // 将30度转换为弧度  double degrees = 30;double radians = Math.PI * degrees / 180;double sinValue = Math.Sin(radians);double cosValue = Math.Cos(radians);float sinLen = (float)sinValue * sideLength;float cosLen = (float)cosValue * sideLength;//中心点PointF topVertex_center = new PointF(centerX, centerY);// 计算三角形顶点的位置  PointF topVertex = new PointF(centerX, centerY - cosLen);PointF topVertex_left = new PointF(centerX - cosLen, centerY - cosLen + sinLen);PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);PointF topVertex_buttom = new PointF(centerX, centerY + sinLen*2);PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);PointF topVertex_right = new PointF(centerX + cosLen, centerY - cosLen + sinLen);// 创建一个Brush对象来填充三角形  using (SolidBrush brush = new SolidBrush(Color.LightBlue)){// 绘制等边三角形  //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });// 如果你还想绘制三角形的边框,可以使用Pen对象  using (Pen pen = new Pen(Color.Black, 2)){e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, topVertex_left, leftVertex, topVertex_buttom, rightVertex, topVertex_right });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_left });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_right });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_buttom });}}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new TriangleForm());}
}

2.3 运行结果

 

3 立方体透视图

3.1 概要

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

3.2 代码

using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;public partial class TriangleForm : Form
{public TriangleForm(){//InitializeComponent();// 确保窗体大小足够大,以容纳三角形  this.ClientSize = new Size(300, 300);this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁  }protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定义三角形的大小和位置  int sideLength = 100; // 等边三角形的边长  int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标  int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标  // 将30度转换为弧度  double degrees = 30;double radians = Math.PI * degrees / 180;double sinValue = Math.Sin(radians);double cosValue = Math.Cos(radians);float sinLen = (float)sinValue * sideLength;float cosLen = (float)cosValue * sideLength;float y_yi = 20;float x_yi = 10;//中心点PointF topVertex_center = new PointF(centerX+ x_yi, centerY- y_yi);PointF topVertex_center_hou = new PointF(centerX - x_yi, centerY + y_yi);// 计算三角形顶点的位置  PointF topVertex = new PointF(centerX- x_yi, centerY - cosLen+ y_yi);PointF topVertex_left = new PointF(centerX - cosLen, centerY - cosLen + sinLen);PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);PointF topVertex_buttom = new PointF(centerX+ x_yi, centerY + sinLen*2- y_yi);PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);PointF topVertex_right = new PointF(centerX + cosLen, centerY - cosLen + sinLen);// 创建一个Brush对象来填充三角形  using (SolidBrush brush = new SolidBrush(Color.LightBlue)){// 绘制等边三角形  //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });// 如果你还想绘制三角形的边框,可以使用Pen对象  using (Pen pen = new Pen(Color.Black, 2)){e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, topVertex_left, leftVertex, topVertex_buttom, rightVertex, topVertex_right });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_left });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_right });e.Graphics.DrawPolygon(pen, new PointF[] { topVertex_center, topVertex_buttom });}float[] dashValues = { 50, 5 }; // 虚线由5个像素的实线和5个像素的空白组成  Pen dashedPen = new Pen(Color.Black, 1);e.Graphics.DrawLine(dashedPen, topVertex_center_hou, leftVertex);e.Graphics.DrawLine(dashedPen, topVertex_center_hou, rightVertex);e.Graphics.DrawLine(dashedPen, topVertex_center_hou, topVertex);}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new TriangleForm());}
}

3.3 运行结果

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

4.1 概要

4.2 代码

using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;public partial class TriangleForm : Form
{public TriangleForm(){//InitializeComponent();// 确保窗体大小足够大,以容纳三角形  this.ClientSize = new Size(300, 300);this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁  }protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定义三角形的大小和位置  int sideLength = 100; // 内接圆半径int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标  int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标  // 将30度转换为弧度  double degrees = 30;double radians = Math.PI * degrees / 180;double sinValue = Math.Sin(radians);double cosValue = Math.Cos(radians);float sinLen = (float)sinValue * sideLength;float cosLen = (float)cosValue * sideLength;// 计算三角形顶点的位置  PointF topVertex = new PointF(centerX, centerY - sideLength);PointF leftVertex = new PointF(centerX - cosLen, centerY + sinLen);PointF rightVertex = new PointF(centerX + cosLen, centerY + sinLen);// 设置圆形的边界矩形(位置和大小)  Rectangle rect = new Rectangle(centerX- (int)sinLen, centerY- (int)sinLen, (int)(sinLen*2), (int)(sinLen*2)); // x=50, y=50, 宽度=100, 高度=100Rectangle rect2 = new Rectangle(centerX - (int)sideLength, centerY - (int)sideLength, sideLength*2, sideLength*2); // x=50, y=50, 宽度=100, 高度=100// 创建一个Brush对象来填充三角形  using (SolidBrush brush = new SolidBrush(Color.LightBlue)){// 绘制等边三角形  //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });// 如果你还想绘制三角形的边框,可以使用Pen对象  using (Pen pen = new Pen(Color.Black, 2)){e.Graphics.DrawPolygon(pen, new PointF[] { topVertex,  leftVertex, rightVertex, });e.Graphics.DrawEllipse(pen, rect2);e.Graphics.DrawEllipse(pen, rect);}}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new TriangleForm());}
}

4.3 运行结果

5.直角三角形的内接圆

5.1 概要

5.2 代码

using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;public partial class TriangleForm : Form
{public TriangleForm(){//InitializeComponent();// 确保窗体大小足够大,以容纳三角形  this.ClientSize = new Size(300, 300);this.DoubleBuffered = true; // 启用双缓冲,以减少绘图时的闪烁  }protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定义三角形的大小和位置  int sideLength = 50; // 内接圆半径int centerX = this.ClientSize.Width / 2; // 三角形中心点的X坐标  int centerY = this.ClientSize.Height / 2; // 三角形中心点的Y坐标  // 将30度转换为弧度  double degrees = 22.5;double radians = Math.PI * degrees / 180;double sinValue = Math.Sin(radians);double cosValue = Math.Cos(radians);double tanValue = Math.Tan(radians);float sinLen = (float)sinValue * sideLength;float cosLen = (float)cosValue * sideLength;float tanLen = (float)(sideLength/ tanValue);// 计算三角形顶点的位置  PointF topVertex = new PointF(centerX+ sideLength, centerY - tanLen);PointF leftVertex = new PointF(centerX - tanLen, centerY + sideLength);PointF rightVertex = new PointF(centerX + sideLength, centerY + sideLength);// 设置圆形的边界矩形(位置和大小)  //Rectangle rect = new Rectangle(centerX - (int)sinLen, centerY - (int)sinLen, (int)(sinLen * 2), (int)(sinLen * 2)); // x=50, y=50, 宽度=100, 高度=100Rectangle rect2 = new Rectangle(centerX - (int)sideLength, centerY - (int)sideLength, sideLength * 2, sideLength * 2); // x=50, y=50, 宽度=100, 高度=100// 创建一个Brush对象来填充三角形  using (SolidBrush brush = new SolidBrush(Color.LightBlue)){// 绘制等边三角形  //e.Graphics.FillPolygon(brush, new PointF[] { topVertex, leftVertex, rightVertex });// 如果你还想绘制三角形的边框,可以使用Pen对象  using (Pen pen = new Pen(Color.Black, 2)){e.Graphics.DrawPolygon(pen, new PointF[] { topVertex, leftVertex, rightVertex, });e.Graphics.DrawEllipse(pen, rect2);//e.Graphics.DrawEllipse(pen, rect);}}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new TriangleForm());}
}

5.3 运行结果

 

相关文章:

  • 【Python高级编程】OpenCV来处理视频数据
  • 【数据结构(邓俊辉)学习笔记】二叉搜索树02——查找、插入和删除
  • mysql对VARCHAR和int的误解
  • c++ | 动态编译|虚函数表|虚函数
  • HTML和CSS基础(二)
  • 航空电子设备 MIL-STD-1553 收发器 HI-1573 / HI-1574
  • LinkedList与链表
  • Kubernetes面试整理-Kubernetes的主要组件有哪些?
  • SpringBoot三层架构
  • 高分论文密码---大尺度空间模拟预测与数字制图
  • 使用开源的zip.cpp和unzip.cpp实现压缩包的创建与解压(附源码)
  • 【机器学习】第2章 线性回归及最大熵模型
  • Android Room数据库使用介绍
  • SpringBoot3 整合 Mybatis 完整版
  • 【MySQL】分库分表
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • C学习-枚举(九)
  • JavaScript新鲜事·第5期
  • Java多态
  • JSDuck 与 AngularJS 融合技巧
  • Js基础知识(一) - 变量
  • Linux后台研发超实用命令总结
  • SQLServer之创建显式事务
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 如何选择开源的机器学习框架?
  • 使用agvtool更改app version/build
  • 使用putty远程连接linux
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 一份游戏开发学习路线
  • 主流的CSS水平和垂直居中技术大全
  • Linux权限管理(week1_day5)--技术流ken
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​插件化DPI在商用WIFI中的价值
  • #mysql 8.0 踩坑日记
  • (2022 CVPR) Unbiased Teacher v2
  • (分布式缓存)Redis分片集群
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (接上一篇)前端弄一个变量实现点击次数在前端页面实时更新
  • (转)shell调试方法
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)项目管理杂谈-我所期望的新人
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET 发展历程
  • .Net 基于IIS部署blazor webassembly或WebApi
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .net 验证控件和javaScript的冲突问题
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • /bin、/sbin、/usr/bin、/usr/sbin
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • @Async 异步注解使用