/** * Utility method that iterates over a Shape object and prints out the * points. The flattening is used for a FlatteningPathIterator, controlling * the scope of the path traversal. */ public static void describeShapeDetail(Shape shape, double flattening) { PathIterator pi2 = shape.getPathIterator(null); FlatteningPathIterator pi = new FlatteningPathIterator(pi2, flattening); double[] coords = new double[6]; int pointCount = 0; Debug.output(" -- start describeShapeDetail with flattening[" + flattening + "]"); while (!pi.isDone()) { int type = pi.currentSegment(coords); Debug.output(" Shape point [" + type + "] (" + (pointCount++) + ") " + coords[0] + ", " + coords[1]); pi.next(); } Debug.output(" -- end (" + pointCount + ")"); }
public static Point2D.Double getStartPoint(Shape shape) { System.out.println("In getStartPoint Shape Bounds: " + shape.getBounds()); PathIterator pIter = new FlatteningPathIterator( shape.getPathIterator(null), 1); double x = 0.0, y = 0.0; double[] coords = new double[6]; while (!pIter.isDone()) { if (pIter == null) break; int currSeg = pIter.currentSegment(coords); if (currSeg == PathIterator.SEG_MOVETO) { for (int iter = 0; iter < coords.length; iter++) { if (iter > 1) break; if (iter == 0) x = coords[iter]; if (iter == 1) y = coords[iter]; } return new Point2D.Double(x, y); } pIter.next(); } // while(pIter.next()) return null; }
/** * Compute the length of the given shape. * @param shape a path * @return the length of the given path */ private static float length(Shape shape) { float pathLength = 0f; // the accumulated length PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), TextStroke.FLATNESS); float moveX = 0, moveY = 0, lastX = 0, lastY = 0; while (!it.isDone()) { float coords[] = new float[6]; int segmentType = it.currentSegment(coords); switch(segmentType){ case PathIterator.SEG_MOVETO: // store the new coordinates moveX = lastX = coords[0]; moveY = lastY = coords[1]; break; case PathIterator.SEG_CLOSE: // close the path: in this case, the coords is empty so we put the // last move as the new point... coords[0] = moveX; coords[1] = moveY; // ... treat the segment type as a lineTo segment case PathIterator.SEG_LINETO: pathLength += Point2D.distance(lastX, lastY, coords[0], coords[1]); lastX = coords[0]; lastY = coords[1]; break; } it.next(); // next segment } return pathLength; }
private void doTraceAfterSetup(Shape shape) { int startingX = 0; int startingY = 0; PathIterator fpi = new FlatteningPathIterator(shape.getPathIterator(null), 1.0); float[] coords = new float[2]; while (!fpi.isDone()) { int type = fpi.currentSegment(coords); int x = (int) coords[0]; int y = (int) coords[1]; brushAffectedArea.updateAffectedCoordinates(x, y); switch (type) { case PathIterator.SEG_MOVETO: startingX = x; startingY = y; brush.onDragStart(x, y); break; case PathIterator.SEG_LINETO: brush.onNewMousePoint(x, y); break; case PathIterator.SEG_CLOSE: brush.onNewMousePoint(startingX, startingY); break; default: throw new IllegalArgumentException("type = " + type); } fpi.next(); } }
public float measurePathLength(Shape pShape) { PathIterator it = new FlatteningPathIterator(pShape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float total = 0; while (!it.isDone()) { type = it.currentSegment(points); switch(type){ case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX-lastX; float dy = thisY-lastY; total += (float)Math.sqrt(dx*dx + dy*dy); lastX = thisX; lastY = thisY; break; } it.next(); } return total; }
/** * Determines, whether or not a given point p is in an epsilon-neighbourhood * for the cubic curve. */ public boolean detectCollision(Point p) { if (arrowLinkCurve == null) return false; Rectangle2D rec = getControlPoint(p); // flatten the curve and test for intersection (bug fix, fc, 16.1.2004). FlatteningPathIterator pi = new FlatteningPathIterator( arrowLinkCurve.getPathIterator(null), MAXIMAL_RECTANGLE_SIZE_FOR_COLLISION_DETECTION / 4, 10/* * =maximal * 2^10=1024 * points. */); double oldCoordinateX = 0, oldCoordinateY = 0; while (pi.isDone() == false) { double[] coordinates = new double[6]; int type = pi.currentSegment(coordinates); switch (type) { case PathIterator.SEG_LINETO: if (rec.intersectsLine(oldCoordinateX, oldCoordinateY, coordinates[0], coordinates[1])) return true; /* * this case needs the same action as the next case, thus no * "break" */ case PathIterator.SEG_MOVETO: oldCoordinateX = coordinates[0]; oldCoordinateY = coordinates[1]; break; case PathIterator.SEG_QUADTO: case PathIterator.SEG_CUBICTO: case PathIterator.SEG_CLOSE: default: break; } pi.next(); } return false; }
public static String printShape(Shape shape) { System.out.println("In printShape Shape Bounds: " + shape.getBounds()); StringBuffer mesg = new StringBuffer(); PathIterator pIter = new FlatteningPathIterator( shape.getPathIterator(null), 1); // PathIterator pIter = shape.getPathIterator(null); mesg.append("\n----PRINTING PATH DATA POINTS----\n"); int data_index = 0; double[] coords = new double[6]; String pathSeg = ""; while (!pIter.isDone()) { if (pIter == null) break; int currSeg = pIter.currentSegment(coords); if (currSeg == PathIterator.SEG_MOVETO) pathSeg = "MOVE TO "; else if (currSeg == PathIterator.SEG_LINETO) pathSeg = "LINE TO "; else if (currSeg == PathIterator.SEG_CUBICTO) pathSeg = "CUBIC TO "; else if (currSeg == PathIterator.SEG_QUADTO) pathSeg = "QUAD TO "; else if (currSeg == PathIterator.SEG_CLOSE) pathSeg = "CLOSE PATH "; mesg.append(pathSeg); for (int iter = 0; iter < coords.length; iter++) { mesg.append(coords[iter] + " "); } mesg.append("\n"); pIter.next(); } // while(pIter.next()) return mesg.toString(); }
public static float measurePathLength(Shape shape) { float FLATNESS = 1; PathIterator it = new FlatteningPathIterator( shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float total = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; total += (float) Math.sqrt(dx * dx + dy * dy); lastX = thisX; lastY = thisY; break; } it.next(); } return total; }
/** * @see java.awt.Stroke#createStrokedShape(java.awt.Shape) */ public Shape createStrokedShape (Shape shape) { GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float next = 0; int phase = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); next = wavelength / 2; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float)Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; if ((phase & 1) == 0) result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r); else result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r); next += wavelength; phase++; } } next -= distance; lastX = thisX; lastY = thisY; if (type == PathIterator.SEG_CLOSE) result.closePath(); break; } it.next(); } return new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(result); }
/** * @see java.awt.Stroke#createStrokedShape(java.awt.Shape) */ public Shape createStrokedShape (Shape shape) { GeneralPath result = new GeneralPath(); shape = new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(shape); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float next = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = randomize(points[0]); moveY = lastY = randomize(points[1]); result.moveTo(moveX, moveY); next = 0; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = randomize(points[0]); thisY = randomize(points[1]); float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float)Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; result.lineTo(randomize(x), randomize(y)); next += detail; } } next -= distance; lastX = thisX; lastY = thisY; break; } it.next(); } return result; }
public PathIterator getPathIterator(AffineTransform at, double flatness) { return new FlatteningPathIterator(getPathIterator(at),flatness); }
protected Shape satin(Shape shape) { if (shape == null) { return null; } GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), flatness); float[] points = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX, thisY; int type; float next = 0; int phase = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); next = wavelength / 2; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float) Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; if ((phase & 1) == 0) { result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r); } else { result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r); } next += wavelength; phase++; } } next -= distance; lastX = thisX; lastY = thisY; if (type == PathIterator.SEG_CLOSE) { result.closePath(); } break; } it.next(); } return result; }
private Shape textShape(Shape shape) { AffineTransform t = new AffineTransform(); FontRenderContext frc = new FontRenderContext(null, true, true); GlyphVector glyphVector = font.createGlyphVector(frc, content); GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), 1); float[] points = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float next = 0; int currentChar = 0; int length = glyphVector.getNumGlyphs(); if (length == 0) { return result; } float factor = 1.0f; float nextAdvance = 0; while (currentChar < length && !it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f; next = nextAdvance; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float) Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; float angle = (float) Math.atan2(dy, dx); while (currentChar < length && distance >= next) { Shape glyph = glyphVector.getGlyphOutline(currentChar); Point2D p = glyphVector.getGlyphPosition(currentChar); float px = (float) p.getX(); float py = (float) p.getY(); float x = lastX + next * dx * r; float y = lastY + next * dy * r; float advance = nextAdvance; nextAdvance = currentChar < length - 1 ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f : 0; t.setToTranslation(x, y); t.rotate(angle); t.translate(-px - advance, -py); result.append(t.createTransformedShape(glyph), false); next += (advance + nextAdvance) * factor; currentChar++; } } next -= distance; lastX = thisX; lastY = thisY; break; } it.next(); } return result; }
public static double getPathLength(Path2D path, double scaleX, double scaleY) { PathIterator outline = new FlatteningPathIterator(path .getPathIterator(null), 1e-4); double length = 0; double[] pointData = new double[6]; Point2D.Double start = new Point2D.Double(); Point2D.Double last = new Point2D.Double(); Point2D.Double current = new Point2D.Double(); double resultLength = 0; double segLength = 0; boolean first = true; double dx = 0; double dy = 0; outline.next(); while (!outline.isDone()) { if (outline.currentSegment(pointData) != PathIterator.SEG_CLOSE) { if (first) { first = false; start.x = pointData[0]; start.y = pointData[1]; current.x = pointData[0]; current.y = pointData[1]; } else { last.x = current.x; last.y = current.y; current.x = pointData[0]; current.y = pointData[1]; dx = (last.x - current.x) * scaleX; dy = (last.y - current.y) * scaleY; segLength = Math.sqrt(dx * dx + dy * dy); resultLength += segLength; } } outline.next(); } return resultLength; }
/** * {@inheritDoc} */ @Override public PathIterator getPathIterator(AffineTransform at, double flatness) { return new FlatteningPathIterator(getPathIterator(at), flatness); }
@Test public void testGetPathIterator() { assertFalse(morph.getPathIterator(AffineTransform.getRotateInstance(.5)) instanceof FlatteningPathIterator); assertTrue(morph.getPathIterator(AffineTransform.getRotateInstance(.5), .7) instanceof FlatteningPathIterator); }
public Shape createStrokedShape(Shape shape) { FontRenderContext lFRC = new FontRenderContext(null, true, true); GlyphVector lGlyphVector = mFont.createGlyphVector(lFRC, mText); GeneralPath lResult = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; boolean first = false; float next = 0; int currentChar = 0; int length = lGlyphVector.getNumGlyphs(); if (length == 0) return lResult; float lKerningFactor = mStretchToFit ? measurePathLength(shape)/(float)lGlyphVector.getLogicalBounds().getWidth() : 1.0f; float nextAdvance = 0; while (currentChar < length && !it.isDone()) { type = it.currentSegment(points); switch(type){ case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; lResult.moveTo(moveX, moveY); first = true; nextAdvance = lGlyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f; next = nextAdvance; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX-lastX; float dy = thisY-lastY; float distance = (float)Math.sqrt(dx*dx + dy*dy); if (distance >= next) { float r = 1.0f/distance; float angle = (float)Math.atan2(dy, dx); while (currentChar < length && distance >= next) { Shape glyph = lGlyphVector.getGlyphOutline(currentChar); Point2D p = lGlyphVector.getGlyphPosition(currentChar); float px = (float)p.getX(); float py = (float)p.getY(); float x = lastX + next * dx * r; float y = lastY + next * dy * r; float advance = nextAdvance; nextAdvance = currentChar < length-1 ? lGlyphVector.getGlyphMetrics(currentChar+1).getAdvance() * 0.5f : 0; mTransform.setToTranslation(x, y); mTransform.rotate(angle); mTransform.translate(-px - advance, -py); lResult.append(mTransform.createTransformedShape(glyph), false); next += (advance+nextAdvance) * lKerningFactor; currentChar++; if (mRepeat) { currentChar %= length; } } } next -= distance; first = false; lastX = thisX; lastY = thisY; break; } it.next(); } return lResult; }
public PathIterator getPathIterator(AffineTransform at, double flatness) { return new FlatteningPathIterator(getPathIterator(at), flatness); }
/** * @{inheritDoc} */ public PathIterator getPathIterator(AffineTransform at, double flatness) { return new FlatteningPathIterator(getPathIterator(at), flatness); }
/** * {@inheritDoc} */ public PathIterator getPathIterator(AffineTransform at, double flatness) { return new FlatteningPathIterator(getPathIterator(at), flatness); }
public Shape createStrokedShape (Shape shape) { GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float next = 0; int phase = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); next = wavelength / 2; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float)Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; if ((phase & 1) == 0) result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r); else result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r); next += wavelength; phase++; } } next -= distance; lastX = thisX; lastY = thisY; if (type == PathIterator.SEG_CLOSE) result.closePath(); break; } it.next(); } return new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(result); }
public Shape createStrokedShape (Shape shape) { GeneralPath result = new GeneralPath(); shape = new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(shape); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; float next = 0; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = randomize(points[0]); moveY = lastY = randomize(points[1]); result.moveTo(moveX, moveY); next = 0; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = randomize(points[0]); thisY = randomize(points[1]); float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float)Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; result.lineTo(randomize(x), randomize(y)); next += detail; } } next -= distance; lastX = thisX; lastY = thisY; break; } it.next(); } return result; }
/** * Convert AWT shape to image map coordinates. * * @param shape * @return */ private String shape2polyCoords( Shape shape ) { if ( shape == null ) { return null; } ArrayList<Double> al = new ArrayList<Double>( ); FlatteningPathIterator pitr = new FlatteningPathIterator( shape.getPathIterator( null ), 1 ); double[] data = new double[6]; while ( !pitr.isDone( ) ) { int type = pitr.currentSegment( data ); switch ( type ) { case PathIterator.SEG_MOVETO : al.add( new Double( data[0] ) ); al.add( new Double( data[1] ) ); break; case PathIterator.SEG_LINETO : al.add( new Double( data[0] ) ); al.add( new Double( data[1] ) ); break; case PathIterator.SEG_QUADTO : al.add( new Double( data[0] ) ); al.add( new Double( data[1] ) ); al.add( new Double( data[2] ) ); al.add( new Double( data[3] ) ); break; case PathIterator.SEG_CUBICTO : al.add( new Double( data[0] ) ); al.add( new Double( data[1] ) ); al.add( new Double( data[2] ) ); al.add( new Double( data[3] ) ); al.add( new Double( data[4] ) ); al.add( new Double( data[5] ) ); break; case PathIterator.SEG_CLOSE : break; } pitr.next( ); } if ( al.size( ) == 0 ) { return null; } StringBuffer sb = new StringBuffer( ); for ( int i = 0; i < al.size( ); i++ ) { Double db = al.get( i ); if ( i > 0 ) { sb.append( "," ); //$NON-NLS-1$ } sb.append( (int) translateCoor( db.doubleValue( ) ) ); } return sb.toString( ); }
private int[] shape2polyCoords( Shape shape ) { if ( shape == null ) { return null; } ArrayList<Integer> al = new ArrayList<Integer>( ); FlatteningPathIterator pitr = new FlatteningPathIterator( shape.getPathIterator( null ), 1 ); double[] data = new double[6]; while ( !pitr.isDone( ) ) { int type = pitr.currentSegment( data ); switch ( type ) { case PathIterator.SEG_MOVETO : al.add( (int) data[0] ); al.add( (int) data[1] ); break; case PathIterator.SEG_LINETO : al.add( (int) data[0] ); al.add( (int) data[1] ); break; case PathIterator.SEG_QUADTO : al.add( (int) data[0] ); al.add( (int) data[1] ); al.add( (int) data[2] ); al.add( (int) data[3] ); break; case PathIterator.SEG_CUBICTO : al.add( (int) data[0] ); al.add( (int) data[1] ); al.add( (int) data[2] ); al.add( (int) data[3] ); al.add( (int) data[4] ); al.add( (int) data[5] ); break; case PathIterator.SEG_CLOSE : break; } pitr.next( ); } if ( al.size( ) == 0 ) { return null; } int[] coords = new int[al.size( )]; for ( int i = 0; i < al.size( ); i++ ) { coords[i] = al.get( i ); } return coords; }