001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009package imagingbook.common.geometry.shape; 010 011import imagingbook.common.geometry.basic.Pnt2d; 012 013import java.awt.Shape; 014import java.awt.geom.FlatteningPathIterator; 015import java.awt.geom.PathIterator; 016import java.util.Iterator; 017 018/** 019 * A small wrapper class to create a simple {@link Iterator} of {@link Pnt2d} to step over the individual points of a 020 * AWT {@link Shape} object. Curved shapes are flattened by a {@link FlatteningPathIterator}. 021 * 022 * @author WB 023 * @see PathIterator 024 * @see FlatteningPathIterator 025 */ 026public class ShapePointIterator implements Iterator<Pnt2d> { 027 028 private final PathIterator pi; 029 private final double coords[] = new double[6]; 030 031 public ShapePointIterator(Shape shape, double flatness) { 032 this.pi = shape.getPathIterator(null, flatness); 033 } 034 035 @Override 036 public boolean hasNext() { 037 return !pi.isDone(); 038 } 039 040 @Override 041 public Pnt2d next() { 042 if (pi.isDone()) { 043 return null; 044 } 045 else { 046 pi.currentSegment(coords); 047 pi.next(); 048 return Pnt2d.from(coords[0], coords[1]); 049 } 050 } 051 052 053// public static void main(String[] args) { 054// Pnt2d ctr = Pnt2d.from(20, 30); 055// GeometricCircle gc = new GeometricCircle(20, 30, 100); 056// Shape circle = gc.getShape(); 057// System.out.println("circle = " + circle); 058// 059//// ShapePointIterator iter = new ShapePointIterator(circle, 0.01); 060//// while(iter.hasNext()) { 061//// Pnt2d p = iter.next(); 062//// System.out.println(p + " dist = " + gc.getDistance(p)); 063//// } 064// 065// System.out.println("checkShape = " + gc.checkShape(circle, 0.5)); 066// 067// } 068}