C++:检查矢量中所有对象属性的值

C++: Check values of all object properties in a vector

本文关键字:对象 属性 检查 C++      更新时间:2023-10-16

我认为最好用一些伪代码来解释:

std::vector<Yes> objs;
//The Yes Class constructor: Yes(x,y)
//Let's imagine I instantiated this vector with one object somewhere appropriately
void inSomeFunction()
{
   for(int i = 0; i < 20; ++i)
   {
      int randX = rand() % mapWidth;
      int randY = rand() % mapHeight;
      for(int j = 0; j < objs.size(); ++j)
      {
         if(randX > x + threshold, for all objects in my vector && randY > y + threshold, for all objects in my vector)
         {
            objs.push_back(Yes(randX,randY));
         }
      }
   }
}

所以我有一个窗口,它的尺寸是mapWidth和mapHeight,我基本上只是试图制作20个在xy平面中不相互重叠的对象。

我还想确保 randX 和 randY 不重叠,但与所有其他现有对象也有一定的阈值距离。因此,假设我的阈值 = 20,那么我想确保 randX 和 randY 不包含在半径为 20 的圆中,该圆围绕向量中的任何/所有现有对象。

为了清楚起见,第一个 Yes 对象位于 (x,y) = (10,20) 处,我的阈值 = 20,我想创建第二个对象,以 randX 和 randY 为参数,并将其推送到我的向量中;但是,我想确保点 (randX,randY) 不在半径为 20 的圆圈内,并以 (10,20) 为中心,这是我第一个对象的坐标。该程序可以生成另一个随机 (x,y),也可以只是生成 randX 和 randY 以适应我想要的条件,但是当我创建更多对象时,我需要它来继续检查向量中所有对象的情况。

我想知道如何做到这一点?同样为了更清楚起见,它是为了游戏。我正在尝试在 2D 地图中生成多个建筑物,但我显然不希望它们重叠或彼此靠近。我将如何实现这一目标?

我会把它分解成更小的函数。
像这样:

bool overlaps(const Yes& thing, int x, int y)
{
   // See if (x, y) overlaps 'thing' in whichever way is appropriate.
}
bool overlaps_any(const std::vector<Yes>& things, int x, int y)
{
   for (const Yes& thing : things)
   {
      if (overlaps(thing, x, y))
      {
         return true;
      }
   }
   return false;
}
void inSomeFunction(std::vector<Yes>& objs)
{
   for(int i = 0; i < 20; ++i)
   {
      int x = 0;
      int y = 0;
      do {
         x = rand() % mapWidth;
         y = rand() % mapHeight;
      } while (overlaps_any(objs, x, y));
      objs.push_back(Yes(x,y));
    }
}

可能有更有效的方法,但由于您只生成一次地图,因此我目前不会担心效率。

这是遍历向量的方法。您可以自己将元素与所需的值进行比较。

std::vector<Object> objs;
void inSomeFunction()
{
    for (int i = 0; i < 20; ++i)
    {
        int randX = rand() % mapWidth;
        int randY = rand() % mapHeight;
        for (std::vector<Object>::iterator itrObj = objs.begin(); itrObj != objs.end(); ++itrObj)
        {
            //Here you can access with operator -> the members of the class
            //itrObj->x
            //Or
            //(*itrObj).x
            //This is how you iterate through a std::vector<T>
            //
            //Do stuff with the values here
            //
            //If you do not wish to change the members
            //of the class Object stored in this vector and only compare them
            //use a const_iterator instead of iterator
        }
    }

这是一种检查它是否在范围内的方法:

struct Window
{
    Window (int x, int y, int width, int height)
        : x(x)
        , y(y)
        , width(width)
        , height(height)
    {}
    int x;
    int y;
    int width;
    int height;
};
struct Position
{
    Position (int x, int y)
        : x(x)
        , y(y)
    {}
    int x;
    int y;
};
bool IsWithinRange (const Window &Range, const Position &Check)
{
    int r_x         = Range.x,
        r_y         = Range.y,
        r_width     = Range.width,
        r_height    = Range.height,
        r_max_x     = r_width + r_x,
        r_max_y     = r_height + r_y;
    if (Check.x >= r_x && Check.x <= r_max_x)
        if (Check.y >= r_y && Check.y <= r_max_y)
            return true;
    return false;
}

我不确定您的 Object 类是如何构建的,但我相信您可以进行细微的调整。

我将根据我正在阅读的内容在这里四处走动。可能不是最好的方法,但它很简单。如果不完全了解您的项目,很难说。但是,如果您向我们提供完整的项目,我们会告诉您像您已经做的那样缩短时间。所以,我假设你最多会得到推测性的答案。

尽管如此,一个想法:

创建一个 max-x 和 max-y 的 2D 布尔数组。对于每个对象,通过将值设置为 true,它将占用该数组中的 x, y 空间。然后,您可以通过扫描距离给定随机值太远来检查某些内容是否太近,或者它是否已经在使用中。