finding coordinates through distance formulation

3 views (last 30 days)
How can I find coordinates of intermediate points that are equidistantly spaced (say 1 mm) between two points of known coordinates (they are lying on a straight line)in 3 dimensions? What is the way I can find pixel coordinate that is nearest neighbor of previous known pixel coordinate? Thanks for your help.

Answers (4)

Sean de Wolski
Sean de Wolski on 25 Oct 2011
I'd say the formula for a line is a good place to start.
Please give us more detail so we have something to work with. Do you want the nearest neighbors on a rectangular grid?
Update per new info: I wrote a function to do this in two dimensions (it was applied in three dimensions but was a straight slice through the third dimension). You can play with it as you wish.
function I = makeCut(pts, width,sz)
% Developing cut algorithm for fiberSever.
%SCd 05/08/2010
%
%Inputs:
% -points: 2x2 matrix of coordinates ([row1 col1; row2 col2]);
% -width: width of line
% -sz: size of image
%
%Outputs:
% I: inverse mask of size, sz, of line (line is false, everything else is true.
%
%Error Checking
assert(nargin==3,'Three inputs expected');
assert(numel(pts)==4,'pts should be a 4 element array');
assert(isscalar(width),'width is supposed to be a scalar')
assert(numel(sz)==2,'size is expected to be 2 elements long');
I = true(sz); %mask
dc = (diff(round(pts(3:4)))); %difference
dr = (diff(round(pts(1:2))));
catchflag = 0;
if abs(dr) <= abs(dc)
transposeflag = 0;
else
transposeflag = 1;
I = I';
temp = dr;
dr = dc;
dc = temp;
pts = fliplr(pts);
end
if dr == 0 && dc ==0
errordlg(sprintf('Only a Single Point has been selected.\n2 Points must be selected for the cutplane!\nNo cut was made.'))
catchflag = 1;
end
if ~catchflag
m = dr/dc;
b = pts(1)-(m*pts(3));
pts([3 4]) = round(sort(pts([3 4])));
y = m*(pts(3):pts(4))+b;
I(sub2ind(size(I),round(y),pts(3):pts(4))) = 0;
I = ~imdilate(~I,strel('square',width));
if transposeflag
I = I';
end
end
end
and an example you could call it with:
imshow(makeCut([2 2;45 90], 1,[100 100]))
Extension to three dimensions should be easy.

Bhaskar
Bhaskar on 25 Oct 2011
You have two questions. Let me answer only the first question.
Let the two known points be (x1,y1,z1) and (x2,y2,z2). All points that are equidistant from these two points lie on a plane that is perpendicular to the line joining these tow points and passing through the mid-point ((x1+x2)/2,(y1+y2)/2,(z1+z2)/2) = (x0,x0,z0) (say). A vector that is normal to this plane is the line joining the two points -- (x2-x1,y2-y1,z2-z1) = (a,b,c) (say). Then the equation of the plane is: a(x-x0) + b(y-y0) +c(z-z0) = 0 where (a,b,c) is the normal vector and (x0,y0,z0) is the mid point. All points whose coordinates will satisfy this equation are equidistant from (x1,y1,z1) and (x2,y2,z2).
  1 Comment
Walter Roberson
Walter Roberson on 25 Oct 2011
True, but unfortunately the poster was asking about equidistantly _spaced_ -- a constant distance between points. Which has a much easier solution.

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Oct 2011
Calculate the direction vector from one point to the other. Normalize that to find the delta-X, delta-Y and delta-Z that would give pixel centers that are one pixel-distance apart.
Note that if your pixels are not cubic then you may need to scale before you find the direction vector, and then again when you have found 3-space coordinates you would have to scale back to find the equivalent non-uniform pixel locations.
To find locations some particular distance such as 1 mm apart, you will need some additional information that converts intra-pixel distances in to real-world distances.
If by "1 mm apart" you meant "1 mm apart on the screen" (i.e., if you were to put a ruler up against the screen the marks would be 1mm apart on the ruler), then that is, in practice, a messy task that is near-hopeless on a CRT but do-able on any one particular LCD monitor (and has to be recalibrated for the next LCD monitor along even of the exact same model.) See my response to http://www.mathworks.com/matlabcentral/answers/19020-setting-the-distance-between-the-tick-mark

Ganesh
Ganesh on 1 Nov 2011
My problem is something related to ray tracing. Let's say a ray starts at point A(3,14,7)and ends at point B(13,4,2)on the image.Here A and B are central pixel coordinates(in 3 dimensions) at beginning and end locations of the ray. So through linspace or direction cosines I would be able to find coordinates for intermediate points which lie in that ray passing straight through A and B. Now I need to find nearest neighbour coordinates (I guess they should be integer numbers)corresponding to these intermediate points so that they correspond to central pixel coordinates.
-Bibek
  1 Comment
Walter Roberson
Walter Roberson on 1 Nov 2011
The nearest neighbors coordinates will seldom be equidistant from each other.
Anyhow, it sounds to me like what you really want is
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!