classdef Poly2 < handle
    % Same as Poly1 except that its properties are private
    
    properties(Access= private)
        % These properties cannot be accessed outside the class
        x = [];    % x-coordinates of the vertices
        y = [];    % y-coordinates of the vertices
        xc         % x-coordinate of the centroid
        yc         % y-coordinate of the centroid
    end
    
    methods
        function P = Poly2(x,y)
        % Constructs a Poly2 object. 
        % x and y are column n-vectors
            P.x = x;
            P.y = y;
            n = length(x);
            P.xc = sum(x)/n;
            P.yc = sum(y)/n;
        end
        
        function DeleteLast(ThisPoly2)
        % Deletes the last vertex in ThisPoly2. Assumes that the
        % represented polygon has at least 2 sides.
        n = length(ThisPoly2.x);
        ThisPoly2.x = ThisPoly2.x(1:n-1);
        ThisPoly2.y = ThisPoly2.y(1:n-1);
        ThisPoly2.xc = sum(ThisPoly2.x)/(n-1);
        ThisPoly2.yc = sum(ThisPoly2.y)/(n-1);  
        end
            
        function MoveToOrigin(ThisPoly2)
        % Translates ThisPoly2 to the origin
            ThisPoly2.x = ThisPoly2.x - ThisPoly2.xc;
            ThisPoly2.y = ThisPoly2.y - ThisPoly2.yc;
            ThisPoly2.xc = 0;
            ThisPoly2.yc = 0;
        end
        
        function Show(ThisPoly2,c)
        % Displays ThisPoly1 in the current figure window with color c.
        % Assumes hold is on.
            x = ThisPoly2.x;
            y = ThisPoly2.y;
            xc = ThisPoly2.xc;
            yc = ThisPoly2.yc;
            plot([x x(1)],[y y(1)],c,'linewidth',2)
            plot(x,y,'ok')
            plot(xc,yc,['.' c],'Markersize',20)
        end
    end
end

