classdef Poly1 < handle
    % A Poly1 object is a polygon and its centroid
    
    properties
        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 = Poly1(x,y)
        % Constructs a Poly1 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 MoveToOrigin(ThisPoly1)
        % Translates ThisPoly1 to the origin
            ThisPoly1.x = ThisPoly1.x - ThisPoly1.xc;
            ThisPoly1.y = ThisPoly1.y - ThisPoly1.yc;
            ThisPoly1.xc = 0;
            ThisPoly1.yc = 0;
        end
        
        function Show(ThisPoly1,c)
        % Displays ThisPoly1 in the current figure window with color c.
        % Assumes hold is on.
            x = ThisPoly1.x;
            y = ThisPoly1.y;
            xc = ThisPoly1.xc;
            yc = ThisPoly1.yc;
            plot([x x(1)],[y y(1)],c,'linewidth',2)
            plot(x,y,'ok')
            plot(xc,yc,['.' c],'Markersize',20)
        end
    end
    
end

