How to create two paths parallel to a non-uniform curve on its both sides?
Mostrar comentarios más antiguos
Suppose I have a path y that can be defined in different ways x1 = 1:0.1:20;
y1 = [3*x1(1:20) + 6, ... 5*x1(21:100) - 9, ... -2*x(101:end)];
% or
x2 = 0:0.1*pi:pi;
y2 = [sin(x2(1:5)), cos(2*x2(6:end))];
% or
x3 = [0, 5, 10, 10];
y3 = [5, -5, 15, 20]; how can I create two paths/curves parallel to y on its both sides with a constant offset (euclidean distance) = 1? I need to find the two parallel paths for each case of the three given above knowing that I need the parallel paths start and end at the normal vectors to the curve at the start and end points, respectively.
1 comentario
Dyuman Joshi
el 24 de Jul. de 2023
Respuestas (1)
Hi Diaa,
I understand you want to plot two lines parallel to your curve "y" with "offset = 1". To achieve this, you can define a simple function which returns two lines "lower_y" and "upper_y" which are parallel to the line "y" and are at an “offset” of 1. Then all the three lines can be plotted together to get the desired result.
The following code demosntrates the same -
x1 = 1:0.1:20;
y1 = [3*x1(1:20) + 6, ...
5*x1(21:100) - 9, ...
-2*x1(101:end)];
figure;
myplot(x1,y1);
x2 = 0:0.1*pi:pi;
y2 = [sin(x2(1:5)), cos(2*x2(6:end))];
figure;
myplot(x2,y2);
x3 = [0, 5, 10, 10];
y3 = [5, -5, 15, 20];
figure;
myplot(x3,y3);
% function that plots the lines
function myplot(x,y)
[uy, ly] = parallel_lines(y, 1); % define the value of offset
plot(x,y);
hold on;
plot(x,uy);
plot(x,ly);
hold off;
end
%function that returns lines parallel to y
function [upper_y, lower_y] = parallel_lines(y, offset)
upper_y = y + offset;
lower_y = y - offset;
end
Refer to the following documentation for more information on functions -
I hope this provides you with the required information regarding your query.
Thank you,
Shushant
3 comentarios
Dyuman Joshi
el 7 de Ag. de 2023
This does not work for vertical lines or curves with vertical sections, as is the case in the Example #1 you have shown.
Diaa
el 7 de Ag. de 2023
Dyuman Joshi
el 7 de Ag. de 2023
@Diaa check out my suggestion.
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


