First of all, instead of storing your coordinates in a vector, you would be better off using std::pair
or a custom class:
struct Point
{
int x;
int y;
};
Then you just need to have a way of generating random points, such as
Point randomPoint(Point const & min, Point const & max)
{
static std::mt19937 gen;
std::uniform_int_distribution<> distribX(min.x, max.x);
std::uniform_int_distribution<> distribY(min.y, max.y);
return Point{distribX(gen), distribY(gen)};
}
You can then use this generation function to fill your vector, for instance with generate_n
:
unsigned int const nbPoints = 100;
std::vector<Point> points;
std::generate_n(back_inserter(points), nbPoints,
std::bind(randomPoint, Point{0, 0}, Point{1000, 1000}));
Note that this will generate random points, so you are not guaranteed to end up with a square, a triangle, etc. If you want to generate a could, you could either use a non-uniform distribution (if you know what distribution your coordinates follow) to generate your numbers, or use rejection sampling to discard points that are not in the area you want them to be.
Generating a triangle boils down to drawing three random points.
To generate a square, you can draw two points, corresponding to two opposite corners of the square.
And so on... I don't think there is a "universal" solution that would work for any shapes.