当前位置: 首页 > 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 运行结果

 

相关文章:

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 Siz…...

Nvidia TensorRT系列01-TensorRT的功能1

Nvidia TensorRT系列01-TensorRT的功能1 B站:肆十二-的个人空间-肆十二-个人主页-哔哩哔哩视频 (bilibili.com) 博客:肆十二-CSDN博客 问答:(10 封私信 / 72 条消息) 肆十二 - 知乎 (zhihu.com) C和Python API TensorRT的API同时支持C和Pyth…...

Vatee万腾平台:创新科技,助力企业腾飞

在全球化竞争日益激烈的今天,企业如何借助科技力量实现转型升级,已成为摆在众多企业家面前的重大课题。Vatee万腾平台凭借其卓越的创新科技和专业的服务能力,成为众多企业实现腾飞的得力助手。 一、创新科技,引领企业前行 Vatee万…...

搭建k8s集群报错unknown command “\u00a0“ for “kubeadm init“

搭建k8s报错unknown command “\u00a0” for “kubeadm init” 网上搜了一下,是因为复制过来的命令前面包含了空格,将复制的命令放到idea可以清楚看到几个命令前面有空格,删除掉就好了,记录一下...

【数据结构】三路快速排序

1. 简介 传统快速排序用的是双路快速排序,即将大于基准值的部分放到基准值右侧,小于基准值的部分放到基准值左侧,但是这种算法面对过多的重复数据的数组,时间复杂度会增多,于是就有了三路快速排序的思想,其…...

中国菜刀,蚁剑,哥斯拉,冰蝎的流量特征区别

中国菜刀、蚁剑、哥斯拉、冰蝎这四种Webshell连接工具的流量特征各有区别,以下是它们之间的主要差异: 中国菜刀(CaiDao) 流量特征: 请求包: UA头可能伪装为百度、火狐等浏览器的User-Agent。请求体中存在…...

华为OD刷题C卷 - 每日刷题32(执行任务赚积分,计算三叉搜索树的高度)

1、(执行任务赚积分): 这段代码是解决“执行任务赚积分”的问题。它提供了一个Java类Main,其中包含main方法和getResult方法,用于计算在有限的时间内,处理任务可以获得的最多积分。 main方法首先读取任务…...

QT系列教程(11) TextEdit实现Qt 文本高亮

文本高亮 对于textedit里录入的部分单词我们可以实现高亮,实现高亮主要依赖于QSyntaxHighlighter。 我们先创建一个Qt Application类,类名MainWindow, 然后新增一个C类,类名为MySyntaxHighlighter。 #ifndef MYSYNTAXHIGHLIGHTER_H #define …...

蓝队-溯源技巧

溯源技巧 大致思想 通常情况下,接到溯源任务时,获得的信息如下 攻击时间 攻击 IP 预警平台 攻击类型 恶意文件 受攻击域名/IP其中攻击 IP、攻击类型、恶意文件、攻击详情是溯源入手的点。 通过攻击类型分析攻击详情的请求包,看有没有攻击者…...

【5】JDK、JRE和JVM的区别与联系

JDK、JRE和JVM的区别与联系 Java是一种广泛使用的编程语言,它的跨平台特性得益于Java虚拟机(JVM)。然而,在Java的世界里,JDK、JRE和JVM这三个术语常常让人感到困惑。本文将阐述它们各自的功能,以及它们是如…...

【DevOps】Logstash详解:高效日志管理与分析工具

在现代软件开发和运维过程中,日志管理与分析是至关重要的环节。日志可以帮助我们追踪系统行为、诊断问题、优化性能以及确保安全合规。Logstash,作为ELK Stack(Elasticsearch、Logstash、Kibana)的核心组件之一,是一个…...

Vue3 之 Pinia 核心概念(八)

核心概念 State:这是你的应用程序的状态,是一个响应式的对象。 Getters:类似于 Vuex 中的 getters,它们是基于 state 的计算属性。 Actions:类似于 Vuex 中的 mutations 和 actions,它们用于改变 state。但…...

【办公类-04-03】华为助手导出照片视频分类(根据图片、视频的文件名日期分类导出)

