You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Finding distance between two curves at diffrent points
9 views (last 30 days)
Show older comments
1.Suppose the curves are defined, how do i find the distance between two curves at diffrent points.
Curve 1= f(x)
curve 2 = g(x)
Do i need to define points on one of the curve to find out the required distance???
For exmple I am posting an image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276770/image.png)
Important note-
I need distance at diffrent points between two curves and not single point nor am I looking for minimum distance between curves.
Perpendicular distance from tangent as shown in image.
2 Comments
Praveen Patnaik
on 12 Mar 2020
It is just a general concept.... These are just images. Suppose the function of these curves are defined . Then how will we do it???
Accepted Answer
darova
on 12 Mar 2020
With use of polyxpoly
clc,clear
x = 0:0.1:10;
y1 = sin(x); % data 1
y2 = sqrt(x); % data 2
% index of point
ix = 70;
x0 = x(ix);
y0 = y2(ix);
% tangent vector
dy = diff(y2(ix:ix+1));
dx = diff(x(ix:ix+1));
% normal vector
xv = [0 dy]*50+x0;
yv = [0 -dx]*50+y0;
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y1);
plot(x,y1,x,y2)
hold on
plot(xv,yv)
plot(xc,yc,'or')
hold off
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276846/image.png)
28 Comments
Praveen Patnaik
on 13 Mar 2020
Can this also be done for other points on green line??
any command that can take plot 10 points on green line randomly and then after we get those 10 points we would store them and then apply tangent and normal to get distance???.....
Praveen Patnaik
on 13 Mar 2020
Edited: darova
on 20 Jun 2020
clc,clear
x = (0:0.1:10)';
y1 = sin(x); % data 1
y2 = sqrt(x); % data 2
% index of point
ind = randi(length(x),10,1); % 10 random points
XC = ind*nan;
YC = ind*nan;
for i = 1:length(ind)
ix = ind(i);
x0 = x(ix);
y0 = y2(ix);
% tangent vector
dy = diff(y2(ix:ix+1));
dx = diff(x(ix:ix+1));
% normal vector
xv = [0 dy]*50+x0;
yv = [0 -dx]*50+y0;
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y1);
if ~isempty(xc)
XC(i) = xc;
YC(i) = yc;
end
end
plot(x,y1,x,y2)
hold on
plot([XC x(ind)]',[YC y2(ind)]','.-r')
hold off
axis equal
This above script you sent worked......(Thanks a ton)
Any modification that I can get on above script so that I would get the value of length of those normal lines and add it as a sum and store it in one variable
darova
on 13 Mar 2020
x(ind) and y2(ind) are points on a curve
XC and YC are intersection points
D = hypot(x(ind)-XC,y(ind)-YC); % some of them can be NaN (no intersections)
ii = isnan(D);
sumD = sum(D(~ii)); % sum only number (without NaN)
Praveen Patnaik
on 13 Mar 2020
The y variable is not defined.....sorry for bothring you , but i am running the code line by line and trying to understand .
Praveen Patnaik
on 13 Mar 2020
sorry got the answer right.....its y2 in place of y....
Thanks a Ton Darova.
Praveen Patnaik
on 15 Mar 2020
With this similar approach Can you look into another optimization problem I have posted...???
Kindly look into the post as I am unable to understand the answer to solve it...
Kindly look into it and write the script for same if you can understand it....
darova
on 15 Mar 2020
I don't know how to solve the problem you posted. The only idea i have it's to use my script
Ron Herman
on 11 May 2020
Edited: Ron Herman
on 11 May 2020
Can you tell me in your script why you multiplied 50 in normal vector???
can you explain what is xv,yv???
what is dy and dx??
unable to understand how you found out tangent and normal vector.
Can you write few comments on these codes so I may get better clarity??
thank you in advance
darova
on 11 May 2020
- Can you tell me in your script why you multiplied 50 in normal vector???
Because dx and dy are small so i wanted to the line longer
- can you explain what is xv,yv???
xv and yv are coordinates of two points (line between (x(i),y(i)) and (x(i)+50*dx,y(i)+50*dy))
- what is dy and dx??
Look on the picture
- unable to understand how you found out tangent and normal vector.
Tangent vector is just a difference between (i) and (i+1) points
Normal vector is tangent vector rotated on 90 degree. If tangent vector [dx,dy] then normal vector is [dy;-dx]
Here is a simple scheme
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/292075/image.png)
Image Analyst
on 11 May 2020
Edited: darova
on 11 May 2020
Pretty smart and simple solution. Learned something new. THanks
Ron Herman
on 19 Jun 2020
I am following the code you have given but I am facing a problem at a cetain point. For reference I put the image.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319171/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319171/image.png)
I am drawing normal from small curve based on tangent at certain points. As per code the normal when we draw goes only in one direction from left to right. Problem arises when the small curve is to the right of big circle ,since tangent can only be drawn from left to right and not in both directions. When curve is towards right of big circle the normal from small curve dosent intersect the big circle but when small curve is to the left of big circle the normal from small curve intersects big circle as shown in figure.
Any modification in above code that will enable normal line to extend in both direction so that we will always get an intercept.
Ron Herman
on 19 Jun 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319174/image.png)
The code that you wrote is for the blue line. Can any modificaion be done so that the normal line will extend in both the direction. (it means a normal line that it will extend along red line as well as blue line)
Ron Herman
on 20 Jun 2020
Thanks a ton darova it worked. I have understood the way how code works. Its quite innovative
Ron Herman
on 20 Jun 2020
Edited: Ron Herman
on 21 Jun 2020
I modified the code and made my own curves, but I am getting an unexpected output as shown in figure. Can you point out the mistake as code is exacly not working as shown in above example.
% End part of code
plot(x,y,x1,y1)
hold on
plot([XC x1(ind)]',[YC y1(ind)]','.-r') % x(ind)& y2(ind)are points on the curve
hold off % XC and YC are intersection points
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319498/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319501/image.png)
For single values of "ind" like ind=[20] or ind=[80] I am getting normal from curve intersecting the circle but when I take two index values ind= [20 80] in "ind" I dont get desired output.
What is the reason??? Is it something to do with polyxpoly?? ( I read its syntax but unable to find the problem here)
Ron Herman
on 21 Jun 2020
Thanks a ton. You have always been a great help with your response .Let me know if I could be of any help from my end.
Ron Herman
on 3 Jul 2020
Edited: Ron Herman
on 4 Jul 2020
When I write this all in one code I get the desired output but same doesnot happen when I break this code into functions (Why)????
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326075/image.png)
Breaking above code into functions doesnot give me the desired result (Why)????
Ron Herman
on 4 Jul 2020
Edited: Ron Herman
on 4 Jul 2020
I suppose I have got the code correct.
x1 becomes x_1 inside intercection function as x_1=x1+a [both x1 and a are sent in parameter of intercection() ]
I am getting the arcs at desired position and even points on the arc at desired position but when I break the whole programme into opti(), intersection(), obj() as shown above I get intersection for only 3 points.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326690/image.png)
Writing the code as one without breaking it into function
XC= [54.0414 53.6743 53.2747 52.8450 52.3849 51.8917 ]
YC= [ 10.2174 11.9993 13.6638 15.2397 16.7520 18.2235 ]
Vs
Writing the code by breaking it into function
XC= [ NaN NaN 53.2747 52.8450 52.3849 51.8917 ]
YC= [ NaN NaN 13.6638 15.2397 16.7520 18.2235 ]
XC,YC= coordinates of intercepts on the quarter circle
Due to this Sum value is affected.
darova
on 4 Jul 2020
If you make some changes to your code
add line inside intersection function to see normal lines you draw
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326777/image.png)
replace hold on command
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326780/image.png)
you will see that (xv,yv) vector are just too short
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326783/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326786/image.png)
Ron Herman
on 4 Jul 2020
Thanks a ton your comments gave me insites .
Since vectors were short instead of multiplying 50 I increased the number.
xv = [0 dy]*150+x0;
yv = [0 -dx]*150+y0;
Ron Herman
on 17 Nov 2020
Edited: Ron Herman
on 17 Nov 2020
I need the following output
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/416948/image.png)
I am unable to get the following output for some reason I an unable to draw horizontal line in all 3 points
(horizontal line from middle point is not getting constructed)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/416953/image.png)
For some reason index =50 or at ind=50 no line is being constructed. I donot the reason for this yet.
clc,clear
r = 10; % arc length
R = 55; % radius of a circle
aa = 60*pi/180; % arc angle
ap = 0*pi/180; % arc position angle
k=0;
%xa=60;
% a=10;
% b=50;
ind=[25 50 75];
t = linspace(0,pi);
[x,y] = pol2cart(t,R); % circle data
t1 = linspace(0,aa)-aa/2+ap;
[x1,y1] = pol2cart(t1,r); % arc data
xc=65;
yc=15;
%shifting the arc mid point to (65,15)
mp=length(x1)/2;
delx=xc-x1(mp);
dely=yc-y1(mp);
x1=x1+delx;
y1=y1+dely;
XC = ind*nan;
YC = ind*nan;
for i = 1:length(ind)
ix = ind(i);
x0 = x1(ix);
y0 = y1(ix);
% minor difference
dy = diff(y1(ix:ix+1));
dx = diff(x1(ix:ix+1));
% horizontal
xv = [dx -dx]*1500+x0;
yv = [0 0]*1500+y0;
%line(xv,yv)
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y);
if ~isempty(xc)
XC(i) = xc
YC(i) = yc
end
end
plot(x,y,x1,y1)
hold on
plot([XC;x1(ind)],[YC;y1(ind)],'.-r') % x1(ind)& y1(ind)are points on the curve
hold off % XC and YC are intersection points
axis equal
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Tags
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 (한국어)