Euclidean distance to calculate if something is near something - java

currently i have using a framework and it has a function called distance2D, and it has this description:
Calculate the Euclidean distance
between two points (considering a
point as a vector object). Disregards
the Z component of the vectors and is
thus a little faster.
and this is how i use it
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) > 1)
System.out.println("Near");
i have totally no idea what a Euclidean distance is, i am thinking that it can be used to calculate how far 2 points are? because i am trying to compare distance between 2 objects and if they are near within a certain range i want to do something. how would i be able to use this?

Euclidean distance is the distance between 2 points as if you were using a ruler. I don't know what are the dimensions of your Euclidean space, but be careful because the function you are using just takes in consideration the first two dimensions (x,y). Thus if you have a space with 3 dimensions(x,y,z) it will only use the first two(x,y of x,y,z) to calculate the distance. This may give a wrong result.
For what I understood, if you want to trigger some action when two points are within some range you should make:
<!-- language: lang-java -->
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) < RANGE)
System.out.println("Near");

The Euclidean distance is calculated tracing a straight line between two points and measuring as the hypotenuse of a imaginary isosceles triangle between the two lines and a complementary point. This measure is scalar, so it's a good metric for your calculations.

Euclidean geometry is a coordinate system in which space is flat, not curved. You don't need to care about non-Euclidean geometry unless for example you're dealing with coordinates mapped onto a sphere, such as calculating the shortest travel distance between two places on Earth.
I imagine this function will basically use Pythagoras' theorem to calculate the distance between the two objects. However, as the description says, it disregards the Z component. In otherwords, it will only give a correct answer if both points have the same Z value (aka "depth").

If you wish to compare distances and save time, use not the distance itself, but its square: (x1-x2)^2 + (y1-y2)^2. Don't take sqrt. So, your distances will work exactly as euclidian ones, but quickly.

Related

Determining if a point is within an angle

So I'm attempting to calculate if a point is inside of an angle. While researching, I came across many terms I am unfamiliar with. Essentially from a point (A) with a 120° angle protruding from point (A). I want to test if a secondary point (B) would be inside of the angle. All that is known is the degree of the angle and the degree at which the angle is facing and the X and Y values of both points. This will all be done in Java so any and all help is appreciated!
To better explain it:
There is a point with two vectors protruding from said point. The angle that is known is the angle that is created by the protrusion of the two vectors.
First of all, an angle is not defined for two points -- only for two lines.
Define a line that is your 0° 2D-space. For a line, you need a point and a direction.
Calculate the normal-vector for your line (Turn your directional vector by 90°); normalize both your directional and normal vector so that sqrt(x^2+y^2) = 1.
Calculate the distance vector between your initial point and the other point, this is your second line, sharing the same initial point.
Calculate the dot-product of a and b:
a = distance vector × normal vector
b = distance vector × directional vector
Use simple trigonometry to calculate the angle. It's the arctangent of (a/b) or (b/a).
You probably wanna take the absolute value of the result as well if you don't care about left and right.

Finding an equation to connect the max number of possible points

I would like to find a way, given a set of any points on a 2 dimensional (or 3 dimensional if possible) plane, to connect as many of these points as possible with an equation, preferably in the form of X^n+BX^n and so on. X being of course a variable, and b and n being any numbers.
This would hopefully work in a way that given say, 50 random points, I would be able to use the equation to draw a line that would pass through as many of these points as possible.
I plan on using this in a compression format where data is converted to X,Y coordinate pairs, the goal is then to create equations that can reproduce these points. The equation would then be stored and the data would be replaced with a pointer to the equation as well as the number to enter into the equation to get the data back.
Any feedback is nice, this is just an idea I thought of during class and wanted to see if it would be possible to implement in a usable format.
To connect n points you need a polynomial of at most degree n-1. You can use Polynomial Regression to form your line.

How to calculate distance between two points in Android FAST for MANY points

