View Raw SPL
/*****************************************************************************
*                                                                            *
*   CAREA.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_CAREA

    CAREA

    Purpose: Calculates the area of a closed loop.

    Syntax:  CAREA(s)

               s - A series, the input series.

    Alternate Syntax:

              CAREA(xseries, yseries)

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

    Returns: A real, the closed loop area.

    Example:
             W1: gsin(1000, 1/1000, 1)
             W2: gsin(1000, 1/1000, 1)
             W3: xy(w1, w2)
             W4: {carea(w3)}

             W3 contains a circular series with a radius of 1.0.
             W4 contains the value 3.14593, the approximate value of pi.

    Example:
             W1: 3*gsin(1000, 1/1000, 1)
             W2: 3*gsin(1000, 1/1000, 1)
             W3: xy(w1, w2)
             W4: {carea(w3)}

             W3 contains a circular series with a radius of 3.0.
             W4 contains the value 28.274334 the approximate value of 
             9 * pi.

    Example:
             W1: 3*gsin(1000, 1/1000, 1)
             W2: 3*gsin(1000, 1/1000, 1)
             W4: {carea(w1, w2)}

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

    Remarks:
             CAREA expects a closed loop curve and automatically adds
             the closing point if the starting point does not equal
             the ending point.

             CAREA is based on AREA and uses the modified Simpson
             rule to compute the absolute area.

             See POLYAREA to compute the area of a closed polygon.

    See Also:
             Area
             Integ
             Polyarea
#endif



/* returns the positive area of a closed loop */
ITERATE carea(x, y)
{
        local a;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        /* default to current window */
                        if (isempty(w0))
                        {
                                return(0.0);
                        }

                        x = xvals(w0);
                        y = yvals(w0);
                }
                else
                {
                        y = x;
                        x = xvals(y);
                }
        }

        /* check if closed loop */
        if (x[1] != x[end])
        {
                /* close by adding start point */
                x = {x, x[1]};
                y = {y, y[1]};
        }

        /* area */
        a = abs(area(xy(x, y)));

        return(a);
}