You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Calculate distance between the circumference of two ellipse
4 views (last 30 days)
Show older comments
I have the following code
This makes two ellipses: the first one by interpolation of 100 points, and the second one by a parametric equation. I want to calculate the maximum and minimum difference between both the circumferences. Attached image will make it clear what I want. Any help will be deeply appreciated. Thank you
s=pi/180;
i=1:100;
t=3.6*s*i;
x=30*cos(t);
y=15*sin(t);
c=linspace(0,2*pi);
a=30*cos(c);
b=15*sin(c);
plot(x,y,a,b)
4 Comments
Image Analyst
on 25 Sep 2015
Your image didn't make it. I'm not sure what you mean. Both sets of points are plotted on the same perimeter, though they are spaced differently around the perimeter due to your "t" value. There is no difference in the overall shape of the ellipse. Do you want the distance along the perimeter? If so the min would be zero since at least one of the points overlaps exactly. The max is a little difficult but perhaps you can find a formula for "arc length" along the perimeter of an ellipse but I don't think it will be an easy formula like s=r*theta like it is for a circle.
qasim zia
on 25 Sep 2015
I have to ellipse and this figure shows them one is green and the other one is drawn in blue. I agree that minimum distance is zero and I need the maximum difference actually i want to calculate the error which should be 0.001. I can do it in excel taking two consecutive points finding the midpoint of the line joining them and then calculating the difference between that point and arc. but i want to do it matlab. i was hoping it would be easier.
Image Analyst
on 25 Sep 2015
Your image still didn't come through. Try copying your image to the clipboard, and then using http://snag.gy and just pasting your image in to that web site, and then using the brown and green frame icon to paste in the URL it gives you and your image will appear in your post.
Accepted Answer
Image Analyst
on 25 Sep 2015
It's a little tricky since you can't just measure the distance of the kth point of ellipse 1 to the kth point of ellipse 2 because of the spacing parameter which might cause the kth point of ellipse 1 to lie between other points, like the (k+3)rd point and the (k+4)th point. So what I would do is for every point in ellipse 1, calculate the distance to every point in ellipse 2 (this is one line of code in a for loop). Then for all those distances, select the two closest. Hint: use both return arguments of sort(). That will tell you which two points of ellipse 2 it's between. Then for the distances to those two points, take the maximum. Since it sounds really a lot like homework, I'll leave the coding up to you.
21 Comments
qasim zia
on 25 Sep 2015
yes this is homework and I have done it in excel but ive failed in matlab because im not familiar with it. and ive tried calculating the distance of points between ellipse 1 and 2 and failed.
Image Analyst
on 25 Sep 2015
Edited: Image Analyst
on 26 Sep 2015
Hint. Inside the for loop over k, have this:
allDistances = sqrt((x(k)-a).^2+(y(k)-b).^2);
For sorting:
[sortedValues, sortOrder] = sort(allDistances, 'Ascend');
See if you can get the rest.
qasim zia
on 25 Sep 2015
Thank you very much for the help but the results are way off then expected it gives the maximum distance to be 1.4614 which should not be more than 0.1. and Excel has the same results. well thankyou for the help.
qasim zia
on 25 Sep 2015
ok I have figured out the problem. Can u please guide me how can i use just the scalar quantities for example if the coordinate on first ellipse is -21 and respective co-ordinate from second ellipse -23. i just want to use the values not their sign. while calculating distance that is the only issue that would be a great help
Image Analyst
on 25 Sep 2015
Why didn't it work? Share your new code if you want more help. You can use sqrt() or hypot() -- whichever you want to calculate distances.
qasim zia
on 26 Sep 2015
Edited: Image Analyst
on 26 Sep 2015
s=pi/180;
i=1:100;
t=3.6*s*i;
x=30*cos(t);
y=15*sin(t);
c=linspace(0,2*pi);
a=30*cos(c);
b=15*sin(c);
plot(x,y,a,b)
allDistances = sqrt((x-a).^2+(y-b).^2);
[sortedValues, sortOrder] = sort(allDistances, 'Descend'); *bold*
I have also tried using the absolute values because I thought that may be it is because the values of x,a and y,b coordinates will be negative but the results didn't change.
Image Analyst
on 26 Sep 2015
Look what I told you to do, and look at what you did. They're different aren't they? You didn't do what I said. You don't have a for loop over k, and your allDistances formula is different. And you didn't do anything with the sorted values to figure out which two elements of the other ellipse were closest. Why did you not follow my directions?
qasim zia
on 26 Sep 2015
i tried to follow your directions, here is what i thought/think is happening. The all distance formula is calculating the distance between first and second ellipse for every degree and for 100 points. then the sorted values is sorting the distance in a descending order. I checked the values and i didnt understand how and why to use the for loop.
Image Analyst
on 26 Sep 2015
Edited: Image Analyst
on 26 Sep 2015
You have an ellipse, and you have two sets of points that lie exactly on the ellipse. Let's assume set 1 is the first ellipse and those are your reference points. Now points from set 2 will in general lie in between points from set 1, though occasionally it may lie exactly on top of a point from set 1. For example, point 3 of set 2 may lie in between points 4 and 5 of set 1, or it may lie in between points 7 and 8, depending on what that spacing factor you gave it. So, to be perfectly general, you need to find the index numbers from set 2 that your point from set 1 lies in between. So check for each (x,y), check the distance to all of the (a,b) coordinates and find out which two indexes from set 2 are closest to that point of set 1.
for k = 1 : length(x)
allDistances = sqrt((x(k)-a).^2+(y(k)-b).^2);
[sortedValues, sortOrder] = sort(allDistances, 'Ascend');
closestIndex1 = sortOrder(1); % Extract 2 closest
closestIndex2 = sortOrder(2); % Extract 2 closest
% Get the max of those two distances.
distance(k) = max([allDistances(closestIndex1), allDistances(closestIndex2 )]);
end
I really can't do any more or I'll be doing virtually your whole homework for you.
qasim zia
on 26 Sep 2015
I am really thankful for your help. I hope i can get it from here. I really appreciate you. I am new with these algorithms and specially matlab thats why its taking me extra long to learn. :D
qasim zia
on 27 Sep 2015
just out of curiosity what was left to do in that code. i tried to sort the distances out but it was out of the error region.
Image Analyst
on 27 Sep 2015
Nothing really, except to maybe fancy it up a bit:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s=pi/180;
i=1:100;
t=3.6*s*i;
x=30*cos(t);
y=15*sin(t);
c=linspace(0,2*pi);
a=30*cos(c);
b=15*sin(c);
plot(x,y, 'b-', 'LineWidth', 2);
grid on;
hold on;
plot(a,b, 'r', 'LineWidth', 2) ;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Plot the x,y point
MarkerSize = 14;
p1 = plot(x(1), y(1), 'b+', 'MarkerSize', MarkerSize);
% Plot the first (a,b) point.
p2 = plot(a(1), b(1), 'ro', 'MarkerSize', MarkerSize);
% Plot the first (a,b) point.
p3 = plot(a(2), b(2), 'ro', 'MarkerSize', MarkerSize);
for k = 1 : length(x)
allDistances = sqrt((x(k)-a).^2+(y(k)-b).^2);
[sortedValues, sortOrder] = sort(allDistances, 'Ascend');
closestIndex1 = sortOrder(1); % Extract 2 closest
closestIndex2 = sortOrder(2); % Extract 2 closest
% Delete old points
delete(p1);
delete(p2);
delete(p3);
% Plot the x,y point
p1 = plot(x(k), y(k), 'b+', 'MarkerSize', MarkerSize);
% Plot the first (a,b) point.
p2 = plot(a(closestIndex1), b(closestIndex1), 'ro', 'MarkerSize', MarkerSize);
% Plot the first (a,b) point.
p3 = plot(a(closestIndex2), b(closestIndex2), 'mo', 'MarkerSize', MarkerSize);
% Get the max of those two distances.
distance(k) = max([allDistances(closestIndex1), allDistances(closestIndex2 )]);
promptMessage = sprintf('Max distance = %.3f\nDo you want to Continue processing,\nor Quit?', distance(k));
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
break;
end
end
Image Analyst
on 27 Sep 2015
Then perhaps we're not thinking of the same thing. I'm finding the max distance between points of one ellipse and points of the other ellipse. Maybe you don't want that. Maybe you want the min instead of the max. Can you show a zoomed in diagram with a few of the points and indicate which distance you are trying to measure?
Image Analyst
on 28 Sep 2015
You do not have that situation. None (except the first) of your points are at the same angle.
If you want, you could use polyarea() for the two and compare the results to the theoretical area = pi*a*b where a and b are the major and minor semi-axes.
More Answers (0)
See Also
Categories
Find more on 3-D Scene Control 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!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 (한국어)