MATLAB exercise program (generate a Hilbert curve)

Source: Internet
Author: User

This line can be used to traverse all pixels in the image, but this is not done here, but only generates such a curve.

In the program, H and W are the height and width of the final image, and N is the order of the Hilbert curve.

Here, if n is equal to log2 (h) or log2 (W), the image will be completely white, and it will just traverse all pixels.

Of course, if n is large, the image is completely white. However, that is not just a traversal.

The core functions for generating curves in the code can be found here.

The generated image is as follows:

The Matlab code is as follows:

Main. m

clear all;close all;clc;h=256;w=256;n=5;   imgn=zeros(h,w);[x,y]=hilbert(n);       x=floor((x+0.5)*w)+1;y=floor((y+0.5)*h)+1;l=length(x);for i=1:l-1    imgn=drawline(imgn,x(i),y(i),x(i+1),y(i+1));  endimshow(imgn)

Helbert. m

function [x,y] = hilbert(n)%HILBERT Hilbert curve.%% [x,y]=hilbert(n) gives the vector coordinates of points%   in n-th order Hilbert curve of area 1.%% Example: plot of 5-th order curve%% [x,y]=hilbert(5);line(x,y)%%   Copyright (c) by Federico Forte%   Date: 2000/10/06 if n<=0  x=0;  y=0;else  [xo,yo]=hilbert(n-1);  x=.5*[-.5+yo -.5+xo .5+xo  .5-yo];  y=.5*[-.5+xo  .5+yo .5+yo -.5-xo];end

Drawline. m

Function IMG = drawline (IMG, X1, Y1, X2, Y2) % because the orientation of the image coordinate is opposite to that of the Y axis of the mathematical function, the inverse Number of all Y variables is Y1 =-Y1; y2 =-Y2; If X1 ~ = X2 k = (y2-y1)/(x2-x1); B = y1-k * x1; MI = min (x1, x2); MA = max (x1, x2 ); for I = mi: Ma IMG (-round (I * k + B), I) = 1; end if Y1 ~ = Y2 k = (x2-x1)/(y2-y1); B = x1-k * Y1; MI = min (Y1, Y2); MA = max (Y1, Y2 ); for I = mi: Ma IMG (-I, round (I * k + B) = 1; end

 

MATLAB exercise program (generate a Hilbert curve)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.