/*
 * Copyright (c) 1998 The Regents of the University of California.
 * All rights reserved.  See the file COPYRIGHT for details.
 *
 */

package CookieCutter.g5;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/** 
 * An iterator over Polygon2D. This class is private
 * to this package.
 *
 * @author 	John Reekie
 */
class PolygonIterator implements PathIterator {

    /** The transformed coordinates being iterated.
     */
    private double[] _coords;

    /** The current coordinate index.
     */
    private int _index = 0;

    /** The flag saying we are already done
     */
    private boolean _done = false;

    /** Create a new iterator over the given polygon and
     * with the given transform. If the transform is null,
     * that is taken to be the same as a unit Transform.
     */
    public PolygonIterator (Polygon2D pl, AffineTransform at) {
        int count = pl.getVertexCount() * 2;
        _coords = new double[count];
		Polygon2D d = (Polygon2D) pl;
		if (at == null || at.isIdentity()) {
			//System.arraycopy(d._coords, 0, _coords, 0, count);
			double coords[] = new double[ d.getVertexCount() * 2 ];

			for( int i = 0; i < d.getVertexCount(); i++ ) {
				Point2D.Double vertex = d.getVertex( i );
				coords[i * 2] = vertex.getX();
				coords[i * 2 + 1] = vertex.getY();
			}

			System.arraycopy(coords, 0, _coords, 0, count);
		} else {
			double coords[] = new double[ d.getVertexCount() * 2 ];

			for( int i = 0; i < d.getVertexCount(); i++ ) {
				Point2D.Double vertex = d.getVertex( i );
				coords[i * 2] = vertex.getX();
				coords[i * 2 + 1] = vertex.getY();
			}

			//double new_coords[] = new double[ d.getVertexCount() * 2 ];
			at.transform(coords, 0, _coords, 0, coords.length);

			for( int i = 0; i < d.getVertexCount(); i++ ) {
				Point2D.Double vertex = d.getVertex( i );
				vertex.setLocation( _coords[i * 2], _coords[ i * 2 + 1] );
			}
		}
    }

    public int currentSegment (float coords[]) {
		coords[0] = 0;
		coords[1] = 1;

		return( PathIterator.SEG_LINETO );
	}

    /** Get the current segment
     */
    public int currentSegment (double coords[]) {
        if (_index == _coords.length) {
            if (_done) {
                return PathIterator.SEG_CLOSE;
            } else {
                coords[0] = this._coords[0];
                coords[1] = this._coords[1];
                return PathIterator.SEG_LINETO;
            }
        } else {
            coords[0] = this._coords[_index];
            coords[1] = this._coords[_index+1];
            if (_index == 0) {
                return PathIterator.SEG_MOVETO;
            } else {
                return PathIterator.SEG_LINETO;
            }
        }
    }

    /** Return the winding rule. This is WIND_NON_ZERO.
     */
    public int getWindingRule () {
        return PathIterator.WIND_NON_ZERO;
    }

    /** Test if the iterator is done.
     */
    public boolean isDone () {
        return _done;
    }

    /** Move the iterator along by one point.
     */
    public void next () {
        if (_index == _coords.length) {
            _done = true;
        } else {
            _index+=2;
        }
    }
}