背景需求: 用华为手机助手导出的照片视频,只能将jpg照片(exifread读取图片的exif拍摄日期,Png、JPEG、mp4都无法识别到exif信息) 【办公类-04-02】华为助手导出照片(jpg)读取拍摄时间分类导出…...

TVBOX 最新版下载+视频源教程

下载链接 wx 搜索 Geek 前端 发送电视资源进行获取 操作教程...

2024年了,苹果可以通话录音了

人不走空 🌈个人主页:人不走空 💖系列专栏:算法专题 ⏰诗词歌赋:斯是陋室,惟吾德馨 6月11日凌晨,苹果在WWDC24大会上,密集输出了酝酿多时的AI应用更新。苹果对通话、对话、图…...

书生·浦语大模型实战营第二期作业五

1、开发机创建conda环境: 2、安装第三方库: 3、新建pipeline_transformer.py文件,并运行: 4、运行结果: 5、执行模型: 6、与大模型进行对话: 7、默认占有的显存: 8、--cache-max-en…...

树莓派4B_OpenCv学习笔记9:图片的腐蚀与膨胀

今日继续学习树莓派4B 4G:(Raspberry Pi,简称RPi或RasPi) 本人所用树莓派4B 装载的系统与版本如下: 版本可用命令 (lsb_release -a) 查询: Opencv 版本是4.5.1: 图像的膨胀与腐蚀一般用于灰度图或者二值图,今日便来学习…...

Perplexity AI — 探索网络,发掘知识,沟通思想

体验地址:Perplexity AI (国外网站访问需要梯子) Perplexity AI是一款功能强大的人工智能搜索引擎,其特点和优势主要体现在以下几个方面: 功能: 自然语言搜索:Perplexity AI可以理解用户的自然…...

RPC知识

一、为什么要有RPC: HTTP协议的接口,在接口不多、系统与系统交互较少的情况下,解决信息孤岛初期常使用的一种通信手段;优点就是简单、直接、开发方便,利用现成的HTTP协议进行传输。 但是,如果是一个大型的网…...

【爬虫】requests 结合 BeautifulSoup抓取网页数据

一、BeautifulSoup使用步骤 BeautifulSoup 是一个用于从 HTML 或 XML 文件中提取数据的 Python 库。以下是如何使用 BeautifulSoup 来解析 HTML 并提取信息的基本步骤: 1、安装: 如果你还没有安装 BeautifulSoup,你可以使用 pip 来安装它。…...

MPNet:旋转机械轻量化故障诊断模型详解python代码复现

目录 一、问题背景与挑战 二、MPNet核心架构 2.1 多分支特征融合模块(MBFM) 2.2 残差注意力金字塔模块(RAPM) 2.2.1 空间金字塔注意力(SPA) 2.2.2 金字塔残差块(PRBlock) 2.3 分类器设计 三、关键技术突破 3.1 多尺度特征融合 3.2 轻量化设计策略 3.3 抗噪声…...

手游刚开服就被攻击怎么办?如何防御DDoS?

开服初期是手游最脆弱的阶段,极易成为DDoS攻击的目标。一旦遭遇攻击,可能导致服务器瘫痪、玩家流失,甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案,帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...

(二)原型模式

原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

【2025年】解决Burpsuite抓不到https包的问题

环境:windows11 burpsuite:2025.5 在抓取https网站时,burpsuite抓取不到https数据包,只显示: 解决该问题只需如下三个步骤: 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同,结合所安装的tensorflow的目录结构修改from语句即可。 原语句: from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后: from tensorflow.python.keras.lay…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

云原生安全实战:API网关Kong的鉴权与限流详解

🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关(API Gateway) API网关是微服务架构中的核心组件,负责统一管理所有API的流量入口。它像一座…...

GO协程(Goroutine)问题总结

在使用Go语言来编写代码时,遇到的一些问题总结一下 [参考文档]:https://www.topgoer.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine.html 1. main()函数默认的Goroutine 场景再现: 今天在看到这个教程的时候,在自己的电…...

redis和redission的区别

Redis 和 Redisson 是两个密切相关但又本质不同的技术,它们扮演着完全不同的角色: Redis: 内存数据库/数据结构存储 本质: 它是一个开源的、高性能的、基于内存的 键值存储数据库。它也可以将数据持久化到磁盘。 核心功能: 提供丰…...