001: /*
002: * $Id: PolylineShape.java 2772 2007-05-21 14:06:08Z blowagie $
003: *
004: * Copyright 2007 Bruno Lowagie and Wil
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf.internal;
051:
052: import java.awt.Shape;
053: import java.awt.Rectangle;
054: import java.awt.geom.AffineTransform;
055: import java.awt.geom.PathIterator;
056: import java.awt.geom.Line2D;
057: import java.awt.geom.Point2D;
058: import java.awt.geom.Rectangle2D;
059:
060: /**
061: * Class that defines a Polyline shape.
062: * This class was originally written by wil - amristar.com.au
063: * and integrated into iText by Bruno.
064: */
065: public class PolylineShape implements Shape {
066: /** All the X-values of the coordinates in the polyline. */
067: protected int[] x;
068: /** All the Y-values of the coordinates in the polyline. */
069: protected int[] y;
070: /** The total number of points. */
071: protected int np;
072:
073: /** Creates a PolylineShape. */
074: public PolylineShape(int[] x, int[] y, int nPoints) {
075: // Should copy array (as done in Polygon)
076: this .np = nPoints;
077: // Take a copy.
078: this .x = new int[np];
079: this .y = new int[np];
080: System.arraycopy(x, 0, this .x, 0, np);
081: System.arraycopy(y, 0, this .y, 0, np);
082: }
083:
084: /**
085: * Returns the bounding box of this polyline.
086: *
087: * @return a {@link Rectangle2D} that is the high-precision
088: * bounding box of this line.
089: * @see java.awt.Shape#getBounds2D()
090: */
091: public Rectangle2D getBounds2D() {
092: int[] r = rect();
093: return r == null ? null : new Rectangle2D.Double(r[0], r[1],
094: r[2], r[3]);
095: }
096:
097: /**
098: * Returns the bounding box of this polyline.
099: * @see java.awt.Shape#getBounds()
100: */
101: public Rectangle getBounds() {
102: return getBounds2D().getBounds();
103: }
104:
105: /**
106: * Calculates the origin (X, Y) and the width and height
107: * of a rectangle that contains all the segments of the
108: * polyline.
109: */
110: private int[] rect() {
111: if (np == 0)
112: return null;
113: int xMin = x[0], yMin = y[0], xMax = x[0], yMax = y[0];
114:
115: for (int i = 1; i < np; i++) {
116: if (x[i] < xMin)
117: xMin = x[i];
118: else if (x[i] > xMax)
119: xMax = x[i];
120: if (y[i] < yMin)
121: yMin = y[i];
122: else if (y[i] > yMax)
123: yMax = y[i];
124: }
125:
126: return new int[] { xMin, yMin, xMax - xMin, yMax - yMin };
127: }
128:
129: /**
130: * A polyline can't contain a point.
131: * @see java.awt.Shape#contains(double, double)
132: */
133: public boolean contains(double x, double y) {
134: return false;
135: }
136:
137: /**
138: * A polyline can't contain a point.
139: * @see java.awt.Shape#contains(java.awt.geom.Point2D)
140: */
141: public boolean contains(Point2D p) {
142: return false;
143: }
144:
145: /**
146: * A polyline can't contain a point.
147: * @see java.awt.Shape#contains(double, double, double, double)
148: */
149: public boolean contains(double x, double y, double w, double h) {
150: return false;
151: }
152:
153: /**
154: * A polyline can't contain a point.
155: * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
156: */
157: public boolean contains(Rectangle2D r) {
158: return false;
159: }
160:
161: /**
162: * Checks if one of the lines in the polyline intersects
163: * with a given rectangle.
164: * @see java.awt.Shape#intersects(double, double, double, double)
165: */
166: public boolean intersects(double x, double y, double w, double h) {
167: return intersects(new Rectangle2D.Double(x, y, w, h));
168: }
169:
170: /**
171: * Checks if one of the lines in the polyline intersects
172: * with a given rectangle.
173: * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
174: */
175: public boolean intersects(Rectangle2D r) {
176: if (np == 0)
177: return false;
178: Line2D line = new Line2D.Double(x[0], y[0], x[0], y[0]);
179: for (int i = 1; i < np; i++) {
180: line.setLine(x[i - 1], y[i - 1], x[i], y[i]);
181: if (line.intersects(r))
182: return true;
183: }
184: return false;
185: }
186:
187: /**
188: * Returns an iteration object that defines the boundary of the polyline.
189: * @param at the specified {@link AffineTransform}
190: * @return a {@link PathIterator} that defines the boundary of this polyline.
191: * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
192: */
193: public PathIterator getPathIterator(AffineTransform at) {
194: return new PolylineShapeIterator(this , at);
195: }
196:
197: /**
198: * There's no difference with getPathIterator(AffineTransform at);
199: * we just need this method to implement the Shape interface.
200: */
201: public PathIterator getPathIterator(AffineTransform at,
202: double flatness) {
203: return new PolylineShapeIterator(this, at);
204: }
205:
206: }
|