Is square root required when sorting points by distance to a reference point? - square-root

Given a large bunch of points in 2D and a reference point x_0, in order to find the closest (couple of) points to x_0, i.e. distance d, I could calculate the distance of each point from x_0 using Euclidean distance formula. The formula however needs taking a square root, which is computationally expensive. Would I get the same result if I'd just calculate the square of the distance d^2 and choose the points with lowest d^2? I'm not interested in the real distance, just in the relative ordering.

Related

Bezier curve, get consistent points on axis?

So I have a bezier curve >
Now, I would like to get a y point, value every says, 0.01 value on x-axis.
As far as I know, there is no method to "find Y given X" using bezier.
So I have to subdivide it into flat straight chunks and then get... "somehow" nearest chunk value as an approximation?
So my question is, how can I... "equally" subdivide the bezier curve so that chunk distance is more equal on... x-axis?
Right now the chunk distancing that I get is quite... "random" using https://en.wikipedia.org/wiki/B%C3%A9zier_curve as my current algo.
Regards
Dariusz
As far as I know, there is no method to "find Y given X" using bezier.
Then you probably need to look for "finding Y given X" a bit more =) https://pomax.github.io/bezierinfo/#yforx
As for the result you want, with regularly spaced intervals based on distance along the curve is the general problem of reparameterizing the curve for distance rather than time., and it's a hard problem for quadratic, and literally impossible problem for higher order Beziers.
So instead of trying to do that, what we typically do is just build a lookup table by sampling the curve, storing those "t maps to x/y and distance d along the curve", and then we use that combined with interpolation to get good-enough approximate coordinates (e.g. sub-sub-pixel accurate) rather than mathematically perfect coordinates. https://pomax.github.io/bezierinfo/#tracing

Finding the relative shift and rotation that gives the maximum overlap for two point-clouds

I have two pointclouds in a 3D euclidean space, and I want to find the relative rotation and translation between the two pointclouds that gives the most overlap (or in other words, that minimizes the Wasserstein Distance, or any other optimal-transport metric, between the pointclouds).
I know that for translation one would simply shift the "center of mass". However rotation doesn't seem very intuitive to me.
What I have tried:
My approach was to compute the inertia tensor and rotate the point clouds to align their principal axes. However this is numerically unstable for pointclouds with high degrees of symmetry.

Which one is the best for small distance like 10 meters Haversine or Law of Cosines?

I am Using Haversine Formula to find the great circle distance in my work
But I want to know The most Exact formula for small distance like 10 meters only
Continue using the haversine formula. The [spherical] law of cosines formula is known to be inaccurate at short distances. See https://en.wikipedia.org/wiki/Great-circle_distance ... also this SO question: MySQL WordPress Query Returning a Distance of Zero for Some Records
There are a couple of alternative options.
One is to convert points from geodetic to Cartesian system of coordinates and then use Euclidean distance in space. Relative error due to curvature is about 10^-9 for distances below 1 km and 10^-3 for distances below 1000 km.
Another option is to project locations on plane that is tangent to surface of the Earth and then calculate Euclidean distance on the plane. But this solution will not work near poles and additional care should be taken at 180th meridian. A careful implementation for ellipsoid datum can use just one computation if sine and one computation of square root aside of arithmetic operations, and can potentially be faster than Haversine formula for spherical datum, at the same time being much more accurate.

Computing Minkowski Difference For Circles and Convex Polygons

