Get a plot next to an existing plot

4 views (last 30 days)
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.

Accepted Answer

John D'Errico
John D'Errico on 5 Oct 2022
Edited: John D'Errico on 5 Oct 2022
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 Comments
Tim Schaller
Tim Schaller on 6 Oct 2022
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!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!