gistlib
main.mfunction hilbert_curve(order) % Initialize the Hilbert curve with a straight line, order 1 curve = [0 0; 1 0; 1 1; 0 1]; % Generate the curve up to the specified order for i = 2:order curve = hilbert_iterate(curve); end % Plot the Hilbert curve plot(curve(:,1), curve(:,2)); axis equal; title(['Hilbert Curve of Order ', num2str(order)]); end function new_curve = hilbert_iterate(curve) % Rotation matrix for 90 degrees rotate90 = [0 -1; 1 0]; % Generate new empty curve new_curve = []; for i = 1:size(curve, 1)-1 p1 = curve(i,:); p2 = curve(i+1,:); % Calculate 4 points within the square new_points = []; for j = 0:3 new_points(j+1,:) = (p1 + p2 + 1)/2 + 0.5^2 * rotate90^j * (p1 - p2); end new_curve = [new_curve; new_points(1:end-1, :); p2]; end end % Generate Hilbert curve of order 3 order = 3; hilbert_curve(order); 998 chars40 lines
function hilbert_curve(order) % Initialize the Hilbert curve with a straight line, order 1 curve = [0 0; 1 0; 1 1; 0 1]; % Generate the curve up to the specified order for i = 2:order curve = hilbert_iterate(curve); end % Plot the Hilbert curve plot(curve(:,1), curve(:,2)); axis equal; title(['Hilbert Curve of Order ', num2str(order)]); end function new_curve = hilbert_iterate(curve) % Rotation matrix for 90 degrees rotate90 = [0 -1; 1 0]; % Generate new empty curve new_curve = []; for i = 1:size(curve, 1)-1 p1 = curve(i,:); p2 = curve(i+1,:); % Calculate 4 points within the square new_points = []; for j = 0:3 new_points(j+1,:) = (p1 + p2 + 1)/2 + 0.5^2 * rotate90^j * (p1 - p2); end new_curve = [new_curve; new_points(1:end-1, :); p2]; end end % Generate Hilbert curve of order 3 order = 3; hilbert_curve(order);
gistlibby LogSnag