- #1
John O' Meara
- 330
- 0
Code:
using namespace std;
enum direction {north, east, south, west };
direction right(direction d)
{ // return the direction to the right (clockwise)
switch(d) {
case north: return east;
case east: return south;
case south: return west;
case west: return north;
}
}
ostream& operator<<(ostream& c, direction d)
{ // write a direction
switch(d) {
case north: c << "north"; break;
case east: c << " east"; break;
case south: c << "south"; break;
case west: c << " west"; break;
}
}
void edge(direction d, int i);
direction left(direction d);
void Sierpinski(int i)
{ // Draw Sierpinski curve of order i
direction d = north;
do {
edge(d, i);
d = right(d);
}while(d != north);
}
direction left(direction d, int i)
{ // Return direction to the left (counterclockwise)
switch(d) {
case north: return west;
case east: return north;
case south: return east;
case west: return south;
}
}
void edge(direction d, int i)
{
if(i == -1)
cout << d << " 2 cm\n";
else {
edge(left(d), i-1);
edge(d, i-1);
edge(right(d), i-1);
}
}
int main()
{
Sierpinski(1);
return 0;
}