• 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博客

     

  • 相关阅读:
    STL标准模板库(Standard Template Library)一周学习总结
    VB.Net 任务管理器相关操作
    小红书热点是什么,怎么找到热点话题!
    数据分析必备:6大步骤+5大类型+2大分析方法
    GitHub上架即下架!《分布式系统人人都是架构师》全彩笔记开源
    Verilog刷题[hdlbits] :Always case
    Bugku MISC easy_nbt & telnet
    报考PMP怕上当,都会遇到哪些坑,我该如何避开这些坑?
    【Kafka系列 06】Kafka Producer源码解析
    【机器学习】课程设计布置:某闯关类手游用户流失预测
  • 原文地址:https://blog.csdn.net/wangnaisheng/article/details/134071750