Get a plot next to an existing plot

조회 수: 5 (최근 30일)
Tim Schaller
Tim Schaller 2022년 10월 5일
댓글: Tim Schaller 2022년 10월 6일
Hi Guys,
My Problem is, that I need to get a second plot next to an existing plot.
I thought, that I need to get Points, that are on the plotted Line and try to calculate normal vectors that show me the new points that I need.
The main Problem is, that the differences from start to end are not equal and have to be linear.
Well the new one should start at the coordinates (1 b) and end at (5 a).
n = 3;
n1 = n-1;
a = 20; % Breite des Krummers entlang x
b = 10; % Höhe des Krümmers entlang y
P = [0 b;0 0;a 0];
x = P(:,1); % x-Werte für scatter
y = P(:,2); % y-Werte für scatter
T = 21; % Anazhl an Teilpunkten für den Plot
syms t
B = bernsteinMatrix(n1,t);
bezierCurve = simplify(B*P) % simplify für undefinierte Variablen verwenden
bezierCurve = 
fplot(bezierCurve(1), bezierCurve(2), [0, 1]) % [0,1] = Intervall für t
axis equal
grid on
hold on
scatter(x,y,'filled')
hold off
clear t
t = linspace(0,1);
B = bernsteinMatrix(n1,t);
bezierCurve = B*P;
px = bezierCurve(:,1);
py = bezierCurve(:,2);
pt = interparc(T,px,py,'spline') % x,y-Werte!!
Unrecognized function or variable 'interparc'.
x = pt(:,1);
z = pt(:,2);
plot(px,py,'r*',pt(:,1),pt(:,2),'b-o')
axis([-1.1 1.1 -1.1 1.1])
axis equal
axis tight
grid on
I hope at least somebody understands my problem and can give me a little hint.
Thanks in advance.

채택된 답변

John D'Errico
John D'Errico 2022년 10월 5일
편집: John D'Errico 2022년 10월 5일
interparc is a function found on the file exchange (though written by me.) It is not part of MATLAB, until you get it from the file exchange. So you would need to download it from the file exchange, from here:
Or you should be able to use the addons, though I've never really tried that myself. :) Just call me a computational Luddite.
Anyway, you defined t as a vector, but then passed in T into interparc. That just won't work anyway.
Having said that, I'm not at all certain why you need equally spaced points for your problem. Computing a normal vector does not need that at all. You say you are trying to find a new curve that is "next" to the existing curve. Do you want to find a curve that is at some uniform distance from this existing curve? Be careful, if the distance chosen is too large, then the curve will have some interesting irregularities. That is, if the distance from your existing curve is larger than the radius of curvature of that curve, then the new curve will not make you happy. But it WILL probably be interesting, in a cardioidal sort of way.
Anyway, what might I do? Yes, you could use interparc. But I just don't trust the guy who wrote that code. ;-) I mean, would you buy a used car from him? oh. Wait. I just said I wrote it. DEFINITELY do not buy a used car from me.
n = 3;
n1 = n-1;
a = 20; % Breite des Krummers entlang x
b = 10; % Höhe des Krümmers entlang y
P = [0 b;0 0;a 0];
x = P(:,1); % x-Werte für scatter
y = P(:,2); % y-Werte für scatter
T = 21; % Anazhl an Teilpunkten für den Plot
syms t
B = bernsteinMatrix(n1,t);
bezierCurve = simplify(B*P)
bezierCurve = 
So that is your parametric curve, following that path in the (x,y) plane, as t moves from 0 to 1.
We can find the normal vector simply enough, as a simple 90 degree rotation.
normalToCurve = diff(bezierCurve)*[0 1;-1 0]
normalToCurve = 
Make it a unit normal vector.
normalToCurve = normalToCurve/norm(normalToCurve)
normalToCurve = 
Now plot the new, offset curve, as well as the old one. I'll pick a small distance, of 0.2 units.
fplot(bezierCurve(1), bezierCurve(2), [0, 1],'k') % [0,1] = Intervall für t
axis equal
grid on
hold on
delta = 0.2;
fplot(bezierCurve(1)+normalToCurve(1)*delta, bezierCurve(2)+normalToCurve(2)*delta, [0, 1],'r')
scatter(x,y,'filled')
Again, beware, as if delta is too large, things will go crazy. For that, I'd need to compute the radius of curvature, and that is just too much thinking for one day for me. ;-)
Hmm. Looking back at your question, you said:
"Well the new one should start at the coordinates (1 b) and end at (5 a)."
Honestly, I have no idea what that means in context of the rest of what you said. Oh well.
  댓글 수: 2
Tim Schaller
Tim Schaller 2022년 10월 6일
Tanks a lot for your answer.
This already really helped me.
"Well the new one should start at the coordinates (1 b) and end at (5 a)."
In the beginning of my code I defined a and b. My task is that you will be able to change these two variables.
So with (1 b) I meant that the start of the second plot is supposed to be at x-coordinate = 1 and y-coordinate = b (in my example is a = 20 and b = 10).
So the distance of both plot is supposed to not be in an uniform distance.
And I need those equally spaced points, because in the end of my Task I will need some coordinates to copy in Siemens NX CAD.
I hope that you can understand ist now a little bit better and also can help me to get further with it.
Tim Schaller
Tim Schaller 2022년 10월 6일
It's me again.
So what I figured out is that your code with the delta perfectly works.
My main problem now is that I need to define a function for "delta" that changes on thy arclength of my plot.
To point that out: at the start it should be delta = R (R is a variable I also need to define, e.g.: R=2)
And at the End delta = r (r is another variable, different to the first one, e.g.: r =3)
I think that points out my problem pretty good.
Thanks in advance for your time!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by