% Chad Myers
% ELE 571
% File: generate_ellipse.m
% ------------------------
% The generate_ellipse function is part of the snakeGUI program for
% demonstrating SNAKE active contours. The function generates a set of x,y
% coordinates on an ellipse when given the number of points desired, the
% standard deviation of the points from a true ellipse, the center of the
% ellipse, and the x-y axis lengths.


function ellipse = generate_ellipse(points, std_dev, center, axes_lengths)

x_errors = normrnd(0,std_dev,1,points);
y_errors = normrnd(0,std_dev,1,points);

x_coord = (center(1) - axes_lengths(1)) + rand(1, floor(points/2))*(axes_lengths(1)*2);

% setup ellipse dot pattern
for i=1:floor(points/2)
    rand_num = rand(1,1);
    if(rand_num > .5) y_coord(i) = ((axes_lengths(2)^2)*(1-(1/(axes_lengths(1)^2))*(x_coord(i) - center(1))^2))^.5 +center(2); % place point on upper half of ellipse
    else y_coord(i) = -((axes_lengths(2)^2)*(1-(1/(axes_lengths(1)^2))*(x_coord(i) - center(1))^2))^.5 +center(2); % place point on lower half of ellipse
    end
end

for i=ceil(points/2):points
    y_coord(i) = (center(2) - axes_lengths(2)) + rand(1,1)*(axes_lengths(2)*2); 
    rand_num = rand(1,1);
    if(rand_num > .5) x_coord(i) = ((axes_lengths(1)^2)*(1-(1/(axes_lengths(2)^2))*(y_coord(i) - center(2))^2))^.5 + center(1); % place point on right half of ellipse
    else x_coord(i) = -((axes_lengths(1)^2)*(1-(1/(axes_lengths(2)^2))*(y_coord(i) - center(2))^2))^.5 +center(1); % place point on left half of ellipse
    end
end

 y_coord(1:points) = y_coord(1:points) + y_errors(1:points);     % add random errors to ellipse pattern
 x_coord(1:points) = x_coord(1:points) + x_errors(1:points);
    
 ellipse = zeros(points,2);
 for i=1:points
     ellipse(i,1) = x_coord(i);
     ellipse(i,2) = y_coord(i);
 end


