/***********************************************************************\ * triangles2.d * * * * Triangle Challenge * * * * Second console version * * Enumerates the triangles * * * * by Stewart Gordon * * October 2004 * \***********************************************************************/ import std.stdio; import std.c.stdio; int n1, n2, numberCounted; void main() { char response = 'y'; while (response == 'y' || response == 'Y') { writef("Number of divisions along each edge: "); fflush(stdin); scanf("%d%d", &n1, &n2); while(kbhit()) getch(); int totalNumber = n1 * n2 * (n1 + n2) / 2; numberCounted = 0; writefln("Number of triangles: %d", totalNumber); // now enumerate them // left-vertex triangles for (int lowerEdge = 1; lowerEdge < n1; lowerEdge++) { for (int upperEdge = lowerEdge + 1; upperEdge <= n1; upperEdge++) { for (int len = 1; len <= n2; len++) { showTriangle(lowerEdge, upperEdge, 0, len); } } } // right-vertex triangles for (int lowerEdge = 1; lowerEdge < n2; lowerEdge++) { for (int upperEdge = lowerEdge + 1; upperEdge <= n2; upperEdge++) { for (int len = 1; len <= n1; len++) { showTriangle(0, len, lowerEdge, upperEdge); } } } // both-vertex triangles for (int axis1 = 1; axis1 <= n1; axis1++) { for (int axis2 = 1; axis2 <= n2; axis2++) { showTriangle(0, axis1, 0, axis2); } } assert (numberCounted == totalNumber); writef("Again? "); fflush(stdin); response = getchar(); } } void showTriangle(int lower1, int upper1, int lower2, int upper2) { writefln("Triangle %d: (%d..%d), (%d..%d)", ++numberCounted, lower1, upper1, lower2, upper2); // print each row of the triangle for (int row = 0; row < n1 + n2 - 1; row++) { // alignment spaces for (int col = 39; col > row; col--) putchar(' '); // print each element of the row for (int axis1 = n1 - 1, axis2 = n2 - 1 - row; axis1 >= 0; axis1--, axis2++) { if (axis2 < 0 || axis1 >= n1 || axis2 >= n2) { writef(" "); } else if (axis1 >= lower1 && axis1 < upper1 && axis2 >= lower2 && axis2 < upper2) { writef("# "); } else { writef(". "); } } writefln(); } getch(); }