I'm needing to implement a Minkowski sum function that can return the Minkowski sum of either 2 circles, 2 convex polygons or a circle and a convex polygon. I found this thread that explained how to do this for convex polygons, but I'm not sure how to do this for a circle and polygon. Also, how would I even represent the answer?! I'd like the algorithm to run in O(n) time but beggars can't be choosers.
Circle is trivial -- just add the center points, and add the radii. Circle + ConvexPoly is nearly as simple: move each segment perpendicularly outward by the circle radius, and connect adjacent segments with circular arcs centered at the original poly vertices. Translate the whole by the circle center point.
As for how you represent the answer: Well, it depends on what you want to do with it. You could convert it to a NURBS if you just want to draw it with a vector drawing library. You could approximate the circular arcs with polylines if you just want a polygonal approximation. Or you might store it as is -- "this polygon, expanded by such-and-such a radius". That would be the best choice for things like raycasting, for instance. Or as a compromise, you could connect adjacent segments linearly instead of with circular arcs, and store it as the union of the (new) convex polygon and a list of circles at the vertices.
Oh, about ConvexPoly + ConvexPoly. That's the trickiest one, but still straightforward. The basic idea is that you take the list of segment vectors for each polygon (starting from some particular extremal point, like the point on each poly with the lowest X coordinate), then merge the two lists together, keeping it sorted by angle. Sum the two points you started with, then apply each vector from the merged vector list to produce the other points.

Search optimization problem

Suppose you have a list of 2D points with an orientation assigned to them. Let the set S be defined as:
S={ (x,y,a) | (x,y) is a 2D point, a is an orientation (an angle) }.
Given an element s of S, we will indicate with s_p the point part and with s_a the angle part. I would like to know if there exist an efficient data structure such that, given a query point q, is able to return all the elements s in S such that
(dist(q_p, s_p) < threshold_1) AND (angle_diff(q_a, s_a) < threshold_2) (1)
where dist(p1,p2), with p1,p2 2D points, is the euclidean distance, and angle_diff(a1,a2), with a1,a2 angles, is the difference between angles (taken to be the smallest one). The data structure should be efficient w.r.t. insertion/deletion of elements and the search as defined above. The number of vectors can grow up to 10.000 and more, but take this with a grain of salt.
Now suppose to change the above requirement: instead of using the condition (1), let's request all the elements of S such that, given a distance function d, we want all elements of S such that d(q,s) < threshold. If i remember well, this last setup is called range-search. I don't know if the first case can be transformed in the second.
For the distance search I believe the accepted best method is a Binary Space Partition tree. This can be stored as a series of bits. Each two bits (for a 2D tree) or three bits (for a 3D tree) subdivides the space one more level, increasing resolution.
Using a BSP, locating a set of objects to compare distances with is pretty easy. Just find the smallest set of squares or cubes which contain the edges of your distance box.
For the angle, I don't know of anything. I suppose that you could store each object in a second list or tree sorted by its angle. Then you would find every object at the proper distance using the BSP, every object at the proper angles using the angle tree, then do a set intersection.
You have effectively described a "three dimensional cyclindrical space", ie. a space that is locally three dimensional but where one dimension is topologically cyclic. In other words, it is locally flat and may be modeled as the boundary of a four-dimensional object C4 in (x, y, z, w) defined by
z^2 + w^2 = 1
where
a = arctan(w/z)
With this model, the space defined by your constraints is a 2-dimensional cylinder wrapped "lengthwise" around a cross section wedge, where the wedge wraps around the 4-d cylindrical space with an angle of 2 * threshold_2. This can be modeled using a "modified k-d tree" approach (modified 3-d tree), where the data structure is not a tree but actually a graph (it has cycles). You can still partition this space into cells with hyperplane separation, but traveling along the curve defined by (z, w) in the positive direction may encounter a point encountered in the negative direction. The tree should be modified to actually lead to these nodes from both directions, so that the edges are bidirectional (in the z-w curve direction - the others are obviously still unidirectional).
These cycles do not change the effectiveness of the data structure in locating nearby points or allowing your constraint search. In fact, for the most part, those algorithms are only slightly modified (the simplest approach being to hold a visited node data structure to prevent cycles in the search - you test the next neighbors about to be searched).
This will work especially well for your criteria, since the region you define is effectively bounded by these axis-defined hyperplane-bounded cells of a k-d tree, and so the search termination will leave a region on average populated around pi / 4 percent of the area.