C++: Check values of all object properties in a vector -
i think best explained pseudo-code:
std::vector<yes> objs; //the yes class constructor: yes(x,y) //let's imagine instantiated vector 1 object somewhere appropriately void insomefunction() { for(int = 0; < 20; ++i) { int randx = rand() % mapwidth; int randy = rand() % mapheight; for(int j = 0; j < objs.size(); ++j) { if(randx > x + threshold, objects in vector && randy > y + threshold, objects in vector) { objs.push_back(yes(randx,randy)); } } } }
so have window, dimensions mapwidth , mapheight , trying make 20 objects not overlap 1 in xy-plane.
i want make sure randx , randy not overlapping threshold distance away other existing objects. let's threshold = 20, want make sure randx , randy not contained circle of radius 20 surrounding any/all existing object(s) in vector.
example clarity: first yes object @ (x,y) = (10,20) , threshold = 20, want create second object, taking randx , randy parameters, , push vector; however, want make sure point (randx,randy) doesn't lie in circle of radius 20 , centered @ (10,20) coordinates of first object. program can either generate random (x,y) or generate randx , randy in way fit conditions want, need keep checking objects in vector create more objects.
i want know how accomplish this? more clarity, it's game. i'm trying generate multiple buildings in 2d map, don't want them overlap or close each other. how go accomplishing this?
i break down smaller functions.
this:
bool overlaps(const yes& thing, int x, int y) { // see if (x, y) overlaps 'thing' in whichever way appropriate. } bool overlaps_any(const std::vector<yes>& things, int x, int y) { (const yes& thing : things) { if (overlaps(thing, x, y)) { return true; } } return false; } void insomefunction(std::vector<yes>& objs) { for(int = 0; < 20; ++i) { int x = 0; int y = 0; { x = rand() % mapwidth; y = rand() % mapheight; } while (overlaps_any(objs, x, y)); objs.push_back(yes(x,y)); } }
there possibly more efficient ways, since generate map once, wouldn't worry efficiency @ moment.
Comments
Post a Comment