当前位置: 首页 > 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 来安装它。…...

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具,该工具基于TUN接口实现其功能,利用反向TCP/TLS连接建立一条隐蔽的通信信道,支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式,适应复杂网…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度(创建索引的主要原因)。3. 可以加速表和表之间的连接,实现数据的参考完整性。4. 可以在查询过程中,…...

UE5 学习系列(三)创建和移动物体

这篇博客是该系列的第三篇,是在之前两篇博客的基础上展开,主要介绍如何在操作界面中创建和拖动物体,这篇博客跟随的视频链接如下: B 站视频:s03-创建和移动物体 如果你不打算开之前的博客并且对UE5 比较熟的话按照以…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说,传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度,通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

Java 二维码

Java 二维码 **技术&#xff1a;**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...

C++使用 new 来创建动态数组

问题&#xff1a; 不能使用变量定义数组大小 原因&#xff1a; 这是因为数组在内存中是连续存储的&#xff0c;编译器需要在编译阶段就确定数组的大小&#xff0c;以便正确地分配内存空间。如果允许使用变量来定义数组的大小&#xff0c;那么编译器就无法在编译时确定数组的大…...

0x-3-Oracle 23 ai-sqlcl 25.1 集成安装-配置和优化

是不是受够了安装了oracle database之后sqlplus的简陋&#xff0c;无法删除无法上下翻页的苦恼。 可以安装readline和rlwrap插件的话&#xff0c;配置.bahs_profile后也能解决上下翻页这些&#xff0c;但是很多生产环境无法安装rpm包。 oracle提供了sqlcl免费许可&#xff0c…...

Monorepo架构: Nx Cloud 扩展能力与缓存加速

借助 Nx Cloud 实现项目协同与加速构建 1 &#xff09; 缓存工作原理分析 在了解了本地缓存和远程缓存之后&#xff0c;我们来探究缓存是如何工作的。以计算文件的哈希串为例&#xff0c;若后续运行任务时文件哈希串未变&#xff0c;系统会直接使用对应的输出和制品文件。 2 …...

客户案例 | 短视频点播企业海外视频加速与成本优化:MediaPackage+Cloudfront 技术重构实践

01技术背景与业务挑战 某短视频点播企业深耕国内用户市场&#xff0c;但其后台应用系统部署于东南亚印尼 IDC 机房。 随着业务规模扩大&#xff0c;传统架构已较难满足当前企业发展的需求&#xff0c;企业面临着三重挑战&#xff1a; ① 业务&#xff1a;国内用户访问海外服…...

webpack面试题

面试题&#xff1a;webpack介绍和简单使用 一、webpack&#xff08;模块化打包工具&#xff09;1. webpack是把项目当作一个整体&#xff0c;通过给定的一个主文件&#xff0c;webpack将从这个主文件开始找到你项目当中的所有依赖文件&#xff0c;使用loaders来处理它们&#x…...