请给我一些关于如何评估覆盖所有给定区域的最大区域的建议?

发布于 2024-12-07 02:07:22 字数 544 浏览 0 评论 0原文

假设我有几十个地理区域,可以通过使用以下 c/c++ 结构来定义:

typedef struct tagGEOGRAPHIC_REGION
{    
    float fNorthMost;
    float fSouthMost;
    float fWestMost;
    float fEastMost;
} GEOGRAPHIC_REGION, *PGEOGRAPHIC_REGION;

现在我想要获得最大区域,它将覆盖所有给定区域。函数模板可能如下所示:

const GEOGRAPHIC_REGION& GetMaxRegion(const std:vector<GEOGRAPHIC_REGION>& vRegions)
{  
    ......
}

我可以将 GEOGRAPHIC_REGION 结构体的四个组成部分放入 4 个不同的浮点向量中,然后计算它们各自的最大值。最后,可以将四个最大值组合起来形成最大值区域。我认为这一定是一个简单的方法。您能给我一些建议吗?非常感谢!

Suppose I have dozens of geographic regions, which can be defined through the use of the following c/c++ structure:

typedef struct tagGEOGRAPHIC_REGION
{    
    float fNorthMost;
    float fSouthMost;
    float fWestMost;
    float fEastMost;
} GEOGRAPHIC_REGION, *PGEOGRAPHIC_REGION;

And I now want to get the maximum region, which will cover all given regions. The function template may look like the following:

const GEOGRAPHIC_REGION& GetMaxRegion(const std:vector<GEOGRAPHIC_REGION>& vRegions)
{  
    ......
}

I can put the four components of GEOGRAPHIC_REGION struct into 4 different float vectors, and then evaluate their respective maximum values. Finally, the four maxium values can be combined to form the maximum region. I think it must be a simple way to do that. Would you please give me some advice? Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-14 02:07:22

难道你不能只迭代向量,并仅用一个循环获得最大北、最小南等吗?

Can't you just iterate over the vector, and get the maximum north, minimum south, etc, with just one loop?

回忆凄美了谁 2024-12-14 02:07:22

我建议以这样的方式作为起点:

  • 实时观看:http://ideone.com/LQk8U
  • 请注意,我可能猜错了你的轴方向(在这种情况下,你可能需要交换一些最小/最大) 编辑根据评论修复“北是正,东为正”

  • 对于向量,只需执行 std::acummulate(v.begin(), v.end(), v [0]....)

如果您的场景变得更复杂,请参阅 Boost 几何库

#include <iostream>
#include <numeric>

struct GEOGRAPHIC_REGION
{    
    float fNorthMost;
    float fSouthMost;
    float fWestMost;
    float fEastMost;
};

GEOGRAPHIC_REGION combine(const GEOGRAPHIC_REGION& accum, const GEOGRAPHIC_REGION& tocombine)
{
    GEOGRAPHIC_REGION combined = { 
        std::max(accum.fNorthMost, tocombine.fNorthMost),
        std::min(accum.fSouthMost, tocombine.fSouthMost),
        std::min(accum.fWestMost,  tocombine.fWestMost),
        std::max(accum.fEastMost,  tocombine.fEastMost)
    };
    return combined;
}

int main()
{
    const GEOGRAPHIC_REGION regions[] = 
    {
        { 2,-1,-1,1 },
        { 1,-2,-1,1 },
        { 1,-1,-2,1 },
        { 1,-1,-1,2 },
    };

    GEOGRAPHIC_REGION super = std::accumulate(regions, regions+4, regions[0], combine);

    std::cout << "{ " << super.fNorthMost << ", " 
                      << super.fSouthMost << ", "
                      << super.fWestMost << ", "
                      << super.fEastMost << " }" << std::endl;
}

I suggest something like this as a starting point:

  • see it live: http://ideone.com/LQk8U
  • note that I might have your axis direction guessed wrong (you might need to swap some min/max in that case) Edit fixed according to comment "North is positive, and East is positive"

  • for a vector, just do std::acummulate(v.begin(), v.end(), v[0]....)

In case your scenarios get more complicated, see the Boost Geometry Library

.

#include <iostream>
#include <numeric>

struct GEOGRAPHIC_REGION
{    
    float fNorthMost;
    float fSouthMost;
    float fWestMost;
    float fEastMost;
};

GEOGRAPHIC_REGION combine(const GEOGRAPHIC_REGION& accum, const GEOGRAPHIC_REGION& tocombine)
{
    GEOGRAPHIC_REGION combined = { 
        std::max(accum.fNorthMost, tocombine.fNorthMost),
        std::min(accum.fSouthMost, tocombine.fSouthMost),
        std::min(accum.fWestMost,  tocombine.fWestMost),
        std::max(accum.fEastMost,  tocombine.fEastMost)
    };
    return combined;
}

int main()
{
    const GEOGRAPHIC_REGION regions[] = 
    {
        { 2,-1,-1,1 },
        { 1,-2,-1,1 },
        { 1,-1,-2,1 },
        { 1,-1,-1,2 },
    };

    GEOGRAPHIC_REGION super = std::accumulate(regions, regions+4, regions[0], combine);

    std::cout << "{ " << super.fNorthMost << ", " 
                      << super.fSouthMost << ", "
                      << super.fWestMost << ", "
                      << super.fEastMost << " }" << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文