Distances along a plotted line?
10 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Walter Roberson
2016 年 12 月 23 日
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 件のコメント
Walter Roberson
2017 年 1 月 2 日
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
その他の回答 (2 件)
Roger Stafford
2016 年 12 月 23 日
編集済み: Roger Stafford
2016 年 12 月 23 日
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 件のコメント
Image Analyst
2016 年 12 月 23 日
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!