Figure 7: Header for class Polygon

// **********************************************************
//   File: Polygon.h
//   Description: Definition of class Polygon and class 
//                Polygon::iterator (implemented as inlines)
//
//   Author: Carlos Moreno
// **********************************************************

#ifndef __POLYGON_H__
#define __POLYGON_H__

#include "Point.h"
#include <list>

using namespace std;


const bool draw_vertices = true;
const bool no_draw_vertices = false;

class Polygon  
{
public:
        // Geometric and miscelaneous operations
    void draw (bool = no_draw_vertices, int radius = 2) const;

    int orientation () const;
    bool contains (const Point &) const;

    Polygon convex_hull() const;


        // Vertices manipulation
    class iterator;
    class const_iterator;

    iterator begin();
    const_iterator begin() const;
    void push_back (const Point &);
    void push_front (const Point &);
    void insert (const iterator &, const Point &);
    iterator remove (const iterator &);

private:
    list<Point> vertices;


// ********************************************************
//          Definition of class Polygon::iterator
// ********************************************************

public:
    class iterator
    {
    public:
        iterator (const list<Point>::iterator & _i =
                      list<Point>::iterator(), 
                  const list<Point>::iterator & _last =
                      list<Point>::iterator())
            : i(_i), last(_last) {}

        list<Point>::iterator get_i () const
        {
            return i;
        }

        list<Point>::iterator get_last () const
        {
            return last;
        }

        const iterator & operator++ ()
        {
            if (++i == last)
            {
                i++;
            }
            return *this;
        }

        const iterator operator++ (int)
        {
            iterator original = *this;

            if (++i == last)
            {
                i++;
            }
            return original;
        }

        const iterator operator+ (int n) const
        {
            iterator result = *this;

            for (int i = 0; i < n; i++)
            {
                result++;
            }

            return result;
        }

        const iterator & operator-- ()
        {
            if (--i == last)
            {
                i--;
            }
            return *this;
        }

        const iterator operator-- (int)
        {
            iterator original = *this;

            if (--i == last)
            {
                i--;
            }
            return original;
        }

        const iterator operator- (int n) const
        {
            iterator result = *this;

            for (int i = 0; i < n; i++)
            {
                result--;
            }

            return result;
        }

        Point & operator* () const
        {
            return *i;
        }

        bool operator== (const iterator & other) const
        {
            return i == other.i && last == other.last;
        }

        bool operator!= (const iterator & other) const
        {
            return !(*this == other);
        }

    private:
        list<Point>::iterator i;
        list<Point>::iterator last;
    };


/****************************************************************
  The definition of class Polygon::const_iterator is omited to 
  save magazine space.  It is almost identical to the definition 
  of iterator (with parameters and local variables of type 
  const_iterator instead of iterator), except for a conversion 
  constructor from iterator to const_iterator.

  The complete version of this file can be downloaded from the 
  Journal's web site.
 ****************************************************************/

};

#endif
/* End of File */