1 18 package org.apache.batik.ext.awt.geom; 19 20 import java.awt.Shape ; 21 import java.awt.geom.PathIterator ; 22 import java.awt.geom.Point2D ; 23 import java.awt.geom.Rectangle2D ; 24 import java.util.List ; 25 import java.util.LinkedList ; 26 import java.util.Iterator ; 27 28 33 public class SegmentList { 34 List segments = new LinkedList (); 35 36 public SegmentList() { 37 } 38 39 public SegmentList(Shape s) { 40 PathIterator pi = s.getPathIterator(null); 41 float pts [] = new float[6]; 42 int type; 43 Point2D.Double loc = null; 44 Point2D.Double openLoc = null; 45 while (!pi.isDone()) { 46 type = pi.currentSegment(pts); 47 switch (type) { 48 case PathIterator.SEG_MOVETO: 49 openLoc = loc = new Point2D.Double (pts[0], pts[1] ); 50 break; 51 case PathIterator.SEG_LINETO: { 52 Point2D.Double p0 = new Point2D.Double (pts[0], pts[1] ); 53 segments.add(new Linear(loc, p0)); 54 loc = p0; 55 } 56 break; 57 58 case PathIterator.SEG_QUADTO: { 59 Point2D.Double p0 = new Point2D.Double (pts[0], pts[1] ); 60 Point2D.Double p1 = new Point2D.Double (pts[2], pts[3] ); 61 segments.add(new Quadradic(loc, p0, p1)); 62 loc = p1; 63 } 64 break; 65 66 case PathIterator.SEG_CUBICTO: { 67 Point2D.Double p0 = new Point2D.Double (pts[0], pts[1] ); 68 Point2D.Double p1 = new Point2D.Double (pts[2], pts[3] ); 69 Point2D.Double p2 = new Point2D.Double (pts[4], pts[5] ); 70 segments.add(new Cubic(loc, p0, p1, p2)); 71 loc = p2; 72 } 73 break; 74 75 case PathIterator.SEG_CLOSE: 76 segments.add(new Linear(loc, openLoc)); 77 loc = openLoc; 78 break; 79 } 80 pi.next(); 81 } 82 } 83 84 public Rectangle2D getBounds2D() { 85 Iterator iter = iterator(); 86 if (!iter.hasNext()) return null; 87 88 Rectangle2D ret; 89 ret = (Rectangle2D )((Segment)iter.next()).getBounds2D().clone(); 90 while (iter.hasNext()) { 91 Segment seg = (Segment)iter.next(); 92 Rectangle2D segB = seg.getBounds2D(); 93 Rectangle2D.union(segB, ret, ret); 94 } 95 return ret; 96 } 97 98 public void add(Segment s) { 99 segments.add(s); 100 } 101 102 public Iterator iterator() { return segments.iterator(); } 103 104 public int size() { return segments.size(); } 105 106 public SegmentList.SplitResults split(double y) { 107 Iterator iter = segments.iterator(); 108 SegmentList above = null; 109 SegmentList below = null; 110 while (iter.hasNext()) { 111 Segment seg = (Segment)iter.next(); 112 Segment.SplitResults results = seg.split(y); 113 if (results == null) { 114 Rectangle2D bounds = seg.getBounds2D(); 115 if (bounds.getY() > y) { 116 if (below == null) below = new SegmentList(); 117 below.add(seg); 118 } else if (bounds.getY() == y) { 119 if (bounds.getHeight() != 0) { 120 if (below == null) below = new SegmentList(); 121 below.add(seg); 122 } 123 } else { 124 if (above == null) above = new SegmentList(); 125 above.add(seg); 126 } 127 continue; 128 } 129 130 Segment [] resAbove = results.getAbove(); 131 for(int i=0; i<resAbove.length; i++) { 132 if (above == null) above = new SegmentList(); 133 above.add(resAbove[i]); 134 } 135 136 Segment [] resBelow = results.getBelow(); 137 for(int i=0; i<resBelow.length; i++) { 138 if (below == null) below = new SegmentList(); 139 below.add(resBelow[i]); 140 } 141 } 142 return new SegmentList.SplitResults(above, below); 143 } 144 145 public static class SplitResults { 146 SegmentList above, below; 147 public SplitResults(SegmentList above, SegmentList below) { 148 this.above = above; 149 this.below = below; 150 } 151 152 public SegmentList getAbove() { return above; } 153 public SegmentList getBelow() { return below; } 154 } 155 } 156 | Popular Tags |