You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?
10 views (last 30 days)
Show older comments
Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?
3 Comments
huazai2020
on 29 Jun 2020
The oual can be both given in the form of an equation or data points. How to use the bwdist(), could you share me the code?
Accepted Answer
Matt J
on 30 Jun 2020
Edited: Matt J
on 30 Jun 2020
You can use trustregprob from the File Exchange
For example, consider the following ellipse and external point y,
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
z=[0.584808315593597 ; 0.201052451754066]; %Closest point
ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;
hold on
fimplicit(ellipsefun, [-1 1.3 -1 1.3])
plot(y(1),y(2),'rx',z(1),z(2),'bo');
axis equal
hold off
I found the closest point z using the code below,
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
and the minimum distance is just,
>> distance=norm(z-y)
distance =
0.5116
20 Comments
huazai2020
on 3 Jul 2020
How can you know the Closest point(z=[0.584808315593597 ; 0.201052451754066]; %) before calculation?
huazai2020
on 4 Jul 2020
Thank you,because I am not familar with the codes,would you please paste them in a right sequence?
Matt J
on 4 Jul 2020
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
huazai2020
on 4 Jul 2020
This is codes I use:
clc; clear all;
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;
hold on
fimplicit(ellipsefun, [-1 1.3 -1 1.3])
plot(y(1),y(2),'rx',z(1),z(2),'bo');
axis equal
hold off
I have the error like this:
Undefined function 'fimplicit' for input arguments of type 'function_handle'.
huazai2020
on 4 Jul 2020
In addition, I do not what is the meaning of “ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;”, I cannot find in my
matlab.
Matt J
on 4 Jul 2020
Edited: Matt J
on 4 Jul 2020
Undefined function 'fimplicit' for input arguments of type 'function_handle'.
You obviously have an older version of Matlab, but enough of the code should have run to give you z, which is the answer you are looking for. The rest is just for plotting.
I do not what is the meaning of “ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1
That is the function that desribes the ellipse. If you had a newer version of Matlab, you would be able to plot the ellipse using
fimplicit(ellipsefun,...)
huazai2020
on 5 Jul 2020
Thank, I use the matlab R2012b, do there have any other substitution equation for the fimplicit function, or how could I revise it?
Second, I have not understood the meaning of “@(p,q) sum([p;q].*(A*[p;q]))-1 ;” , do you have any discribe of it? If my oval is x^2+y^2/4=1, how could I revise the oval equation?
Matt J
on 5 Jul 2020
Edited: Matt J
on 5 Jul 2020
For R21012b, you can use ezplot instead. Both of the following are equivalent ways to write the formula for your ellipse and plot it.
figure
ezplot( 'x^2+y^2/4-1')
figure
A=[1,0;0,1/4];
ezplot(@(p,q) sum([p.';q.'].*(A*[p.';q.']),1)-1) %same as x^2+y^2/4-1 = 0
To uderstand the 2nd form better, you can read,
Matt J
on 6 Jul 2020
Edited: Matt J
on 6 Jul 2020
Those are the operations that the function executes. All points (p,q) on the ellipse must satisfy,
sum([p.';q.'].*(A*[p.';q.']),1)-1 == 0
This is, in any case, quite a separate question from the one you originally posted, which was about how to find z. If you are now comfortably projecting points onto ellipses with an Answer from this thread, I suggest that you Accept-click the answer and pose your plotting questions in a separate post.
huazai2020
on 6 Jul 2020
Could you tell me how what is the meaning of "sum([p.';q.'].*(A*[p.';q.']),1)-1 == 0"
Matt J
on 6 Jul 2020
Once again, it is the equation for the ellipse. It is exactly the same as writing . And once again, if you are still having trouble with the concept, you should post it as a new question, ideally with some elaboration on what is unclear and what needs explanation.
huazai2020
on 6 Jul 2020
Could you show me the link that describe the equation ? I want to see the meeaning of it.
Bruno Luong
on 7 Jul 2020
In the FEX I post bellow there are 2 functions that convert between ellipse parametric explicit eqauation to quadratic implicit equation used by Matt.
Read the H1 line of those functions to get a flavor, and read what it does exactly in their body. The math behind is quadratic form.
huazai2020
on 7 Jul 2020
What is FEX AND H1? Are they these?
Find the projection of point P in R^n on the ellipsoid
E = { x = x0 + U*(z.*radii) : |z| = 1 }, where U is orthogonal matrix of the orientation of E, radii are the axis lengths, and x0 is the center.
Or on generalized conic E = { x : x'*A*x + b'*x + c = 0 }.
The projection is the minimization problem:
min | x - P | (or max | x - P|) for x in E.
Method: solve the Euler Lagrange equation with respect to the Lagrange multiplier, which can be written as polynomial equation (from an idea by Roger Stafford)
Bruno Luong
on 8 Jul 2020
FEX short word for File Exchange. Click on it I put the link on my message.
H1: https://www.mathworks.com/help/matlab/matlab_prog/add-help-for-your-program.html
More Answers (2)
Image Analyst
on 4 Jul 2020
"The oual can be both given in the form of an equation or data points." <== if you have data points (xb, yb) on the boundary of an ellipse/oval, you can use sqrt() to find the distances to some other point (xp, yp). Then use in() to find the minimum distance.
distances = sqrt(xb-xp).^2 + (yb-yp).^2);
minDistance = min(distances)
If you have an image then you need to get the boundary points first:
boundaries = bwboundaries(binaryImageOfEllipse);
boundaries = boundaries{1}; % Extract double array from cell array.
xb = boundaries(:, 2);
yb = boundaries(:, 1);
10 Comments
huazai2020
on 4 Jul 2020
For the input method, I have input the data points and boundary points(only a half oval), would you please share me the code to calculate the minimum distance?
For the second method, I do not have image,but I know the the equation of oval ,how can I do it?
Image Analyst
on 4 Jul 2020
outpoint has two worksheets in it, and walldata has 1. I'm not sure what's what or what's where. Which is the single point, and which columns are the (x,y) boundary points? Please give me code to read in the point (xp, yp) and list of (xb,yb) boundary points. But after that, it's just like I said:
distances = sqrt(xb-xp).^2 + (yb-yp).^2);
minDistance = min(distances)
huazai2020
on 5 Jul 2020
I have noted the x and y on the top of columns , you can see that in the files.
I have solved the problem. Could you tell me if I do not know the boundary points, how can I calculation the minimum distance by the oval equation?
Image Analyst
on 5 Jul 2020
I don't know off the top of my head. If all you have is a point and an analytical equation for the ellipse, that is a geometry question and I'd just Google "point ellipse distance" and see what hits you get.
huazai2020
on 8 Jul 2020
Hi, after I get the minDistance, I also want to know the wall point coordinate, how could I do it?
Image Analyst
on 8 Jul 2020
Again, I don't know what to do for the analytical equation situation, like in answer you accepted. I'd only know what to do for the situation where you have an array for the ellipse boundary and the point coordinates, but you said you'd solved that one already.
huazai2020
on 9 Jul 2020
Yes, I just want to ask how to get the wall point coordinate when I use the array for the ellipse boundary and the point coordinates.
huazai2020
on 18 Jul 2020
Edited: Image Analyst
on 18 Jul 2020
Hi. This is the code that I write to find the minimum distance between the points (column A and column B in the data1.xlsx) with the wall (column C and column D in the data1.xlsx), but it has errors. Who can help me? Thank you so much.
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for i=1:1:Num1;
x1=xpoint(i);
y1=ypoint(i);
for j=1:1:Num2;
x2=xwoint(j);
y2=ywoint(j);
distances = sqrt((x2-x1).^2 + (y2-y1).^2);
minDistance(i) = min(distances)*1000;
end
fdata=minDistance;
end
xlswrite('thick.xlsx',fdata,'sheet1');
%%
The below is the errors:
Error using xlswrite (line 220)
Excel returned: Error: Object returned error code: 0x800A03EC.
Image Analyst
on 18 Jul 2020
No, you shouldn't use an inner loop because your minDistance will just be the distance between (x1,y1) and xwoint(Num2) and ywoint(Num2) instead of the whole array. Do it like this:
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for k=1:1:Num1
x1 = xpoint(k);
y1 = ypoint(k);
distances = sqrt((xwoint-x1).^2 + (ywoint-y1).^2);
minDistance(k) = min(distances)*1000;
end
plot(minDistance, 'b-');
% xlswrite('thick.xlsx',minDistance,'sheet1');
Not sure why you wanted to multiply by 1000 though.
huazai2020
on 18 Jul 2020
Edited: huazai2020
on 18 Jul 2020
1000 is only for the unit changing. Thank you so much for your help.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)