Distances along a plotted line?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Keith Lewis
el 23 de Dic. de 2016
Comentada: Keith Lewis
el 5 de En. de 2017
I have x and y matrices with y representing elevations at points x.
For instance
x = [0 5 10]
y = [2 4 1]
plot(x,y) gives me a cross section of elevations.
I want to mark various distances along the plotted line. For instance, place a marker every 1m along the line, or put individual markers at specific distances along the line.
How can I do this?
Thanks!
0 comentarios
Respuesta aceptada
Walter Roberson
el 23 de Dic. de 2016
marker_dist = 1;
x = [0 5 10];
y = [2 4 1];
dist_from_start = cumsum( [0, sqrt((x(2:end)-x(1:end-1)).^2 + (y(2:end)-y(1:end-1)).^2)] );
marker_locs = marker_dist : marker_dist : dist_from_start(end); %replace with specific distances if desired
marker_indices = interp1( dist_from_start, 1 : length(dist_from_start), marker_locs);
marker_base_pos = floor(marker_indices);
weight_second = marker_indices - marker_base_pos;
marker_x = x(marker_base_pos) .* (1-weight_second) + x(marker_base_pos+1) .* weight_second;
marker_y = y(marker_base_pos) .* (1-weight_second) + y(marker_base_pos+1) .* weight_second;
plot(x, y);
hold on;
plot(marker_x, marker_y, 'r+');
hold off
2 comentarios
Walter Roberson
el 2 de En. de 2017
See my update to this code at http://www.mathworks.com/matlabcentral/answers/318745-plotting-dots-on-the-edges-of-an-object-uniformly#comment_417470
Más respuestas (2)
Roger Stafford
el 23 de Dic. de 2016
Editada: Roger Stafford
el 23 de Dic. de 2016
The approximate arclength along your curve from (x(i1),y(i1)) to (x(i2),y(i2)) can be computed as the sum of the line segment lengths connecting successive points between the two end points:
n = i2-i1;
s = sum(sqrt((x(i1+(1:n))-x(i1+(0:n-1))).^2-(y(i1+(1:n))-y(i1+(0:n-1))).^2));
0 comentarios
Image Analyst
el 23 de Dic. de 2016
For straight lines it's pretty trivial. For more general curves, see John D'Errico's interparc(): http://www.mathworks.com/matlabcentral/fileexchange/34874-interparc
Description
A common request is to interpolate a set of points at fixed distances along some curve in space (2 or more dimensions.) The user typically has a set of points along a curve, some of which are closely spaced, others not so close, and they wish to create a new set which is uniformly spaced along the same curve.
When the interpolation is assumed to be piecewise linear, this is easy. However, if the curve is to be a spline, perhaps interpolated as a function of chordal arclength between the points, this gets a bit more difficult. A nice trick is to formulate the problem in terms of differential equations that describe the path along the curve. Then the interpolation can be done using an ODE solver.
As an example of use, I'll pick a random set of points around a circle in the plane, then generate a new set of points that are equally spaced in terms of arc length along the curve, so around the perimeter of the circle.
0 comentarios
Ver también
Categorías
Más información sobre Smoothing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!