function [x,y] = cantor_set(levels)
% base case when level is 0, i.e. largest line
% the longest line has length 1
if levels == 0
x = (0:1);
y = zeros(1, length(x));
plot(x, y, 'b', 'LineWidth', 2);
axis ([0 1 -1*levels-1 1]);
hold on;
% recursive case for all levels not 0
else
% update the new sets of lines with 1/3 length of the previous ones
[x, y] = cantor_set(levels-1);
x = [x x+2].*(1/3);
i = 1;
% plot all lines for each level, i.e. 2 for level 1, 4 for level 2 etc.
while i < length(x)
% tmp shows the range of each line in the level
tmp = x(i) : 0.000001 : x(i+1);
y = ones(1, length(tmp));
% The value -levels is random, simply to separate the lines at
% each level
y(1, :) = -levels;
plot(tmp, y, 'b', 'LineWidth', 2);
axis ([0 1 -1*levels-1 1]);
hold on;
% create the line between the next pair of point
i = i + 2;
end
end