Biggest distance between two points in an array - iphone

Let's say I have an array with CGPoints (wrapped with NSValues). How can I get the two points which are most distant from each other. I mean the distance between these two points is the biggest? I can just check every two points but that does not look efficient. Is there a better way of doing this?
Thanks for help!

If there are not too many points (up to 1000, but if intensive, around 100), use the naive brute-force method O(n2).
I haven't read the details, but the biggest distance probably is computable in O(nlog n) with convex hull algorithm + rotating caliper.

Related

Understanding 3D distance outputs in matlab

Being neither great at math nor coding, I am trying to understand the output I am getting when I try to calculate the linear distance between pairs of 3D points. Essentially, I have the 3D points of a bird that is moving in a confined area towards a stationary reward. I would like to calculate the distance of the animal to the reward at each point. However, when looking online for the best way to do this, I tried several options and get different results that I'm not sure how to interpret.
Example data:
reward = [[0.381605200000000,6.00214980000000,0.596942400000000]];
animal_path = = [2.08638710671220,-1.06496059617432,0.774253689976102;2.06262715454806,-1.01019576900787,0.773933446776898;2.03912411242035,-0.954888684677576,0.773408777383975;2.01583648760496,-0.898935333316342,0.772602855030873];
distance1 = sqrt(sum(([animal_path]-[reward]).^2));
distance2 = norm(animal_path - reward);
distance3 = pdist2(animal_path, reward);
Distance 1 gives 3.33919107083497 13.9693378592353 0.353216791787775
Distance 2 gives 14.3672145652704
Distance 3 gives 7.27198528565078
7.21319284516199
7.15394253573951
7.09412041863743
Why do these all yield different values (and different numbers of values)? Distance 3 seems to make the most sense for my purposes, even though the values are too large for the dimensions of the animal enclosure, which should be something like 3 or 4 meters.
Can someone please explain this in simple terms and/or point me to something less technical and jargon-y than the Matlab pages?
There are many things mathematicians call distance. What you normally associate with distance is the eucledian distance. This is what you want in this situation. The length of the line between two points. Now to your problem. The Euclidean distance distance is also called norm (or 2-norm).
For two points you can use the norm function, which means with distance2 you are already close to a solution. The problem is only, you input all your points at once. This does not calculate the distance for each point, instead it calculates the norm of the matrix. Something of no interest for you. This means you have to call norm once for each row point on the path:
k=nan(size(animal_path,1),1)
for p=1:size(animal_path,1),
k(p)=norm(animal_path(p,:) - reward);
end
Alternatively you can follow the idea you had in distance1. The only mistake you made there, you calculated the sum for each column, where the sum of each row was needed. Simple fix, you can control this using the second input argument of sum:
distance1 = sqrt(sum((animal_path-reward).^2,2))

To find Nearest Point

We have set of (x,y) coordinates in matrix form. we want to fit a curve through those points. but it does not take points in sequence(nearest point) rather it takes the points arranged according to the sequence arranged in matrix
I think #mathematician1975 is quite right, you do not need the points arranged in sequence before do curve fitting, at least for OLS. http://en.wikipedia.org/wiki/Ordinary_least_squares If you do want the sequence, sort the coordinates by variable is fine. In both case, you do not need the nearest point.
The question is quite unclear but based on your comments, you could simply create a new matrix of ordered points by deciding on a start point and using the Euclidean norm to identify the nearest neighbour and add that to your new matrix. Continue this for each point (of course making sure that you exclude all previous points from the nearest neighbour evaluation at each step) until you have just one point left.
Once this is done you have your points in the order you want and can fit your parametric curve accordingly.
It is quite possible that there is a magic command in matlab that will do this for you but if there is I am not aware of it. If there is I am sure you will get an answer telling you so in due course.
If not you can still obtain what (I think) you want with loops and the norm function. It may not be optimal but it will give you what you want.

Matlab Finding All Possible Triangles

I have several datasets and smallest have around 1000 points and largest have around 1,000,000 points. These points are consist of Longitude and Latitude information.
I would like to create triangles for all possible combinations of these points. I am planning to use Matlab. I will appreciate any answer about how to create triplets of points from these datasets by using Matlab.
One other problem is as you can see there are huge number of points in my dataset so how can I find a fast way to do this. Thanks for any help.
You can call combnk( points, k);
http://www.mathworks.in/help/stats/combnk.html

Using triplequad to calculate density (in Matlab)

