-
Consider the following program, drawing_plane.cpp
(download it here):
#include <iostream>
#include <cmath> // for std::abs
const int PLANE_WIDTH = 11;
const int PLANE_HEIGHT = 10;
void point( int x1, int y1, char plane[PLANE_HEIGHT][PLANE_WIDTH] );
void tessellate( int x1, int y1, int x2, int y2, char plane[PLANE_HEIGHT][PLANE_WIDTH] );
void display( char plane[PLANE_HEIGHT][PLANE_WIDTH] );
void main()
{
static char plane[PLANE_HEIGHT][PLANE_WIDTH] = {
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n' }
};
int x1;
int y1;
int x2;
int y2;
for(;;) {
std::cout << "segment points: "; // prompt for input
std::cin >> x1 >> y1 >> x2 >> y2; // get segment coordinates
point( x1, y1, plane ); // draw first point
point( x2, y2, plane ); // draw second point
tessellate( x1, y1, x2, y2, plane ); // connect points
display( plane ); // display results
}
}
// Mark point on the plane
void point( int xx, int yy, char plane[PLANE_HEIGHT][PLANE_WIDTH] )
{
plane[yy][xx] = 'X';
}
// Connect two points
void tessellate( int x1, int y1, int x2, int y2, char plane[PLANE_HEIGHT][PLANE_WIDTH] )
{
if ( std::abs( x1 - x2 ) < 2 && std::abs( y1 - y2 ) < 2 )
// Nothing else left to do, two points are next to each other:
return;
// find center
int cx = ( x1 + x2 )/2;
int cy = ( y1 + y2 )/2;
// mark center
plane[cy][cx] = '*';
// recurse and repeat
tessellate( x1, y1, cx, cy, plane );
tessellate( x2, y2, cx, cy, plane );
}
// Print content of the plane array
void display( char plane[PLANE_HEIGHT][PLANE_WIDTH] )
{
for ( int yy = 0; yy < PLANE_HEIGHT; ++yy )
for ( int xx = 0; xx < PLANE_WIDTH; ++xx )
std::cout << plane[yy][xx];
}
-
The sample program creates a 2-dimensional array of characters named
plane[ ]. The array gets initialized by spaces and new
line characters. In the main loop, the user enters four integers, representing
coordinates of the segment to be drawn on the plane. The two endpoints are
drawn by calling function point( ). The endpoints are
connected by a call to tessellate( ) function. The display(
) call prints the current drawing on the screen. For example,
after entering 9 0 6 4 and 2 2 7 8,
the two segments will appear as
X
*
X *
* *
* X
*
*
*
X
-
Implementation of the sample program is rather straightforward. One exception
is tessellate( ) function, which calls itself recursively.
In each call the function calculates a mid-point of the given segment and marks
it on the drawing plane with an asterisk '*'. Since mid-point
divides the segment into two half-segments, the function calls itself two
more times, once for each half-segment. In this process, the half-segments
become smaller and smaller. Eventually, when the end-points are found right
next to each other, the recursion stops, and tessellate( ) returns.
The result of the tessellation is a mosaic of asterisk characters
connecting the endpoints of the segment.
|