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


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

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

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

    1. using System.Drawing;
    2. using System.Drawing.Drawing2D;
    3. using System.Drawing.Imaging;
    4. public class Program
    5. {
    6. public static void Main()
    7. {
    8. // 假设你已经有了两个Rectangle,rect1和rect2
    9. Rectangle rect1 = new Rectangle(new Point(10, 10), new Size(50, 50));
    10. Rectangle rect2 = new Rectangle(new Point(20, 20), new Size(40, 40));
    11. // 创建两个 GraphicsPath 对象
    12. GraphicsPath path1 = new GraphicsPath();
    13. GraphicsPath path2 = new GraphicsPath();
    14. // 向第一个 GraphicsPath 对象添加较大矩形
    15. path1.AddRectangle(rect1);
    16. // 向第二个 GraphicsPath 对象添加较小矩形
    17. path2.AddRectangle(rect2);
    18. // 创建两个 Region 对象,一个用于第一个 GraphicsPath,一个用于第二个 GraphicsPath
    19. Region region1 = new Region(path1);
    20. Region region2 = new Region(path2);
    21. // 使用 Exclude 方法计算两个 Region 对象的差集
    22. region1.Exclude(region2);
    23. // 创建一个Brush对象来填充非重叠部分的颜色
    24. SolidBrush m_Brush = new SolidBrush(Color.Red);
    25. // 使用Graphics.FillPath方法填充非重叠部分
    26. using (Graphics g = this.CreateGraphics())
    27. {
    28. g.FillRegion(m_Brush, region1);
    29. }
    30. }
    31. }

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

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

    1. using System.Drawing;
    2. using System.Drawing.Drawing2D;
    3. using System.Drawing.Imaging;
    4. public class Program
    5. {
    6. public static void Main()
    7. {
    8. // 假设你已经有了两个Rectangle,rect1和rect2
    9. Rectangle rect1 = new Rectangle(new Point(10, 10), new Size(50, 50));
    10. Rectangle rect2 = new Rectangle(new Point(20, 20), new Size(40, 40));
    11. // 创建两个 GraphicsPath 对象
    12. GraphicsPath path1 = new GraphicsPath();
    13. GraphicsPath path2 = new GraphicsPath();
    14. // 向第一个 GraphicsPath 对象添加较大矩形
    15. path1.AddRectangle(rect1);
    16. // 向第二个 GraphicsPath 对象添加较小矩形
    17. path2.AddRectangle(rect2);
    18. // 创建两个 Region 对象,一个用于第一个 GraphicsPath,一个用于第二个 GraphicsPath
    19. Region region1 = new Region(path1);
    20. Region region2 = new Region(path2);
    21. // 使用 Intersect 方法计算两个 Region 对象的差集
    22. region1.Intersect(region2);
    23. // 创建一个Brush对象来填充非重叠部分的颜色
    24. SolidBrush m_Brush = new SolidBrush(Color.Red);
    25. // 使用Graphics.FillPath方法填充非重叠部分
    26. using (Graphics g = this.CreateGraphics())
    27. {
    28. g.FillRegion(m_Brush, region1);
    29. }
    30. }
    31. }

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

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

     

  • 相关阅读:
    Elasticsearch 从入门到实践
    How to install ansible in rhel 8.7【红帽8.7 安装 ansible】
    新手小白怎样开始学做自媒体呢?
    华清 c++ day3 9月10
    node.js - http、模块化、npm
    链表面试题:链表的回文结构+链表分割+相交链表+环形链表(思路+图文+代码详解)
    Java基础面试题突击系列5
    S7-200SMART PLC Modbus TCP通信(多服务器多从站轮询)
    虹科分享 | 网络流量监控 | 使用 ntopng 收件人和端点进行灵活的警报处理
    一张图让你牢记MySQL主从复制原理|原创
  • 原文地址:https://blog.csdn.net/wangnaisheng/article/details/134071750