Y.C.
Y.C.

Reputation: 169

Filling in polar flower plot as curves are being traced

I'd like to animate a polar flower plot being drawn. I would hope for the result to look like this plot: enter image description here

However the current result looks like this: enter image description here

I've been rearranging the coordinates into multidimensional structs x1 and y1, so it isn't just filling in as a plain cosine wave. Help is much appreciated.

clear;close all;
x = 0:0.01:pi*2;
r = cos(2*x).*sin(2*x);
figure
polar(x,r);
input('Press ENTER to start.');
hold on;
for x0 = x
  r0 = cos(2*x0).*sin(2*x0);
  polar(x0,r0,'o');
  x1 = [x0 2*pi 0];
  r1 = [r0 0 0];
  fill(x1,r1,'b');
  pause(0.033);
end
hold off;

Upvotes: 2

Views: 519

Answers (1)

S. Gougeon
S. Gougeon

Reputation: 911

fill(..) takes cartesian coordinates, not polar ones. So you must convert polar parameters into cartesian coordinates before calling fill(). Then, unless you need the polar frame, calling polar(..) is not required.

th = 0:0.01:pi*2;
r = cos(2*th) .* sin(2*th);
clf
x = r .* cos(th);
y = r .* sin(th);
c = fill(x,y,'b');
axis square tight

enter image description here

Upvotes: 3

Related Questions