首页 > 编程语言 >C#将矩形平铺在大的矩形内,支持旋转调整

C#将矩形平铺在大的矩形内,支持旋转调整

时间:2023-06-05 17:34:02浏览次数:42  
标签:平铺 container C# double Height Width int 矩形 rect

public static List<Rect> TileRects(Size rect, Size container)
{
    List<Rect> result = new List<Rect>();

    if (rect.Width > container.Width && rect.Width > container.Height
        || rect.Height > container.Height && rect.Height > container.Width)
        return null;
    int angle = 0;
    int count = 0;
    if(container.Width >= rect.Width && container.Height >= rect.Height)
    {
        int h = (int)Math.Floor(container.Width / rect.Width);
        int v = (int)Math.Floor(container.Height / rect.Height);
        count = h * v;
        for (int i = 0; i < v; i++)
        {
            for (int j = 0; j < h; j++)
            {
                result.Add(new Rect(j * rect.Width, i * rect.Height, rect.Width, rect.Height, angle));
            }
        }

        //如果有剩余区域可以填充旋转后的rect,
        //右侧区域填充是否占用底部区域
        bool coverBottom = false;
        //填充右侧剩余区域
        if (container.Width % rect.Width >= rect.Height)
        {
            var l = TileRects(new Size(rect.Width, rect.Height), new Size(container.Width % rect.Width, container.Height));
            if (l != null)
            {
                double left = h * rect.Width;
                l.ForEach(f => result.Add(new Rect(left, f.Top, f.Width, f.Height, f.Angle)));

                if (container.Height % rect.Width > 0)
                    coverBottom = true;
            }
        }

        //填充底部剩余区域
        if (container.Height % rect.Height >= rect.Width)
        {
            var l = TileRects(new Size(rect.Width, rect.Height), new Size(coverBottom ? container.Width -  rect.Height : container.Width, container.Height % rect.Height));
            double top = v * rect.Height;
            l?.ForEach(f => result.Add(new Rect(f.Left, top, f.Width, f.Height, f.Angle)));
        }
    }
    else
    {
        angle = rect.Width > container.Width ? -90 : 90;
        int h = (int)Math.Floor(container.Width / rect.Height);
        int v = (int)Math.Floor(container.Height / rect.Width);
        count = h * v;
        for (int i = 0; i < v; i++)
        {
            for (int j = 0; j < h; j++)
            {
                result.Add(new Rect(j * rect.Height, i * rect.Width, rect.Height, rect.Width, angle));
            }
        }
    }

    return result;
}
public struct Size
{
    public double Width { get; set; }
    public double Height { get; set; }
    public Size(double w = 0, double h = 0)
    {
        Width = w;
        Height = h;
    }
}
public struct Rect
{
    public double Left { get; set; }
    public double Top { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
    public int Angle { get; set; }
    public Rect(double w = 0, double h = 0) : this(0, 0, w, h) { }
    public Rect(double l, double t, double w, double h, int a = 0)
    {
        Left = l;
        Top = t;
        Width = w;
        Height = h;
        Angle = a;
    }
}

 

标签:平铺,container,C#,double,Height,Width,int,矩形,rect
From: https://www.cnblogs.com/RedSky/p/17458512.html

相关文章

  • centos8在更新yum源时提示失败 错误:为 repo ‘AppStream’下载元数据失败
    https://blog.csdn.net/Remberthename/article/details/124070710 centos8在更新yum源时提示失败错误:为repo'AppStream’下载元数据失败解决方法:原因:因原有系统中使用的镜像访问地址在国内访问过慢导致解决方案:1、将目录切换到/etc/yum.repos.d目录下2、修改CentOS-......
  • 2023-06-05 hexo 分页图标不显示,显示的是【<i class="iconfont icon-arrow-left"></i>
    问题描述:如题。注意:我使用的主题为next。解决方案:全局搜索:navclass="pagination"或者找到路径:你的博客\themes\hexo-theme-next\layout\_partials里的【pagination.swig】文件,对其进行修改;修改前:{%ifpage.prevorpage.next%}<navclass="pagination">{{p......
  • GDPU C语言 天码行空11
    ......
  • -bash: jps: command not found
    Linux安装openJDK找不到jps 解决办法 执行安装java-1.8.0-openjdk-develyuminstalljava-1.8.0-openjdk-devel ......
  • CATIA-CATIA V5-6R2017 WIN10 64位版本安装+许可证的安装配置(CATIA启动时必须要调用许
    CATIAV5-6R2017WIN1064位安装步骤:1.先使用“百度网盘客户端”下载CATIAV5-6R2017软件安装包到电脑磁盘英文路径文件夹下,并解压缩,安装前先断开电脑网络,然后双击打开CATIAV5R2017文件夹,找到setup.exe,鼠标右击选择【以管理员身份运行】2.正在准备安装中,稍等片刻自动进入安......
  • [AGC050F] NAND Tree
    求一个计数方案奇偶性的题考虑套路的交换两个元素。考虑最开始选的两条边,如果它们没有交,那么互换顺序之后结果不变。我们只需要统计相交的情况即可。再考虑边相邻的情况。对于y---x---z,按两种顺序缩边的结果分别为\(\operatorname{NAND}(\operatorname{NAND}(y,x),z)\)和\(\op......
  • GDPU C语言 天码行空12
    ......
  • Visible Lattice Points 题解
    VisibleLatticePoints题目大意给定一个\(N×N×N\)的由若干点组成的立方体,点的坐标从\((0,0,0)\)到\((N,N,N)\),求从点\((0,0,0)\)处可以看见多少个点。思路分析我们将立方体上的点分成三类:在\(xy,yz,xz\)平面上的点。在\(x,y,z\)轴上的点。即不在平面,也不在......
  • 近万条儿童科普知识大全ACCESS\EXCEL数据库
    仍然从昨天采集的儿童教育网站中采集了上万条的儿童科普知识,因为其分类和内容都比较符合儿童(很多大人也都不知道哦),对儿童的教育、对儿童的知识面会起到很好的作用,用处也广只要是儿童教育方面的都可以使用。分类情况统计:暴风雪里的夏天(25)、地球家园(211)、动物(314)、海洋技术(41)、健......
  • DRTREE - Dynamically-Rooted Tree 题解
    DRTREE-Dynamically-RootedTree本题建议评蓝。思路:题目就是要对一颗不定根树求子树权值和。这题不带修,如果带修难度会增加一点,就跟遥远的国度差不多。首先分析一下在以不同根下子树的变化。当一颗树以1号节点为根时,比如说长这样:假设每个点的权值为1,那么这8个点......