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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <chrono>
using namespace std;
struct Point{ double x, y; };
void getPoints( vector<Point> &pts );
void drawgraph( const vector<Point> &pts, int width, int height, bool autoscale, double xmin = 0.0, double xmax = 1.0, double ymin = 0.0, double ymax = 1.0 );
const int WIDTH = 80, HEIGHT = 40;
const double XMIN = 0.0, XMAX = 100.0;
const double YMIN = 0.0, YMAX = 50.0;
//======================================================================
int main()
{
vector<Point> pts;
getPoints( pts );
drawgraph( pts, WIDTH, HEIGHT, false, XMIN, XMAX, YMIN, YMAX );
}
//======================================================================
void getPoints( vector<Point> &pts )
{
const int NCIRCLES = 6;
const int NPTS = 100;
const double RADMIN = 3.0, RADMAX = 10.0;
const double TwoPi = 6.283185;
unsigned int seed( chrono::system_clock::now().time_since_epoch().count() );
mt19937 gen( seed );
uniform_real_distribution<double> dist( 0.0, 1.0 );
pts.clear();
for ( int c = 1; c <= NCIRCLES; c++ )
{
double x0 = XMIN + ( XMAX - XMIN ) * dist( gen );
double y0 = XMIN + ( YMAX - YMIN ) * dist( gen );
double R = RADMIN + ( RADMAX - RADMIN ) * dist( gen );
for ( int n = 0; n < NPTS; n++ )
{
double angle = TwoPi * n / NPTS;
double x = x0 + R * cos( angle );
double y = y0 + R * sin( angle );
pts.push_back( { x, y } );
}
}
}
//======================================================================
void drawgraph( const vector<Point> &pts, int width, int height, bool autoscale, double xmin, double xmax, double ymin, double ymax )
{
int i, j;
char MARK = '*';
if ( autoscale )
{
xmin = pts[0].x;
xmax = pts[0].x;
ymin = pts[0].y;
ymax = pts[0].y;
for ( Point p : pts )
{
if ( p.x < xmin ) xmin = p.x;
if ( p.x > xmax ) xmax = p.x;
if ( p.y < ymin ) ymin = p.y;
if ( p.y > ymax ) ymax = p.y;
}
}
vector<char> grid( width * height, ' ' );
// Assign points to grid
for ( Point p : pts )
{
double fx = ( p.x - xmin ) / ( xmax - xmin );
double fy = ( p.y - ymin ) / ( ymax - ymin );
i = fx * ( width - 1 ) + 0.5;
j = fy * ( height - 1 ) + 0.5;
if ( i >= 0 && i < width && j >= 0 && j < height ) grid[j*width+i] = MARK;
}
// Plot
for ( int j = height - 1; j >= 0; j-- )
{
for ( int i = 0; i < width; i++ ) cout << grid[j*width+i];
cout << endl;
}
}
|