As i've explained in a previous question: I have a dataset consisting of a large semi-random collection of points in three dimensional euclidian space. In this collection of points, i am trying to find the point that is closest to the area with the highest density of points.
As high performance mark answered;
the most straightforward thing to do would be to divide your subset of
Euclidean space into lots of little unit volumes (voxels) and count
how many points there are in each one. The voxel with the most points
is where the density of points is at its highest. Perhaps initially
dividing your space into 2 x 2 x 2 voxels, then choosing the voxel
with most points and sub-dividing that in turn until your criteria are
satisfied.
Mark suggested i use triplequad for this, but this is not a function i am familiar with, or understand very well. Does anyone have any pointers on how i could go about using this function in Matlab for what i am trying to do?
For example, say i have a random normally distributed matrix A = randn([300,300,300]), how could i use triplequad to find the point i am looking for? Because as i understand currently, i also have to provide triplequad with a function fun when using it. Which function should that be for this problem?
Here's an answer which doesn't use triplequad.
For the purposes of exposition I define an array of data like this:
A = rand([30,3])*10;
which gives me 30 points uniformly distributed in the box (0:10,0:10,0:10). Note that in this explanation a point in 3D space is represented by each row in A. Now define a 3D array for the counts of points in each voxel:
counts = zeros(10,10,10)
Here I've chosen to have a 10x10x10 array of voxels, but this is just for convenience, it would be only a little more difficult to have chosen some other number of voxels in each dimension, and there don't have to be the same number of voxels along each axis. Then the code
for ix = 1:size(A,1)
counts(ceil(A(ix,1)),ceil(A(ix,2)),ceil(A(ix,3))) = counts(ceil(A(ix,1)),ceil(A(ix,2)),ceil(A(ix,3)))+1
end
will count up the number of points in each of the voxels in counts.
EDIT
Unfortunately I have to do some work this afternoon and won't be able to get back to wrestling with the triplequad solution until later. Hope this is OK in the meantime.

Area of Union of n circles(MATLAB code)

I am trying to calculate the area of union of n circles in a plane when it is known that all circles are of equal radii and their centers are also known(of all n circles). I was trying to follow the set theory approach(inclusion-exclusion principle), where we know the formula for union of n sets. I was just using an operator Ar() which gives the area,i.e. Ar(A) gives me the area of A. I first tried to find out which circle is intersecting with which other circle(s) with the help of D<2R(D=dist between the centers of the two circles), then I was trying to calculate the area of intersection between them pairwise and hence find the area of union. But I am getting stuck for n>4. Can anyone provide a soln to this(soln by the set theory approach is necessary). Thanks in advance
If your problem was just for pairs of circles, you'll use the known result about Circle-Circle intersection areas. The formula for the pairwise area between any two circles, based on a standard parameterization of all circles involved, is given there. But as n gets large, the formulas for these areas are not commonly known. There might be a clever way to use recursion to compute the formulas for the intersection of two circles (n=2), the intersection of two asymmetric lens shapes (n=3), the intersection of two instances of whatever shape is the intersection of two asymmetric lens shapes (n=4), and so on. If you can derive formulas for those areas, you can always use inclusion-exclusion to figure out the intersection. The key insight is that the intersection of n instances of the previous shape is really the intersection of n-1 instances of intersections-of-previous-shape. But like the commenter above has said, that question really belongs at Math Overflow.
Practical Aside
For anyone reading who is interested in a practical way to solve this problem, Monte Carlo integration is an excellent choice. All you need to do is compute a large rectangle that bounds all of the circles, and then draw points uniformly in that rectangle. For each circle, check if the point is inside or outside. If it is ever inside, then increment a counter and break out of doing any more checks. At the end, the proportion of that counter to the total points drawn, multiplied by the area of the rectangle, will give the area.
If we assume that for each n-wise intersection area, we need to do n different O(1) steps (assuming we get an analytical formula that we can just plug the radii and center data straight into, which might be optimistic), then this analytical method is still O(n^2).
Monte Carlo is worse, O(Mn) where M is the number of points we draw, if we make the pessimistic assumption that we have to check against all n circles for every point. For moderate n, while M won't need to be intractably large, it certainly won't be close to n. However, we get the added benefit that our function automatically generalizes to the case of mixed radii (for which the general solution is much harder). From a practitioner's point of view, the analytical solution here is not very useful unless the circles barely overlap and the bounding rectangle contains a large amount of empty space.