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

C# 找出两个Rectangle或是矩形的相互重合与非重合部分?

一、找出两个Rectangle或是矩形的相互重合与非重合部分?

示例代码1,求非重合部分:

使用GraphicsPath获取到非重合的路径,然后使用FillPath填充非重合部分Brush颜色。

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;public class Program
{public static void Main(){// 假设你已经有了两个Rectangle,rect1和rect2Rectangle rect1 = new Rectangle(new Point(10, 10), new Size(50, 50));Rectangle rect2 = new Rectangle(new Point(20, 20), new Size(40, 40));// 创建两个 GraphicsPath 对象GraphicsPath path1 = new GraphicsPath();GraphicsPath path2 = new GraphicsPath();// 向第一个 GraphicsPath 对象添加较大矩形path1.AddRectangle(rect1);// 向第二个 GraphicsPath 对象添加较小矩形path2.AddRectangle(rect2);// 创建两个 Region 对象,一个用于第一个 GraphicsPath,一个用于第二个 GraphicsPathRegion region1 = new Region(path1);Region region2 = new Region(path2);// 使用 Exclude 方法计算两个 Region 对象的差集region1.Exclude(region2);// 创建一个Brush对象来填充非重叠部分的颜色SolidBrush m_Brush = new SolidBrush(Color.Red);// 使用Graphics.FillPath方法填充非重叠部分using (Graphics g = this.CreateGraphics()){g.FillRegion(m_Brush, region1);}}
}

示例代码2,求重合部分::

使用GraphicsPath获取到重合的路径,然后使用FillPath填充重合部分Brush颜色。

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;public class Program
{public static void Main(){// 假设你已经有了两个Rectangle,rect1和rect2Rectangle rect1 = new Rectangle(new Point(10, 10), new Size(50, 50));Rectangle rect2 = new Rectangle(new Point(20, 20), new Size(40, 40));// 创建两个 GraphicsPath 对象GraphicsPath path1 = new GraphicsPath();GraphicsPath path2 = new GraphicsPath();// 向第一个 GraphicsPath 对象添加较大矩形path1.AddRectangle(rect1);// 向第二个 GraphicsPath 对象添加较小矩形path2.AddRectangle(rect2);// 创建两个 Region 对象,一个用于第一个 GraphicsPath,一个用于第二个 GraphicsPathRegion region1 = new Region(path1);Region region2 = new Region(path2);// 使用 Intersect 方法计算两个 Region 对象的差集region1.Intersect(region2);// 创建一个Brush对象来填充非重叠部分的颜色SolidBrush m_Brush = new SolidBrush(Color.Red);// 使用Graphics.FillPath方法填充非重叠部分using (Graphics g = this.CreateGraphics()){g.FillRegion(m_Brush, region1);}}
}

二、在矩形内获取一个指定大小的矩形(两个矩形的中心点是重合的)

C# 在矩形内获取一个指定大小的矩形(两个矩形的中心点是重合的)-CSDN博客

 

相关文章:

  • 【C语言】常见的动态内存管理错误
  • AI Agent
  • 【Web】get请求和post请求的区别
  • fork创建子进程及僵尸进程的产生及规避
  • 百度交出2023年业绩答卷:全力提速AI布局,注入业绩增长新动能
  • React withRouter的使用及源码实现
  • AVL 树
  • Rocketmq java hello world 入门案例
  • LaMa Image Inpainting 图像修复 Onnx Demo
  • 贪心算法(算法竞赛、蓝桥杯)--修理牛棚
  • jmeter接口测试
  • Docker 第十九章 : 阿里云个人镜像仓使用
  • FPGA之带有进位逻辑的加法运算
  • docker单机启动mysql、redis容器命令
  • 2023中国PostgreSQL数据库生态大会:洞察前沿趋势,探索无限可能(附核心PPT资料下载)
  • 【翻译】babel对TC39装饰器草案的实现
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 77. Combinations
  • Android开源项目规范总结
  • javascript面向对象之创建对象
  • JavaScript中的对象个人分享
  • leetcode讲解--894. All Possible Full Binary Trees
  • Linux下的乱码问题
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • Selenium实战教程系列(二)---元素定位
  • SpringCloud集成分布式事务LCN (一)
  • vue自定义指令实现v-tap插件
  • webpack项目中使用grunt监听文件变动自动打包编译
  • windows-nginx-https-本地配置
  • 第十八天-企业应用架构模式-基本模式
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 原生js练习题---第五课
  • FaaS 的简单实践
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • ()、[]、{}、(())、[[]]命令替换
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (2020)Java后端开发----(面试题和笔试题)
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (原)本想说脏话,奈何已放下
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .apk 成为历史!
  • .net 4.0发布后不能正常显示图片问题
  • .NET Framework 服务实现监控可观测性最佳实践
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET处理HTTP请求
  • @Autowired @Resource @Qualifier的区别
  • @RequestMapping-占位符映射
  • [2018][note]用于超快偏振开关和动态光束分裂的all-optical有源THz超表——
  • [AX]AX2012 SSRS报表Drill through action
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)