Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to calculate the distance between two CGPoints?

Distance between two points:

sqrt((x1-x2)^2 + (y1-y2)^2)

Is there a way to do this math faster in objective-C ?

EDIT: I think I need to clarify above. I wrote the formula above just to clarify what formula I am using to calculate the distance. ^ is not meant to represent xor - I just wanted to represent the mathematical formula without using any functions like pow or anything, so I meant to use ^ to "raise to the power off". I was wondering if anyone knows if using bitwise operators, or otherwise writing code in assembly would give an optimized version. I am using the formula in an iPhone / iPad application.

like image 718
xcoder Avatar asked Feb 02 '12 06:02

xcoder


People also ask

What is the formula to find the distance between two coordinates?

The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²). This formula is used to find the distance between any two points on a coordinate plane or x-y plane.

How do you find the distance between two diagonals?

This means that you will square the x-axis distance (x2 - x1), and that you will separately square the y-axis distance (y2 - y1). Add the squared values together. This will give you the square of the diagonal, linear distance between your two points.

How do you find the distance between two points on a plane?

The distance between two points on a 2D coordinate plane can be found using the following distance formula where (x 1, y 1) and (x 2, y 2) are the coordinates of the two points involved. The order of the points does not matter for the formula as long as the points chosen are consistent.

How to calculate distance between two points in Python?

To calculate the Euclidean distance between the points (x1, y1) and (x2, y2) you can use the formula: For example, the distance between points (2, 3) and (5, 7) is 5. Note that the above formula can be extended to n-dimensions. Now that we know how the distance between two points is computed mathematically, we can proceed to compute it in Python.

How to calculate the distance between two GPS coordinates?

The use of the arithmetic formula is the quickest and simplest way to calculate the distance between two GPS coordinates. Now, follow the following steps for this purpose. Create a new row titled Distance (Miles). The Radian Function converts the value in Degree units into a value of Radian unit.

How to find distance in Cartesian coordinate system?

The arithmetic formula for calculating distance in a 2-D Cartesian Coordinate System is as follows: x1 = Distance of point 1 from x axis. x2 = Distance of point 2 from x axis. y1 = Distance of point 1 from y axis. y2 = Distance of point 2 from y axis. d = Distance between point 1 and point 2. What Is Geodetic Coordinate System?


1 Answers

No, if you need the exact distance you cannot beat that formula.

Although to be clear ^ is not an operator for squaring a value, but a bit operator that does xor.

you will need something like

double dx = (x2-x1);
double dy = (y2-y1);
double dist = sqrt(dx*dx + dy*dy);

If you can live with just the square (which is useful when you just want to do something like sort by distance, you can use the much more efficient

double dx = (x2-x1);
double dy = (y2-y1);
double dist = dx*dx + dy*dy;

These will be at least as good as a solution pow. At worst, pow() will use the stack and be less efficient, but maybe your compiler turns it into x*x for this case.

like image 179
Michael Chinen Avatar answered Sep 20 '22 10:09

Michael Chinen