1 49 50 package com.lowagie.text.pdf.internal; 51 52 import java.awt.Shape ; 53 import java.awt.Rectangle ; 54 import java.awt.geom.AffineTransform ; 55 import java.awt.geom.PathIterator ; 56 import java.awt.geom.Line2D ; 57 import java.awt.geom.Point2D ; 58 import java.awt.geom.Rectangle2D ; 59 60 65 public class PolylineShape implements Shape { 66 67 protected int[] x; 68 69 protected int[] y; 70 71 protected int np; 72 73 74 public PolylineShape(int[] x, int[] y, int nPoints) { 75 this.np = nPoints; 77 this.x = new int[np]; 79 this.y = new int[np]; 80 System.arraycopy(x, 0, this.x, 0, np); 81 System.arraycopy(y, 0, this.y, 0, np); 82 } 83 84 91 public Rectangle2D getBounds2D() { 92 int[] r = rect(); 93 return r==null?null:new Rectangle2D.Double (r[0], r[1], r[2], r[3]); 94 } 95 96 100 public Rectangle getBounds() { 101 return getBounds2D().getBounds(); 102 } 103 104 109 private int[] rect() { 110 if(np==0)return null; 111 int xMin = x[0], yMin=y[0], xMax=x[0],yMax=y[0]; 112 113 for(int i=1;i<np;i++) { 114 if(x[i]<xMin)xMin=x[i]; 115 else if(x[i]>xMax)xMax=x[i]; 116 if(y[i]<yMin)yMin=y[i]; 117 else if(y[i]>yMax)yMax=y[i]; 118 } 119 120 return new int[] { xMin, yMin, xMax-xMin, yMax-yMin }; 121 } 122 123 127 public boolean contains(double x, double y) { return false; } 128 129 133 public boolean contains(Point2D p) { return false; } 134 135 139 public boolean contains(double x, double y, double w, double h) { return false; } 140 141 145 public boolean contains(Rectangle2D r) { return false; } 146 147 152 public boolean intersects(double x, double y, double w, double h) { 153 return intersects(new Rectangle2D.Double (x, y, w, h)); 154 } 155 156 161 public boolean intersects(Rectangle2D r) { 162 if(np==0)return false; 163 Line2D line = new Line2D.Double (x[0],y[0],x[0],y[0]); 164 for (int i = 1; i < np; i++) { 165 line.setLine(x[i-1], y[i-1], x[i], y[i]); 166 if(line.intersects(r))return true; 167 } 168 return false; 169 } 170 171 177 public PathIterator getPathIterator(AffineTransform at) { 178 return new PolylineShapeIterator(this, at); 179 } 180 181 185 public PathIterator getPathIterator(AffineTransform at, double flatness) { 186 return new PolylineShapeIterator(this, at); 187 } 188 189 } 190 191 | Popular Tags |