#include #include #include #include using namespace std; class Coord { public: Coord(int px, int py) : x(px), y(py) {} bool operator<(Coord c) const { return x < c.x || (x == c.x && y < c.y); } Coord operator*(int n) const { return Coord(x*n, y*n); } void center(Coord const& c); private: int x; int y; }; // Small utility function to calculate the number right between 'a' and 'b' int middle_number(int a, int b) { return (a + b) / 2; } // Move the current coordinate half distance toward 'c' void Coord::center(Coord const& c) { x = middle_number(x, c.x); y = middle_number(y, c.y); } int main() { int size; int count; cout << "Enter figure size: "; cin >> size; cout << "Enter step count: "; cin >> count; srand(time(nullptr)); Coord corner[3] = { Coord(0,0), Coord(0,1), Coord(1,0) }; set filled; Coord current(rand() % size, rand() % size); // Calculate the coordinates in the sierpinski set (approximate) for (int i = 0; i < count; ++i) { current.center(corner[rand() % 3]*size); filled.insert(current); } // Plot the coordinate to see the figure for (int r = 0; r < size; ++r) { for (int c = 0; c < size; ++c) { if ( filled.find({c,r}) != filled.end() ) cout << '/'; else cout << ' '; } cout << endl; } cout << endl; return 0; }