Finding distance between two curves at diffrent points
7 次查看(过去 30 天)
显示 更早的评论
Praveen Patnaik
2020-3-12
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.

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 个评论
Praveen Patnaik
2020-3-12
It is just a general concept.... These are just images. Suppose the function of these curves are defined . Then how will we do it???
采纳的回答
darova
2020-3-12
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

28 个评论
Praveen Patnaik
2020-3-13
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
2020-3-13
编辑:darova
2020-6-20
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
2020-3-13
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
2020-3-13
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
2020-3-13
sorry got the answer right.....its y2 in place of y....
Thanks a Ton Darova.
Praveen Patnaik
2020-3-15
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
2020-3-15
I don't know how to solve the problem you posted. The only idea i have it's to use my script
Ron Herman
2020-5-11
编辑:Ron Herman
2020-5-11
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
2020-5-11
- 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

Image Analyst
2020-5-11
编辑:darova
2020-5-11
Pretty smart and simple solution. Learned something new. THanks
Ron Herman
2020-6-19
I am following the code you have given but I am facing a problem at a cetain point. For reference I put the image.

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
2020-6-19

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
2020-6-20
Thanks a ton darova it worked. I have understood the way how code works. Its quite innovative
Ron Herman
2020-6-20
编辑:Ron Herman
2020-6-21
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


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
2020-6-21
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
2020-7-3
编辑:Ron Herman
2020-7-4
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)????

Breaking above code into functions doesnot give me the desired result (Why)????
Ron Herman
2020-7-4
编辑:Ron Herman
2020-7-4
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.

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
2020-7-4
If you make some changes to your code
add line inside intersection function to see normal lines you draw

replace hold on command

you will see that (xv,yv) vector are just too short


Ron Herman
2020-7-4
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
2020-11-17
编辑:Ron Herman
2020-11-17
I need the following output

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)

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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Smoothing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)