I have around 1000 points. I'm trying to group this points base on distance. Im using the harversine formula, but it seems to be super slow. In android for 1000 points takes 4 seconds. In my local environment takes 60 ms.
I do not care about precession and the points are no more than 25 km apart.
Is there another formula I can use?
First, for items that close to each other, curvature of the Earth is not going to matter too much. Hence, you can treat it as flat, at which point you're looking at the Pythagorean Theorem for distance (square root of the sum of the squares of the x/y distances).
Second, if all you are doing is sorting/grouping, you can drop the square root calculation and just sort/group on the square of the distance. On devices lacking a floating point coprocessor, such as the first couple of generations of Android phones, that will do a lot of good right there.
Third, you don't indicate the coordinate system you are using for the points, but if you can do your calculations using fixed-point math, that too will boost performance, particularly on coprocessor-less devices. That's why the Google Maps add-on for Android uses GeoPoint and microdegrees rather than the Java double degrees in the Location you get back from LocationManager.
So long as you don't need to cope with near the polls and an aproximation is OK which for grouping it should be. Then you can work out the relative scaling between the lattitude degrees and the longitude degrees just the once and use it for every straight X squared + y squared calculation, for relative distances you can skip the square root.
If your working with degrees to scale them to be the same relative distance for lattitude and longitude you use cos of the lattitude. I would scale the latitude to the longitude then each degrees map to a good knowen distance the calculation will will be something like.
(lattitude diference for two points) * 1/cos(latitude)
You work out the 1/cos(latitude) just the once for all points assuming the latitude is not changeing much over your sample set.
Perhaps remove the calculation of the curvature of the earth..?
If the functionality of your app permits this, do so.
This format always holds true. Given two points, you can always plot them, draw the right triangle, and then find the length of the hypotenuse. The length of the hypotenuse is the distance between the two points. Since this format always works, it can be turned into a formula:
Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance between these points is given by the formula: http://www.purplemath.com/modules/distform.htm
Distance = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
Update with correct notation:
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
As far as I know, best way to do this is to use Graph Theory, and it has Dikstra's algorithm , it's the fastest algorthm in my knowledge for this kind of task.
Really worth learning, optimizes work very well.

Find nearest point relative to an object without using the distance formula in Java

If I have an object with properties of x an y, how can I tell which point in an array is the closest without using the distance formula?
You can't get an accurate result without using some variant of the distance formula. But you can save a few cycles by not taking the square root after the fact; the comparison will remain valid.
r = dx2 + dy2
If you don't care about the exact distance, you could perhaps take the difference between the x and y coordinates of your source and destination points to provide you with some ordering.
//The following code does not return the closest point,
//but it somewhat does what you need and complies with
//your requirement to not use the distance formula
//it finds the sum of x and y displacements
Point destination=...
Point nearestPoint= points.get(0);
for (Point p : points){
closenessCoefficient= Math.abs(destination.x-p.x) + Math.abs(a.destination-p.y);
nearestPoint=Math.Min(closenessCoefficient, nearestPoint);
}
return nearestPoint;
If you have to find exactly the closest neighbour, there is no way around evaluating the distance formula, at least for a couple of points. As already pointed out, you can avoid evaluating the expensive sqrt for most of the time when simply comparing the distance-squared r^2 = x^2 + y^2.
However, if you have a big number of points spread out over a large range of distances, you can first use an approximation like the ones shown here http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml . Then you can calculate the real distance formula only for the points closest as given by the approximation. On architectures where also the multiplication is expensive, this can make a big difference. On modern x86/x86-64 architectures this should not matter much though.

minimum distance between bounding box in kdtree

I need to find the minimum distance b/w two kdtree bounding box's of same tree in euclidean space. Suppose each box maintain a 5 elements. I need the minimum Euclidean distance using java.
double QHRect[][] = QNode.m_NodesRectBounds;
double RHRect[][] = RNode.m_NodesRectBounds;
QHRect[][]: 5.74842E-4,7.76626E-5,6.72655E-4,
0.5002329025,0.2499048942,0.25046735625
RHRect[][]:
0.75006193275,0.7495593574,0.75005675875,
0.999890963,0.999386589,0.99985146
There is nothing tricky about a Java implementation compared with any other language in this matter. You need to know the general algorithm to handle a problem like this. I believe it is this:
Enumerate all 12 vertices of the two bounding boxes (cubes).
Enumerate all 12 faces of the two bounding boxes.
Find the Euclidean distance between each vertex of one and face of the other. This is similar to the shortest distance between a point and a plane.
Of these 2*6*6=72 combinations, pick the smallest and you have your answer.
In the general version of the problem, you would also have to check for if the two bounding boxes intersect as well.

Categories

Resources