1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = 4.0 * atan( 1.0 );
//======================================================================
void point( vector<string> &canvas, int i, int j )
{
const int N = canvas.size() - 1;
if ( i >= 0 && i <= N && j >= 0 && j <= N ) canvas[i][j] = '*';
}
//======================================================================
void line( vector<string> &canvas, int i1, int j1, int i2, int j2 )
{
if ( i1 == i2 && j1 == j2 )
{
point( canvas, i1, j1 );
return;
}
if ( abs( i2 - i1 ) >= abs( j2 - j1 ) )
{
if ( i2 < i1 ) { swap( i1, i2 ); swap( j1, j2 ); }
double slope = (double)( j2 - j1 ) / ( i2 - i1 );
for ( int i = i1; i <= i2; i++ )
{
int j = j1 + round( slope * ( i - i1 ) );
point( canvas, i, j );
}
}
else
{
if ( j2 < j1 ) { swap( j1, j2 ); swap( i1, i2 ); }
double slope = (double)( i2 - i1 ) / ( j2 - j1 );
for ( int j = j1; j <= j2; j++ )
{
int i = i1 + round( slope * ( j - j1 ) );
point( canvas, i, j );
}
}
}
//======================================================================
int main()
{
const int N = 80;
vector<string> canvas( N + 1, string( N + 1, ' ' ) );
int xc, yc, r;
xc = yc = r = N / 2;
for ( int deg = 0; deg < 360; deg += 15 )
{
double rad = deg * PI / 180.0;
int x = xc + r * cos( rad ) + 0.5;
int y = yc + r * sin( rad ) + 0.5;
line( canvas, N - yc, xc, N - y, x );
}
for ( string s : canvas ) cout << s << '\n';
}
//======================================================================
|