I'm using nanoflann to find the k nearest neighbors (e.g., the k closest points to x). Say, I find k points and define r to be the smallest radius that contains all k points. I then add a new point y such that y lies on the edge of the ball. When I again find the k closest points to x, y is NOT included. It seems that nanoflann's default behavior if two points are equidistant from a point is to return the point that was added first. Is there a way to switch this so it returns the point that was added last?
Related
I have X-Y plane and points (xi, yi) where x, y and i are integers. Now if I draw infinite lines of slope 1 and -1, I have to find those 2 points which either will lie on the same line or if none of them lie then should output:
Case : If atmost 1 point lies on a line the 2nd point should be the point which has minimum distance from the line. In such cases we can draw the line exactly between those 2 points to minimize the distance.
I am not able to find the solution to this problem. My approach was to look at the points in opposite quadrants but I did not get any solution better than O(n^2).
First, I would transform the points into a different coordinate system that is rotated by 45°:
u = x + y
v = x - y
If the original points lie on a line with slope 1, their v coordinate will be equal. If they lie on a line with slope -1, their u coordinate will be equal.
Now, create two lists of points. One sorted by u, the other sorted by v. Then iterate all the points. To find the point that is closest to the corresponding line, you just have to check the neighbors in the sorted order. If there are neighbors with the same u/v coordinate, you are done. If not, find the neighbor with the smallest u/v difference and remember it. Do this for all the points and report the pair with the smallest distance.
In advance I will tell you that I am starting to learn to program.
The problem is the following:
I have an array of length N and I want to find all the indices that lie inside a circle of radius R centered one index, say the jth.
I have an idea, but it might be very inefficient.
I would convert some kth index in [0,N-1] to Cartesian coordinates using:
int x = k / side;
int y= k % side;
where side is sqrt(N), and test whether it satisfies the equation of the circle:
(x_xC)*(x-xC)+ (y_yC)*(y-yC)<=R*R
where (xC, yC) are the coordinates of the jth element. If yes, I would store the index associated with (x, y) or else do it again for the next element until I cover up the whole array.
Is it a good idea or is it too inefficient for very large arrays?
There is a way to loop over not the entire array, but only the elements which do lie in the circle:
Compute xC and yC.
Let y loop from yC-R to yC+R (with proper clipping at the array boundaries, and with proper rounding in case R is not an integer).
For each such y, let r=sqrt(R*R-(y-yC)*(y-yC)) and let x loop from xC-r to xC+r with proper rounding (and, again, proper clipping at the array boundaries).
Convert x and y back to an array index.
I am attempting to use CGAL to find out "all intersection points" AND "intersected segments for each intersection point" from a list of segments in 2D. I want to use Bentley–Ottmann algorithm for some reasons. CGAL library has an c++ implementation of this algorithm called Sweepline 2 but with this i am able to find intersection points only. Is there any other implementation exist in CGAL? or how can I solve this problem?
Edit:
The really quick fix would just be to generate all intersection points using Sweep line 2, loop through all generated intersection points, and for each intersection point record both the point's coordinates and all line segments that contain that point into a structure of your choice.
A quick solution (albeit not the most efficient) is to:
//Probably start off with a struct to store your information
struct intersection{
//This stores the x and y position of the intersection.
int[2] xyCoords;
//This stores the segment ids or indexes of the intersection line segments.
//For example, if the 1st and 5th line segment intersect, you would store 1 and 5 in here.
int[2] segmentIDs;
}
Then just create a vector of the intersection struct so you can store all of your different intersections.
vector<intersection> intersectionVector;
Then just loop through all of the line segments you have in something similar to the following:
for (int i = 0; i < numSegments; i++){
for (int j = 0; j < numSegments; j++){
//Don't look at the same points.
if (i == j)
continue;
//See below for pseudocode for this part.
}
}
Now to fill in that block, instead of reinventing anything just refer to How do you detect where two line segments intersect?.
As shown in the above link, calculate the values of r × s and (q − p) × r where × is the vector cross product. From there, just use if/else blocks to account for the four cases (ctrl f for "Now there are four cases:") if you care about detail. If you just want what you outlined in your question, just check for the validity of case 3. If it is valid, store the indexes of your line segments as well as the x and y coordinates in the vector/whatever structure you're using.
The only extra thing you need to do is use the calculate u and apply it to your second line segment of the two you're checking, in order to store the x y coordinates of the intersection.
I am trying to write an efficient algorithm that counts the number of points inside a Sphere of Radius R and Dimension D. The sphere is always at the origin. Suppose we have a sphere of dimension 2 (circle) with radius 5.
My strategy is to generate all possible points within the first quadrant, so for the above example we know that (1,2) is in the circle, so must all + / - combinations of that point which is simply dimension squared. So for each point found in a single quadrant of an n-dimensional sphere we add 2 ^ dimension to the total count.
I'm not sure if there is a much more efficient solution to this problem but this is what I have so far in terms of implementation.
int count_lattice_points(const double radius, const int dimension) {
int R = static_cast<int>(radius);
int count = 0;
std::vector<int> points;
std::vector<int> point;
for(int i = 0; i <= R; i++)
points.push_back(i);
do {
for(int i = 0; i < dimension - 1; i++)
point.push_back(points.at(i));
if(isPointWithinSphere(point, radius)) count += std::pow(2,dimension);
point.clear();
}while(std::next_permutation(points.begin(), points.end()));
return count + 3;
}
What can I fix or improve in this situation ?
For 2D case this is Gauss's circle problem. One possible formula:
N(r) = 1 + 4 * r + 4 * Sum[i=1..r]{Floor(Sqrt(r^2-i^2))}
(central point + four quadrants, 4*r for points at the axis, others for in-quadrant region).
Note that there is no known simple closed math expression for 2D case.
In general your idea with quadrants, octants etc is right, but checking all the points is too expensive.
One might find the number of ways to compose all squares from 0 to r^2 from 1..D
integer squares (extension of (4) formula).
Note that combinatorics would help to make calculation faster. For example, it is enough to find the number of ways to
make X^2 from D natural squares, and multiply by 2^D (different sign combinations); find the number of ways to make X^2 from D-1 natural squares, and multiply by D*2^(D-1) (different sign combinations + D places for zero addend) etc
Example for D=2, R=3
addends: 0,1,4,9
possible sum compositions number of variants
0 0+0 1
1 0+1,1+0 2*2=4
2 1+1 4
4 0+4,4+0 2*2=4
5 1+4,4+1 2*4=8
8 4+4 4
9 0+9,9+0 2*2=4
-------------------------------------
29
I presented my algorithm for 2D here (with some source code and an ugly but handy illustration):
https://stackoverflow.com/a/42373448/5298879
It's around 3.4x faster than MBo's counting points between the origin and the edge of the circle in one of the quarters.
You just imagine an inscribed square and count only one-eighth of what's outside that square inside that circle.
public static int gaussCircleProblem(int radius) {
int allPoints=0; //holds the sum of points
double y=0; //will hold the precise y coordinate of a point on the circle edge for a given x coordinate.
long inscribedSquare=(long) Math.sqrt(radius*radius/2); //the length of the side of an inscribed square in the upper right quarter of the circle
int x=(int)inscribedSquare; //will hold x coordinate - starts on the edge of the inscribed square
while(x<=radius){
allPoints+=(long) y; //returns floor of y, which is initially 0
x++; //because we need to start behind the inscribed square and move outwards from there
y=Math.sqrt(radius*radius-x*x); // Pythagorean equation - returns how many points there are vertically between the X axis and the edge of the circle for given x
}
allPoints*=8; //because we were counting points in the right half of the upper right corner of that circle, so we had just one-eightth
allPoints+=(4*inscribedSquare*inscribedSquare); //how many points there are in the inscribed square
allPoints+=(4*radius+1); //the loop and the inscribed square calculations did not touch the points on the axis and in the center
return allPoints;
}
An approach similar to that described by MBo, including source code, can be found at
https://monsiterdex.wordpress.com/2013/04/05/integer-lattice-in-n-dimensional-sphere-count-of-points-with-integer-coordinates-using-parallel-programming-part-i/.
The approach consists in finding partitions of the radius, and then for each partition in the sphere compute the number of ways it can be represented in the sphere by both permuting coordinates and flipping the signs of nonzero coordinates.
I have a 2D matrix represented as a vector of values, an index representing the first cell and a pair of coordinate representing the second cell.
vector<double> matrix;
auto index = 10;
auto x1 = index % width;
auto y1 = index / width;
auto x2 = ...
auto y2 = ...
I need to find the distance between these two cells, where the distance is equals to 1 for the first "ring" of the 8 neighbor cells, 2 for the second ring, and so on.
Is there a way faster than the euclidean distance?
What you need is something like a modified Manhattan Distance. I think there may be a specific name for your use case, but I don't know it. Anyway, this is how I'd do it.
Suppose the two points are x rows away and y columns away. Then x+y is the Manhattan Distance. But in your case, diagonal movements are also allowed. So, if you moved diagonally towards the point initially, you'd cover the smaller of x and y, with some amount remaining in the other. You can then move horizontally/vertically to cover the remaining distance. Hence, the distance by your metric would be max(x,y).
Given points (x1,y1) and (x2,y2), the answer would be max(|x1-x2|,|y1-y2|)