View Raw SPL
/*****************************************************************************
*                                                                            *
*   POLYAREA.SPL   Copyright (C) 2012 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:        Randy Race                                                *
*                                                                            *
*   Synopsis:      Closed loop area                                          *
*                                                                            *
*   Revisions:      9 Jan 2012  RRR  Creation                                *
*                                                                            *
*****************************************************************************/

#if @HELP_POLYAREA

    POLYAREA

    Purpose: Calculates the area of a closed polygon.

    Syntax:  POLYAREA(s)

               s - A series, the input series.

    Alternate Syntax:

              POLYAREA(xseries, yseries)

               xseries - A series, the X values.
               yseries - A series, the Y values.

    Returns: A real, the closed polygon area.

    Example:
             W1: gsin(6, 1, 1/5)
             W2: gcos(6, 1, 1/5)
             W3: xy(w1, w2)
             W4: {polyarea(w3)}

             W3 contains a regular pentagon.  W4 contains the value
             2.377641, the approximate area of the pentagon.

    Example:
             f := 6;
             W1: gsin(f+1, 1, 1/f)
             W2: gcos(f+1, 1, 1/f)
             W3: xy(w1, w2)
             W4: {polyarea(w3)}

             W3 contains a regular hexagon parameterize by the hot
             variable f.  W4 contains the value 2.598076, the
             approximate area of the hexagon.

             f = 8;

             Changing f to 8 creates an octagon in W3 with the area
             calculated in W4 as 2.828427.

    Example:
             f := 6;
             W1: gsin(f+1, 1, 1/f)
             W2: gcos(f+1, 1, 1/f)
             W3: {polyarea(w1, w2)}

             Same as the previous example except the X and Y values
             are provided as separate series.

    Remarks:
             POLYAREA expects a closed POLYGON and automatically adds
             the closing point if the starting point does not equal
             the ending point.

             See CAREA to compute the area of an arbitrary closed loop curve.

    See Also:
             Area
             Carea
             Integ
#endif


/* area of an irregular polygon */
ITERATE polyarea(x, y)
{
        local a;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        x = xvals(w0);
                        y = yvals(w0);
                }
                else
                {
                        y = x;
                        x = xvals(y);
                }
        }

        /* compute area using circular shift */
        a = abs(sum(x * (circshift(y, -1) - circshift(y, 1)))) / 2;
        
        return(a);
}