How to write a code for "N points on a Fibonacci spiral placed within the circle"?

Hi.
First excuse me for my bad English and my knowledge for Matlab. I found a web link http://people.sc.fsu.edu/~%20jburkardt/m_src/circle_grid/circle_grid.html circle_grid* that has this kind of code under name: circle_grid_test02.m But i cant get the test program to work,always some errors.. Could somebody check that link please and copy here the code that i have to paste it to the editor to make it work? Or perhaps there is some easy way for that or some other program? I just need to specify radius of a circle and how many dots i want in that. Thanks

 Accepted Answer

I have checked out the 'circle_grid_fibonacci' function on the web site you referenced (although the link you gave had a mistake in spelling) and found that this function works very well and gives what appears to be a true Fibonacci spiral. The purpose behind the spiral is presumably to arrange the "grid" points so as to have area-wise as uniform a distribution as possible within a circle.
I don't know what kind of trouble you had with the test routine 'circle_grid_test02.m', but it is very easy to test out 'circle_grid_fibonacci' with your own code. The output of this function is an array which is of n x 2 size, so you just need to plot the first column as the x coordinates against the second column as the y coordinates.
If I were writing the code I would have used the following more compact version, where n is the desired number of points, r is the circle's radius, and c is a 2-element vector contained the x,y coordinates of the circle's center.
R = r*sqrt(linspace(1/2,n-1/2,n))/sqrt(n-1/2);
T = 4/(1+sqrt(5))*pi*(1:n);
X = c(1)+R.*cos(T);
Y = c(2)+R.*sin(T);
plot(X,Y,'yo')
axis equal

3 Comments

The link did not work for me. What are some values for r, n, and c you used? For the values I tried I did not get a spiral - it looked kind of messy, though if you look at unconnected spots you can sort of see possible spiral pattern but they're not connected..
r = 50;
n = 100;
c = [4 9]
R = r*sqrt(linspace(1/2,n-1/2,n))/sqrt(n-1/2);
T = 4/(1+sqrt(5))*pi*(1:n);
X = c(1)+R.*cos(T);
Y = c(2)+R.*sin(T);
plot(X,Y,'bo-')
axis equal
grid on;
Well, it's actually a spiral in that each successive step moves outward to a larger distance from the center, and the angle change for each step is always the same. However, at each step it sweeps a whopping 222 degrees, so it looks rather strange as spirals go. If you use a very high value of n, such as 1000, and omit the connecting lines, it makes a striking pattern.
here is the correct link,try it now:
http://people.sc.fsu.edu/~%20jburkardt/m_src/circle_grid/circle_grid.html
also..as a result there are at the bottom of the page some examples with plot result:
http://people.sc.fsu.edu/~%20jburkardt/m_src/circle_grid/circle_grid_test02.png
Bassicly i just want to enter a N-number of points,radius of a circle and thats it. When a copy code from file circle_grid_test02.m its just wont give me that option. here is a code from that page:
fprintf ( 1, '\n' );
fprintf ( 1, 'TEST02:\n' );
fprintf ( 1, ' CIRCLE_GRID_FIBONACCI can define a grid of N points\n' );
fprintf ( 1, ' \n' );
n = 1000;
r = 2.0;
c = [ 1.0, 5.0 ];
fprintf ( 1, '\n' );
fprintf ( 1, ' We use N = %d\n', n );
fprintf ( 1, ' Radius R = %f\n', r );
fprintf ( 1, ' Center C = (%f,%f)\n', c(1), c(2) );
g = circle_grid_fibonacci ( n, r, c );
r82vec_print_part ( n, g, 20, ' Part of the grid point array:' );
filename = 'circle_grid_test02.xy';
r8mat_write ( filename, 2, n, g );
fprintf ( 1, '\n' );
fprintf ( 1, ' Gridpoint coordinate data saved in file "%s".\n', filename );
%
% Plot the points.
%
i = 0 : 50;
t = 2.0 * pi * i / 50.0;
cx = c(1);
cx = cx + r .* cos ( t );
cy = c(2);
cy = cy + r .* sin ( t );
figure ( )
clf
hold on
plot ( cx, cy, 'r-', 'Linewidth', 3 )
plot ( g(1,1:n), g(2,1:n), 'b.' )
hold off
axis equal
xlabel ( '<---X--->', 'Fontsize', 16 )
ylabel ( '<---Y--->', 'Fontsize', 16 )
title ( sprintf ( 'Fibonacci grid with %d points', n ), 'Fontsize', 16 );
filename = 'circle_grid_test02.png';
print ( '-dpng', filename );
fprintf ( 1, '\n' );
fprintf ( 1, ' Graphics data saved in file "%s"\n', filename );
return

Sign in to comment.

More Answers (0)

Asked:

on 10 Dec 2013

Commented:

on 11 Dec 2013

Community Treasure Hunt

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

Start Hunting!