001package fr.aumgn.dac2.shape.iterator; 002 003import org.apache.commons.lang.Validate; 004 005import com.google.common.collect.AbstractIterator; 006 007import fr.aumgn.bukkitutils.geom.Vector2D; 008import fr.aumgn.dac2.shape.FlatShape; 009import fr.aumgn.dac2.shape.column.Column; 010 011public class CuboidColumnsIterator extends AbstractIterator<Column> { 012 013 protected final FlatShape shape; 014 private final double minZ; 015 private final double maxX; 016 private final double maxZ; 017 018 private double x; 019 private double z; 020 021 public CuboidColumnsIterator(FlatShape shape) { 022 Vector2D min = shape.getMin2D(); 023 Vector2D max = shape.getMax2D(); 024 Validate.isTrue(min.getX() <= max.getX()); 025 Validate.isTrue(min.getZ() <= max.getZ()); 026 027 this.shape = shape; 028 x = min.getX(); 029 minZ = min.getZ(); 030 z = minZ - 1; 031 maxX = max.getX(); 032 maxZ = max.getZ(); 033 } 034 035 @Override 036 protected Column computeNext() { 037 Vector2D pt = computeNextVector2D(); 038 return pt == null ? endOfData() : new Column(shape, pt); 039 } 040 041 protected Vector2D computeNextVector2D() { 042 if (z >= maxZ) { 043 if (x >= maxX) { 044 return null; 045 } else { 046 z = minZ; 047 x++; 048 } 049 } else { 050 z++; 051 } 052 053 return new Vector2D(x, z); 054 } 055}