Lets say I have two AABB based areas, each area defined by two coordinates mins{x, y} and maxs{x, y}, I want to find the middle connection point between them.
Since my english is not good, I can't explain all with my words,
see the following picture for easier understanding:
http://i.*.com/WokivEe.png
All I need to find is the red point coordinates.
so If we move this into programming question, actual data structures would look like this:
struct Vec2D {
float x, y;
}
struct Rectangle {
Vec2D min;
Vec2D max;
}
Rectangle obj[2]
Anyone got an idea for an algorithm?
Along either the X or Y axis, sort the coordinates of the sides that touch into order. Then average the 2nd and 3rd ones in that list to find their midpoint. I hope this answers the question sufficiently.
Here is a little algorithm that first find which sides of the objects are closest, and then uses the 4 points along the common side to make a list, sorted along the common axis. The average of the 2 middle points of the sorted list are the answer. This will work for both horizontal and vertical sides. I added accessor functions to the data structures so that they can be indexed; e.g., for a Vec2D, coordinate(0) is the x value and coordinate(1) is the y value.
#include <math.h>
#include <iostream>
#include <limits>
struct Vec2D {
float x, y;
float coordinate(int axis)
{
return (axis & 1) ? y : x;
}
};
struct Rectangle {
Vec2D min;
Vec2D max;
Vec2D corner(int j)
{
return (j & 1) ? max : min;
}
// Get the other corner along the given axis
Vec2D along(int j, int ax)
{
Vec2D p = corner(j);
if (0 == ax)
{
p.x = corner(1-j).x;
}
else
{
p.y = corner(1-j).y;
}
return p;
}
};
using namespace std;
inline Vec2D* vp(const void* p)
{
return (Vec2D*) p;
}
static int compare_x(const void*a, const void*b)
{
if (vp(a)->x < vp(b)->x)
{
return -1;
}
else
if (vp(a)->x > vp(b)->x)
{
return 1;
}
return 0;
}
static int compare_y(const void*a, const void*b)
{
if (vp(a)->y < vp(b)->y)
{
return -1;
}
else
if (vp(a)->y > vp(b)->y)
{
return 1;
}
return 0;
}
int main(void) {
int ax; // axis index
int c0, c1;
float gap = numeric_limits<float>::max();
struct Rectangle obj[2] = {0,2,10,10,10,5,15,20};
struct
{
int ax,c0,c1;
} closest;
// Find out which sides are the closest to each other
for(ax = 0; 2 > ax; ++ax) // Look at x axis and y axis
{
for(c0 = 0; 2 > c0; ++c0) // Look at both corners of obj[0]
{
for(c1 = 0; 2 > c1; ++c1) // Look at both corners of obj[1]
{
float dist = fabs(obj[0].corner(c0).coordinate(ax) - obj[1].corner(c1).coordinate(ax));
if (dist < gap)
{
gap = dist;
closest.ax = ax;
closest.c0 = c0;
closest.c1 = c1;
}
}
}
}
int other = 1 - closest.ax; // The other axis
cout << "The closest gap is along the " << (closest.ax ? 'y' : 'x') << " axis\n";
cout << "The common side is along the " << (other ? 'y' : 'x') << " direction\n";
// Make a list of the 4 points along the common side
Vec2D list[4];
list[0] = obj[0].corner(closest.c0);
list[1] = obj[0].along(closest.c0, other);
list[2] = obj[1].corner(closest.c1);
list[3] = obj[1].along(closest.c1, other);
// Sort them into order along the common axis
qsort(list, 4, sizeof(Vec2D), closest.ax ? compare_x : compare_y);
// Get the average of the 2 middle points along the common axis.
Vec2D answer = {
(list[1].x + list[2].x) / 2,
(list[1].y + list[2].y) / 2
};
cout << "(" << answer.x << "," << answer.y << ")\n";
}
Related
Looking for a C++ solution.
I had thoughts whether to ask this question over here or MathExchange etc. but since it's more of a programming based question thus posting here.
Real problem:
I have an XML with field as:
<part name='01' x='351' y='151'/>
Where x and y, I am storing as QPointf object. Also, I require the name=01 value which I am mapping together with the QPointf object to create a map.
Now there are certain operations that I need to do on this map :
First I need to get all the points(QPointf) and draw over an image.
Second, I will modify the x and y value of certain points getting the points from the GUI.
When I get the points from GUI, I need to check x and y value in each Qpointf inside map.
Problem in simpler form:
I am looking for a data structure such that instead of having a map of key and QPointf, it makes parsing and looking for x and y value of points(QPointf) easier. Just that the QPointf and key should form a unique pair with each other, so that parsing through the entire list of points to find certain (x,y) and modifying it is faster and even when x and y value of QPointf is modified the key attached to it is same.
PS: I hope I was clear with the problem, if there is anything unclear then please edit/comment so that the question can be improved.
My guess is here that your most important aspect is finding a certain set of x and y points when a user uses the UI. There are many acceleration structures possible, but I would probably recommend a point index grid. That is, you partition the indices of points into 2D buckets. When a user chooses a point in the UI, you can do a quick look-up on what bucket the point is in, and you can then iterate over only the points present in that bucket to find the actual point.
As for your data, I would store it in an array:
struct NamePoint {
int name, x, y;
};
std::vector<NamePoint> points;
Now you would create a point index grid that refers to the points array. Implementing one yourself might be worthwhile, but otherwise I know that there exists an OpenVDB version that works.
I made a small dirty implementation so you can see the principle. I have no checks for inputs, so if you're not careful you will access out of the bounds of the vector (e.g., calling pointIndexGrid.indicesForPoint(5, 5) gives segmentation fault).
#include <iostream>
#include <vector>
#include <limits>
struct NamePoint {
int name, x, y;
};
template <typename T> // Just so any array type can work
struct PointIndexGrid {
using ArrayType = T;
using size_type = typename ArrayType::size_type;
PointIndexGrid(const ArrayType& points, int gridSize)
: mGridSize(gridSize)
{
// Find the domain. We will create a 2D vector which will all contain another vector with indices.
maxX = maxY = std::numeric_limits<int>::min();
minX = minY = std::numeric_limits<int>::max();
for (const auto& p : points) {
maxX = p.x > maxX ? p.x : maxX;
maxY = p.y > maxY ? p.y : maxY;
minX = p.x < minX ? p.x : minX;
minY = p.x < minY ? p.x : minY;
}
// create buckets
int nbrXBuckets = (maxX - minX)/mGridSize + 1; // Due to integer arithmetics we round down -- lets add one extra just in case
int nbrYBuckets = (maxY - minY)/mGridSize + 1;
for (int n = 0; n < nbrXBuckets; ++n) {
mBuckets.emplace_back(std::vector<std::vector<size_type>>(nbrYBuckets));
}
// Partition points
for (size_type i = 0; i < points.size(); ++i) {
int xBucket = (points[i].x - minX)/mGridSize; // this is the method how to easily calculate the bucket. Pure arithmetics -- goes fast
int yBucket = (points[i].y - minY)/mGridSize;
mBuckets[xBucket][yBucket].emplace_back(i);
}
}
std::vector<size_type> indicesForPoint(int x, int y)
{
int xBucket = (x - minX)/mGridSize; // Same as above
int yBucket = (y - minY)/mGridSize;
return mBuckets[xBucket][yBucket];
}
private:
int mGridSize;
int maxX, minX;
int maxY, minY;
std::vector<std::vector<std::vector<size_type>>> mBuckets;
};
int main() {
std::vector<NamePoint> points;
points.emplace_back(NamePoint{1, 1, 1});
points.emplace_back(NamePoint{2, 1, 2});
points.emplace_back(NamePoint{3, 1, 2});
points.emplace_back(NamePoint{4, 2, 2});
points.emplace_back(NamePoint{5, 3, 3});
PointIndexGrid<std::vector<NamePoint>> pointIndexGrid(points, 2);
std::cout << "Indices for (1, 1): " << std::endl;
for (const auto& i : pointIndexGrid.indicesForPoint(1, 1)) {
std::cout << " " << i << std::endl;
}
std::cout << "Indices for (3, 3): " << std::endl;
for (const auto& i : pointIndexGrid.indicesForPoint(3, 3)) {
std::cout << " " << i << std::endl;
}
}
This prints out:
Indices for (1, 1):
0
1
2
3
Indices for (3, 3):
4
So to find a point at a specific (x, y):
Partition all points using the PointIndexGrid.
Use pointIndexGrid.indicesForPoint(x, y).
Iterate through all indices there (and look up the points in the points-array).
Grab the point that you want.
So if I understood correctly you just want an easy way to store your points data.
This isn't the best in terms of performance but if you are not planing on changing it every frame it will work no problem:
#include <map>
typedef std::map<int, std::pair<float, float>> PointContainer;
QPointf* fromPointContainer(PointContainer points)
{
QPointf array = new QPointf[points.size()];
for (size_t i; i < points.size(); i++)
array[i] = QPointf(points[i].first, points[i].second);
return array;
}
int main() {
PointContainer points;
points[0] = { 1.6f/*x*/, 5.8f/*y*/ };
QPointf* array = fromPointContainer(points);
//render here
delete array;//don't forget to delete the array allocated with new
return 0;
}
This question already has an answer here:
Overloaded Addition assignment operator in C++ for two /more than two objects?
(1 answer)
Closed 5 years ago.
Trying to develop operator == to compare two balls where two balls are considered equal if they have the same radius and operator > to compare two balls. To see if one ball has a bigger radius than another one, for let's say ball x is > than another ball y. += to add the volume of the right-side-operand to the volume of the left-side-operand. It is like to melt two metal balls to make one metal ball. The new ball's radius is cube root of (r1^3 + r2^3). Wish to use pow() function to calculate the cube value and cube root value. operator + to add the two balls together and return a new ball. The size of the new ball is the sum of the size of the two operands connected by the +.
In the main() function, couldn't add ball m(10) with ball n(20) to create another ball d, like d = m+n.
int main()
{
//use ball
ball x; float re;
//radius of ball y is set to 10
ball y(10);
//asks for radius of x?
cout << "Enter radius for ball x: ";
cin >> re;
//sets the radius of x
x.set_radius(re);
ball m(10);
ball n(20);
ball d;
d = m + n;
//cout << "The radius of ball d is " << m.;
system("pause");
return 0;
}
//ball.h
{
class ball
{
public:
//sets the intial raduis to 0
ball() {
radius = 0;
}
ball(float radii) {
radius = radii;
}
float get_radius() {
return radius;
}
void set_radius(float redly) {
radius = redly;
}
bool operator == (ball x) {
if (radius == x.radius)
return true;
else
return false;
}
bool operator > (ball x) {
if (radius > x.radius)
return true;
else
return false;
}
bool operator += (ball x) {
radius += x.radius;
}
ball operator + (ball a, ball b) {
ball d;
d += a;
d += b;
return d;
}
private:
float radius;
};
}
#endif
If you are only looking for (x_volume/ y_volume)% and (x_surfacearea/y_surfacearea)%
I suggest doing :
float vol_over_y() {
float v;
v = ((radius * radius * radius)/(10*10*10));
return v;
}
float sa_over_y() {
float a;
a = (radius * radius /(10*10));
return a;
}
because other constants like (4.0/3.0)* 3.14 in volume and 3.14 in surface area cancel out.
If in case y changes,
float vol_over_y(float y_rad) {
float v;
v = ((radius * radius * radius)/(y_rad*y_rad*y_rad));
return v;
}
float sa_over_y(float y_rad) {
float a;
a = (radius * radius /(y_rad*y_rad));
return a;
}
You thought too much. It is simply a little tweak in your main logic:
ball x;
ball y(10);
// your code to construct x
cout << "The volume of x is " << ( x.volume() / y.volume() )<< "% of the volume of y."<< endl;
// similar for surface area, try it out yourself
If in case you really need to do it in a method(which is quite ridiculous imho), you should just pass the other ball in and calculate:
float volume_ratio_to(const ball& base) {
return volume() / base.volume();
}
And your main logic become
cout << "The volume of x is " << x.volume_ratio_to(y) << "% of the volume of y."<< endl;
Edited for your updated question:
The reason why + operator does not work for you is because you didn't overloaded the operator right.
If you want it to be member of ball, the + operator should only takes one argument.
i.e. looks like:
class ball {
//....
ball operator + (ball another);
}
Or, don't make it member of ball:
// outside ball class
ball operator + (ball a, ball b) {....}
Refer to Overloaded Addition assignment operator in C++ for two /more than two objects? for more detailed description on how to overload addition operator.
There are quite a lot of other problems in your code too
You should have passed balls to methods by reference instead of by value
You should have considered adding const in various places
You += logic is totally wrong. Pay attention to what you quoted:
+= to add the volume of the right-side-operand to the volume of the
left-side-operand. It is like to melt two metal balls to make one
metal ball. The new ball's radius is cube root of (r1^3 + r2^3)
//This helps
int main()
{
//use ball
ball x; float re;
ball y(10);
cout << "Enter radius for ball x: " << endl;
cin >> re;
x.setl(re);
cout << "The volume of x is " << (x.volume()/y.volume())*100 << "%
of the volume of y."<< endl;
cout << "The surfacearea of x box is " <<
(x.surface_area()/y.surface_area())*100 << "% of
the surfacearea of y." << endl;
system("pause");
return 0;
}
//ball.h
#pragma once
#ifndef Ball
#define Ball
namespace bsize
{
class ball
{
public:
ball(){
radius = 0;
}
ball(float radii) {
radius = radii;
}
float volume() {
float v;
v = ((4.0/3.0)* 3.14 * (radius * radius * radius));
return v;
}
float surface_area() {
float a;
a = (4 * 3.14 * radius * radius);
return a;
}
float get_radius(){
return radius;
}
void set_radius(float redly) {
radius = redly;
}
private:
float radius;
};
}
#endif
I have a 2 vectors of size 4 to store coordinates of a shape (square/rectangle). 1st vector is for x and 2nd for y. To find out the area of the shape, I need the difference in their length. How do I find the difference between 2 elements within the same vector? Using square as an example:
vector<int> x(4);
vector<int> y(4);
double Square::computeArea()
{
int length;
double area;
if (x[0] == x[1]) //x coordinates are the same
{
length = y[0] - y[1]; //difference in y coordinates to find the length, need help here
}
else if (x[1] == x[2]
{
length = y[1] - y[2];
}
else if ... //repeat
area = length * length;
if (area < 0) { area = -area; }
setArea(area)
return area;
}
If your rectangle has edges which are parallel to the axis, and the points are ordered clockwise (or counterclockwise), you can simply use the first and third element of the arrays:
int yedge, xedge;
xedge = abs(x[0] - x[2]);
if ( xedge == 0 ) //beware, this check works well only for ints!
return area = 0.0;
else yedge = abs(y[0] - y[2]);
return area = xedge * yedge;
If you have more general convex quadrilaterals use something like this:
int dx20 = x[2] - x[0];
int dy10 = y[1] - y[0];
int dy20 = y[2] - y[0];
int dx10 = x[1] - x[0];
int dy30 = y[3] - y[0];
int dx30 = x[3] - x[0];
area = 0.5*abs(dx20*dy10-dy20*dx10);
area += 0.5*abs(dx20*dy30-dy20*dx30);
The beauty of C++ and OOP is that you can think more in terms of the problem than how to program it.
If I were in your place I would use std::pair to save the coordinates.
And have a class representing the rectangle.
I am using the distance between point 1 and 2 as length, and point 1 and 4 as width. It may not be the correct approach in all cases but it should show you have to go about programming your function.
using namespace std;
class Rectangle // Class Rectangle
{
public:
Rectangle(vector<pair<double, double>> neWcoordinates);
double computeArea();
private:
vector<pair<double, double>> coordinates;
};
double Rectangle::computeArea()
{
double length = sqrt(pow(coordinates[0].first-coordinates[1].first,2)+pow(coordinates[0].second-coordinates[1].second,2)
);
double width = sqrt(pow(coordinates[0].first-coordinates[3].first,2)+pow(coordinates[0].second-coordinates[3].second,2));
return length*width;
}
If I have a simple 2-D matrix with normalized values on x-axis between 0 and 1 and y-axys between 0 and 1, and I have 3 points in this matrix e.g. P1 (x=0.2,y=0.9), P2 (x=0.5,y=0.1) and P3 (x=0.9,y=0.4).
How can I simply calculate a curve thru this points, meaning having a function which is giving me the y for any x.
I now that there are any number of possible curves thru 3 points. But hey, you know what I mean: I want a smooth curve thru it, usable for audio-sample-interpolation, usable for calculation a volume-fade-curve, usable for calculating a monster-walking-path in a game.
Now I have searched the net for this question about 3 days, and I cannot believe that there is no usable solution for this task. All the text dealing about Catmull-rom-Splines, bezier-curves and all that theroretical stuff has all at least one point which doesn't make it for me usable. For example Catmull-Rom-splines need to have a fix distance between the control-points (I would use this code and set the 4. point-y to the 3. point y) :
void CatmullRomSpline(float *x,float *y,float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4,float u)
{
//x,y are calculated for x1,y1,x2,y2,x3,y3 and x4,y4 if u is the normalized distance (0-1) in relation to the distance between x2 and x3 for my whiched point
float u3,u2,f1,f2,f3,f4;
u3=u*u*u;
u2=u*u;
f1=-0.5f * u3 + u2 -0.5f *u;
f2= 1.5f * u3 -2.5f * u2+1.0f;
f3=-1.5f * u3 +2.0f * u2+0.5f*u;
f4=0.5f*u3-0.5f*u2;
*x=x1*f1+x2*f2+x3*f3+x4*f4;
*y=y1*f1+y2*f2+y3*f3+y4*f4;
}
But I don't see that x1 to x4 have any affect on the calculation of y, so I think x1 to x4 must have the same distance?
...
Or bezier-code doesn't calcuate the curve thru the points. The points (at least the 2. point) seem only to have a force-effect on the line.
typedef struct Point2D
{
double x;
double y;
} Point2D;
class bezier
{
std::vector<Point2D> points;
bezier();
void PushPoint2D( Point2D point );
Point2D GetPoint( double time );
~bezier();
};
void bezier::PushPoint2D(Point2D point)
{
points.push_back(point);
}
Point2D bezier::GetPoint( double x )
{
int i;
Point2D p;
p.x=0;
p.y=0;
if( points.size() == 1 ) return points[0];
if( points.size() == 0 ) return p;
bezier b;
for (i=0;i<(int)points.size()-1;i++)
{
p.x = ( points[i+1].x - points[i].x ) * x + points[i].x;
p.y = ( points[i+1].y - points[i].y ) * x + points[i].y;
if (points.size()<=2) return p;
b.PushPoint2D(p);
}
return b.GetPoint(x);
}
double GetLogicalYAtX(double x)
{
bezier bz;
Point2D p;
p.x=0.2;
p.y=0.9;
bz.PushPoint2D(p);
p.x=0.5;
p.y=0.1;
bz.PushPoint2D(p);
p.x=0.9;
p.y=0.4;
bz.PushPoint2D(p);
p=bz.GetPoint(x);
return p.y;
}
This is better than nothing, but it is 1. very slow (recursive) and 2. as I said doesn't really calculate the line thru the 2. point.
Is there a mathematical brain outside which could help me?
Thank you TOCS (Scott) for providing your code, I will also try it if I have some time. But what I have tested now is the hint by INFACT (answer 3): This "Largrange polynomials" are very very close to what I am searching for:
I have renamed my class bezier to curve, because I have added some code for lagrangeinterpolation. I also have added 3 pictures of graphical presentation what the code is calculation.
In Picture 1 you can see the loose middle point of the old bezier-function.
In Picture 2 you can now see the going thru all-points-result of lagrange interpolation.
In Picture 3 you can see the only problem, or should I say "thing which I also need to be solved" (anyway its the best solution till now): If I move the middle point, the curve to going to fast, to quick to the upper or lower boundaries). I would like it to go more smoothely to the upper and lower. So that it looks more logarithm-function like. So that it doesn't exeed the y-boundaries between 0 and 1 too soon.
Now my code looks like this:
curve::curve(void)
{
}
void curve::PushPoint2D(Point2D point)
{
points.push_back(point);
}
Point2D curve::GetPoint( double x )
{
//GetPoint y for x with bezier-mathematics...
//was the only calculating function in old class "bezier"
//now the class is renamed "curve"
int i;
Point2D p;
p.x=0;
p.y=0;
if( points.size() == 1 ) return points[0];
if( points.size() == 0 ) return p;
curve b;
for (i=0;i<(int)points.size()-1;i++)
{
p.x = ( points[i+1].x - points[i].x ) * x + points[i].x;
p.y = ( points[i+1].y - points[i].y ) * x + points[i].y;
if (points.size()<=2) return p;
b.PushPoint2D(p);
}
return b.GetPoint(x);
}
//THIS IS NEW AND VERY VERY COOL
double curve::LagrangeInterpolation(double x)
{
double y = 0;
for (int i = 0; i <= (int)points.size()-1; i++)
{
double numerator = 1;
double denominator = 1;
for (int c = 0; c <= (int)points.size()-1; c++)
{
if (c != i)
{
numerator *= (x - points[c].x);
denominator *= (points[i].x - points[c].x);
}
}
y += (points[i].y * (numerator / denominator));
}
return y;
}
curve::~curve(void)
{
}
double GetLogicalYAtX(double x)
{
curve cv;
Point2D p;
p.x=0; //always left edge
p.y=y1; //now by var
cv.PushPoint2D(p);
p.x=x2; //now by var
p.y=y2; //now by var
cv.PushPoint2D(p);
p.x=1; //always right edge
p.y=y3; //now by var
cv.PushPoint2D(p);
//p=cv.GetPoint(x);
//return p.y;
return cv.LagrangeInterpolation(x);
}
Do you have any ideas how I could get the new solution a little bit more "soft"? So that I can move the 2. Point in larger areas without the curve going out of boundaries? Thank you
static bezier From3Points(const Point2D &a, const Point2D &b, const Point2D &c)
{
bezier result;
result.PushPoint2D(a);
Point2D middle;
middle.x = 2*b.x - a.x/2 - c.x/2;
middle.y = 2*b.y - a.y/2 - c.y/2;
result.PushPoint2D(middle);
result.PushPoint2D(c);
return result;
}
Untested, but should return a bezier curve where at t=0.5 the curve passes through point 'b'.
Additionally (more untested code ahead), you can calculate your points using bernstein basis polynomials like so.
static int binomialcoefficient (int n, int k)
{
if (k == 0)
return 1;
if (n == 0)
return 0;
int result = 0;
for (int i = 1; i <= k; ++i)
{
result += (n - (k - i))/i;
}
return result;
}
static double bernstein (int v, int n, double t)
{
return binomialcoefficient(v,n) * pow(t,v) * pow(1 - t,n - v);
}
Point2D GetPoint (double t)
{
Point2D result;
result.x = 0;
result.y = 0;
for (int i = 0; i < points.size(); ++i)
{
double coeff = bernstein (i,points.size(),t);
result.x += coeff * points[i].x;
result.y += coeff * points[i].y;
}
return result;
}
How would you solve the problem of finding the points of a (integer) grid within a circle centered on the origin of the axis, with the results ordered by norm, as in distance from the centre, in C++?
I wrote an implementation that works (yeah, I know, it is extremely inefficient, but for my problem anything more would be overkill). I'm extremely new to C++, so my biggest problem was finding a data structure capable of
being sort-able;
being able to save an array in one of its elements,
rather than the implementation of the algorithm. My code is as follows. Thanks in advance, everyone!
typedef std::pair<int, int[2]> norm_vec2d;
bool norm_vec2d_cmp (norm_vec2d a, norm_vec2d b)
{
bool bo;
bo = (a.first < b.first ? true: false);
return bo;
}
int energy_to_momenta_2D (int energy, std::list<norm_vec2d> *momenta)
{
int i, j, norm, n=0;
int energy_root = (int) std::sqrt(energy);
norm_vec2d temp;
for (i=-energy_root; i<=energy_root; i++)
{
for (j =-energy_root; j<=energy_root; j++)
{
norm = i*i + j*j;
if (norm <= energy)
{
temp.first = norm;
temp.second[0] = i;
temp.second[1] = j;
(*momenta).push_back (temp);
n++;
}
}
}
(*momenta).sort(norm_vec2d_cmp);
return n;
}
How would you solve the problem of finding the points of a (integer) grid within a circle centered on the origin of the axis, with the results ordered by norm, as in distance from the centre, in C++?
I wouldn't use a std::pair to hold the points. I'd create my own more descriptive type.
struct Point {
int x;
int y;
int square() const { return x*x + y*y; }
Point(int x = 0, int y = 0)
: x(x), y(y) {}
bool operator<(const Point& pt) const {
if( square() < pt.square() )
return true;
if( pt.square() < square() )
return false;
if( x < pt.x )
return true;
if( pt.x < x)
return false;
return y < pt.y;
}
friend std::ostream& operator<<(std::ostream& os, const Point& pt) {
return os << "(" << pt.x << "," << pt.y << ")";
}
};
This data structure is (probably) exactly the same size as two ints, it is less-than comparable, it is assignable, and it is easily printable.
The algorithm walks through all of the valid points that satisfy x=[0,radius] && y=[0,x] && (x,y) inside circle:
std::set<Point>
GetListOfPointsInsideCircle(double radius = 1) {
std::set<Point> result;
// Only examine bottom half of quadrant 1, then
// apply symmetry 8 ways
for(Point pt(0,0); pt.x <= radius; pt.x++, pt.y = 0) {
for(; pt.y <= pt.x && pt.square()<=radius*radius; pt.y++) {
result.insert(pt);
result.insert(Point(-pt.x, pt.y));
result.insert(Point(pt.x, -pt.y));
result.insert(Point(-pt.x, -pt.y));
result.insert(Point(pt.y, pt.x));
result.insert(Point(-pt.y, pt.x));
result.insert(Point(pt.y, -pt.x));
result.insert(Point(-pt.y, -pt.x));
}
}
return result;
}
I chose a std::set to hold the data for two reasons:
It is stored is sorted order, so I don't have to std::sort it, and
It rejects duplicates, so I don't have to worry about points whose reflection are identical
Finally, using this algorithm is dead simple:
int main () {
std::set<Point> vp = GetListOfPointsInsideCircle(2);
std::copy(vp.begin(), vp.end(),
std::ostream_iterator<Point>(std::cout, "\n"));
}
It's always worth it to add a point class for such geometric problem, since usually you have more than one to solve. But I don't think it's a good idea to overload the 'less' operator to satisfy the first need encountered. Because:
Specifying the comparator where you sort will make it clear what order you want there.
Specifying the comparator will allow to easily change it without affecting your generic point class.
Distance to origin is not a bad order, but for a grid but it's probably better to use row and columns (sort by x first then y).
Such comparator is slower and will thus slow any other set of points where you don't even care about norm.
Anyway, here is a simple solution using a specific comparator and trying to optimize a bit:
struct v2i{
int x,y;
v2i(int px, int py) : x(px), y(py) {}
int norm() const {return x*x+y*y;}
};
bool r_comp(const v2i& a, const v2i& b)
{ return a.norm() < b.norm(); }
std::vector<v2i> result;
for(int x = -r; x <= r; ++x) {
int my = r*r - x*x;
for(int y = 0; y*y <= my; ++y) {
result.push_back(v2i(x,y));
if(y > 0)
result.push_back(v2i(x,-y));
}
}
std::sort(result.begin(), result.end(), r_comp);