clear
close all
n=1;
xmax=n*2*pi;
ymax=2*n*2*pi;
dx=700;
dy=700;
x=0:xmax/(dx+1):xmax;
y=0:ymax/(dy+1):ymax;
lx=length(x);
ly=length(y);
ny=ymax/dy;
nx=xmax/dx;
Z=zeros(length(y),length(x));
f=0;
imName='Violin_and_Candlestick.jpg';
imName2='Nude_Descending.jpg';
imName3='Descending_Nude.jpg';
imName4='Guitar_Player.jpg';
imName5='Picasso2.jpg';
I=imread(imName);
ncolors=25;
[M,MAP]=rgb2ind(I,ncolors);
for iter=1:25
    q=3*rand();%probability of getting a sinusoid or a line as a cut
    s=rand();% probability of aking an outline
    a=ymax*rand();%sinusoidal amplitude
    p=2*pi*rand();%sinusoidal phase shift
    c=xmax*rand();%linear x shift
    m=10*rand();%linear slope
    b=ymax*.5*rand();%linear y shift
    fr=2*rand();%sinusoidal frequency
    %f=1;
    d=2*rand();%randomized change in elevation
    r=12*rand();%randomizes shifts up and down for the cut waveforms
    if r<=3
        m=-m;
        b=-b;
        p=-p;
        a=-a;
        d=-d;
    elseif r>=3 && r<=6
        p=-p;
        m=-m;
    elseif r>=6 && r <=9
        a=-a;
        b=-b;
    else
        m=-m;
        d=-d;
        p=p;
        a=a;
    end
    if q>=0 && q<=1
        cut=a*sin(fr*x+p)+.5*ymax;%sets sine wave with random amplitude frequency and phase shift
    elseif q>1 && q <=3
        cut=(1+m)*(x+c)+b;%sets line with random, slope and x and y intercepts
    end
    for i=1:lx
        for j=1:ly
            if sqrt((x(i))^2+(y(j))^2) > sqrt(x(i)^2+(cut(i)^2));% lowers and raises the evelation of the different parts of the image
                Z(i,j)=(Z(i,j)+d);
            else
                Z(i,j)=(Z(i,j)-d);
            end
            if s<=.5 && norm(y(j)-cut(i))<= .01 && q>.4 && q <=3% creates outlines, more often on lines than on curves
                Z(i,j)=min(min(Z));
            end
        end
    end
    %subplot (1,2,1)
    [X,Y]=meshgrid(y,x);
    props.edgecolor='none';
    props.FaceLighting='gouraud';
    surface(X,Y,Z,props);
    colormap(MAP)
    colorbar
    view(90,90)
    axis tight
    pause(0.01)
    drawnow
    %saveas(gcf, ['CANDLESTICKS_',sprintf('%03d',iter),'.png'])
    %f = f+1;
end