필터 지우기
필터 지우기

How to draw a smooth, closed curve given 4 points

조회 수: 27 (최근 30일)
David
David 2017년 7월 19일
댓글: Image Analyst 2017년 7월 20일
Is there an easy way to draw a smooth, closed curve from 4 points? Two points will be on the x axis, and two on the y axis. Of each set of two points, one will be on the positive axis and one will be on the negative axis. I would like to have the final closed curve be smooth like an egg shaped curve. An example data set might be:
x = [0 560 0 -60];
y = [310 0 -185 0];
Thanks.
  댓글 수: 2
Jan
Jan 2017년 7월 19일
There is an infinite number of smooth closed curves through the points. Do you have any further restrictions? How smooth should the curve be? Would a cubic spline be sufficient?
David
David 2017년 7월 19일
I don't really have any further restrictions currently. There is some curve that would be "correct", but I don't currently have a way to calculate it. This is just to have an approximate way to visualize what it would look like that is more visually appealing than just plotting x and y with straight lines connecting them. So I'm not sure about smoothness or a cubic spline. Is there a quick way for me to try it out and see what it looks like?

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

채택된 답변

Image Analyst
Image Analyst 2017년 7월 19일
See if you can adapt this snippet:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
x = [0, 560, 0, -60];
y = [310, 0, -185, 0];
% Append first point to last to close the curve
x = [x, x(1)];
y = [y, y(1)];
plot(x, y, 'r*');
grid on;
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Spline Image Analysis Demo','numbertitle','off')
% Calculate the area within the blue spline curve.
% You do not need to connect the last point back to the first point.
knots = [x; y];
areaOfPolygon = polyarea(x,y);
numberOfPoints = length(x);
% Interpolate with a spline curve and finer spacing.
originalSpacing = 1 : numberOfPoints;
% Make 9 points in between our original points that the user clicked on.
finerSpacing = 1 : 0.1 : numberOfPoints;
% Do the spline interpolation.
splineXY = spline(originalSpacing, knots, finerSpacing);
% Plot the interpolated curve.
hold off;
plot(knots(1, :), knots(2, :), 'ro', 'LineWidth', 2, 'MarkerSize', 16);
hold on;
plot(splineXY(1, :), splineXY(2, :), 'b+-', 'LineWidth', 2, 'MarkerSize', 16);
title('Blue Spline Between Red Knots', 'FontSize', fontSize);
legend('Knots', 'Spline');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
hold off;
% Calculate the area within the blue spline curve.
% You do not need to connect the last point back to the first point.
x = splineXY(1, :);
y = splineXY(2, :);
areaInsideSplineCurve = polyarea(x,y);
% Give the area calculations.
message = sprintf('The area inside the polygon you drew is %.2f.\nThe area inside the blue spline curve is %.2f', ...
areaOfPolygon, areaInsideSplineCurve);
fprintf(1, '%s', message); % Print to command window.
msgbox(message); % Show user via a popup message box.
  댓글 수: 6
David
David 2017년 7월 19일
I got a pretty decent curve using the method described here ...
Image Analyst
Image Analyst 2017년 7월 20일
If that's smooth enough for you, fine. But it does give sharp points sometimes. Just run a modified version of the code I made that does not give such a round starting set of points:
n = 20;
r = 1 + rand(n-1,1)*2.1;%noisy r's
theta = sort(2*pi*rand(n-1,1));
% closing the circle
r(end+1) = r(1);
theta(end+1) = theta(1);
% convert to cartesian
[x,y] = pol2cart(theta,r);
% interpolate with parameter t
t = (1:n)';
v = [x,y];
tt = linspace(1,n,100);
X = interp1(t,v,tt,'pchip');
% plot
plot(x,y,'o');
hold on
plot(X(:,1),X(:,2));

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by