Selecting a circular region in 2D scatter plot
Apr 18, 2018 at 10:22pm UTC
I have a 2D scatter plot.
Now I want to select a circular region in this plot and use the data inside the circular region for further analysis.
Is there any way to do that in C++?
Thanks in advance
Apr 18, 2018 at 10:53pm UTC
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
#include <iostream>
#include <iomanip>
#include <vector>
#include <random>
using namespace std;
namespace Rnd {
random_device rd;
default_random_engine eng(rd());
// return random integer from 0 to high-1
int n(int high) { return eng() % high; }
// return random double from 0.0 to 1.0
double flt() { return eng() / (double )eng.max(); }
};
struct Point {
double x, y;
// Construct with x and y from -1.0 to 1.0.
Point() : x(Rnd::flt()*2-1), y(Rnd::flt()*2-1) {}
Point(double x, double y) : x(x), y(y) {}
};
ostream& operator <<(ostream& os, Point& p) {
return os << setw(8) << p.x << ' ' << setw(8) << p.y;
}
vector<Point> inCircle(vector<Point>& v, Point center, double radius) {
vector<Point> w;
radius *= radius;
for (auto o: v) {
double x = o.x - center.x;
double y = o.y - center.y;
if (x * x + y * y <= radius)
w.push_back(o);
}
return w;
}
int main() {
vector<Point> v;
for (int i = 0; i < 100; i++)
v.push_back(Point());
cout << fixed << setprecision(3);
for (auto o: v)
cout << o << '\n' ;
vector<Point> w = inCircle(v, Point(.5, .5), .5);
cout << "\n\n" ;
for (auto o: w)
cout << o << '\n' ;
}
Topic archived. No new replies allowed.