Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
area inside spline

Subject: area inside spline

From: Alex

Date: 2 Nov, 2010 17:09:04

Message: 1 of 8

I am using the code from the example of ginput that is:

axis([0 10 0 10])
hold on
% Initially, the list of points is empty.
xy = [];
n = 0;
% Loop, picking up the points.
disp('Left mouse button picks points.')
disp('Right mouse button picks last point.')
but = 1;
while but == 1
    [xi,yi,but] = ginput(1);
    plot(xi,yi,'ro')
    n = n+1;
    xy(:,n) = [xi;yi];
end
% Interpolate with a spline curve and finer spacing.
t = 1:n;
ts = 1: 0.1: n;
xys = spline(t,xy,ts);

% Plot the interpolated curve.
plot(xys(1,:),xys(2,:),'b-');
hold off

I want to find the area inside a the spline. How could I do that?

Alex

Subject: area inside spline

From: Image Analyst

Date: 2 Nov, 2010 17:24:04

Message: 2 of 8

Alex:
How about polyarea(xys(1,:),xys(2,:))?

Subject: area inside spline

From: Bruno Luong

Date: 2 Nov, 2010 17:41:03

Message: 3 of 8

"Alex " <a.mlw.walker@googlemail.com> wrote in message <iapgjg$cu8$1@fred.mathworks.com>...

> I want to find the area inside a the spline. How could I do that?

Very easy, it's 0 since your spline is an open curve.

Bruno

Subject: area inside spline

From: Alex

Date: 2 Nov, 2010 18:11:04

Message: 4 of 8

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <iaphfk$b6k$1@fred.mathworks.com>...
> Alex:
> How about polyarea(xys(1,:),xys(2,:))?

Yeah that seems to be doing it, just to check we are talking about the same thing, if i draw four points, and then click my finishing point on top of the first point, then is matlab understanding that I think that area is closed?

I.e does that line of code explain to close the spline?

Thanks

Subject: area inside spline

From: Bruno Luong

Date: 2 Nov, 2010 18:30:09

Message: 5 of 8

"Alex " <a.mlw.walker@googlemail.com> wrote in message <iapk7n$bh0$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <iaphfk$b6k$1@fred.mathworks.com>...
> > Alex:
> > How about polyarea(xys(1,:),xys(2,:))?
>
> Yeah that seems to be doing it, just to check we are talking about the same thing, if i draw four points, and then click my finishing point on top of the first point, then is matlab understanding that I think that area is closed?

Matlab understands whatever your talk it to do.

>
> I.e does that line of code explain to close the spline?
>

Nope, your spline is not closed. To look for a closed spline, you need to duplicate the first point and put it at the end of the list, and use a periodic spline for fitting/interpolating (such tools are in spline-toolbox or in FEX).

Bruno

Subject: area inside spline

From: Alex

Date: 2 Nov, 2010 20:09:04

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iaplbh$n7i$1@fred.mathworks.com>...
> "Alex " <a.mlw.walker@googlemail.com> wrote in message <iapk7n$bh0$1@fred.mathworks.com>...
> > "Image Analyst" <imageanalyst@mailinator.com> wrote in message <iaphfk$b6k$1@fred.mathworks.com>...
> > > Alex:
> > > How about polyarea(xys(1,:),xys(2,:))?
> >
> > Yeah that seems to be doing it, just to check we are talking about the same thing, if i draw four points, and then click my finishing point on top of the first point, then is matlab understanding that I think that area is closed?
>
> Matlab understands whatever your talk it to do.
>
> >
> > I.e does that line of code explain to close the spline?
> >
>
> Nope, your spline is not closed. To look for a closed spline, you need to duplicate the first point and put it at the end of the list, and use a periodic spline for fitting/interpolating (such tools are in spline-toolbox or in FEX).
>
> Bruno

If the spline is not closed at the moment, what area is polyarea(xys(1,:),xys(2,:)) giving me at the moment? What difference is a periodic spline to the one that I am now using?

This is the first time I have used splines with matlab, so I am new to this.

Alex

Subject: area inside spline

From: Bruno Luong

Date: 2 Nov, 2010 20:30:07

Message: 7 of 8

"Alex " <a.mlw.walker@googlemail.com> wrote in message <iapr50$kc0$1@fred.mathworks.com>...

>
> If the spline is not closed at the moment, what area is polyarea(xys(1,:),xys(2,:)) giving me at the moment?

polyarea is the area of the *polygone*, i.e., the vertices connected by straight line. You could argue it's a spline of order 1.
  
> What difference is a periodic spline to the one that I am now using?

The one you are using is not periodic. It uses another boundary conditions (called not-a-knot), it you connect the boundary with such spline, you 'll not get a smooth curve at the end points but a corner (jump of the derivative).

For the spline order 1, i.e., straight lines, it does not matter because there is jump of derivative at all vertexes, thus periodic and non-periodic gives the same answer. For spline of order >1 such as cubic-spline, this fortunate property is no longer true.

Bruno

Subject: area inside spline

From: Image Analyst

Date: 2 Nov, 2010 20:56:04

Message: 8 of 8

If you just want to calculate the area of the curve and polygon assuming that the last point is connected to the first point with a straight line, then you do not need to "close" the curve explicitly by making that connection. (However the spline curve would be different, and thus its area would be also, depending on whether you close or don't close the curve.) See this demo:

% function SmoothSplineCurve()
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
        cd(fileparts(which(mfilename)));
end
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Spline Demo by ImageAnalyst','numbertitle','off')

axis([0 10 0 10])
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Spline Demo', 'FontSize', fontSize);
hold on
% Initially, the list of points is empty.
knots = [];
numberOfPointsClicked = 0;
% Prompt the user
message = sprintf('Left click to draw some vertex points.\nRight click the final point to finish drawing.');
uiwait(msgbox(message));
buttonThatWasClicked = 1;
% Enter a loop asking user to click on the know vertexes.
while buttonThatWasClicked == 1
    [xKnot, yKnot, buttonThatWasClicked] = ginput(1);
    plot(xKnot, yKnot, 'ro', 'LineWidth', 2)
    numberOfPointsClicked = numberOfPointsClicked+1;
% Make this coordinate a new column.
    knots(:, numberOfPointsClicked) = [xKnot; yKnot];
end

% Calculate the area within the blue spline curve.
% You do not need to connect the last point back to the first point.
x = knots(1, :);
y = knots(2, :);
areaOfPolygon = polyarea(x,y);

% Interpolate with a spline curve and finer spacing.
originalSpacing = 1 : numberOfPointsClicked;
% Make 9 points in between our original points that the user clicked on.
finerSpacing = 1 : 0.1 : numberOfPointsClicked;
% Do the spline interpolation.
splineXY = spline(originalSpacing, knots, finerSpacing);
 
% Plot the interpolated curve.
hold off;
plot(knots(1, :), knots(2, :), 'ro',...
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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us