zoukankan      html  css  js  c++  java
  • C/C++求一个矩形不规则切片的切片区域坐标

    5 5 5 5 2
    5 5 5 5 2
    5 5 5 5 2
    3 3 3 3  

    如上图区所示,长 23 ,宽18,每个格子长宽均为 5,那么第一个小方框的的区域坐标为(0,0)(5,0)(0,5)(5,5)

    第一行最后一个小方块的坐标为(20,0)(22,0)(20,5)(22,5)

    第一列最后一个小方块的坐标为(0,15)(5,15)(0,18)(5,18)

    最右小角的小方块坐标为(20,15)(22,15)(20,18)(22,18)

    已知给出该大方块的 min_x = 0, min_y = 0, max_x = 22, max_y = 18, size = 5 ,数值都是以 double 类型给出。

    算出每个小方框的坐标

    代码如下:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    struct Point
    {
        double x;
        double y;
    };
    
    void print(Point *point);
    
    int main()
    {
        double min_x = 0;
        double min_y = 0;
        double max_x = 22;
        double max_y = 18;
    
        double size = 5;
    
        Point point[4];
    
        for (double i = min_y; i < max_y; i = min(max_y, i+size))
        {
            for (double j = min_x; j < max_x; j = min(max_x, j+size))
            {
                point[0].x = j;
                point[0].y = i;
    
                point[1].x = min(max_x, j+size);
                point[1].y = i;
    
                point[2].x = j;
                point[2].y = min(max_y, i+size);
    
                point[3].x = min(max_x, j+size);
                point[3].y = min(max_y, i+size);
    
                print(point);
            }
        }
    
        return 0;
    }
    
    void print(Point *point)
    {
        for (int i = 0; i < 4; i++)
        {
            printf("%f %f ", point[i].x, point[i].y);
        }
        printf("
    ");
    }

    运行结果:

    0.000000 0.000000 5.000000 0.000000 0.000000 5.000000 5.000000 5.000000
    5.000000 0.000000 10.000000 0.000000 5.000000 5.000000 10.000000 5.000000
    10.000000 0.000000 15.000000 0.000000 10.000000 5.000000 15.000000 5.000000
    15.000000 0.000000 20.000000 0.000000 15.000000 5.000000 20.000000 5.000000
    20.000000 0.000000 22.000000 0.000000 20.000000 5.000000 22.000000 5.000000
    0.000000 5.000000 5.000000 5.000000 0.000000 10.000000 5.000000 10.000000
    5.000000 5.000000 10.000000 5.000000 5.000000 10.000000 10.000000 10.000000
    10.000000 5.000000 15.000000 5.000000 10.000000 10.000000 15.000000 10.000000
    15.000000 5.000000 20.000000 5.000000 15.000000 10.000000 20.000000 10.000000
    20.000000 5.000000 22.000000 5.000000 20.000000 10.000000 22.000000 10.000000
    0.000000 10.000000 5.000000 10.000000 0.000000 15.000000 5.000000 15.000000
    5.000000 10.000000 10.000000 10.000000 5.000000 15.000000 10.000000 15.000000
    10.000000 10.000000 15.000000 10.000000 10.000000 15.000000 15.000000 15.000000
    15.000000 10.000000 20.000000 10.000000 15.000000 15.000000 20.000000 15.000000
    20.000000 10.000000 22.000000 10.000000 20.000000 15.000000 22.000000 15.000000
    0.000000 15.000000 5.000000 15.000000 0.000000 18.000000 5.000000 18.000000
    5.000000 15.000000 10.000000 15.000000 5.000000 18.000000 10.000000 18.000000
    10.000000 15.000000 15.000000 15.000000 10.000000 18.000000 15.000000 18.000000
    15.000000 15.000000 20.000000 15.000000 15.000000 18.000000 20.000000 18.000000
    20.000000 15.000000 22.000000 15.000000 20.000000 18.000000 22.000000 18.000000

  • 相关阅读:
    关于 OpenSmtp 邮件标题过长后出现乱码问题的解决
    用于解析 sohu 新闻页面的 XSLT 文件
    CEGUI 0.7x实现下划线描边图文混排等效果
    Hash算法说明
    D3DXMatrixShadow 函数
    DLL动态链接库和LIB静态链接库之程序员经验分析
    printf格式控制符的完整格式(转载)
    深入说明HDR技术
    Irrlicht不定期分析
    8.3实例程序:平面阴影
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/13019611.html
Copyright © 2011-2022 走看看